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