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.3 2000/11/30 02:38:17 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.3 $"; |
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 |
getLogger().write(toString(), Logger.SYSINIT, "created"); |
83 |
} |
84 |
|
85 |
//---PUBLIC METHODS--- |
86 |
|
87 |
/** |
88 |
* Obtains a CORBA reference through the naming service |
89 |
* for the named object. |
90 |
* This will throw a RuntimeException if there is an error |
91 |
* |
92 |
* @param name the name of the CORBA object to resolve |
93 |
* |
94 |
* @return a reference to the object |
95 |
*/ |
96 |
public org.omg.CORBA.Object getCORBARef(String name) { |
97 |
org.omg.CORBA.Object objRef = null; |
98 |
try { |
99 |
objRef = getNS().resolve(getNS().to_name(name)); |
100 |
} catch (org.omg.CosNaming.NamingContextPackage.CannotProceed e) { |
101 |
throw new RuntimeException("\nCRITICAL:Naming Service Cannot Proceed - when resolving reference to " + name + "!\n" + |
102 |
" Please check with your CORBA naming service provider."); |
103 |
} catch (org.omg.CosNaming.NamingContextPackage.InvalidName e) { |
104 |
throw new RuntimeException("\nCRITICAL:Invalid Name - when resolving reference to " + name + "!\n" + |
105 |
" Please check with your CORBA naming service provider."); |
106 |
} catch (org.omg.CosNaming.NamingContextPackage.NotFound e) { |
107 |
throw new RuntimeException("\nCRITICAL:Not Found - when resolving reference to " + name + "!\n" + |
108 |
" Please check that this component is running."); |
109 |
} |
110 |
return objRef; |
111 |
} |
112 |
|
113 |
/** |
114 |
* Binds a given servant with the given name |
115 |
* to the naming service. |
116 |
* This will throw a RuntimeException if there is an error |
117 |
* |
118 |
* @param objRef a reverence to the servant object |
119 |
* @param name the name to bind to the naming service with |
120 |
*/ |
121 |
public void bindToOrb(org.omg.PortableServer.Servant objRef, String name) { |
122 |
try { |
123 |
getNS().bind(getNS().to_name(name), getRootPOA().servant_to_reference(objRef)); |
124 |
} catch (org.omg.PortableServer.POAPackage.WrongPolicy e) { |
125 |
throw new RuntimeException("\nCRITICAL:Wrong POA Policy - when binding " + name + "!\n" + |
126 |
" This indicates an error with your ORB."); |
127 |
} catch (org.omg.PortableServer.POAPackage.ServantNotActive e) { |
128 |
throw new RuntimeException("\nCRITICAL:ServantNotActive - when binding " + name + "!\n" + |
129 |
" This indicates an error with your ORB."); |
130 |
} catch (org.omg.CosNaming.NamingContextPackage.InvalidName e) { |
131 |
throw new RuntimeException("\nCRITICAL:Invalid Name - when binding " + name + "!\n" + |
132 |
" Please check with your CORBA naming service provider."); |
133 |
} catch (org.omg.CosNaming.NamingContextPackage.CannotProceed e) { |
134 |
throw new RuntimeException("\nCRITICAL:Naming Service Cannot Proceed - when binding " + name + "!\n" + |
135 |
" Please check with your CORBA naming service provider."); |
136 |
} catch (org.omg.CosNaming.NamingContextPackage.NotFound e) { |
137 |
throw new RuntimeException("\nCRITICAL:Naming Service Not Found - when binding " + name + "!\n" + |
138 |
" Please check with your CORBA naming service provider."); |
139 |
} catch (org.omg.CosNaming.NamingContextPackage.AlreadyBound e) { |
140 |
throw new RuntimeException("\nCRITICAL:Already Bound - when binding " + name + "!\n" + |
141 |
" Another component with this name is already running."); |
142 |
} |
143 |
} |
144 |
|
145 |
/** |
146 |
* Activates the POA |
147 |
* This will throw a RuntimeException if there is an error |
148 |
*/ |
149 |
public void activatePOA() { |
150 |
try { |
151 |
getRootPOA().the_POAManager().activate(); |
152 |
} catch (org.omg.PortableServer.POAManagerPackage.AdapterInactive e) { |
153 |
throw new RuntimeException("\nCRITICAL:POA Set Inactive - when trying to activate POA!\n" + |
154 |
" This indicates an error with your ORB."); |
155 |
} |
156 |
} |
157 |
|
158 |
/** |
159 |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
160 |
* method to provide clean logging (every class should have this). |
161 |
* |
162 |
* @return the name of this class and its CVS revision |
163 |
*/ |
164 |
public String toString() { |
165 |
return getName() + "{" + getClass().getName() + "}(" + REVISION.substring(11, REVISION.length() - 2) + ")"; |
166 |
} |
167 |
|
168 |
//---PRIVATE METHODS--- |
169 |
|
170 |
//---ACCESSOR/MUTATOR METHODS--- |
171 |
|
172 |
/** |
173 |
* Returns a reference to the ORB |
174 |
* |
175 |
* @return the reference this class holds |
176 |
*/ |
177 |
public ORB getORB() { |
178 |
return _orb; |
179 |
} |
180 |
|
181 |
/** |
182 |
* Returns a reference to the Root POA |
183 |
* This will throw a RuntimeException if there is an error |
184 |
* |
185 |
* @return the reference this class holds |
186 |
*/ |
187 |
public POA getRootPOA() { |
188 |
if (_rootPOA == null) { |
189 |
org.omg.CORBA.Object objRef = null; |
190 |
try { |
191 |
objRef = getORB().resolve_initial_references("RootPOA"); |
192 |
} catch (org.omg.CORBA.ORBPackage.InvalidName e) { |
193 |
throw new RuntimeException("\nCRITICAL:Unable to resolve reference to the RootPOA!\n" + |
194 |
" This indicates an error with your ORB."); |
195 |
} |
196 |
_rootPOA = POAHelper.narrow(objRef); |
197 |
} |
198 |
return _rootPOA; |
199 |
} |
200 |
|
201 |
/** |
202 |
* Returns a reference to the Naming Service |
203 |
* This will throw a RuntimeException if there is an error |
204 |
* |
205 |
* @return the reference this class holds |
206 |
*/ |
207 |
public NamingContextExt getNS() { |
208 |
if (_ns == null) { |
209 |
org.omg.CORBA.Object objRef = null; |
210 |
try { |
211 |
objRef = getORB().resolve_initial_references("NameService"); |
212 |
} catch (org.omg.CORBA.ORBPackage.InvalidName e) { |
213 |
throw new RuntimeException("\nCRITICAL:Unable to resolve reference to the Naming Service!\n" + |
214 |
" Please check with your CORBA naming service provider."); |
215 |
} |
216 |
_ns = NamingContextExtHelper.narrow(objRef); |
217 |
} |
218 |
return _ns; |
219 |
} |
220 |
|
221 |
/** |
222 |
* Returns a reference to the configuration manager |
223 |
* This will throw a RuntimeException if there is an error |
224 |
* |
225 |
* @return the reference this class holds |
226 |
*/ |
227 |
public ConfigurationManager getCM() { |
228 |
if (_cm == null) { |
229 |
_cm = ConfigurationManagerHelper.narrow(getCORBARef("iscream.ConfigurationManager")); |
230 |
} |
231 |
return _cm; |
232 |
} |
233 |
|
234 |
/** |
235 |
* Returns a reference to the logger |
236 |
* This will throw a RuntimeException if there is an error |
237 |
* |
238 |
* @return the reference this class holds |
239 |
*/ |
240 |
public Logger getLogger() { |
241 |
if (_logger == null) { |
242 |
_logger = LoggerHelper.narrow(getCORBARef("iscream.Logger")); |
243 |
} |
244 |
return _logger; |
245 |
} |
246 |
|
247 |
/** |
248 |
* Sets the reference to the name |
249 |
* |
250 |
* @param the new name |
251 |
*/ |
252 |
public void setName(String name) { |
253 |
_name = name; |
254 |
} |
255 |
|
256 |
/** |
257 |
* Returns a reference to the name |
258 |
* |
259 |
* @return the reference this class holds |
260 |
*/ |
261 |
public String getName() { |
262 |
return _name; |
263 |
} |
264 |
|
265 |
//---ATTRIBUTES--- |
266 |
|
267 |
/** |
268 |
* The ORB |
269 |
*/ |
270 |
private ORB _orb; |
271 |
|
272 |
/** |
273 |
* The Naming Service |
274 |
*/ |
275 |
private NamingContextExt _ns; |
276 |
|
277 |
/** |
278 |
* The Root POA |
279 |
*/ |
280 |
private POA _rootPOA; |
281 |
|
282 |
/** |
283 |
* The configuration manager |
284 |
*/ |
285 |
private ConfigurationManager _cm; |
286 |
|
287 |
/** |
288 |
* The logger |
289 |
*/ |
290 |
private Logger _logger; |
291 |
|
292 |
/** |
293 |
* The name |
294 |
*/ |
295 |
private String _name; |
296 |
|
297 |
//---STATIC ATTRIBUTES--- |
298 |
|
299 |
/** |
300 |
* A reference to the single instance of this class |
301 |
*/ |
302 |
private static ReferenceManager _instance; |
303 |
} |