1 |
//---PACKAGE DECLARATION--- |
2 |
package uk.ac.ukc.iscream.util; |
3 |
|
4 |
//---IMPORTS--- |
5 |
import org.omg.CORBA.ORB; |
6 |
import org.omg.CosNaming.*; |
7 |
import org.omg.PortableServer.*; |
8 |
import uk.ac.ukc.iscream.core.*; |
9 |
|
10 |
/** |
11 |
* This class returns references for global system objects. |
12 |
* This class is used to create and return references to objects |
13 |
* that are required throughout the system. Most of which |
14 |
* are CORBA based. It also manages the ORB for the component. |
15 |
* |
16 |
* It is a singleton object with a static method to obtain a |
17 |
* reference to it. This removes the need for passing |
18 |
* references to all the objects it contains throughout |
19 |
* a component. |
20 |
* |
21 |
* @author $Author: ajm4 $ |
22 |
* @version $Id: ReferenceManager.java,v 1.4 2000/11/30 03:07:25 ajm4 Exp $ |
23 |
*/ |
24 |
public class ReferenceManager { |
25 |
|
26 |
//---FINAL ATTRIBUTES--- |
27 |
|
28 |
/** |
29 |
* The current CVS revision of this class |
30 |
*/ |
31 |
public final String REVISION = "$Revision: 1.4 $"; |
32 |
|
33 |
//---STATIC METHODS--- |
34 |
|
35 |
/** |
36 |
* This creates the single instance of the ReferenceManager by |
37 |
* calling the private constructor. If it is called twice, |
38 |
* it detects an instance already exits and throws an exception. |
39 |
* |
40 |
* @param args the args to be passed to the ORB |
41 |
* @param name the name of the component that will use this singleton |
42 |
* |
43 |
* @return a reference to the ReferenceManager |
44 |
* |
45 |
* @throws AlreadyInitialisedException if this method has already been called |
46 |
*/ |
47 |
public static ReferenceManager init(String[] args, String name) throws AlreadyInitialisedException { |
48 |
if (_instance != null) { |
49 |
throw new AlreadyInitialisedException("init has already been called"); |
50 |
} |
51 |
_instance = new ReferenceManager(args, name); |
52 |
return _instance; |
53 |
} |
54 |
|
55 |
/** |
56 |
* This returns a reference to the single instance of the |
57 |
* ReferenceManager. |
58 |
* |
59 |
* @return a reference to the ReferenceManager |
60 |
* |
61 |
* @throws NotInitialisedException if the reference manager has not been initialised |
62 |
*/ |
63 |
public static ReferenceManager getInstance() throws NotInitialisedException { |
64 |
if (_instance == null) { |
65 |
throw new NotInitialisedException("attempt to obtain reference when not initialised"); |
66 |
} |
67 |
return _instance; |
68 |
} |
69 |
|
70 |
//---CONSTRUCTORS--- |
71 |
|
72 |
/** |
73 |
* This is a private constructor |
74 |
* This ensures that the system performs according to a Singleton design pattern |
75 |
* |
76 |
* @param args the args to be passed to the ORB |
77 |
* @param name the name of the component that will use this singleton |
78 |
*/ |
79 |
private ReferenceManager(String[] args, String name) { |
80 |
_orb = ORB.init(args, null); |
81 |
_name = name; |
82 |
} |
83 |
|
84 |
//---PUBLIC METHODS--- |
85 |
|
86 |
/** |
87 |
* Obtains a CORBA reference through the naming service |
88 |
* for the named object. |
89 |
* This will throw a Error if there is an error |
90 |
* |
91 |
* @param name the name of the CORBA object to resolve |
92 |
* |
93 |
* @return a reference to the object |
94 |
*/ |
95 |
public org.omg.CORBA.Object getCORBARef(String name) { |
96 |
org.omg.CORBA.Object objRef = null; |
97 |
try { |
98 |
objRef = getNS().resolve(getNS().to_name(name)); |
99 |
} catch (org.omg.CosNaming.NamingContextPackage.CannotProceed e) { |
100 |
throw new Error("\nCRITICAL:Naming Service Cannot Proceed - when resolving reference to " + name + "!\n" + |
101 |
" Please check with your CORBA naming service provider."); |
102 |
} catch (org.omg.CosNaming.NamingContextPackage.InvalidName e) { |
103 |
throw new Error("\nCRITICAL:Invalid Name - when resolving reference to " + name + "!\n" + |
104 |
" Please check with your CORBA naming service provider."); |
105 |
} catch (org.omg.CosNaming.NamingContextPackage.NotFound e) { |
106 |
throw new Error("\nCRITICAL:Not Found - when resolving reference to " + name + "!\n" + |
107 |
" Please check that this component is running."); |
108 |
} |
109 |
return objRef; |
110 |
} |
111 |
|
112 |
/** |
113 |
* Binds a given servant with the given name |
114 |
* to the naming service. |
115 |
* This will throw a Error if there is an error |
116 |
* |
117 |
* @param objRef a reverence to the servant object |
118 |
* @param name the name to bind to the naming service with |
119 |
*/ |
120 |
public void bindToOrb(org.omg.PortableServer.Servant objRef, String name) { |
121 |
try { |
122 |
getNS().bind(getNS().to_name(name), getRootPOA().servant_to_reference(objRef)); |
123 |
} catch (org.omg.PortableServer.POAPackage.WrongPolicy e) { |
124 |
throw new Error("\nCRITICAL:Wrong POA Policy - when binding " + name + "!\n" + |
125 |
" This indicates an error with your ORB."); |
126 |
} catch (org.omg.PortableServer.POAPackage.ServantNotActive e) { |
127 |
throw new Error("\nCRITICAL:ServantNotActive - when binding " + name + "!\n" + |
128 |
" This indicates an error with your ORB."); |
129 |
} catch (org.omg.CosNaming.NamingContextPackage.InvalidName e) { |
130 |
throw new Error("\nCRITICAL:Invalid Name - when binding " + name + "!\n" + |
131 |
" Please check with your CORBA naming service provider."); |
132 |
} catch (org.omg.CosNaming.NamingContextPackage.CannotProceed e) { |
133 |
throw new Error("\nCRITICAL:Naming Service Cannot Proceed - when binding " + name + "!\n" + |
134 |
" Please check with your CORBA naming service provider."); |
135 |
} catch (org.omg.CosNaming.NamingContextPackage.NotFound e) { |
136 |
throw new Error("\nCRITICAL:Naming Service Not Found - when binding " + name + "!\n" + |
137 |
" Please check with your CORBA naming service provider."); |
138 |
} catch (org.omg.CosNaming.NamingContextPackage.AlreadyBound e) { |
139 |
throw new Error("\nCRITICAL:Already Bound - when binding " + name + "!\n" + |
140 |
" Another component with this name is already running."); |
141 |
} |
142 |
} |
143 |
|
144 |
/** |
145 |
* Activates the POA |
146 |
* This will throw a Error if there is an error |
147 |
*/ |
148 |
public void activatePOA() { |
149 |
try { |
150 |
getRootPOA().the_POAManager().activate(); |
151 |
} catch (org.omg.PortableServer.POAManagerPackage.AdapterInactive e) { |
152 |
throw new Error("\nCRITICAL:POA Set Inactive - when trying to activate POA!\n" + |
153 |
" This indicates an error with your ORB."); |
154 |
} |
155 |
} |
156 |
|
157 |
/** |
158 |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
159 |
* method to provide clean logging (every class should have this). |
160 |
* |
161 |
* @return the name of this class and its CVS revision |
162 |
*/ |
163 |
public String toString() { |
164 |
return getName() + "{" + getClass().getName() + "}(" + REVISION.substring(11, REVISION.length() - 2) + ")"; |
165 |
} |
166 |
|
167 |
//---PRIVATE METHODS--- |
168 |
|
169 |
//---ACCESSOR/MUTATOR METHODS--- |
170 |
|
171 |
/** |
172 |
* Returns a reference to the ORB |
173 |
* |
174 |
* @return the reference this class holds |
175 |
*/ |
176 |
public ORB getORB() { |
177 |
return _orb; |
178 |
} |
179 |
|
180 |
/** |
181 |
* Returns a reference to the Root POA |
182 |
* This will throw a Error if there is an error |
183 |
* |
184 |
* @return the reference this class holds |
185 |
*/ |
186 |
public POA getRootPOA() { |
187 |
if (_rootPOA == null) { |
188 |
org.omg.CORBA.Object objRef = null; |
189 |
try { |
190 |
objRef = getORB().resolve_initial_references("RootPOA"); |
191 |
} catch (org.omg.CORBA.ORBPackage.InvalidName e) { |
192 |
throw new Error("\nCRITICAL:Unable to resolve reference to the RootPOA!\n" + |
193 |
" This indicates an error with your ORB."); |
194 |
} |
195 |
_rootPOA = POAHelper.narrow(objRef); |
196 |
} |
197 |
return _rootPOA; |
198 |
} |
199 |
|
200 |
/** |
201 |
* Returns a reference to the Naming Service |
202 |
* This will throw a Error if there is an error |
203 |
* |
204 |
* @return the reference this class holds |
205 |
*/ |
206 |
public NamingContextExt getNS() { |
207 |
if (_ns == null) { |
208 |
org.omg.CORBA.Object objRef = null; |
209 |
try { |
210 |
objRef = getORB().resolve_initial_references("NameService"); |
211 |
} catch (org.omg.CORBA.ORBPackage.InvalidName e) { |
212 |
throw new Error("\nCRITICAL:Unable to resolve reference to the Naming Service!\n" + |
213 |
" Please check with your CORBA naming service provider."); |
214 |
} |
215 |
_ns = NamingContextExtHelper.narrow(objRef); |
216 |
} |
217 |
// check we managed to talk to the naming service |
218 |
if (_ns == null) { |
219 |
throw new Error("\nCRITICAL:Unable to resolve reference to the Naming Service!\n" + |
220 |
" Please check with your CORBA naming service provider."); |
221 |
} |
222 |
return _ns; |
223 |
} |
224 |
|
225 |
/** |
226 |
* Returns a reference to the configuration manager |
227 |
* This will throw a Error if there is an error |
228 |
* |
229 |
* @return the reference this class holds |
230 |
*/ |
231 |
public ConfigurationManager getCM() { |
232 |
if (_cm == null) { |
233 |
_cm = ConfigurationManagerHelper.narrow(getCORBARef("iscream.ConfigurationManager")); |
234 |
} |
235 |
// check we managed to talk to the configuration manager |
236 |
if (_cm == null) { |
237 |
throw new Error("\nCRITICAL:Unable to resolve reference to the Configuration Manager!\n" + |
238 |
" Please check with your CORBA naming service provider."); |
239 |
} |
240 |
return _cm; |
241 |
} |
242 |
|
243 |
/** |
244 |
* Returns a reference to the logger |
245 |
* This will throw a Error if there is an error |
246 |
* |
247 |
* @return the reference this class holds |
248 |
*/ |
249 |
public Logger getLogger() { |
250 |
if (_logger == null) { |
251 |
_logger = LoggerHelper.narrow(getCORBARef("iscream.Logger")); |
252 |
} |
253 |
return _logger; |
254 |
} |
255 |
|
256 |
/** |
257 |
* Sets the reference to the name |
258 |
* |
259 |
* @param the new name |
260 |
*/ |
261 |
public void setName(String name) { |
262 |
_name = name; |
263 |
} |
264 |
|
265 |
/** |
266 |
* Returns a reference to the name |
267 |
* |
268 |
* @return the reference this class holds |
269 |
*/ |
270 |
public String getName() { |
271 |
return _name; |
272 |
} |
273 |
|
274 |
//---ATTRIBUTES--- |
275 |
|
276 |
/** |
277 |
* The ORB |
278 |
*/ |
279 |
private ORB _orb; |
280 |
|
281 |
/** |
282 |
* The Naming Service |
283 |
*/ |
284 |
private NamingContextExt _ns; |
285 |
|
286 |
/** |
287 |
* The Root POA |
288 |
*/ |
289 |
private POA _rootPOA; |
290 |
|
291 |
/** |
292 |
* The configuration manager |
293 |
*/ |
294 |
private ConfigurationManager _cm; |
295 |
|
296 |
/** |
297 |
* The logger |
298 |
*/ |
299 |
private Logger _logger; |
300 |
|
301 |
/** |
302 |
* The name |
303 |
*/ |
304 |
private String _name; |
305 |
|
306 |
//---STATIC ATTRIBUTES--- |
307 |
|
308 |
/** |
309 |
* A reference to the single instance of this class |
310 |
*/ |
311 |
private static ReferenceManager _instance; |
312 |
} |