--- 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/11/30 02:19:31 1.2 @@ -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.2 2000/11/30 02:19:31 ajm Exp $ */ public class ReferenceManager { @@ -28,28 +28,11 @@ public class ReferenceManager { /** * The current CVS revision of this class */ - public final String REVISION = "$Revision: 1.1 $"; + public final String REVISION = "$Revision: 1.2 $"; //---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. @@ -102,6 +85,77 @@ public class ReferenceManager { //---PUBLIC METHODS--- /** + * Obtains a CORBA reference through the naming service + * for the named object. + * This will throw a RuntimeException 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) { + throw new RuntimeException("\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."); + } 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."); + } + return objRef; + } + + /** + * Binds a given servant with the given name + * to the naming service. + * This will throw a RuntimeException if there is an error + * + * @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) { + throw new RuntimeException("\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."); + } 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."); + } 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."); + } 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."); + } 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."); + } + } + + /** + * Activates the POA + * This will throw a RuntimeException if there is an error + */ + 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."); + } + } + + /** * Overrides the {@link java.lang.Object#toString() Object.toString()} * method to provide clean logging (every class should have this). * @@ -136,8 +190,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."); + throw new RuntimeException("\nCRITICAL:Unable to resolve reference to the RootPOA!\n" + + " This indicates an error with your ORB."); } _rootPOA = POAHelper.narrow(objRef); } @@ -156,8 +210,8 @@ 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."); + throw new RuntimeException("\nCRITICAL:Unable to resolve reference to the Naming Service!\n" + + " Please check with your CORBA naming service provider."); } _ns = NamingContextExtHelper.narrow(objRef); } @@ -172,20 +226,7 @@ public class ReferenceManager { */ 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")); } return _cm; } @@ -198,20 +239,7 @@ public class ReferenceManager { */ 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; }