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.5 by ajm, Mon Dec 11 16:38:09 2000 UTC vs.
Revision 1.16 by tdb, Tue May 29 17:02:34 2001 UTC

# Line 1 | Line 1
1   //---PACKAGE DECLARATION---
2 < package uk.ac.ukc.iscream.util;
2 > package uk.org.iscream.cms.server.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.cms.server.core.*;
9 > import uk.org.iscream.cms.server.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 synchronized 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;
58 >    private ReferenceManager() {
59 >        _orb = ORB.init(new String[] {}, null);
60      }
61  
62   //---PUBLIC METHODS---
# Line 86 | 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 Error 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 97 | 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 Error("\nCRITICAL:Naming Service Cannot Proceed - when resolving reference to " + name + "!\n" +
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 Error("\nCRITICAL:Invalid Name - when resolving reference to " + name + "!\n" +
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 Error("\nCRITICAL:Not Found - when resolving reference to " + name + "!\n" +
84 >            recoverWithError("\nCRITICAL:Not Found - when resolving reference to " + name + "!\n" +
85                                "         Please check that this component is running.");
86          }
87          return objRef;
# Line 112 | 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 Error 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 121 | 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 Error("\nCRITICAL:Wrong POA Policy - when binding " + name + "!\n" +
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 Error("\nCRITICAL:ServantNotActive - when binding " + name + "!\n" +
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 Error("\nCRITICAL:Invalid Name - when binding " + name + "!\n" +
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 Error("\nCRITICAL:Naming Service Cannot Proceed - when binding " + name + "!\n" +
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 Error("\nCRITICAL:Naming Service Not Found - when binding " + name + "!\n" +
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 Error("\nCRITICAL:Already Bound - when binding " + name + "!\n" +
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 Error 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 Error("\nCRITICAL:POA Set Inactive - when trying to activate POA!\n" +
130 >            dieWithError("\nCRITICAL:POA Set Inactive - when trying to activate POA!\n" +
131                                "         This indicates an error with your ORB.");
132          }
133      }
# Line 158 | 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.cms.server.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 179 | Line 208 | public class ReferenceManager {
208  
209      /**
210       * Returns a reference to the Root POA
211 <     * This will throw a Error if there is an error
211 >     * Calls the dieWithError() if any exceptions happen!
212       *
213       * @return the reference this class holds
214       */
# Line 189 | 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 Error("\nCRITICAL:Unable to resolve reference to the RootPOA!\n" +
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);
# Line 199 | Line 228 | public class ReferenceManager {
228      
229      /**
230       * Returns a reference to the Naming Service
231 <     * This will throw a Error if there is an error
231 >     * Calls the dieWithError() if any exceptions happen!
232       *
233       * @return the reference this class holds
234       */
# Line 209 | 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 Error("\nCRITICAL:Unable to resolve reference to the Naming Service!\n" +
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 <            throw new Error("\nCRITICAL:Unable to resolve reference to the Naming Service!\n" +
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;
# Line 224 | Line 253 | public class ReferenceManager {
253      
254      /**
255       * Returns a reference to the configuration manager
256 <     * This will throw a Error if there is an error
256 >     * Calls the dieWithError() if any exceptions happen!
257       *
258       * @return the reference this class holds
259       */
# Line 234 | Line 263 | public class ReferenceManager {
263          }
264          // check we managed to talk to the configuration manager
265          if (_cm == null) {
266 <            throw new Error("\nCRITICAL:Unable to resolve reference to the Configuration Manager!\n" +
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;
# Line 252 | Line 281 | public class ReferenceManager {
281          }
282          return _logger;
283      }
255
256    /**
257     * Sets the reference to the name
258     *
259     * @param the new name
260     */
261    public void setName(String name) {
262        _name = name;
263    }
284      
265    /**
266     * Returns a reference to the name
267     *
268     * @return the reference this class holds
269     */
270    public String getName() {
271        return _name;
272    }
273
285   //---ATTRIBUTES---
286      
287      /**
# Line 299 | 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      
325      /**
326       * A reference to the single instance of this class
327       */
328 <    private static ReferenceManager _instance;
328 >    private static ReferenceManager _instance = null;
329   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines