--- projects/cms/source/util/uk/org/iscream/cms/util/ReferenceManager.java 2000/11/30 02:19:31 1.2 +++ projects/cms/source/util/uk/org/iscream/cms/util/ReferenceManager.java 2000/12/12 18:24:53 1.7 @@ -1,5 +1,5 @@ //---PACKAGE DECLARATION--- -package uk.ac.ukc.iscream.refman; +package uk.ac.ukc.iscream.util; //---IMPORTS--- import org.omg.CORBA.ORB; @@ -19,7 +19,7 @@ import uk.ac.ukc.iscream.core.*; * a component. * * @author $Author: ajm $ - * @version $Id: ReferenceManager.java,v 1.2 2000/11/30 02:19:31 ajm Exp $ + * @version $Id: ReferenceManager.java,v 1.7 2000/12/12 18:24:53 ajm Exp $ */ public class ReferenceManager { @@ -28,23 +28,16 @@ public class ReferenceManager { /** * The current CVS revision of this class */ - public final String REVISION = "$Revision: 1.2 $"; + public final String REVISION = "$Revision: 1.7 $"; //---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 + * TO BE REMOVED ONCE CODE TIDY HAS COMPLETED! + * @deprecated should no longer use this method to construct a refman */ public static ReferenceManager init(String[] args, String name) throws AlreadyInitialisedException { + System.out.println("LEGACY CALL - SORT THIS OUT! name=" + name); if (_instance != null) { throw new AlreadyInitialisedException("init has already been called"); } @@ -56,13 +49,14 @@ public class ReferenceManager { * 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 +66,19 @@ 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() { + _orb = ORB.init(new String[] {}, null); + } + + /** + * TO BE REMOVED ONCE CODE TIDY HAS COMPLETED! + * @deprecated should no longer use this method to construct a refman + */ private ReferenceManager(String[] args, String name) { + System.out.println("LEGACY CALL - SORT THIS OUT! name=" + name); _orb = ORB.init(args, null); _name = name; - getLogger().write(toString(), Logger.SYSINIT, "created"); } //---PUBLIC METHODS--- @@ -87,7 +86,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 +97,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 +112,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 +121,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 +158,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 +206,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 +216,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 +226,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 +236,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 +259,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 */ @@ -243,13 +279,26 @@ public class ReferenceManager { } return _logger; } + + /** + * Sets the reference to the name + * TO BE REMOVED ONCE CODE TIDY HAS COMPLETED! + * @deprecated not stored in the refman any more - see Template.class + * @param the new name + */ + public void setName(String name) { + System.out.println("LEGACY CALL - SORT THIS OUT! name=" + name); + _name = name; + } /** * Returns a reference to the name - * + * TO BE REMOVED ONCE CODE TIDY HAS COMPLETED! + * @deprecated not stored in the refman any more - see Template.class * @return the reference this class holds */ public String getName() { + System.out.println("LEGACY CALL - SORT THIS OUT! name=" + _name); return _name; } @@ -281,9 +330,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---