1 |
ajm |
1.1 |
//---PACKAGE DECLARATION--- |
2 |
tdb |
1.13 |
package uk.org.iscream.componentmanager; |
3 |
ajm |
1.1 |
|
4 |
|
|
//---IMPORTS--- |
5 |
|
|
import org.omg.CORBA.ORB; |
6 |
|
|
import org.omg.CosNaming.*; |
7 |
|
|
import org.omg.PortableServer.*; |
8 |
tdb |
1.13 |
import uk.org.iscream.core.*; |
9 |
|
|
import uk.org.iscream.util.*; |
10 |
ajm |
1.1 |
|
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 |
ajm |
1.2 |
* are CORBA based. It also manages the ORB for the component. |
16 |
ajm |
1.1 |
* |
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 |
ajm |
1.12 |
* @author $Author: ajm4 $ |
23 |
tdb |
1.13 |
* @version $Id: ReferenceManager.java,v 1.12 2001/02/23 16:55:12 ajm4 Exp $ |
24 |
ajm |
1.1 |
*/ |
25 |
|
|
public class ReferenceManager { |
26 |
|
|
|
27 |
|
|
//---FINAL ATTRIBUTES--- |
28 |
|
|
|
29 |
|
|
/** |
30 |
|
|
* The current CVS revision of this class |
31 |
|
|
*/ |
32 |
tdb |
1.13 |
public final String REVISION = "$Revision: 1.12 $"; |
33 |
ajm |
1.1 |
|
34 |
|
|
//---STATIC METHODS--- |
35 |
|
|
|
36 |
|
|
/** |
37 |
|
|
* This returns a reference to the single instance of the |
38 |
|
|
* ReferenceManager. |
39 |
|
|
* |
40 |
ajm |
1.6 |
* This creates the single instance of the ReferenceManager |
41 |
|
|
* if it does not exist by calling the private constructor. |
42 |
|
|
* |
43 |
ajm |
1.1 |
* @return a reference to the ReferenceManager |
44 |
|
|
*/ |
45 |
ajm |
1.6 |
public static ReferenceManager getInstance() { |
46 |
ajm |
1.1 |
if (_instance == null) { |
47 |
ajm |
1.6 |
_instance = new ReferenceManager(); |
48 |
ajm |
1.1 |
} |
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 |
ajm |
1.6 |
*/ |
58 |
|
|
private ReferenceManager() { |
59 |
|
|
_orb = ORB.init(new String[] {}, null); |
60 |
|
|
} |
61 |
ajm |
1.1 |
|
62 |
|
|
//---PUBLIC METHODS--- |
63 |
|
|
|
64 |
|
|
/** |
65 |
ajm |
1.2 |
* Obtains a CORBA reference through the naming service |
66 |
|
|
* for the named object. |
67 |
ajm |
1.7 |
* Calls the dieWithError() if any exceptions happen! |
68 |
ajm |
1.2 |
* |
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 |
ajm |
1.6 |
dieWithError("\nCRITICAL:Naming Service Cannot Proceed - when resolving reference to " + name + "!\n" + |
79 |
ajm |
1.5 |
" Please check with your CORBA naming service provider."); |
80 |
ajm |
1.2 |
} catch (org.omg.CosNaming.NamingContextPackage.InvalidName e) { |
81 |
ajm |
1.6 |
dieWithError("\nCRITICAL:Invalid Name - when resolving reference to " + name + "!\n" + |
82 |
ajm |
1.5 |
" Please check with your CORBA naming service provider."); |
83 |
ajm |
1.2 |
} catch (org.omg.CosNaming.NamingContextPackage.NotFound e) { |
84 |
ajm |
1.12 |
recoverWithError("\nCRITICAL:Not Found - when resolving reference to " + name + "!\n" + |
85 |
ajm |
1.5 |
" Please check that this component is running."); |
86 |
ajm |
1.2 |
} |
87 |
|
|
return objRef; |
88 |
|
|
} |
89 |
|
|
|
90 |
|
|
/** |
91 |
|
|
* Binds a given servant with the given name |
92 |
|
|
* to the naming service. |
93 |
ajm |
1.7 |
* Calls the dieWithError() if any exceptions happen! |
94 |
ajm |
1.2 |
* |
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 |
ajm |
1.6 |
dieWithError("\nCRITICAL:Wrong POA Policy - when binding " + name + "!\n" + |
103 |
ajm |
1.5 |
" This indicates an error with your ORB."); |
104 |
ajm |
1.2 |
} catch (org.omg.PortableServer.POAPackage.ServantNotActive e) { |
105 |
ajm |
1.6 |
dieWithError("\nCRITICAL:ServantNotActive - when binding " + name + "!\n" + |
106 |
ajm |
1.5 |
" This indicates an error with your ORB."); |
107 |
ajm |
1.2 |
} catch (org.omg.CosNaming.NamingContextPackage.InvalidName e) { |
108 |
ajm |
1.6 |
dieWithError("\nCRITICAL:Invalid Name - when binding " + name + "!\n" + |
109 |
ajm |
1.5 |
" Please check with your CORBA naming service provider."); |
110 |
ajm |
1.2 |
} catch (org.omg.CosNaming.NamingContextPackage.CannotProceed e) { |
111 |
ajm |
1.6 |
dieWithError("\nCRITICAL:Naming Service Cannot Proceed - when binding " + name + "!\n" + |
112 |
ajm |
1.5 |
" Please check with your CORBA naming service provider."); |
113 |
ajm |
1.2 |
} catch (org.omg.CosNaming.NamingContextPackage.NotFound e) { |
114 |
ajm |
1.12 |
recoverWithError("\nCRITICAL:Naming Service Not Found - when binding " + name + "!\n" + |
115 |
ajm |
1.5 |
" Please check with your CORBA naming service provider."); |
116 |
ajm |
1.2 |
} catch (org.omg.CosNaming.NamingContextPackage.AlreadyBound e) { |
117 |
ajm |
1.6 |
dieWithError("\nCRITICAL:Already Bound - when binding " + name + "!\n" + |
118 |
ajm |
1.5 |
" Another component with this name is already running."); |
119 |
ajm |
1.2 |
} |
120 |
|
|
} |
121 |
|
|
|
122 |
|
|
/** |
123 |
|
|
* Activates the POA |
124 |
ajm |
1.7 |
* Calls the dieWithError() if any exceptions happen! |
125 |
ajm |
1.2 |
*/ |
126 |
|
|
public void activatePOA() { |
127 |
|
|
try { |
128 |
|
|
getRootPOA().the_POAManager().activate(); |
129 |
|
|
} catch (org.omg.PortableServer.POAManagerPackage.AdapterInactive e) { |
130 |
ajm |
1.6 |
dieWithError("\nCRITICAL:POA Set Inactive - when trying to activate POA!\n" + |
131 |
ajm |
1.5 |
" This indicates an error with your ORB."); |
132 |
ajm |
1.2 |
} |
133 |
|
|
} |
134 |
|
|
|
135 |
|
|
/** |
136 |
ajm |
1.1 |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
137 |
|
|
* method to provide clean logging (every class should have this). |
138 |
|
|
* |
139 |
tdb |
1.13 |
* This uses the uk.org.iscream.util.NameFormat class |
140 |
ajm |
1.6 |
* to format the toString() |
141 |
|
|
* |
142 |
ajm |
1.1 |
* @return the name of this class and its CVS revision |
143 |
|
|
*/ |
144 |
|
|
public String toString() { |
145 |
ajm |
1.6 |
return FormatName.getName( |
146 |
ajm |
1.7 |
_name, |
147 |
|
|
getClass().getName(), |
148 |
ajm |
1.6 |
REVISION); |
149 |
ajm |
1.1 |
} |
150 |
|
|
|
151 |
|
|
//---PRIVATE METHODS--- |
152 |
|
|
|
153 |
ajm |
1.6 |
/** |
154 |
|
|
* If there are any CORBA errors this method is called with the |
155 |
|
|
* error message. |
156 |
|
|
* |
157 |
ajm |
1.11 |
* 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 |
ajm |
1.6 |
* |
163 |
ajm |
1.11 |
* Also, if it can, it will log the message with the logger. |
164 |
ajm |
1.6 |
* |
165 |
|
|
* @param message the error message to die with |
166 |
|
|
*/ |
167 |
ajm |
1.12 |
private void recoverWithError(String message) throws ComponentCORBAException { |
168 |
ajm |
1.6 |
if (_logger != null) { |
169 |
ajm |
1.11 |
_logger.write(toString(), Logger.WARNING, "component CORBA error - " + message); |
170 |
ajm |
1.6 |
} |
171 |
ajm |
1.11 |
throw new ComponentCORBAException(message); |
172 |
ajm |
1.6 |
} |
173 |
ajm |
1.12 |
|
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 |
ajm |
1.6 |
|
198 |
ajm |
1.1 |
//---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 |
ajm |
1.7 |
* Calls the dieWithError() if any exceptions happen! |
212 |
ajm |
1.1 |
* |
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 |
ajm |
1.6 |
dieWithError("\nCRITICAL:Unable to resolve reference to the RootPOA!\n" + |
222 |
ajm |
1.5 |
" This indicates an error with your ORB."); |
223 |
ajm |
1.1 |
} |
224 |
|
|
_rootPOA = POAHelper.narrow(objRef); |
225 |
|
|
} |
226 |
|
|
return _rootPOA; |
227 |
|
|
} |
228 |
|
|
|
229 |
|
|
/** |
230 |
|
|
* Returns a reference to the Naming Service |
231 |
ajm |
1.7 |
* Calls the dieWithError() if any exceptions happen! |
232 |
ajm |
1.1 |
* |
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 |
ajm |
1.6 |
dieWithError("\nCRITICAL:Unable to resolve reference to the Naming Service!\n" + |
242 |
ajm |
1.5 |
" Please check with your CORBA naming service provider."); |
243 |
ajm |
1.1 |
} |
244 |
|
|
_ns = NamingContextExtHelper.narrow(objRef); |
245 |
|
|
} |
246 |
ajm |
1.5 |
// check we managed to talk to the naming service |
247 |
|
|
if (_ns == null) { |
248 |
ajm |
1.6 |
dieWithError("\nCRITICAL:Unable to resolve reference to the Naming Service!\n" + |
249 |
ajm |
1.5 |
" Please check with your CORBA naming service provider."); |
250 |
|
|
} |
251 |
ajm |
1.1 |
return _ns; |
252 |
|
|
} |
253 |
|
|
|
254 |
|
|
/** |
255 |
|
|
* Returns a reference to the configuration manager |
256 |
ajm |
1.7 |
* Calls the dieWithError() if any exceptions happen! |
257 |
ajm |
1.1 |
* |
258 |
|
|
* @return the reference this class holds |
259 |
|
|
*/ |
260 |
|
|
public ConfigurationManager getCM() { |
261 |
|
|
if (_cm == null) { |
262 |
ajm |
1.2 |
_cm = ConfigurationManagerHelper.narrow(getCORBARef("iscream.ConfigurationManager")); |
263 |
ajm |
1.1 |
} |
264 |
ajm |
1.5 |
// check we managed to talk to the configuration manager |
265 |
|
|
if (_cm == null) { |
266 |
ajm |
1.6 |
dieWithError("\nCRITICAL:Unable to resolve reference to the Configuration Manager!\n" + |
267 |
ajm |
1.5 |
" Please check with your CORBA naming service provider."); |
268 |
|
|
} |
269 |
ajm |
1.1 |
return _cm; |
270 |
|
|
} |
271 |
|
|
|
272 |
|
|
/** |
273 |
|
|
* Returns a reference to the logger |
274 |
ajm |
1.5 |
* This will throw a Error if there is an error |
275 |
ajm |
1.1 |
* |
276 |
|
|
* @return the reference this class holds |
277 |
|
|
*/ |
278 |
|
|
public Logger getLogger() { |
279 |
|
|
if (_logger == null) { |
280 |
ajm |
1.2 |
_logger = LoggerHelper.narrow(getCORBARef("iscream.Logger")); |
281 |
ajm |
1.1 |
} |
282 |
|
|
return _logger; |
283 |
ajm |
1.4 |
} |
284 |
ajm |
1.1 |
|
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 |
ajm |
1.7 |
* 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 |
ajm |
1.1 |
*/ |
321 |
ajm |
1.7 |
private String _name = null; |
322 |
ajm |
1.1 |
|
323 |
|
|
//---STATIC ATTRIBUTES--- |
324 |
|
|
|
325 |
|
|
/** |
326 |
|
|
* A reference to the single instance of this class |
327 |
|
|
*/ |
328 |
|
|
private static ReferenceManager _instance; |
329 |
|
|
} |