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
(Generate patch)

Comparing projects/cms/source/util/uk/org/iscream/cms/util/ReferenceManager.java (file contents):
Revision 1.1 by ajm, Thu Nov 30 00:54:27 2000 UTC vs.
Revision 1.8 by ajm, Wed Dec 13 13:42:50 2000 UTC

# Line 1 | Line 1
1   //---PACKAGE DECLARATION---
2 < package uk.ac.ukc.iscream.refman;
2 > package uk.ac.ukc.iscream.util;
3  
4   //---IMPORTS---
5   import org.omg.CORBA.ORB;
# Line 11 | Line 11 | import uk.ac.ukc.iscream.core.*;
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.
14 > * are CORBA based.  It also manages the ORB for the component.
15   *
16   * It is a singleton object with a static method to obtain a
17   * reference to it.  This removes the need for passing
# Line 33 | Line 33 | public class ReferenceManager {
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    /**
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      }
# Line 89 | Line 53 | public class ReferenceManager {
53      /**
54       * This is a private constructor
55       * 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
56       */
57 <    private ReferenceManager(String[] args, String name) {
58 <        _orb = ORB.init(args, null);
98 <        _name = name;
99 <        getLogger().write(toString(), Logger.SYSINIT, "created");
57 >    private ReferenceManager() {
58 >        _orb = ORB.init(new String[] {}, null);
59      }
60  
61   //---PUBLIC METHODS---
62  
63      /**
64 +     * Obtains a CORBA reference through the naming service
65 +     * for the named object.
66 +     * Calls the dieWithError() if any exceptions happen!
67 +     *
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 +            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 +            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 +            dieWithError("\nCRITICAL:Not Found - when resolving reference to " + name + "!\n" +
84 +                              "         Please check that this component is running.");
85 +        }
86 +        return objRef;
87 +    }
88 +
89 +    /**
90 +     * Binds a given servant with the given name
91 +     * to the naming service.
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
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 +            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 +            dieWithError("\nCRITICAL:ServantNotActive - when binding " + name + "!\n" +
105 +                              "         This indicates an error with your ORB.");
106 +        } catch (org.omg.CosNaming.NamingContextPackage.InvalidName e) {
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 +            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 +            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 +            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 +     * 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 +            dieWithError("\nCRITICAL:POA Set Inactive - when trying to activate POA!\n" +
130 +                              "         This indicates an error with your ORB.");
131 +        }
132 +    }
133 +    
134 +    /**
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      /**
# Line 126 | Line 183 | public class ReferenceManager {
183  
184      /**
185       * Returns a reference to the Root POA
186 <     * This will throw a RuntimeException if there is an error
186 >     * Calls the dieWithError() if any exceptions happen!
187       *
188       * @return the reference this class holds
189       */
# Line 136 | Line 193 | public class ReferenceManager {
193              try {
194                  objRef = getORB().resolve_initial_references("RootPOA");
195              } catch (org.omg.CORBA.ORBPackage.InvalidName e) {
196 <                throw new RuntimeException("CRITICAL:Unable to resolve reference to the RootPOA!\n" +
197 <                                           "         This indicates an error with your ORB.");
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);
200          }
# Line 146 | Line 203 | public class ReferenceManager {
203      
204      /**
205       * Returns a reference to the Naming Service
206 <     * This will throw a RuntimeException if there is an error
206 >     * Calls the dieWithError() if any exceptions happen!
207       *
208       * @return the reference this class holds
209       */
# Line 156 | Line 213 | public class ReferenceManager {
213              try {
214                  objRef = getORB().resolve_initial_references("NameService");
215              } catch (org.omg.CORBA.ORBPackage.InvalidName e) {
216 <                throw new RuntimeException("CRITICAL:Unable to resolve reference to the Naming Service!\n" +
217 <                                           "         Please check with your CORBA naming service provider.");
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 +            dieWithError("\nCRITICAL:Unable to resolve reference to the Naming Service!\n" +
224 +                              "         Please check with your CORBA naming service provider.");
225 +        }
226          return _ns;
227      }
228      
229      /**
230       * Returns a reference to the configuration manager
231 <     * This will throw a RuntimeException if there is an error
231 >     * Calls the dieWithError() if any exceptions happen!
232       *
233       * @return the reference this class holds
234       */
235      public ConfigurationManager getCM() {
236          if (_cm == null) {
237 <            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);
237 >            _cm = ConfigurationManagerHelper.narrow(getCORBARef("iscream.ConfigurationManager"));
238          }
239 +        // check we managed to talk to the configuration manager
240 +        if (_cm == null) {
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;
245      }
246  
247      /**
248       * Returns a reference to the logger
249 <     * This will throw a RuntimeException if there is an error
249 >     * This will throw a Error if there is an error
250       *
251       * @return the reference this class holds
252       */
253      public Logger getLogger() {
254          if (_logger == null) {
255 <            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);
255 >            _logger = LoggerHelper.narrow(getCORBARef("iscream.Logger"));
256          }
257          return _logger;
258      }
259      
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
260   //---ATTRIBUTES---
261      
262      /**
# Line 253 | Line 285 | public class ReferenceManager {
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      

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines