ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/server/uk/org/iscream/cms/server/componentmanager/ReferenceManager.java
(Generate patch)

Comparing projects/cms/source/server/uk/org/iscream/cms/server/componentmanager/ReferenceManager.java (file contents):
Revision 1.3 by ajm, Thu Nov 30 02:38:17 2000 UTC vs.
Revision 1.13 by tdb, Wed Mar 14 23:25:29 2001 UTC

# Line 1 | Line 1
1   //---PACKAGE DECLARATION---
2 < package uk.ac.ukc.iscream.util;
2 > package uk.org.iscream.componentmanager;
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.*;
8 > import uk.org.iscream.core.*;
9 > import uk.org.iscream.util.*;
10  
11   /**
12   * This class returns references for global system objects.
# Line 33 | Line 34 | public class ReferenceManager {
34   //---STATIC METHODS---
35  
36      /**
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    /**
37       * This returns a reference to the single instance of the
38       * ReferenceManager.
39       *
40 <     * @return a reference to the ReferenceManager
40 >     * This creates the single instance of the ReferenceManager
41 >     * if it does not exist by calling the private constructor.
42       *
43 <     * @throws NotInitialisedException if the reference manager has not been initialised
43 >     * @return a reference to the ReferenceManager
44       */
45 <    public static ReferenceManager getInstance() throws NotInitialisedException {
45 >    public static ReferenceManager getInstance() {
46          if (_instance == null) {
47 <            throw new NotInitialisedException("attempt to obtain reference when not initialised");
47 >            _instance = new ReferenceManager();
48          }
49          return _instance;
50      }
# Line 72 | Line 54 | public class ReferenceManager {
54      /**
55       * This is a private constructor
56       * 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
57       */
58 <    private ReferenceManager(String[] args, String name) {
59 <        _orb = ORB.init(args, null);
81 <        _name = name;
82 <        getLogger().write(toString(), Logger.SYSINIT, "created");
58 >    private ReferenceManager() {
59 >        _orb = ORB.init(new String[] {}, null);
60      }
61  
62   //---PUBLIC METHODS---
# Line 87 | Line 64 | public class ReferenceManager {
64      /**
65       * Obtains a CORBA reference through the naming service
66       * for the named object.
67 <     * This will throw a RuntimeException if there is an error
67 >     * Calls the dieWithError() if any exceptions happen!
68       *
69       * @param name the name of the CORBA object to resolve
70       *
# Line 98 | Line 75 | public class ReferenceManager {
75          try {
76              objRef = getNS().resolve(getNS().to_name(name));
77          } catch (org.omg.CosNaming.NamingContextPackage.CannotProceed e) {
78 <            throw new RuntimeException("\nCRITICAL:Naming Service Cannot Proceed - when resolving reference to " + name + "!\n" +
79 <                                         "         Please check with your CORBA naming service provider.");
78 >            dieWithError("\nCRITICAL:Naming Service Cannot Proceed - when resolving reference to " + name + "!\n" +
79 >                              "         Please check with your CORBA naming service provider.");
80          } catch (org.omg.CosNaming.NamingContextPackage.InvalidName e) {
81 <            throw new RuntimeException("\nCRITICAL:Invalid Name - when resolving reference to " + name + "!\n" +
82 <                                         "         Please check with your CORBA naming service provider.");
81 >            dieWithError("\nCRITICAL:Invalid Name - when resolving reference to " + name + "!\n" +
82 >                              "         Please check with your CORBA naming service provider.");
83          } catch (org.omg.CosNaming.NamingContextPackage.NotFound e) {
84 <            throw new RuntimeException("\nCRITICAL:Not Found - when resolving reference to " + name + "!\n" +
85 <                                         "         Please check that this component is running.");
84 >            recoverWithError("\nCRITICAL:Not Found - when resolving reference to " + name + "!\n" +
85 >                              "         Please check that this component is running.");
86          }
87          return objRef;
88      }
# Line 113 | Line 90 | public class ReferenceManager {
90      /**
91       * Binds a given servant with the given name
92       * to the naming service.
93 <     * This will throw a RuntimeException if there is an error
93 >     * Calls the dieWithError() if any exceptions happen!
94       *
95       * @param objRef a reverence to the servant object
96       * @param name the name to bind to the naming service with
# Line 122 | Line 99 | public class ReferenceManager {
99          try {
100              getNS().bind(getNS().to_name(name), getRootPOA().servant_to_reference(objRef));
101          } catch (org.omg.PortableServer.POAPackage.WrongPolicy e) {
102 <            throw new RuntimeException("\nCRITICAL:Wrong POA Policy - when binding " + name + "!\n" +
103 <                                         "         This indicates an error with your ORB.");
102 >            dieWithError("\nCRITICAL:Wrong POA Policy - when binding " + name + "!\n" +
103 >                              "         This indicates an error with your ORB.");
104          } catch (org.omg.PortableServer.POAPackage.ServantNotActive e) {
105 <            throw new RuntimeException("\nCRITICAL:ServantNotActive - when binding " + name + "!\n" +
106 <                                         "         This indicates an error with your ORB.");
105 >            dieWithError("\nCRITICAL:ServantNotActive - when binding " + name + "!\n" +
106 >                              "         This indicates an error with your ORB.");
107          } catch (org.omg.CosNaming.NamingContextPackage.InvalidName e) {
108 <            throw new RuntimeException("\nCRITICAL:Invalid Name - when binding " + name + "!\n" +
109 <                                         "         Please check with your CORBA naming service provider.");
108 >            dieWithError("\nCRITICAL:Invalid Name - when binding " + name + "!\n" +
109 >                              "         Please check with your CORBA naming service provider.");
110          } catch (org.omg.CosNaming.NamingContextPackage.CannotProceed e) {
111 <            throw new RuntimeException("\nCRITICAL:Naming Service Cannot Proceed - when binding " + name + "!\n" +
112 <                                         "         Please check with your CORBA naming service provider.");
111 >            dieWithError("\nCRITICAL:Naming Service Cannot Proceed - when binding " + name + "!\n" +
112 >                              "         Please check with your CORBA naming service provider.");
113          } catch (org.omg.CosNaming.NamingContextPackage.NotFound e) {
114 <            throw new RuntimeException("\nCRITICAL:Naming Service Not Found - when binding " + name + "!\n" +
115 <                                         "         Please check with your CORBA naming service provider.");
114 >            recoverWithError("\nCRITICAL:Naming Service Not Found - when binding " + name + "!\n" +
115 >                              "         Please check with your CORBA naming service provider.");
116          } catch (org.omg.CosNaming.NamingContextPackage.AlreadyBound e) {
117 <            throw new RuntimeException("\nCRITICAL:Already Bound - when binding " + name + "!\n" +
118 <                                         "         Another component with this name is already running.");
117 >            dieWithError("\nCRITICAL:Already Bound - when binding " + name + "!\n" +
118 >                              "         Another component with this name is already running.");
119          }
120      }
121      
122      /**
123       * Activates the POA
124 <     * This will throw a RuntimeException if there is an error
124 >     * Calls the dieWithError() if any exceptions happen!
125       */
126      public void activatePOA() {
127          try {
128              getRootPOA().the_POAManager().activate();
129          } catch (org.omg.PortableServer.POAManagerPackage.AdapterInactive e) {
130 <            throw new RuntimeException("\nCRITICAL:POA Set Inactive - when trying to activate POA!\n" +
131 <                                         "         This indicates an error with your ORB.");
130 >            dieWithError("\nCRITICAL:POA Set Inactive - when trying to activate POA!\n" +
131 >                              "         This indicates an error with your ORB.");
132          }
133      }
134      
# Line 159 | Line 136 | public class ReferenceManager {
136       * Overrides the {@link java.lang.Object#toString() Object.toString()}
137       * method to provide clean logging (every class should have this).
138       *
139 +     * This uses the uk.org.iscream.util.NameFormat class
140 +     * to format the toString()
141 +     *
142       * @return the name of this class and its CVS revision
143       */
144      public String toString() {
145 <        return getName() + "{" + getClass().getName() + "}(" + REVISION.substring(11, REVISION.length() - 2) + ")";
145 >        return FormatName.getName(
146 >            _name,
147 >            getClass().getName(),
148 >            REVISION);
149      }
150  
151   //---PRIVATE METHODS---
152  
153 +    /**
154 +     * If there are any CORBA errors this method is called with the
155 +     * error message.
156 +     *
157 +     * The exception this throws is generic to CORBA communication
158 +     * problems, this is a Runtime exception and is typically only
159 +     * explicitly caught in the ComponentManager for the current VM
160 +     * The ComponentManager can then decide what course of action to
161 +     * take.
162 +     *
163 +     * Also, if it can, it will log the message with the logger.
164 +     *
165 +     * @param message the error message to die with
166 +     */
167 +    private void recoverWithError(String message) throws ComponentCORBAException {
168 +        if (_logger != null) {
169 +            _logger.write(toString(), Logger.WARNING, "component CORBA error - " + message);
170 +        }
171 +        throw new ComponentCORBAException(message);
172 +    }
173 +
174 +
175 +    /**
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 +
197 +
198   //---ACCESSOR/MUTATOR METHODS---
199  
200      /**
# Line 180 | Line 208 | public class ReferenceManager {
208  
209      /**
210       * Returns a reference to the Root POA
211 <     * This will throw a RuntimeException if there is an error
211 >     * Calls the dieWithError() if any exceptions happen!
212       *
213       * @return the reference this class holds
214       */
# Line 190 | Line 218 | public class ReferenceManager {
218              try {
219                  objRef = getORB().resolve_initial_references("RootPOA");
220              } catch (org.omg.CORBA.ORBPackage.InvalidName e) {
221 <                throw new RuntimeException("\nCRITICAL:Unable to resolve reference to the RootPOA!\n" +
222 <                                             "         This indicates an error with your ORB.");
221 >                dieWithError("\nCRITICAL:Unable to resolve reference to the RootPOA!\n" +
222 >                                  "         This indicates an error with your ORB.");
223              }
224              _rootPOA = POAHelper.narrow(objRef);
225          }
# Line 200 | Line 228 | public class ReferenceManager {
228      
229      /**
230       * Returns a reference to the Naming Service
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       */
# Line 210 | Line 238 | public class ReferenceManager {
238              try {
239                  objRef = getORB().resolve_initial_references("NameService");
240              } catch (org.omg.CORBA.ORBPackage.InvalidName e) {
241 <                throw new RuntimeException("\nCRITICAL:Unable to resolve reference to the Naming Service!\n" +
242 <                                             "         Please check with your CORBA naming service provider.");
241 >                dieWithError("\nCRITICAL:Unable to resolve reference to the Naming Service!\n" +
242 >                                  "         Please check with your CORBA naming service provider.");
243              }
244              _ns = NamingContextExtHelper.narrow(objRef);
245          }
246 +        // check we managed to talk to the naming service
247 +        if (_ns == null) {
248 +            dieWithError("\nCRITICAL:Unable to resolve reference to the Naming Service!\n" +
249 +                              "         Please check with your CORBA naming service provider.");
250 +        }
251          return _ns;
252      }
253      
254      /**
255       * Returns a reference to the configuration manager
256 <     * This will throw a RuntimeException if there is an error
256 >     * Calls the dieWithError() if any exceptions happen!
257       *
258       * @return the reference this class holds
259       */
# Line 228 | Line 261 | public class ReferenceManager {
261          if (_cm == null) {
262              _cm = ConfigurationManagerHelper.narrow(getCORBARef("iscream.ConfigurationManager"));
263          }
264 +        // check we managed to talk to the configuration manager
265 +        if (_cm == null) {
266 +            dieWithError("\nCRITICAL:Unable to resolve reference to the Configuration Manager!\n" +
267 +                              "         Please check with your CORBA naming service provider.");
268 +        }
269          return _cm;
270      }
271  
272      /**
273       * Returns a reference to the logger
274 <     * This will throw a RuntimeException if there is an error
274 >     * This will throw a Error if there is an error
275       *
276       * @return the reference this class holds
277       */
# Line 244 | Line 282 | public class ReferenceManager {
282          return _logger;
283      }
284      
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
285   //---ATTRIBUTES---
286      
287      /**
# Line 281 | Line 310 | public class ReferenceManager {
310      private Logger _logger;
311      
312      /**
313 <     * The name
313 >     * This is the friendly identifier of the
314 >     * component this class is running in.
315 >     * eg, a Filter may be called "filter1",
316 >     * If this class does not have an owning
317 >     * component,  a name from the configuration
318 >     * can be placed here.  This name could also
319 >     * be changed to null for utility classes.
320       */
321 <    private String _name;
321 >    private String _name = null;
322  
323   //---STATIC ATTRIBUTES---
324      

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines