--- projects/cms/source/util/uk/org/iscream/cms/util/ReferenceManager.java 2000/11/30 02:38:17 1.3 +++ projects/cms/source/util/uk/org/iscream/cms/util/ReferenceManager.java 2000/12/13 13:42:50 1.8 @@ -19,7 +19,7 @@ import uk.ac.ukc.iscream.core.*; * a component. * * @author $Author: ajm $ - * @version $Id: ReferenceManager.java,v 1.3 2000/11/30 02:38:17 ajm Exp $ + * @version $Id: ReferenceManager.java,v 1.8 2000/12/13 13:42:50 ajm Exp $ */ public class ReferenceManager { @@ -28,41 +28,22 @@ public class ReferenceManager { /** * The current CVS revision of this class */ - public final String REVISION = "$Revision: 1.3 $"; + public final String REVISION = "$Revision: 1.8 $"; //---STATIC METHODS--- /** - * This creates the single instance of the ReferenceManager by - * calling the private constructor. If it is called twice, - * it detects an instance already exits and throws an exception. - * - * @param args the args to be passed to the ORB - * @param name the name of the component that will use this singleton - * - * @return a reference to the ReferenceManager - * - * @throws AlreadyInitialisedException if this method has already been called - */ - public static ReferenceManager init(String[] args, String name) throws AlreadyInitialisedException { - if (_instance != null) { - throw new AlreadyInitialisedException("init has already been called"); - } - _instance = new ReferenceManager(args, name); - return _instance; - } - - /** * This returns a reference to the single instance of the * ReferenceManager. * - * @return a reference to the ReferenceManager + * This creates the single instance of the ReferenceManager + * if it does not exist by calling the private constructor. * - * @throws NotInitialisedException if the reference manager has not been initialised + * @return a reference to the ReferenceManager */ - public static ReferenceManager getInstance() throws NotInitialisedException { + public static ReferenceManager getInstance() { if (_instance == null) { - throw new NotInitialisedException("attempt to obtain reference when not initialised"); + _instance = new ReferenceManager(); } return _instance; } @@ -72,14 +53,9 @@ public class ReferenceManager { /** * This is a private constructor * This ensures that the system performs according to a Singleton design pattern - * - * @param args the args to be passed to the ORB - * @param name the name of the component that will use this singleton */ - private ReferenceManager(String[] args, String name) { - _orb = ORB.init(args, null); - _name = name; - getLogger().write(toString(), Logger.SYSINIT, "created"); + private ReferenceManager() { + _orb = ORB.init(new String[] {}, null); } //---PUBLIC METHODS--- @@ -87,7 +63,7 @@ public class ReferenceManager { /** * Obtains a CORBA reference through the naming service * for the named object. - * This will throw a RuntimeException if there is an error + * Calls the dieWithError() if any exceptions happen! * * @param name the name of the CORBA object to resolve * @@ -98,14 +74,14 @@ public class ReferenceManager { try { objRef = getNS().resolve(getNS().to_name(name)); } catch (org.omg.CosNaming.NamingContextPackage.CannotProceed e) { - throw new RuntimeException("\nCRITICAL:Naming Service Cannot Proceed - when resolving reference to " + name + "!\n" + - " Please check with your CORBA naming service provider."); + dieWithError("\nCRITICAL:Naming Service Cannot Proceed - when resolving reference to " + name + "!\n" + + " Please check with your CORBA naming service provider."); } catch (org.omg.CosNaming.NamingContextPackage.InvalidName e) { - throw new RuntimeException("\nCRITICAL:Invalid Name - when resolving reference to " + name + "!\n" + - " Please check with your CORBA naming service provider."); + dieWithError("\nCRITICAL:Invalid Name - when resolving reference to " + name + "!\n" + + " Please check with your CORBA naming service provider."); } catch (org.omg.CosNaming.NamingContextPackage.NotFound e) { - throw new RuntimeException("\nCRITICAL:Not Found - when resolving reference to " + name + "!\n" + - " Please check that this component is running."); + dieWithError("\nCRITICAL:Not Found - when resolving reference to " + name + "!\n" + + " Please check that this component is running."); } return objRef; } @@ -113,7 +89,7 @@ public class ReferenceManager { /** * Binds a given servant with the given name * to the naming service. - * This will throw a RuntimeException if there is an error + * Calls the dieWithError() if any exceptions happen! * * @param objRef a reverence to the servant object * @param name the name to bind to the naming service with @@ -122,36 +98,36 @@ public class ReferenceManager { try { getNS().bind(getNS().to_name(name), getRootPOA().servant_to_reference(objRef)); } catch (org.omg.PortableServer.POAPackage.WrongPolicy e) { - throw new RuntimeException("\nCRITICAL:Wrong POA Policy - when binding " + name + "!\n" + - " This indicates an error with your ORB."); + dieWithError("\nCRITICAL:Wrong POA Policy - when binding " + name + "!\n" + + " This indicates an error with your ORB."); } catch (org.omg.PortableServer.POAPackage.ServantNotActive e) { - throw new RuntimeException("\nCRITICAL:ServantNotActive - when binding " + name + "!\n" + - " This indicates an error with your ORB."); + dieWithError("\nCRITICAL:ServantNotActive - when binding " + name + "!\n" + + " This indicates an error with your ORB."); } catch (org.omg.CosNaming.NamingContextPackage.InvalidName e) { - throw new RuntimeException("\nCRITICAL:Invalid Name - when binding " + name + "!\n" + - " Please check with your CORBA naming service provider."); + dieWithError("\nCRITICAL:Invalid Name - when binding " + name + "!\n" + + " Please check with your CORBA naming service provider."); } catch (org.omg.CosNaming.NamingContextPackage.CannotProceed e) { - throw new RuntimeException("\nCRITICAL:Naming Service Cannot Proceed - when binding " + name + "!\n" + - " Please check with your CORBA naming service provider."); + dieWithError("\nCRITICAL:Naming Service Cannot Proceed - when binding " + name + "!\n" + + " Please check with your CORBA naming service provider."); } catch (org.omg.CosNaming.NamingContextPackage.NotFound e) { - throw new RuntimeException("\nCRITICAL:Naming Service Not Found - when binding " + name + "!\n" + - " Please check with your CORBA naming service provider."); + dieWithError("\nCRITICAL:Naming Service Not Found - when binding " + name + "!\n" + + " Please check with your CORBA naming service provider."); } catch (org.omg.CosNaming.NamingContextPackage.AlreadyBound e) { - throw new RuntimeException("\nCRITICAL:Already Bound - when binding " + name + "!\n" + - " Another component with this name is already running."); + dieWithError("\nCRITICAL:Already Bound - when binding " + name + "!\n" + + " Another component with this name is already running."); } } /** * Activates the POA - * This will throw a RuntimeException if there is an error + * Calls the dieWithError() if any exceptions happen! */ public void activatePOA() { try { getRootPOA().the_POAManager().activate(); } catch (org.omg.PortableServer.POAManagerPackage.AdapterInactive e) { - throw new RuntimeException("\nCRITICAL:POA Set Inactive - when trying to activate POA!\n" + - " This indicates an error with your ORB."); + dieWithError("\nCRITICAL:POA Set Inactive - when trying to activate POA!\n" + + " This indicates an error with your ORB."); } } @@ -159,14 +135,41 @@ public class ReferenceManager { * Overrides the {@link java.lang.Object#toString() Object.toString()} * method to provide clean logging (every class should have this). * + * This uses the uk.ac.ukc.iscream.util.NameFormat class + * to format the toString() + * * @return the name of this class and its CVS revision */ public String toString() { - return getName() + "{" + getClass().getName() + "}(" + REVISION.substring(11, REVISION.length() - 2) + ")"; + return FormatName.getName( + _name, + getClass().getName(), + REVISION); } //---PRIVATE METHODS--- + /** + * If there are any CORBA errors this method is called with the + * error message. + * + * Currently this prints the error, on the local err stream. Will + * print to the logger if one is available. + * + * ANY CALLS TO THIS METHOD CAUSE + * THE SYSTEM TO EXIT WITH A NON + * ZERO CODE! + * + * @param message the error message to die with + */ + private void dieWithError(String message) { + System.err.println(message); + if (_logger != null) { + _logger.write(toString(), Logger.FATAL, message); + } + System.exit(1); + } + //---ACCESSOR/MUTATOR METHODS--- /** @@ -180,7 +183,7 @@ public class ReferenceManager { /** * Returns a reference to the Root POA - * This will throw a RuntimeException if there is an error + * Calls the dieWithError() if any exceptions happen! * * @return the reference this class holds */ @@ -190,8 +193,8 @@ public class ReferenceManager { try { objRef = getORB().resolve_initial_references("RootPOA"); } catch (org.omg.CORBA.ORBPackage.InvalidName e) { - throw new RuntimeException("\nCRITICAL:Unable to resolve reference to the RootPOA!\n" + - " This indicates an error with your ORB."); + dieWithError("\nCRITICAL:Unable to resolve reference to the RootPOA!\n" + + " This indicates an error with your ORB."); } _rootPOA = POAHelper.narrow(objRef); } @@ -200,7 +203,7 @@ public class ReferenceManager { /** * Returns a reference to the Naming Service - * This will throw a RuntimeException if there is an error + * Calls the dieWithError() if any exceptions happen! * * @return the reference this class holds */ @@ -210,17 +213,22 @@ public class ReferenceManager { try { objRef = getORB().resolve_initial_references("NameService"); } catch (org.omg.CORBA.ORBPackage.InvalidName e) { - throw new RuntimeException("\nCRITICAL:Unable to resolve reference to the Naming Service!\n" + - " Please check with your CORBA naming service provider."); + dieWithError("\nCRITICAL:Unable to resolve reference to the Naming Service!\n" + + " Please check with your CORBA naming service provider."); } _ns = NamingContextExtHelper.narrow(objRef); } + // check we managed to talk to the naming service + if (_ns == null) { + dieWithError("\nCRITICAL:Unable to resolve reference to the Naming Service!\n" + + " Please check with your CORBA naming service provider."); + } return _ns; } /** * Returns a reference to the configuration manager - * This will throw a RuntimeException if there is an error + * Calls the dieWithError() if any exceptions happen! * * @return the reference this class holds */ @@ -228,12 +236,17 @@ public class ReferenceManager { if (_cm == null) { _cm = ConfigurationManagerHelper.narrow(getCORBARef("iscream.ConfigurationManager")); } + // check we managed to talk to the configuration manager + if (_cm == null) { + dieWithError("\nCRITICAL:Unable to resolve reference to the Configuration Manager!\n" + + " Please check with your CORBA naming service provider."); + } return _cm; } /** * Returns a reference to the logger - * This will throw a RuntimeException if there is an error + * This will throw a Error if there is an error * * @return the reference this class holds */ @@ -244,15 +257,6 @@ public class ReferenceManager { return _logger; } - /** - * Returns a reference to the name - * - * @return the reference this class holds - */ - public String getName() { - return _name; - } - //---ATTRIBUTES--- /** @@ -281,9 +285,15 @@ public class ReferenceManager { private Logger _logger; /** - * The name + * This is the friendly identifier of the + * component this class is running in. + * eg, a Filter may be called "filter1", + * If this class does not have an owning + * component, a name from the configuration + * can be placed here. This name could also + * be changed to null for utility classes. */ - private String _name; + private String _name = null; //---STATIC ATTRIBUTES---