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