diff options
author | Benoit Foucher <benoit@zeroc.com> | 2012-08-30 14:43:15 +0200 |
---|---|---|
committer | Benoit Foucher <benoit@zeroc.com> | 2012-08-30 14:43:15 +0200 |
commit | aab12cf1719b425b5a2c571b8938d47cdd71d151 (patch) | |
tree | 876561b05764721306eb8629883e7b0b9cbbc99a /java/src/IceBox/ServiceManagerI.java | |
parent | minor g++ warning (diff) | |
download | ice-aab12cf1719b425b5a2c571b8938d47cdd71d151.tar.bz2 ice-aab12cf1719b425b5a2c571b8938d47cdd71d151.tar.xz ice-aab12cf1719b425b5a2c571b8938d47cdd71d151.zip |
ICE-4774: Merged skype_props enhancement
Diffstat (limited to 'java/src/IceBox/ServiceManagerI.java')
-rw-r--r-- | java/src/IceBox/ServiceManagerI.java | 477 |
1 files changed, 230 insertions, 247 deletions
diff --git a/java/src/IceBox/ServiceManagerI.java b/java/src/IceBox/ServiceManagerI.java index aa066fb2e7e..a4893c52061 100644 --- a/java/src/IceBox/ServiceManagerI.java +++ b/java/src/IceBox/ServiceManagerI.java @@ -389,17 +389,6 @@ public class ServiceManagerI extends _ServiceManagerDisp try { _communicator.addAdminFacet(this, "IceBox.ServiceManager"); - - // - // Add a Properties facet for each service - // - for(ServiceInfo info: _services) - { - Ice.Communicator communicator = info.communicator != null ? info.communicator : _sharedCommunicator; - _communicator.addAdminFacet(new PropertiesAdminI(communicator.getProperties()), - "IceBox.Service." + info.name + ".Properties"); - } - _communicator.getAdmin(); } catch(Ice.ObjectAdapterDeactivatedException ex) @@ -464,173 +453,99 @@ public class ServiceManagerI extends _ServiceManagerDisp throws FailureException { // - // Instantiate the class. + // Load the class. // - ServiceInfo info = new ServiceInfo(); - info.name = service; - info.status = StatusStopped; - info.args = args; - try + // + // Use a class loader if the user specified a JAR file or class directory. + // + Class<?> c = null; + if(classDir != null) { - Class<?> c = null; - - // - // Use a class loader if the user specified a JAR file or class directory. - // - if(classDir != null) + try { - try - { - if(!absolutePath) - { - classDir = new java.io.File(System.getProperty("user.dir") + java.io.File.separator + - classDir).getCanonicalPath(); - } - - if(!classDir.endsWith(java.io.File.separator) && !classDir.endsWith(".jar")) - { - classDir += java.io.File.separator; - } - - // - // Reuse an existing class loader if we have already loaded a plug-in with - // the same value for classDir, otherwise create a new one. - // - ClassLoader cl = null; - - if(_classLoaders == null) - { - _classLoaders = new java.util.HashMap<String, ClassLoader>(); - } - else - { - cl = _classLoaders.get(classDir); - } - - if(cl == null) - { - final java.net.URL[] url = new java.net.URL[] { new java.net.URL("file:///" + classDir) }; - - cl = new java.net.URLClassLoader(url); - - _classLoaders.put(classDir, cl); - } - - c = cl.loadClass(className); - } - catch(java.net.MalformedURLException ex) + if(!absolutePath) { - throw new FailureException("ServiceManager: invalid entry point format `" + classDir + "'", ex); + classDir = new java.io.File(System.getProperty("user.dir") + java.io.File.separator + + classDir).getCanonicalPath(); } - catch(java.io.IOException ex) + + if(!classDir.endsWith(java.io.File.separator) && !classDir.endsWith(".jar")) { - throw new FailureException("ServiceManager: invalid path in plug-in entry point `" + classDir + - "'", ex); + classDir += java.io.File.separator; } - catch(java.lang.ClassNotFoundException ex) + + // + // Reuse an existing class loader if we have already loaded a plug-in with + // the same value for classDir, otherwise create a new one. + // + ClassLoader cl = null; + + if(_classLoaders == null) { - // Ignored + _classLoaders = new java.util.HashMap<String, ClassLoader>(); } - } - else - { - c = IceInternal.Util.findClass(className, null); - } - - if(c == null) - { - throw new FailureException("ServiceManager: class " + className + " not found"); - } - - // - // If the service class provides a constructor that accepts an Ice.Communicator argument, - // use that in preference to the default constructor. - // - java.lang.Object obj = null; - try - { - java.lang.reflect.Constructor<?> con = c.getDeclaredConstructor(Ice.Communicator.class); - obj = con.newInstance(_communicator); - } - catch(IllegalAccessException ex) - { - throw new FailureException( - "ServiceManager: unable to access service constructor " + className + "(Ice.Communicator)", ex); - } - catch(NoSuchMethodException ex) - { - // Ignore. - } - catch(java.lang.reflect.InvocationTargetException ex) - { - if(ex.getCause() != null) + else { - throw ex.getCause(); + cl = _classLoaders.get(classDir); } - else + + if(cl == null) { - throw new FailureException("ServiceManager: exception in service constructor for " + className, ex); + final java.net.URL[] url = new java.net.URL[] { new java.net.URL("file:///" + classDir) }; + + cl = new java.net.URLClassLoader(url); + + _classLoaders.put(classDir, cl); } + + c = cl.loadClass(className); } - - if(obj == null) + catch(java.net.MalformedURLException ex) { - // - // Fall back to the default constructor. - // - try - { - obj = c.newInstance(); - } - catch(IllegalAccessException ex) - { - throw new FailureException( - "ServiceManager: unable to access default service constructor in class " + className, ex); - } + throw new FailureException("ServiceManager: invalid entry point format `" + classDir + "'", ex); } - - try + catch(java.io.IOException ex) { - info.service = (Service)obj; + throw new FailureException("ServiceManager: invalid path in plug-in entry point `" + classDir + + "'", ex); } - catch(ClassCastException ex) + catch(java.lang.ClassNotFoundException ex) { - throw new FailureException("ServiceManager: class " + className + " does not implement IceBox.Service"); + // Ignored } } - catch(InstantiationException ex) - { - throw new FailureException("ServiceManager: unable to instantiate class " + className, ex); - } - catch(FailureException ex) + else { - throw ex; + c = IceInternal.Util.findClass(className, null); } - catch(Throwable ex) + + if(c == null) { - throw new FailureException("ServiceManager: exception in service constructor for " + className, ex); + throw new FailureException("ServiceManager: class " + className + " not found"); } + ServiceInfo info = new ServiceInfo(); + info.name = service; + info.status = StatusStopped; + info.args = args; + // - // Invoke Service::start(). + // If Ice.UseSharedCommunicator.<name> is defined, create a + // communicator for the service. The communicator inherits + // from the shared communicator properties. If it's not + // defined, add the service properties to the shared + // commnunicator property set. // - try + Ice.Communicator communicator; + if(_communicator.getProperties().getPropertyAsInt("IceBox.UseSharedCommunicator." + service) > 0) { - // - // If Ice.UseSharedCommunicator.<name> is defined, create a - // communicator for the service. The communicator inherits - // from the shared communicator properties. If it's not - // defined, add the service properties to the shared - // commnunicator property set. - // - Ice.Communicator communicator; - if(_communicator.getProperties().getPropertyAsInt("IceBox.UseSharedCommunicator." + service) > 0) - { - assert(_sharedCommunicator != null); - communicator = _sharedCommunicator; - } - else + assert(_sharedCommunicator != null); + communicator = _sharedCommunicator; + } + else + { + try { // // Create the service properties. We use the communicator properties as the default @@ -653,12 +568,12 @@ public class ServiceManagerI extends _ServiceManagerDisp // serviceArgs.value = initData.properties.parseCommandLineOptions(service, serviceArgs.value); } - + // // Clone the logger to assign a new prefix. // initData.logger = _logger.cloneWithPrefix(initData.properties.getProperty("Ice.ProgramName")); - + // // Remaining command line options are passed to the communicator. This is // necessary for Ice plug-in properties (e.g.: IceSSL). @@ -667,71 +582,155 @@ public class ServiceManagerI extends _ServiceManagerDisp info.args = serviceArgs.value; communicator = info.communicator; } + catch(Throwable ex) + { + FailureException e = new FailureException(); + e.reason = "ServiceManager: exception while starting service " + service; + e.initCause(ex); + throw e; + } + } + + try + { + // + // Add a PropertiesAdmin facet to the service manager's communicator that provides + // access to this service's property set. We do this prior to instantiating the + // service so that the service's constructor is able to access the facet (e.g., + // in case it wants to set a callback). + // + final String facetName = "IceBox.Service." + service + ".Properties"; + _communicator.addAdminFacet( + new IceInternal.PropertiesAdminI(facetName, communicator.getProperties(), communicator.getLogger()), + facetName); + // + // Instantiate the service. + // try { - info.service.start(service, communicator, info.args); - info.status = StatusStarted; - // - // There is no need to notify the observers since the 'start all' - // (that indirectly calls this method) occurs before the creation of - // the Server Admin object, and before the activation of the main - // object adapter (so before any observer can be registered) + // If the service class provides a constructor that accepts an Ice.Communicator argument, + // use that in preference to the default constructor. // - } - catch(Throwable ex) - { - if(info.communicator != null) + java.lang.Object obj = null; + try { - try - { - info.communicator.shutdown(); - info.communicator.waitForShutdown(); - } - catch(Ice.CommunicatorDestroyedException e) + java.lang.reflect.Constructor<?> con = c.getDeclaredConstructor(Ice.Communicator.class); + obj = con.newInstance(_communicator); + } + catch(IllegalAccessException ex) + { + throw new FailureException( + "ServiceManager: unable to access service constructor " + className + "(Ice.Communicator)", ex); + } + catch(NoSuchMethodException ex) + { + // Ignore. + } + catch(java.lang.reflect.InvocationTargetException ex) + { + if(ex.getCause() != null) { - // - // Ignore, the service might have already destroyed - // the communicator for its own reasons. - // + throw ex.getCause(); } - catch(java.lang.Exception e) + else { - java.io.StringWriter sw = new java.io.StringWriter(); - java.io.PrintWriter pw = new java.io.PrintWriter(sw); - e.printStackTrace(pw); - pw.flush(); - _logger.warning("ServiceManager: exception while shutting down communicator for service " - + service + ":\n" + sw.toString()); + throw new FailureException("ServiceManager: exception in service constructor for " + className, ex); } + } + if(obj == null) + { + // + // Fall back to the default constructor. + // try { - info.communicator.destroy(); + obj = c.newInstance(); } - catch(java.lang.Exception e) + catch(IllegalAccessException ex) { - java.io.StringWriter sw = new java.io.StringWriter(); - java.io.PrintWriter pw = new java.io.PrintWriter(sw); - e.printStackTrace(pw); - pw.flush(); - _logger.warning("ServiceManager: exception while destroying communicator for service " - + service + ":\n" + sw.toString()); + throw new FailureException( + "ServiceManager: unable to access default service constructor in class " + className, ex); } } + + try + { + info.service = (Service)obj; + } + catch(ClassCastException ex) + { + throw new FailureException("ServiceManager: class " + className + " does not implement IceBox.Service"); + } + } + catch(InstantiationException ex) + { + throw new FailureException("ServiceManager: unable to instantiate class " + className, ex); + } + catch(FailureException ex) + { + throw ex; + } + catch(Throwable ex) + { + throw new FailureException("ServiceManager: exception in service constructor for " + className, ex); + } + + try + { + info.service.start(service, communicator, info.args); + + // + // There is no need to notify the observers since the 'start all' + // (that indirectly calls this method) occurs before the creation of + // the Server Admin object, and before the activation of the main + // object adapter (so before any observer can be registered) + // + } + catch(FailureException ex) + { throw ex; } + catch(Throwable ex) + { + FailureException e = new FailureException(); + e.reason = "ServiceManager: exception while starting service " + service; + e.initCause(ex); + throw e; + } + info.status = StatusStarted; _services.add(info); } - catch(FailureException ex) + catch(Ice.ObjectAdapterDeactivatedException ex) { - throw ex; + // + // Can be raised by addAdminFacet if the service manager communicator has been shut down. + // + if(info.communicator != null) + { + destroyServiceCommunicator(service, info.communicator); + } } - catch(Throwable ex) + catch(RuntimeException ex) { - throw new FailureException("ServiceManager: exception while starting service " + service, ex); + try + { + _communicator.removeAdminFacet("IceBox.Service." + service + ".Properties"); + } + catch(Ice.LocalException e) + { + // Ignored + } + + if(info.communicator != null) + { + destroyServiceCommunicator(service, info.communicator); + } + + throw ex; } } @@ -791,41 +790,7 @@ public class ServiceManagerI extends _ServiceManagerDisp if(info.communicator != null) { - try - { - info.communicator.shutdown(); - info.communicator.waitForShutdown(); - } - catch(Ice.CommunicatorDestroyedException e) - { - // - // Ignore, the service might have already destroyed - // the communicator for its own reasons. - // - } - catch(java.lang.Exception e) - { - java.io.StringWriter sw = new java.io.StringWriter(); - java.io.PrintWriter pw = new java.io.PrintWriter(sw); - e.printStackTrace(pw); - pw.flush(); - _logger.warning("ServiceManager: exception while stopping service " + info.name + ":\n" + - sw.toString()); - } - - try - { - info.communicator.destroy(); - } - catch(java.lang.Exception e) - { - java.io.StringWriter sw = new java.io.StringWriter(); - java.io.PrintWriter pw = new java.io.PrintWriter(sw); - e.printStackTrace(pw); - pw.flush(); - _logger.warning("ServiceManager: exception while stopping service " + info.name + ":\n" + - sw.toString()); - } + destroyServiceCommunicator(info.name, info.communicator); } } @@ -924,28 +889,6 @@ public class ServiceManagerI extends _ServiceManagerDisp public String[] args; } - static class PropertiesAdminI extends Ice._PropertiesAdminDisp - { - PropertiesAdminI(Ice.Properties properties) - { - _properties = properties; - } - - public String - getProperty(String name, Ice.Current current) - { - return _properties.getProperty(name); - } - - public java.util.TreeMap<String, String> - getPropertiesForPrefix(String name, Ice.Current current) - { - return new java.util.TreeMap<String, String>(_properties.getPropertiesForPrefix(name)); - } - - private final Ice.Properties _properties; - } - static class StartServiceInfo { StartServiceInfo(String service, String value, String[] serverArgs) @@ -1081,6 +1024,46 @@ public class ServiceManagerI extends _ServiceManagerDisp return properties; } + private void + destroyServiceCommunicator(String service, Ice.Communicator communicator) + { + try + { + communicator.shutdown(); + communicator.waitForShutdown(); + } + catch(Ice.CommunicatorDestroyedException e) + { + // + // Ignore, the service might have already destroyed + // the communicator for its own reasons. + // + } + catch(java.lang.Exception e) + { + java.io.StringWriter sw = new java.io.StringWriter(); + java.io.PrintWriter pw = new java.io.PrintWriter(sw); + e.printStackTrace(pw); + pw.flush(); + _logger.warning("ServiceManager: exception in shutting down communicator for service " + + service + "\n" + sw.toString()); + } + + try + { + communicator.destroy(); + } + catch(java.lang.Exception e) + { + java.io.StringWriter sw = new java.io.StringWriter(); + java.io.PrintWriter pw = new java.io.PrintWriter(sw); + e.printStackTrace(pw); + pw.flush(); + _logger.warning("ServiceManager: exception in destroying communicator for service " + + service + "\n" + sw.toString()); + } + } + private Ice.Communicator _communicator; private Ice.Communicator _sharedCommunicator; private Ice.Logger _logger; |