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 |
– |
/** |
36 |
|
* This returns a reference to the single instance of the |
37 |
|
* ReferenceManager. |
38 |
|
* |
39 |
< |
* @return a reference to the ReferenceManager |
39 |
> |
* This creates the single instance of the ReferenceManager |
40 |
> |
* if it does not exist by calling the private constructor. |
41 |
|
* |
42 |
< |
* @throws NotInitialisedException if the reference manager has not been initialised |
42 |
> |
* @return a reference to the ReferenceManager |
43 |
|
*/ |
44 |
< |
public static ReferenceManager getInstance() throws NotInitialisedException { |
44 |
> |
public static ReferenceManager getInstance() { |
45 |
|
if (_instance == null) { |
46 |
< |
throw new NotInitialisedException("attempt to obtain reference when not initialised"); |
46 |
> |
_instance = new ReferenceManager(); |
47 |
|
} |
48 |
|
return _instance; |
49 |
|
} |
53 |
|
/** |
54 |
|
* This is a private constructor |
55 |
|
* 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 |
56 |
|
*/ |
57 |
< |
private ReferenceManager(String[] args, String name) { |
58 |
< |
_orb = ORB.init(args, null); |
81 |
< |
_name = name; |
57 |
> |
private ReferenceManager() { |
58 |
> |
_orb = ORB.init(new String[] {}, null); |
59 |
|
} |
60 |
|
|
61 |
|
//---PUBLIC METHODS--- |
63 |
|
/** |
64 |
|
* Obtains a CORBA reference through the naming service |
65 |
|
* for the named object. |
66 |
< |
* This will throw a Error if there is an error |
66 |
> |
* Calls the dieWithError() if any exceptions happen! |
67 |
|
* |
68 |
|
* @param name the name of the CORBA object to resolve |
69 |
|
* |
74 |
|
try { |
75 |
|
objRef = getNS().resolve(getNS().to_name(name)); |
76 |
|
} catch (org.omg.CosNaming.NamingContextPackage.CannotProceed e) { |
77 |
< |
throw new Error("\nCRITICAL:Naming Service Cannot Proceed - when resolving reference to " + name + "!\n" + |
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 |
< |
throw new Error("\nCRITICAL:Invalid Name - when resolving reference to " + name + "!\n" + |
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 |
< |
throw new Error("\nCRITICAL:Not Found - when resolving reference to " + name + "!\n" + |
83 |
> |
dieWithError("\nCRITICAL:Not Found - when resolving reference to " + name + "!\n" + |
84 |
|
" Please check that this component is running."); |
85 |
|
} |
86 |
|
return objRef; |
89 |
|
/** |
90 |
|
* Binds a given servant with the given name |
91 |
|
* to the naming service. |
92 |
< |
* This will throw a Error if there is an error |
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 |
98 |
|
try { |
99 |
|
getNS().bind(getNS().to_name(name), getRootPOA().servant_to_reference(objRef)); |
100 |
|
} catch (org.omg.PortableServer.POAPackage.WrongPolicy e) { |
101 |
< |
throw new Error("\nCRITICAL:Wrong POA Policy - when binding " + name + "!\n" + |
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 |
< |
throw new Error("\nCRITICAL:ServantNotActive - when binding " + name + "!\n" + |
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 |
< |
throw new Error("\nCRITICAL:Invalid Name - when binding " + name + "!\n" + |
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 |
< |
throw new Error("\nCRITICAL:Naming Service Cannot Proceed - when binding " + name + "!\n" + |
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 |
< |
throw new Error("\nCRITICAL:Naming Service Not Found - when binding " + name + "!\n" + |
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 |
< |
throw new Error("\nCRITICAL:Already Bound - when binding " + name + "!\n" + |
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 |
< |
* This will throw a Error if there is an error |
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 |
< |
throw new Error("\nCRITICAL:POA Set Inactive - when trying to activate POA!\n" + |
129 |
> |
dieWithError("\nCRITICAL:POA Set Inactive - when trying to activate POA!\n" + |
130 |
|
" This indicates an error with your ORB."); |
131 |
|
} |
132 |
|
} |
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 getName() + "{" + getClass().getName() + "}(" + REVISION.substring(11, REVISION.length() - 2) + ")"; |
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 |
|
/** |
183 |
|
|
184 |
|
/** |
185 |
|
* Returns a reference to the Root POA |
186 |
< |
* This will throw a Error if there is an error |
186 |
> |
* Calls the dieWithError() if any exceptions happen! |
187 |
|
* |
188 |
|
* @return the reference this class holds |
189 |
|
*/ |
193 |
|
try { |
194 |
|
objRef = getORB().resolve_initial_references("RootPOA"); |
195 |
|
} catch (org.omg.CORBA.ORBPackage.InvalidName e) { |
196 |
< |
throw new Error("\nCRITICAL:Unable to resolve reference to the RootPOA!\n" + |
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); |
203 |
|
|
204 |
|
/** |
205 |
|
* Returns a reference to the Naming Service |
206 |
< |
* This will throw a Error if there is an error |
206 |
> |
* Calls the dieWithError() if any exceptions happen! |
207 |
|
* |
208 |
|
* @return the reference this class holds |
209 |
|
*/ |
213 |
|
try { |
214 |
|
objRef = getORB().resolve_initial_references("NameService"); |
215 |
|
} catch (org.omg.CORBA.ORBPackage.InvalidName e) { |
216 |
< |
throw new Error("\nCRITICAL:Unable to resolve reference to the Naming Service!\n" + |
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 |
< |
throw new Error("\nCRITICAL:Unable to resolve reference to the Naming Service!\n" + |
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; |
228 |
|
|
229 |
|
/** |
230 |
|
* Returns a reference to the configuration manager |
231 |
< |
* This will throw a Error if there is an error |
231 |
> |
* Calls the dieWithError() if any exceptions happen! |
232 |
|
* |
233 |
|
* @return the reference this class holds |
234 |
|
*/ |
238 |
|
} |
239 |
|
// check we managed to talk to the configuration manager |
240 |
|
if (_cm == null) { |
241 |
< |
throw new Error("\nCRITICAL:Unable to resolve reference to the Configuration Manager!\n" + |
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; |
256 |
|
} |
257 |
|
return _logger; |
258 |
|
} |
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 |
– |
} |
259 |
|
|
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 |
– |
|
260 |
|
//---ATTRIBUTES--- |
261 |
|
|
262 |
|
/** |
285 |
|
private Logger _logger; |
286 |
|
|
287 |
|
/** |
288 |
< |
* The name |
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; |
296 |
> |
private String _name = null; |
297 |
|
|
298 |
|
//---STATIC ATTRIBUTES--- |
299 |
|
|