--- projects/cms/source/util/uk/org/iscream/cms/util/ReferenceManager.java 2000/11/30 00:54:27 1.1 +++ projects/cms/source/util/uk/org/iscream/cms/util/ReferenceManager.java 2000/12/12 17:13:22 1.6 @@ -1,5 +1,5 @@ //---PACKAGE DECLARATION--- -package uk.ac.ukc.iscream.refman; +package uk.ac.ukc.iscream.util; //---IMPORTS--- import org.omg.CORBA.ORB; @@ -11,7 +11,7 @@ import uk.ac.ukc.iscream.core.*; * This class returns references for global system objects. * This class is used to create and return references to objects * that are required throughout the system. Most of which - * are CORBA based. It also handles ORB initialisation. + * are CORBA based. It also manages the ORB for the component. * * It is a singleton object with a static method to obtain a * reference to it. This removes the need for passing @@ -19,7 +19,7 @@ import uk.ac.ukc.iscream.core.*; * a component. * * @author $Author: ajm $ - * @version $Id: ReferenceManager.java,v 1.1 2000/11/30 00:54:27 ajm Exp $ + * @version $Id: ReferenceManager.java,v 1.6 2000/12/12 17:13:22 ajm Exp $ */ public class ReferenceManager { @@ -28,40 +28,16 @@ public class ReferenceManager { /** * The current CVS revision of this class */ - public final String REVISION = "$Revision: 1.1 $"; + public final String REVISION = "$Revision: 1.6 $"; //---STATIC METHODS--- /** - * This essentially formats the toString() source for an - * object wanting to make a log entry. - * - * @param source the source of the call - * @param rev the CVS revision number of the caller - * - * @return the formatted string - - public static String logString(Object source, String rev) { - if (_instance == null) { - return "unamed{" + source.getClass().getName() + "}(" + rev.substring(11, rev.length() - 2) + ")"; - } - return getInstance().getName() + "{" + source.getClass().getName() + "}(" + rev.substring(11, rev.length() - 2) + ")"; - } -` */ - - /** - * 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 know 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"); } @@ -73,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; } @@ -89,30 +66,133 @@ 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 know 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--- /** + * Obtains a CORBA reference through the naming service + * for the named object. + * This will throw a Error if there is an error + * + * @param name the name of the CORBA object to resolve + * + * @return a reference to the object + */ + public org.omg.CORBA.Object getCORBARef(String name) { + org.omg.CORBA.Object objRef = null; + try { + objRef = getNS().resolve(getNS().to_name(name)); + } catch (org.omg.CosNaming.NamingContextPackage.CannotProceed e) { + 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) { + 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) { + dieWithError("\nCRITICAL:Not Found - when resolving reference to " + name + "!\n" + + " Please check that this component is running."); + } + return objRef; + } + + /** + * Binds a given servant with the given name + * to the naming service. + * Calls the dieWithError()! + * + * @param objRef a reverence to the servant object + * @param name the name to bind to the naming service with + */ + public void bindToOrb(org.omg.PortableServer.Servant objRef, String name) { + try { + getNS().bind(getNS().to_name(name), getRootPOA().servant_to_reference(objRef)); + } catch (org.omg.PortableServer.POAPackage.WrongPolicy e) { + dieWithError("\nCRITICAL:Wrong POA Policy - when binding " + name + "!\n" + + " This indicates an error with your ORB."); + } catch (org.omg.PortableServer.POAPackage.ServantNotActive e) { + dieWithError("\nCRITICAL:ServantNotActive - when binding " + name + "!\n" + + " This indicates an error with your ORB."); + } catch (org.omg.CosNaming.NamingContextPackage.InvalidName e) { + dieWithError("\nCRITICAL:Invalid Name - when binding " + name + "!\n" + + " Please check with your CORBA naming service provider."); + } catch (org.omg.CosNaming.NamingContextPackage.CannotProceed e) { + 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) { + 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) { + dieWithError("\nCRITICAL:Already Bound - when binding " + name + "!\n" + + " Another component with this name is already running."); + } + } + + /** + * Activates the POA + * This will throw a Error if there is an error + */ + public void activatePOA() { + try { + getRootPOA().the_POAManager().activate(); + } catch (org.omg.PortableServer.POAManagerPackage.AdapterInactive e) { + dieWithError("\nCRITICAL:POA Set Inactive - when trying to activate POA!\n" + + " This indicates an error with your ORB."); + } + } + + /** * 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( + null, + this.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--- /** @@ -126,7 +206,7 @@ public class ReferenceManager { /** * Returns a reference to the Root POA - * 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 */ @@ -136,8 +216,8 @@ public class ReferenceManager { try { objRef = getORB().resolve_initial_references("RootPOA"); } catch (org.omg.CORBA.ORBPackage.InvalidName e) { - throw new RuntimeException("CRITICAL: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); } @@ -146,7 +226,7 @@ public class ReferenceManager { /** * Returns a reference to the Naming Service - * 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 */ @@ -156,72 +236,69 @@ public class ReferenceManager { try { objRef = getORB().resolve_initial_references("NameService"); } catch (org.omg.CORBA.ORBPackage.InvalidName e) { - throw new RuntimeException("CRITICAL: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 + * This will throw a Error if there is an error * * @return the reference this class holds */ public ConfigurationManager getCM() { if (_cm == null) { - org.omg.CORBA.Object objRef = null; - try { - objRef = getNS().resolve(getNS().to_name("iscream.ConfigurationManager")); - } catch (org.omg.CosNaming.NamingContextPackage.CannotProceed e) { - throw new RuntimeException("CRITICAL:Cannot Proceed - when resolving reference to iscream.ConfigurationManager!\n" + - " This indicates an error with your ORB."); - } catch (org.omg.CosNaming.NamingContextPackage.InvalidName e) { - throw new RuntimeException("CRITICAL:Invalid Name - when resolving reference to iscream.ConfigurationManager!\n" + - " This indicates an error with your ORB."); - } catch (org.omg.CosNaming.NamingContextPackage.NotFound e) { - throw new RuntimeException("CRITICAL:Not Found - when resolving reference to iscream.ConfigurationManager!\n" + - " Please check that this component is running."); - } - _cm = ConfigurationManagerHelper.narrow(objRef); + _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 */ public Logger getLogger() { if (_logger == null) { - org.omg.CORBA.Object objRef = null; - try { - objRef = getNS().resolve(getNS().to_name("iscream.Logger")); - } catch (org.omg.CosNaming.NamingContextPackage.CannotProceed e) { - throw new RuntimeException("CRITICAL:Cannot Proceed - when resolving reference to iscream.Logger!\n" + - " This indicates an error with your ORB."); - } catch (org.omg.CosNaming.NamingContextPackage.InvalidName e) { - throw new RuntimeException("CRITICAL:Invalid Name - when resolving reference to iscream.Logger!\n" + - " This indicates an error with your ORB."); - } catch (org.omg.CosNaming.NamingContextPackage.NotFound e) { - throw new RuntimeException("CRITICAL:Not Found - when resolving reference to iscream.Logger!\n" + - " Please check that this component is running."); - } - _logger = LoggerHelper.narrow(objRef); + _logger = LoggerHelper.narrow(getCORBARef("iscream.Logger")); } 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; } @@ -254,6 +331,8 @@ public class ReferenceManager { /** * The name + * TO BE REMOVED ONCE CODE TIDY HAS COMPLETED! + * @deprecated not stored in the refman any more - see Template.class */ private String _name;