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.9
Committed: Thu Jan 18 22:49:45 2001 UTC (23 years, 3 months ago) by tdb
Branch: MAIN
CVS Tags: HEAD
Changes since 1.8: +2 -2 lines
State: FILE REMOVED
Log Message:
These three files have been moved into the uk.ac.ukc.iscream.ComponentManager
package. This was because they weren't really suited to util, which has now
become a package of classes for use in the whole system, not just the server.

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 tdb 1.9 * @version $Id: ReferenceManager.java,v 1.8 2000/12/13 13:42:50 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 tdb 1.9 public final String REVISION = "$Revision: 1.8 $";
32 ajm 1.1
33     //---STATIC METHODS---
34    
35     /**
36     * This returns a reference to the single instance of the
37     * ReferenceManager.
38     *
39 ajm 1.6 * This creates the single instance of the ReferenceManager
40     * if it does not exist by calling the private constructor.
41     *
42 ajm 1.1 * @return a reference to the ReferenceManager
43     */
44 ajm 1.6 public static ReferenceManager getInstance() {
45 ajm 1.1 if (_instance == null) {
46 ajm 1.6 _instance = new ReferenceManager();
47 ajm 1.1 }
48     return _instance;
49     }
50    
51     //---CONSTRUCTORS---
52    
53     /**
54     * This is a private constructor
55     * This ensures that the system performs according to a Singleton design pattern
56 ajm 1.6 */
57     private ReferenceManager() {
58     _orb = ORB.init(new String[] {}, null);
59     }
60 ajm 1.1
61     //---PUBLIC METHODS---
62    
63     /**
64 ajm 1.2 * Obtains a CORBA reference through the naming service
65     * for the named object.
66 ajm 1.7 * Calls the dieWithError() if any exceptions happen!
67 ajm 1.2 *
68     * @param name the name of the CORBA object to resolve
69     *
70     * @return a reference to the object
71     */
72     public org.omg.CORBA.Object getCORBARef(String name) {
73     org.omg.CORBA.Object objRef = null;
74     try {
75     objRef = getNS().resolve(getNS().to_name(name));
76     } catch (org.omg.CosNaming.NamingContextPackage.CannotProceed e) {
77 ajm 1.6 dieWithError("\nCRITICAL:Naming Service Cannot Proceed - when resolving reference to " + name + "!\n" +
78 ajm 1.5 " Please check with your CORBA naming service provider.");
79 ajm 1.2 } catch (org.omg.CosNaming.NamingContextPackage.InvalidName e) {
80 ajm 1.6 dieWithError("\nCRITICAL:Invalid Name - when resolving reference to " + name + "!\n" +
81 ajm 1.5 " Please check with your CORBA naming service provider.");
82 ajm 1.2 } catch (org.omg.CosNaming.NamingContextPackage.NotFound e) {
83 ajm 1.6 dieWithError("\nCRITICAL:Not Found - when resolving reference to " + name + "!\n" +
84 ajm 1.5 " Please check that this component is running.");
85 ajm 1.2 }
86     return objRef;
87     }
88    
89     /**
90     * Binds a given servant with the given name
91     * to the naming service.
92 ajm 1.7 * Calls the dieWithError() if any exceptions happen!
93 ajm 1.2 *
94     * @param objRef a reverence to the servant object
95     * @param name the name to bind to the naming service with
96     */
97     public void bindToOrb(org.omg.PortableServer.Servant objRef, String name) {
98     try {
99     getNS().bind(getNS().to_name(name), getRootPOA().servant_to_reference(objRef));
100     } catch (org.omg.PortableServer.POAPackage.WrongPolicy e) {
101 ajm 1.6 dieWithError("\nCRITICAL:Wrong POA Policy - when binding " + name + "!\n" +
102 ajm 1.5 " This indicates an error with your ORB.");
103 ajm 1.2 } catch (org.omg.PortableServer.POAPackage.ServantNotActive e) {
104 ajm 1.6 dieWithError("\nCRITICAL:ServantNotActive - when binding " + name + "!\n" +
105 ajm 1.5 " This indicates an error with your ORB.");
106 ajm 1.2 } catch (org.omg.CosNaming.NamingContextPackage.InvalidName e) {
107 ajm 1.6 dieWithError("\nCRITICAL:Invalid Name - when binding " + name + "!\n" +
108 ajm 1.5 " Please check with your CORBA naming service provider.");
109 ajm 1.2 } catch (org.omg.CosNaming.NamingContextPackage.CannotProceed e) {
110 ajm 1.6 dieWithError("\nCRITICAL:Naming Service Cannot Proceed - when binding " + name + "!\n" +
111 ajm 1.5 " Please check with your CORBA naming service provider.");
112 ajm 1.2 } catch (org.omg.CosNaming.NamingContextPackage.NotFound e) {
113 ajm 1.6 dieWithError("\nCRITICAL:Naming Service Not Found - when binding " + name + "!\n" +
114 ajm 1.5 " Please check with your CORBA naming service provider.");
115 ajm 1.2 } catch (org.omg.CosNaming.NamingContextPackage.AlreadyBound e) {
116 ajm 1.6 dieWithError("\nCRITICAL:Already Bound - when binding " + name + "!\n" +
117 ajm 1.5 " Another component with this name is already running.");
118 ajm 1.2 }
119     }
120    
121     /**
122     * Activates the POA
123 ajm 1.7 * Calls the dieWithError() if any exceptions happen!
124 ajm 1.2 */
125     public void activatePOA() {
126     try {
127     getRootPOA().the_POAManager().activate();
128     } catch (org.omg.PortableServer.POAManagerPackage.AdapterInactive e) {
129 ajm 1.6 dieWithError("\nCRITICAL:POA Set Inactive - when trying to activate POA!\n" +
130 ajm 1.5 " This indicates an error with your ORB.");
131 ajm 1.2 }
132     }
133    
134     /**
135 ajm 1.1 * Overrides the {@link java.lang.Object#toString() Object.toString()}
136     * method to provide clean logging (every class should have this).
137     *
138 ajm 1.6 * This uses the uk.ac.ukc.iscream.util.NameFormat class
139     * to format the toString()
140     *
141 ajm 1.1 * @return the name of this class and its CVS revision
142     */
143     public String toString() {
144 ajm 1.6 return FormatName.getName(
145 ajm 1.7 _name,
146     getClass().getName(),
147 ajm 1.6 REVISION);
148 ajm 1.1 }
149    
150     //---PRIVATE METHODS---
151    
152 ajm 1.6 /**
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 ajm 1.1 //---ACCESSOR/MUTATOR METHODS---
174    
175     /**
176     * Returns a reference to the ORB
177     *
178     * @return the reference this class holds
179     */
180     public ORB getORB() {
181     return _orb;
182     }
183    
184     /**
185     * Returns a reference to the Root POA
186 ajm 1.7 * Calls the dieWithError() if any exceptions happen!
187 ajm 1.1 *
188     * @return the reference this class holds
189     */
190     public POA getRootPOA() {
191     if (_rootPOA == null) {
192     org.omg.CORBA.Object objRef = null;
193     try {
194     objRef = getORB().resolve_initial_references("RootPOA");
195     } catch (org.omg.CORBA.ORBPackage.InvalidName e) {
196 ajm 1.6 dieWithError("\nCRITICAL:Unable to resolve reference to the RootPOA!\n" +
197 ajm 1.5 " This indicates an error with your ORB.");
198 ajm 1.1 }
199     _rootPOA = POAHelper.narrow(objRef);
200     }
201     return _rootPOA;
202     }
203    
204     /**
205     * Returns a reference to the Naming Service
206 ajm 1.7 * Calls the dieWithError() if any exceptions happen!
207 ajm 1.1 *
208     * @return the reference this class holds
209     */
210     public NamingContextExt getNS() {
211     if (_ns == null) {
212     org.omg.CORBA.Object objRef = null;
213     try {
214     objRef = getORB().resolve_initial_references("NameService");
215     } catch (org.omg.CORBA.ORBPackage.InvalidName e) {
216 ajm 1.6 dieWithError("\nCRITICAL:Unable to resolve reference to the Naming Service!\n" +
217 ajm 1.5 " Please check with your CORBA naming service provider.");
218 ajm 1.1 }
219     _ns = NamingContextExtHelper.narrow(objRef);
220     }
221 ajm 1.5 // check we managed to talk to the naming service
222     if (_ns == null) {
223 ajm 1.6 dieWithError("\nCRITICAL:Unable to resolve reference to the Naming Service!\n" +
224 ajm 1.5 " Please check with your CORBA naming service provider.");
225     }
226 ajm 1.1 return _ns;
227     }
228    
229     /**
230     * Returns a reference to the configuration manager
231 ajm 1.7 * Calls the dieWithError() if any exceptions happen!
232 ajm 1.1 *
233     * @return the reference this class holds
234     */
235     public ConfigurationManager getCM() {
236     if (_cm == null) {
237 ajm 1.2 _cm = ConfigurationManagerHelper.narrow(getCORBARef("iscream.ConfigurationManager"));
238 ajm 1.1 }
239 ajm 1.5 // check we managed to talk to the configuration manager
240     if (_cm == null) {
241 ajm 1.6 dieWithError("\nCRITICAL:Unable to resolve reference to the Configuration Manager!\n" +
242 ajm 1.5 " Please check with your CORBA naming service provider.");
243     }
244 ajm 1.1 return _cm;
245     }
246    
247     /**
248     * Returns a reference to the logger
249 ajm 1.5 * This will throw a Error if there is an error
250 ajm 1.1 *
251     * @return the reference this class holds
252     */
253     public Logger getLogger() {
254     if (_logger == null) {
255 ajm 1.2 _logger = LoggerHelper.narrow(getCORBARef("iscream.Logger"));
256 ajm 1.1 }
257     return _logger;
258 ajm 1.4 }
259 ajm 1.1
260     //---ATTRIBUTES---
261    
262     /**
263     * The ORB
264     */
265     private ORB _orb;
266    
267     /**
268     * The Naming Service
269     */
270     private NamingContextExt _ns;
271    
272     /**
273     * The Root POA
274     */
275     private POA _rootPOA;
276    
277     /**
278     * The configuration manager
279     */
280     private ConfigurationManager _cm;
281    
282     /**
283     * The logger
284     */
285     private Logger _logger;
286    
287     /**
288 ajm 1.7 * 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 ajm 1.1 */
296 ajm 1.7 private String _name = null;
297 ajm 1.1
298     //---STATIC ATTRIBUTES---
299    
300     /**
301     * A reference to the single instance of this class
302     */
303     private static ReferenceManager _instance;
304     }