1 |
ajm |
1.1 |
//---PACKAGE DECLARATION--- |
2 |
|
|
package uk.ac.ukc.iscream.refman; |
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 handles ORB initialisation. |
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: tdb1 $ |
22 |
|
|
* @version $Id: TemplateClass.java,v 1.5 2000/11/13 18:20:09 tdb1 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.5 $"; |
32 |
|
|
|
33 |
|
|
//---STATIC METHODS--- |
34 |
|
|
|
35 |
|
|
/** |
36 |
|
|
* This essentially formats the toString() source for an |
37 |
|
|
* object wanting to make a log entry. |
38 |
|
|
* |
39 |
|
|
* @param source the source of the call |
40 |
|
|
* @param rev the CVS revision number of the caller |
41 |
|
|
* |
42 |
|
|
* @return the formatted string |
43 |
|
|
|
44 |
|
|
public static String logString(Object source, String rev) { |
45 |
|
|
if (_instance == null) { |
46 |
|
|
return "unamed{" + source.getClass().getName() + "}(" + rev.substring(11, rev.length() - 2) + ")"; |
47 |
|
|
} |
48 |
|
|
return getInstance().getName() + "{" + source.getClass().getName() + "}(" + rev.substring(11, rev.length() - 2) + ")"; |
49 |
|
|
} |
50 |
|
|
` */ |
51 |
|
|
|
52 |
|
|
/** |
53 |
|
|
* This creates the single instance of the ReferenceManager by |
54 |
|
|
* calling the private constructor. If it is called twice, |
55 |
|
|
* it detects an instance already exits and throws an exception. |
56 |
|
|
* |
57 |
|
|
* @param args the args to be passed to the ORB |
58 |
|
|
* @param name the name of the component that will use this singleton |
59 |
|
|
* |
60 |
|
|
* @return a reference to the ReferenceManager |
61 |
|
|
* |
62 |
|
|
* @throws AlreadyInitialisedException if this method has already been called |
63 |
|
|
*/ |
64 |
|
|
public static ReferenceManager init(String[] args, String name) throws AlreadyInitialisedException { |
65 |
|
|
if (_instance != null) { |
66 |
|
|
throw new AlreadyInitialisedException("init has already been called"); |
67 |
|
|
} |
68 |
|
|
_instance = new ReferenceManager(args, name); |
69 |
|
|
return _instance; |
70 |
|
|
} |
71 |
|
|
|
72 |
|
|
/** |
73 |
|
|
* This returns a reference to the single instance of the |
74 |
|
|
* ReferenceManager. |
75 |
|
|
* |
76 |
|
|
* @return a reference to the ReferenceManager |
77 |
|
|
* |
78 |
|
|
* @throws NotInitialisedException if the reference manager has not been initialised |
79 |
|
|
*/ |
80 |
|
|
public static ReferenceManager getInstance() throws NotInitialisedException { |
81 |
|
|
if (_instance == null) { |
82 |
|
|
throw new NotInitialisedException("attempt to obtain reference when not initialised"); |
83 |
|
|
} |
84 |
|
|
return _instance; |
85 |
|
|
} |
86 |
|
|
|
87 |
|
|
//---CONSTRUCTORS--- |
88 |
|
|
|
89 |
|
|
/** |
90 |
|
|
* This is a private constructor |
91 |
|
|
* This ensures that the system performs according to a Singleton design pattern |
92 |
|
|
* |
93 |
|
|
* @param args the args to be passed to the ORB |
94 |
|
|
* @param name the name of the component that will use this singleton |
95 |
|
|
*/ |
96 |
|
|
private ReferenceManager(String[] args, String name) { |
97 |
|
|
_orb = ORB.init(args, null); |
98 |
|
|
_name = name; |
99 |
|
|
getLogger().write(toString(), Logger.SYSINIT, "created"); |
100 |
|
|
} |
101 |
|
|
|
102 |
|
|
//---PUBLIC METHODS--- |
103 |
|
|
|
104 |
|
|
/** |
105 |
|
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
106 |
|
|
* method to provide clean logging (every class should have this). |
107 |
|
|
* |
108 |
|
|
* @return the name of this class and its CVS revision |
109 |
|
|
*/ |
110 |
|
|
public String toString() { |
111 |
|
|
return getName() + "{" + getClass().getName() + "}(" + REVISION.substring(11, REVISION.length() - 2) + ")"; |
112 |
|
|
} |
113 |
|
|
|
114 |
|
|
//---PRIVATE METHODS--- |
115 |
|
|
|
116 |
|
|
//---ACCESSOR/MUTATOR METHODS--- |
117 |
|
|
|
118 |
|
|
/** |
119 |
|
|
* Returns a reference to the ORB |
120 |
|
|
* |
121 |
|
|
* @return the reference this class holds |
122 |
|
|
*/ |
123 |
|
|
public ORB getORB() { |
124 |
|
|
return _orb; |
125 |
|
|
} |
126 |
|
|
|
127 |
|
|
/** |
128 |
|
|
* Returns a reference to the Root POA |
129 |
|
|
* This will throw a RuntimeException if there is an error |
130 |
|
|
* |
131 |
|
|
* @return the reference this class holds |
132 |
|
|
*/ |
133 |
|
|
public POA getRootPOA() { |
134 |
|
|
if (_rootPOA == null) { |
135 |
|
|
org.omg.CORBA.Object objRef = null; |
136 |
|
|
try { |
137 |
|
|
objRef = getORB().resolve_initial_references("RootPOA"); |
138 |
|
|
} catch (org.omg.CORBA.ORBPackage.InvalidName e) { |
139 |
|
|
throw new RuntimeException("CRITICAL:Unable to resolve reference to the RootPOA!\n" + |
140 |
|
|
" This indicates an error with your ORB."); |
141 |
|
|
} |
142 |
|
|
_rootPOA = POAHelper.narrow(objRef); |
143 |
|
|
} |
144 |
|
|
return _rootPOA; |
145 |
|
|
} |
146 |
|
|
|
147 |
|
|
/** |
148 |
|
|
* Returns a reference to the Naming Service |
149 |
|
|
* This will throw a RuntimeException if there is an error |
150 |
|
|
* |
151 |
|
|
* @return the reference this class holds |
152 |
|
|
*/ |
153 |
|
|
public NamingContextExt getNS() { |
154 |
|
|
if (_ns == null) { |
155 |
|
|
org.omg.CORBA.Object objRef = null; |
156 |
|
|
try { |
157 |
|
|
objRef = getORB().resolve_initial_references("NameService"); |
158 |
|
|
} catch (org.omg.CORBA.ORBPackage.InvalidName e) { |
159 |
|
|
throw new RuntimeException("CRITICAL:Unable to resolve reference to the Naming Service!\n" + |
160 |
|
|
" Please check with your CORBA naming service provider."); |
161 |
|
|
} |
162 |
|
|
_ns = NamingContextExtHelper.narrow(objRef); |
163 |
|
|
} |
164 |
|
|
return _ns; |
165 |
|
|
} |
166 |
|
|
|
167 |
|
|
/** |
168 |
|
|
* Returns a reference to the configuration manager |
169 |
|
|
* This will throw a RuntimeException if there is an error |
170 |
|
|
* |
171 |
|
|
* @return the reference this class holds |
172 |
|
|
*/ |
173 |
|
|
public ConfigurationManager getCM() { |
174 |
|
|
if (_cm == null) { |
175 |
|
|
org.omg.CORBA.Object objRef = null; |
176 |
|
|
try { |
177 |
|
|
objRef = getNS().resolve(getNS().to_name("iscream.ConfigurationManager")); |
178 |
|
|
} catch (org.omg.CosNaming.NamingContextPackage.CannotProceed e) { |
179 |
|
|
throw new RuntimeException("CRITICAL:Cannot Proceed - when resolving reference to iscream.ConfigurationManager!\n" + |
180 |
|
|
" This indicates an error with your ORB."); |
181 |
|
|
} catch (org.omg.CosNaming.NamingContextPackage.InvalidName e) { |
182 |
|
|
throw new RuntimeException("CRITICAL:Invalid Name - when resolving reference to iscream.ConfigurationManager!\n" + |
183 |
|
|
" This indicates an error with your ORB."); |
184 |
|
|
} catch (org.omg.CosNaming.NamingContextPackage.NotFound e) { |
185 |
|
|
throw new RuntimeException("CRITICAL:Not Found - when resolving reference to iscream.ConfigurationManager!\n" + |
186 |
|
|
" Please check that this component is running."); |
187 |
|
|
} |
188 |
|
|
_cm = ConfigurationManagerHelper.narrow(objRef); |
189 |
|
|
} |
190 |
|
|
return _cm; |
191 |
|
|
} |
192 |
|
|
|
193 |
|
|
/** |
194 |
|
|
* Returns a reference to the logger |
195 |
|
|
* This will throw a RuntimeException if there is an error |
196 |
|
|
* |
197 |
|
|
* @return the reference this class holds |
198 |
|
|
*/ |
199 |
|
|
public Logger getLogger() { |
200 |
|
|
if (_logger == null) { |
201 |
|
|
org.omg.CORBA.Object objRef = null; |
202 |
|
|
try { |
203 |
|
|
objRef = getNS().resolve(getNS().to_name("iscream.Logger")); |
204 |
|
|
} catch (org.omg.CosNaming.NamingContextPackage.CannotProceed e) { |
205 |
|
|
throw new RuntimeException("CRITICAL:Cannot Proceed - when resolving reference to iscream.Logger!\n" + |
206 |
|
|
" This indicates an error with your ORB."); |
207 |
|
|
} catch (org.omg.CosNaming.NamingContextPackage.InvalidName e) { |
208 |
|
|
throw new RuntimeException("CRITICAL:Invalid Name - when resolving reference to iscream.Logger!\n" + |
209 |
|
|
" This indicates an error with your ORB."); |
210 |
|
|
} catch (org.omg.CosNaming.NamingContextPackage.NotFound e) { |
211 |
|
|
throw new RuntimeException("CRITICAL:Not Found - when resolving reference to iscream.Logger!\n" + |
212 |
|
|
" Please check that this component is running."); |
213 |
|
|
} |
214 |
|
|
_logger = LoggerHelper.narrow(objRef); |
215 |
|
|
} |
216 |
|
|
return _logger; |
217 |
|
|
} |
218 |
|
|
|
219 |
|
|
/** |
220 |
|
|
* Returns a reference to the name |
221 |
|
|
* |
222 |
|
|
* @return the reference this class holds |
223 |
|
|
*/ |
224 |
|
|
public String getName() { |
225 |
|
|
return _name; |
226 |
|
|
} |
227 |
|
|
|
228 |
|
|
//---ATTRIBUTES--- |
229 |
|
|
|
230 |
|
|
/** |
231 |
|
|
* The ORB |
232 |
|
|
*/ |
233 |
|
|
private ORB _orb; |
234 |
|
|
|
235 |
|
|
/** |
236 |
|
|
* The Naming Service |
237 |
|
|
*/ |
238 |
|
|
private NamingContextExt _ns; |
239 |
|
|
|
240 |
|
|
/** |
241 |
|
|
* The Root POA |
242 |
|
|
*/ |
243 |
|
|
private POA _rootPOA; |
244 |
|
|
|
245 |
|
|
/** |
246 |
|
|
* The configuration manager |
247 |
|
|
*/ |
248 |
|
|
private ConfigurationManager _cm; |
249 |
|
|
|
250 |
|
|
/** |
251 |
|
|
* The logger |
252 |
|
|
*/ |
253 |
|
|
private Logger _logger; |
254 |
|
|
|
255 |
|
|
/** |
256 |
|
|
* The name |
257 |
|
|
*/ |
258 |
|
|
private String _name; |
259 |
|
|
|
260 |
|
|
//---STATIC ATTRIBUTES--- |
261 |
|
|
|
262 |
|
|
/** |
263 |
|
|
* A reference to the single instance of this class |
264 |
|
|
*/ |
265 |
|
|
private static ReferenceManager _instance; |
266 |
|
|
} |