ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/util/uk/org/iscream/cms/util/ReferenceManager.java
Revision: 1.6
Committed: Tue Dec 12 17:13:22 2000 UTC (23 years, 4 months ago) by ajm
Branch: MAIN
Changes since 1.5: +70 -37 lines
Log Message:
Modified
Has deprecated methods for legacy code
Conforms to new standards
Has nicer error handling...

File Contents

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