1 |
+ |
/* |
2 |
+ |
* i-scream central monitoring system |
3 |
+ |
* Copyright (C) 2000-2002 i-scream |
4 |
+ |
* |
5 |
+ |
* This program is free software; you can redistribute it and/or |
6 |
+ |
* modify it under the terms of the GNU General Public License |
7 |
+ |
* as published by the Free Software Foundation; either version 2 |
8 |
+ |
* of the License, or (at your option) any later version. |
9 |
+ |
* |
10 |
+ |
* This program is distributed in the hope that it will be useful, |
11 |
+ |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
+ |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 |
+ |
* GNU General Public License for more details. |
14 |
+ |
* |
15 |
+ |
* You should have received a copy of the GNU General Public License |
16 |
+ |
* along with this program; if not, write to the Free Software |
17 |
+ |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
18 |
+ |
*/ |
19 |
+ |
|
20 |
|
//---PACKAGE DECLARATION--- |
21 |
< |
package uk.ac.ukc.iscream.util; |
21 |
> |
package uk.org.iscream.cms.server.componentmanager; |
22 |
|
|
23 |
|
//---IMPORTS--- |
24 |
|
import org.omg.CORBA.ORB; |
25 |
|
import org.omg.CosNaming.*; |
26 |
|
import org.omg.PortableServer.*; |
27 |
< |
import uk.ac.ukc.iscream.core.*; |
27 |
> |
import uk.org.iscream.cms.server.core.*; |
28 |
> |
import uk.org.iscream.cms.server.util.*; |
29 |
|
|
30 |
|
/** |
31 |
|
* This class returns references for global system objects. |
53 |
|
//---STATIC METHODS--- |
54 |
|
|
55 |
|
/** |
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 |
59 |
> |
* This creates the single instance of the ReferenceManager |
60 |
> |
* if it does not exist by calling the private constructor. |
61 |
|
* |
62 |
< |
* @throws NotInitialisedException if the reference manager has not been initialised |
62 |
> |
* @return a reference to the ReferenceManager |
63 |
|
*/ |
64 |
< |
public static ReferenceManager getInstance() throws NotInitialisedException { |
64 |
> |
public synchronized static ReferenceManager getInstance() { |
65 |
|
if (_instance == null) { |
66 |
< |
throw new NotInitialisedException("attempt to obtain reference when not initialised"); |
66 |
> |
_instance = new ReferenceManager(); |
67 |
|
} |
68 |
|
return _instance; |
69 |
|
} |
73 |
|
/** |
74 |
|
* This is a private constructor |
75 |
|
* 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 |
76 |
|
*/ |
77 |
< |
private ReferenceManager(String[] args, String name) { |
78 |
< |
_orb = ORB.init(args, null); |
81 |
< |
_name = name; |
82 |
< |
getLogger().write(toString(), Logger.SYSINIT, "created"); |
77 |
> |
private ReferenceManager() { |
78 |
> |
_orb = ORB.init(new String[] {}, null); |
79 |
|
} |
80 |
|
|
81 |
|
//---PUBLIC METHODS--- |
83 |
|
/** |
84 |
|
* Obtains a CORBA reference through the naming service |
85 |
|
* for the named object. |
86 |
< |
* This will throw a RuntimeException if there is an error |
86 |
> |
* Calls the dieWithError() if any exceptions happen! |
87 |
|
* |
88 |
|
* @param name the name of the CORBA object to resolve |
89 |
|
* |
94 |
|
try { |
95 |
|
objRef = getNS().resolve(getNS().to_name(name)); |
96 |
|
} catch (org.omg.CosNaming.NamingContextPackage.CannotProceed e) { |
97 |
< |
throw new RuntimeException("\nCRITICAL:Naming Service Cannot Proceed - when resolving reference to " + name + "!\n" + |
98 |
< |
" Please check with your CORBA naming service provider."); |
97 |
> |
dieWithError("\nCRITICAL:Naming Service Cannot Proceed - when resolving reference to " + name + "!\n" + |
98 |
> |
" Please check with your CORBA naming service provider."); |
99 |
|
} catch (org.omg.CosNaming.NamingContextPackage.InvalidName e) { |
100 |
< |
throw new RuntimeException("\nCRITICAL:Invalid Name - when resolving reference to " + name + "!\n" + |
101 |
< |
" Please check with your CORBA naming service provider."); |
100 |
> |
dieWithError("\nCRITICAL:Invalid Name - when resolving reference to " + name + "!\n" + |
101 |
> |
" Please check with your CORBA naming service provider."); |
102 |
|
} catch (org.omg.CosNaming.NamingContextPackage.NotFound e) { |
103 |
< |
throw new RuntimeException("\nCRITICAL:Not Found - when resolving reference to " + name + "!\n" + |
104 |
< |
" Please check that this component is running."); |
103 |
> |
recoverWithError("\nCRITICAL:Not Found - when resolving reference to " + name + "!\n" + |
104 |
> |
" Please check that this component is running."); |
105 |
|
} |
106 |
|
return objRef; |
107 |
|
} |
109 |
|
/** |
110 |
|
* Binds a given servant with the given name |
111 |
|
* to the naming service. |
112 |
< |
* This will throw a RuntimeException if there is an error |
112 |
> |
* Calls the dieWithError() if any exceptions happen! |
113 |
|
* |
114 |
|
* @param objRef a reverence to the servant object |
115 |
|
* @param name the name to bind to the naming service with |
118 |
|
try { |
119 |
|
getNS().bind(getNS().to_name(name), getRootPOA().servant_to_reference(objRef)); |
120 |
|
} catch (org.omg.PortableServer.POAPackage.WrongPolicy e) { |
121 |
< |
throw new RuntimeException("\nCRITICAL:Wrong POA Policy - when binding " + name + "!\n" + |
122 |
< |
" This indicates an error with your ORB."); |
121 |
> |
dieWithError("\nCRITICAL:Wrong POA Policy - when binding " + name + "!\n" + |
122 |
> |
" This indicates an error with your ORB."); |
123 |
|
} catch (org.omg.PortableServer.POAPackage.ServantNotActive e) { |
124 |
< |
throw new RuntimeException("\nCRITICAL:ServantNotActive - when binding " + name + "!\n" + |
125 |
< |
" This indicates an error with your ORB."); |
124 |
> |
dieWithError("\nCRITICAL:ServantNotActive - when binding " + name + "!\n" + |
125 |
> |
" This indicates an error with your ORB."); |
126 |
|
} catch (org.omg.CosNaming.NamingContextPackage.InvalidName e) { |
127 |
< |
throw new RuntimeException("\nCRITICAL:Invalid Name - when binding " + name + "!\n" + |
128 |
< |
" Please check with your CORBA naming service provider."); |
127 |
> |
dieWithError("\nCRITICAL:Invalid Name - when binding " + name + "!\n" + |
128 |
> |
" Please check with your CORBA naming service provider."); |
129 |
|
} catch (org.omg.CosNaming.NamingContextPackage.CannotProceed e) { |
130 |
< |
throw new RuntimeException("\nCRITICAL:Naming Service Cannot Proceed - when binding " + name + "!\n" + |
131 |
< |
" Please check with your CORBA naming service provider."); |
130 |
> |
dieWithError("\nCRITICAL:Naming Service Cannot Proceed - when binding " + name + "!\n" + |
131 |
> |
" Please check with your CORBA naming service provider."); |
132 |
|
} catch (org.omg.CosNaming.NamingContextPackage.NotFound e) { |
133 |
< |
throw new RuntimeException("\nCRITICAL:Naming Service Not Found - when binding " + name + "!\n" + |
134 |
< |
" Please check with your CORBA naming service provider."); |
133 |
> |
recoverWithError("\nCRITICAL:Naming Service Not Found - when binding " + name + "!\n" + |
134 |
> |
" Please check with your CORBA naming service provider."); |
135 |
|
} catch (org.omg.CosNaming.NamingContextPackage.AlreadyBound e) { |
136 |
< |
throw new RuntimeException("\nCRITICAL:Already Bound - when binding " + name + "!\n" + |
137 |
< |
" Another component with this name is already running."); |
136 |
> |
dieWithError("\nCRITICAL:Already Bound - when binding " + name + "!\n" + |
137 |
> |
" Another component with this name is already running."); |
138 |
|
} |
139 |
|
} |
140 |
|
|
141 |
|
/** |
142 |
|
* Activates the POA |
143 |
< |
* This will throw a RuntimeException if there is an error |
143 |
> |
* Calls the dieWithError() if any exceptions happen! |
144 |
|
*/ |
145 |
|
public void activatePOA() { |
146 |
|
try { |
147 |
|
getRootPOA().the_POAManager().activate(); |
148 |
|
} catch (org.omg.PortableServer.POAManagerPackage.AdapterInactive e) { |
149 |
< |
throw new RuntimeException("\nCRITICAL:POA Set Inactive - when trying to activate POA!\n" + |
150 |
< |
" This indicates an error with your ORB."); |
149 |
> |
dieWithError("\nCRITICAL:POA Set Inactive - when trying to activate POA!\n" + |
150 |
> |
" This indicates an error with your ORB."); |
151 |
|
} |
152 |
|
} |
153 |
|
|
155 |
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
156 |
|
* method to provide clean logging (every class should have this). |
157 |
|
* |
158 |
+ |
* This uses the uk.org.iscream.cms.server.util.NameFormat class |
159 |
+ |
* to format the toString() |
160 |
+ |
* |
161 |
|
* @return the name of this class and its CVS revision |
162 |
|
*/ |
163 |
|
public String toString() { |
164 |
< |
return getName() + "{" + getClass().getName() + "}(" + REVISION.substring(11, REVISION.length() - 2) + ")"; |
164 |
> |
return FormatName.getName( |
165 |
> |
_name, |
166 |
> |
getClass().getName(), |
167 |
> |
REVISION); |
168 |
|
} |
169 |
|
|
170 |
|
//---PRIVATE METHODS--- |
171 |
|
|
172 |
+ |
/** |
173 |
+ |
* If there are any CORBA errors this method is called with the |
174 |
+ |
* error message. |
175 |
+ |
* |
176 |
+ |
* The exception this throws is generic to CORBA communication |
177 |
+ |
* problems, this is a Runtime exception and is typically only |
178 |
+ |
* explicitly caught in the ComponentManager for the current VM |
179 |
+ |
* The ComponentManager can then decide what course of action to |
180 |
+ |
* take. |
181 |
+ |
* |
182 |
+ |
* Also, if it can, it will log the message with the logger. |
183 |
+ |
* |
184 |
+ |
* @param message the error message to die with |
185 |
+ |
*/ |
186 |
+ |
private void recoverWithError(String message) throws ComponentCORBAException { |
187 |
+ |
if (_logger != null) { |
188 |
+ |
_logger.write(toString(), Logger.WARNING, "component CORBA error - " + message); |
189 |
+ |
} |
190 |
+ |
throw new ComponentCORBAException(message); |
191 |
+ |
} |
192 |
+ |
|
193 |
+ |
|
194 |
+ |
/** |
195 |
+ |
* If there are any CORBA errors this method is called with the |
196 |
+ |
* error message. |
197 |
+ |
* |
198 |
+ |
* Currently this prints the error, on the local err stream. Will |
199 |
+ |
* print to the logger if one is available. |
200 |
+ |
* |
201 |
+ |
* ANY CALLS TO THIS METHOD CAUSE |
202 |
+ |
* THE SYSTEM TO EXIT WITH A NON |
203 |
+ |
* ZERO CODE! |
204 |
+ |
* |
205 |
+ |
* @param message the error message to die with |
206 |
+ |
*/ |
207 |
+ |
private void dieWithError(String message) { |
208 |
+ |
System.err.println(message); |
209 |
+ |
if (_logger != null) { |
210 |
+ |
_logger.write(toString(), Logger.FATAL, message); |
211 |
+ |
} |
212 |
+ |
System.exit(1); |
213 |
+ |
} |
214 |
+ |
|
215 |
+ |
|
216 |
+ |
|
217 |
|
//---ACCESSOR/MUTATOR METHODS--- |
218 |
|
|
219 |
|
/** |
227 |
|
|
228 |
|
/** |
229 |
|
* Returns a reference to the Root POA |
230 |
< |
* This will throw a RuntimeException if there is an error |
230 |
> |
* Calls the dieWithError() if any exceptions happen! |
231 |
|
* |
232 |
|
* @return the reference this class holds |
233 |
|
*/ |
237 |
|
try { |
238 |
|
objRef = getORB().resolve_initial_references("RootPOA"); |
239 |
|
} catch (org.omg.CORBA.ORBPackage.InvalidName e) { |
240 |
< |
throw new RuntimeException("\nCRITICAL:Unable to resolve reference to the RootPOA!\n" + |
241 |
< |
" This indicates an error with your ORB."); |
240 |
> |
dieWithError("\nCRITICAL:Unable to resolve reference to the RootPOA!\n" + |
241 |
> |
" This indicates an error with your ORB."); |
242 |
|
} |
243 |
|
_rootPOA = POAHelper.narrow(objRef); |
244 |
|
} |
247 |
|
|
248 |
|
/** |
249 |
|
* Returns a reference to the Naming Service |
250 |
< |
* This will throw a RuntimeException if there is an error |
250 |
> |
* Calls the dieWithError() if any exceptions happen! |
251 |
|
* |
252 |
|
* @return the reference this class holds |
253 |
|
*/ |
257 |
|
try { |
258 |
|
objRef = getORB().resolve_initial_references("NameService"); |
259 |
|
} catch (org.omg.CORBA.ORBPackage.InvalidName e) { |
260 |
< |
throw new RuntimeException("\nCRITICAL:Unable to resolve reference to the Naming Service!\n" + |
261 |
< |
" Please check with your CORBA naming service provider."); |
260 |
> |
dieWithError("\nCRITICAL:Unable to resolve reference to the Naming Service!\n" + |
261 |
> |
" Please check with your CORBA naming service provider."); |
262 |
|
} |
263 |
|
_ns = NamingContextExtHelper.narrow(objRef); |
264 |
|
} |
265 |
+ |
// check we managed to talk to the naming service |
266 |
+ |
if (_ns == null) { |
267 |
+ |
dieWithError("\nCRITICAL:Unable to resolve reference to the Naming Service!\n" + |
268 |
+ |
" Please check with your CORBA naming service provider."); |
269 |
+ |
} |
270 |
|
return _ns; |
271 |
|
} |
272 |
|
|
273 |
|
/** |
274 |
|
* Returns a reference to the configuration manager |
275 |
< |
* This will throw a RuntimeException if there is an error |
275 |
> |
* Calls the dieWithError() if any exceptions happen! |
276 |
|
* |
277 |
|
* @return the reference this class holds |
278 |
|
*/ |
280 |
|
if (_cm == null) { |
281 |
|
_cm = ConfigurationManagerHelper.narrow(getCORBARef("iscream.ConfigurationManager")); |
282 |
|
} |
283 |
+ |
// check we managed to talk to the configuration manager |
284 |
+ |
if (_cm == null) { |
285 |
+ |
dieWithError("\nCRITICAL:Unable to resolve reference to the Configuration Manager!\n" + |
286 |
+ |
" Please check with your CORBA naming service provider."); |
287 |
+ |
} |
288 |
|
return _cm; |
289 |
|
} |
290 |
|
|
291 |
|
/** |
292 |
|
* Returns a reference to the logger |
293 |
< |
* This will throw a RuntimeException if there is an error |
293 |
> |
* This will throw a Error if there is an error |
294 |
|
* |
295 |
|
* @return the reference this class holds |
296 |
|
*/ |
301 |
|
return _logger; |
302 |
|
} |
303 |
|
|
247 |
– |
/** |
248 |
– |
* Returns a reference to the name |
249 |
– |
* |
250 |
– |
* @return the reference this class holds |
251 |
– |
*/ |
252 |
– |
public String getName() { |
253 |
– |
return _name; |
254 |
– |
} |
255 |
– |
|
304 |
|
//---ATTRIBUTES--- |
305 |
|
|
306 |
|
/** |
329 |
|
private Logger _logger; |
330 |
|
|
331 |
|
/** |
332 |
< |
* The name |
332 |
> |
* This is the friendly identifier of the |
333 |
> |
* component this class is running in. |
334 |
> |
* eg, a Filter may be called "filter1", |
335 |
> |
* If this class does not have an owning |
336 |
> |
* component, a name from the configuration |
337 |
> |
* can be placed here. This name could also |
338 |
> |
* be changed to null for utility classes. |
339 |
|
*/ |
340 |
< |
private String _name; |
340 |
> |
private String _name = null; |
341 |
|
|
342 |
|
//---STATIC ATTRIBUTES--- |
343 |
|
|
344 |
|
/** |
345 |
|
* A reference to the single instance of this class |
346 |
|
*/ |
347 |
< |
private static ReferenceManager _instance; |
347 |
> |
private static ReferenceManager _instance = null; |
348 |
|
} |