diff options
author | Austin Henriksen <austin.r.henriksen79@gmail.com> | 2018-12-05 14:20:48 -0500 |
---|---|---|
committer | Bernard Normier <bernard@zeroc.com> | 2018-12-05 14:20:48 -0500 |
commit | cfd671290ca684fea36263ea5a01cf1307f86643 (patch) | |
tree | 1225e193b0ac9237bfb0dea35a82a4eb741342c4 | |
parent | More TOC fixes (diff) | |
download | ice-cfd671290ca684fea36263ea5a01cf1307f86643.tar.bz2 ice-cfd671290ca684fea36263ea5a01cf1307f86643.tar.xz ice-cfd671290ca684fea36263ea5a01cf1307f86643.zip |
Removes Ice.Application from IceBox in Java, Java-Compat, and C# (#314)
-rw-r--r-- | csharp/src/IceBox/Server.cs | 117 | ||||
-rw-r--r-- | csharp/src/IceBox/ServiceManagerI.cs | 8 | ||||
-rw-r--r-- | java-compat/src/IceBox/src/main/java/IceBox/Admin.java | 262 | ||||
-rw-r--r-- | java-compat/src/IceBox/src/main/java/IceBox/Server.java | 128 | ||||
-rw-r--r-- | java-compat/src/IceBox/src/main/java/IceBox/ServiceManagerI.java | 9 | ||||
-rw-r--r-- | java/src/IceBox/src/main/java/com/zeroc/IceBox/Admin.java | 250 | ||||
-rw-r--r-- | java/src/IceBox/src/main/java/com/zeroc/IceBox/Server.java | 125 | ||||
-rw-r--r-- | java/src/IceBox/src/main/java/com/zeroc/IceBox/ServiceManagerI.java | 9 |
8 files changed, 515 insertions, 393 deletions
diff --git a/csharp/src/IceBox/Server.cs b/csharp/src/IceBox/Server.cs index 6a06fb5f2fd..21984721892 100644 --- a/csharp/src/IceBox/Server.cs +++ b/csharp/src/IceBox/Server.cs @@ -15,68 +15,85 @@ namespace IceBox public class Server { - public class App : Ice.Application + private static void usage() { - private static void usage() - { - Console.Error.WriteLine("Usage: iceboxnet [options] --Ice.Config=<file>\n"); - Console.Error.WriteLine( - "Options:\n" + - "-h, --help Show this message.\n" - ); - } + Console.Error.WriteLine("Usage: iceboxnet [options] --Ice.Config=<file>\n"); + Console.Error.WriteLine( + "Options:\n" + + "-h, --help Show this message.\n" + ); + } + + public static int Main(string[] args) + { + int status = 0; + List<string> argSeq = new List<string>(); + const string prefix = "IceBox.Service."; + + Ice.InitializationData initData = new Ice.InitializationData(); + initData.properties = Ice.Util.createProperties(); + initData.properties.setProperty("Ice.Admin.DelayCreation", "1"); - public override int run(string[] args) + try { - List<string> argSeq = new List<string>(args); - const string prefix = "IceBox.Service."; - Ice.Properties properties = communicator().getProperties(); - Dictionary<string, string> services = properties.getPropertiesForPrefix(prefix); - foreach(KeyValuePair<string, string> pair in services) + using(var communicator = Ice.Util.initialize(ref args, initData)) { - string name = pair.Key.Substring(prefix.Length); - for(int i = 0; i < argSeq.Count; ++i) + Console.CancelKeyPress += (sender, eventArgs) => + { + eventArgs.Cancel = true; + communicator.shutdown(); + }; + + Ice.Properties properties = communicator.getProperties(); + Dictionary<string, string> services = properties.getPropertiesForPrefix(prefix); + + foreach(string arg in argSeq) { - if(argSeq[i].StartsWith("--" + name, StringComparison.CurrentCulture)) + bool valid = false; + foreach(KeyValuePair<string, string> pair in services) + { + string name = pair.Key.Substring(prefix.Length); + if(arg.StartsWith("--" + name, StringComparison.CurrentCulture)) + { + valid = true; + break; + } + } + if(!valid) { - argSeq.RemoveAt(i); - i--; + if(arg.Equals("-h") || arg.Equals("--help")) + { + usage(); + status = 1; + break; + } + else if(arg.Equals("-v") || arg.Equals("--version")) + { + Console.Out.WriteLine(Ice.Util.stringVersion()); + status = 1; + break; + } + else + { + Console.Error.WriteLine("IceBox.Server: unknown option `" + arg + "'"); + usage(); + status = 1; + break; + } } } - } - foreach(string s in argSeq) - { - if(s.Equals("-h") || s.Equals("--help")) - { - usage(); - return 0; - } - else if(s.Equals("-v") || s.Equals("--version")) - { - Console.Out.WriteLine(Ice.Util.stringVersion()); - return 0; - } - else - { - Console.Error.WriteLine("Server: unknown option `" + s + "'"); - usage(); - return 1; - } + ServiceManagerI serviceManagerImpl = new ServiceManagerI(communicator, argSeq.ToArray()); + status = serviceManagerImpl.run(); } - - ServiceManagerI serviceManagerImpl = new ServiceManagerI(communicator(), args); - return serviceManagerImpl.run(); } - } + catch(Exception ex) + { + Console.Error.WriteLine(ex); + status = 1; + } - public static int Main(string[] args) - { - Ice.InitializationData initData = new Ice.InitializationData(); - initData.properties = Ice.Util.createProperties(); - initData.properties.setProperty("Ice.Admin.DelayCreation", "1"); - App server = new App(); - return server.main(args, initData); + return status; } } } diff --git a/csharp/src/IceBox/ServiceManagerI.cs b/csharp/src/IceBox/ServiceManagerI.cs index 575a2281919..a3a527b8085 100644 --- a/csharp/src/IceBox/ServiceManagerI.cs +++ b/csharp/src/IceBox/ServiceManagerI.cs @@ -407,14 +407,6 @@ class ServiceManagerI : ServiceManagerDisp_ } // - // Don't move after the adapter activation. This allows - // applications to wait for the service manager to be - // reachable before sending a signal to shutdown the - // - // - Ice.Application.shutdownOnInterrupt(); - - // // Register "this" as a facet to the Admin object and create Admin object // try diff --git a/java-compat/src/IceBox/src/main/java/IceBox/Admin.java b/java-compat/src/IceBox/src/main/java/IceBox/Admin.java index 515cf8055f1..4701f24f495 100644 --- a/java-compat/src/IceBox/src/main/java/IceBox/Admin.java +++ b/java-compat/src/IceBox/src/main/java/IceBox/Admin.java @@ -11,174 +11,192 @@ package IceBox; public final class Admin { - private static class Client extends Ice.Application + static class ShutdownHook implements Runnable { - private void - usage() + private Ice.Communicator communicator; + + ShutdownHook(Ice.Communicator communicator) { - System.err.println( - "Usage: " + appName() + " [options] [command...]\n" + - "Options:\n" + - "-h, --help Show this message.\n" + - "\n" + - "Commands:\n" + - "start SERVICE Start a service.\n" + - "stop SERVICE Stop a service.\n" + - "shutdown Shutdown the server."); + this.communicator = communicator; } @Override - public int - run(String[] args) + public void + run() { - java.util.List<String> commands = new java.util.ArrayList<String>(); + communicator.destroy(); + } + } - int idx = 0; - while(idx < args.length) + private static void + usage() + { + System.err.println( + "Usage: IceBox.Admin [options] [command...]\n" + + "Options:\n" + + "-h, --help Show this message.\n" + + "\n" + + "Commands:\n" + + "start SERVICE Start a service.\n" + + "stop SERVICE Stop a service.\n" + + "shutdown Shutdown the server."); + } + + public static void + main(String[] args) + { + int status = 0; + Ice.StringSeqHolder argHolder = new Ice.StringSeqHolder(args); + + try(Ice.Communicator communicator = Ice.Util.initialize(argHolder)) + { + Runtime.getRuntime().addShutdownHook(new Thread(new ShutdownHook(communicator))); + + java.util.List<String> commands = new java.util.ArrayList<String>(java.util.Arrays.asList(argHolder.value)); + + for(String command : commands) { - if(args[idx].equals("-h") || args[idx].equals("--help")) + if(command.equals("-h") || command.equals("--help")) { usage(); - return 1; + status = 1; + break; } - else if(args[idx].charAt(0) == '-') + else if(command.charAt(0) == '-') { - System.err.println(appName() + ": unknown option `" + args[idx] + "'"); + System.err.println("IceBox.Admin: unknown option `" + command + "'"); usage(); - return 1; - } - else - { - commands.add(args[idx]); - ++idx; + status = 1; + break; } } if(commands.isEmpty()) { usage(); - return 0; + status = 0; } + else if(status == 0) + { + status = run(communicator, commands); + } + } + + System.exit(status); + } - Ice.ObjectPrx base = communicator().propertyToProxy("IceBoxAdmin.ServiceManager.Proxy"); + public static int + run(Ice.Communicator communicator, java.util.List<String> commands) + { + Ice.ObjectPrx base = communicator.propertyToProxy("IceBoxAdmin.ServiceManager.Proxy"); - if(base == null) - { - // - // The old deprecated way to retrieve the service manager proxy - // + if(base == null) + { + // + // The old deprecated way to retrieve the service manager proxy + // - Ice.Properties properties = communicator().getProperties(); + Ice.Properties properties = communicator.getProperties(); - Ice.Identity managerIdentity = new Ice.Identity(); - managerIdentity.category = properties.getPropertyWithDefault("IceBox.InstanceName", "IceBox"); - managerIdentity.name = "ServiceManager"; + Ice.Identity managerIdentity = new Ice.Identity(); + managerIdentity.category = properties.getPropertyWithDefault("IceBox.InstanceName", "IceBox"); + managerIdentity.name = "ServiceManager"; - String managerProxy; - if(properties.getProperty("Ice.Default.Locator").length() == 0) + String managerProxy; + if(properties.getProperty("Ice.Default.Locator").length() == 0) + { + String managerEndpoints = properties.getProperty("IceBox.ServiceManager.Endpoints"); + if(managerEndpoints.length() == 0) { - String managerEndpoints = properties.getProperty("IceBox.ServiceManager.Endpoints"); - if(managerEndpoints.length() == 0) - { - System.err.println(appName() + ": property `IceBoxAdmin.ServiceManager.Proxy' is not set"); - return 1; - } - - managerProxy = "\"" + communicator().identityToString(managerIdentity) + "\" :" + managerEndpoints; + System.err.println("IceBox.Admin: property `IceBoxAdmin.ServiceManager.Proxy' is not set"); + return 1; } - else + + managerProxy = "\"" + communicator.identityToString(managerIdentity) + "\" :" + managerEndpoints; + } + else + { + String managerAdapterId = properties.getProperty("IceBox.ServiceManager.AdapterId"); + if(managerAdapterId.length() == 0) { - String managerAdapterId = properties.getProperty("IceBox.ServiceManager.AdapterId"); - if(managerAdapterId.length() == 0) - { - System.err.println(appName() + ": property `IceBoxAdmin.ServiceManager.Proxy' is not set"); - return 1; - } - - managerProxy = "\"" + communicator().identityToString(managerIdentity) + "\" @" + managerAdapterId; + System.err.println("IceBox.Admin: property `IceBoxAdmin.ServiceManager.Proxy' is not set"); + return 1; } - base = communicator().stringToProxy(managerProxy); + managerProxy = "\"" + communicator.identityToString(managerIdentity) + "\" @" + managerAdapterId; } - IceBox.ServiceManagerPrx manager = IceBox.ServiceManagerPrxHelper.checkedCast(base); - if(manager == null) + base = communicator.stringToProxy(managerProxy); + } + + IceBox.ServiceManagerPrx manager = IceBox.ServiceManagerPrxHelper.checkedCast(base); + if(manager == null) + { + System.err.println("IceBox.Admin: `" + base.toString() + "' is not an IceBox::ServiceManager"); + return 1; + } + + for(int i = 0; i < commands.size(); i++) + { + String command = commands.get(i); + if(command.equals("shutdown")) { - System.err.println(appName() + ": `" + base.toString() + "' is not an IceBox::ServiceManager"); - return 1; + manager.shutdown(); } - - for(int i = 0; i < commands.size(); i++) + else if(command.equals("start")) { - String command = commands.get(i); - if(command.equals("shutdown")) + if(++i >= commands.size()) + { + System.err.println("IceBox.Admin: no service name specified."); + return 1; + } + + String service = commands.get(i); + try { - manager.shutdown(); + manager.startService(service); } - else if(command.equals("start")) + catch(IceBox.NoSuchServiceException ex) { - if(++i >= commands.size()) - { - System.err.println(appName() + ": no service name specified."); - return 1; - } - - String service = commands.get(i); - try - { - manager.startService(service); - } - catch(IceBox.NoSuchServiceException ex) - { - System.err.println(appName() + ": unknown service `" + service + "'"); - return 1; - } - catch(IceBox.AlreadyStartedException ex) - { - System.err.println(appName() + "service already started."); - } + System.err.println("IceBox.Admin: unknown service `" + service + "'"); + return 1; } - else if(command.equals("stop")) + catch(IceBox.AlreadyStartedException ex) { - if(++i >= commands.size()) - { - System.err.println(appName() + ": no service name specified."); - return 1; - } - - String service = commands.get(i); - try - { - manager.stopService(service); - } - catch(IceBox.NoSuchServiceException ex) - { - System.err.println(appName() + ": unknown service `" + service + "'"); - return 1; - } - catch(IceBox.AlreadyStoppedException ex) - { - System.err.println(appName() + "service already stopped."); - } + System.err.println("IceBox.Admin: service already started."); } - else + } + else if(command.equals("stop")) + { + if(++i >= commands.size()) { - System.err.println(appName() + ": unknown command `" + command + "'"); - usage(); + System.err.println("IceBox.Admin: no service name specified."); return 1; } - } - return 0; + String service = commands.get(i); + try + { + manager.stopService(service); + } + catch(IceBox.NoSuchServiceException ex) + { + System.err.println("IceBox.Admin: unknown service `" + service + "'"); + return 1; + } + catch(IceBox.AlreadyStoppedException ex) + { + System.err.println("IceBox.Admin: service already stopped."); + } + } + else + { + System.err.println("IceBox.Admin: unknown command `" + command + "'"); + usage(); + return 1; + } } - } - - public static void - main(String[] args) - { - Client app = new Client(); - int rc = app.main("IceBox.Admin", args); - System.exit(rc); + return 0; } } diff --git a/java-compat/src/IceBox/src/main/java/IceBox/Server.java b/java-compat/src/IceBox/src/main/java/IceBox/Server.java index 06f4996c7b7..0a129f82797 100644 --- a/java-compat/src/IceBox/src/main/java/IceBox/Server.java +++ b/java-compat/src/IceBox/src/main/java/IceBox/Server.java @@ -9,8 +9,56 @@ package IceBox; -public final class Server extends Ice.Application +public final class Server { + static class ShutdownHook extends Thread + { + private Ice.Communicator _communicator; + private final java.lang.Object _doneMutex = new java.lang.Object(); + private boolean _done = false; + + ShutdownHook(Ice.Communicator communicator) + { + _communicator = communicator; + } + + @Override + public void + run() + { + _communicator.shutdown(); + + synchronized(_doneMutex) + { + // + // Wait on the server to finish shutting down before exiting the ShutdownHook. This ensures + // that all IceBox services have had a chance to shutdown cleanly before the JVM terminates. + // + while(!_done) + { + try + { + _doneMutex.wait(); + } + catch(InterruptedException ex) + { + break; + } + } + } + } + + public void + done() + { + synchronized(_doneMutex) + { + _done = true; + _doneMutex.notify(); + } + } + } + private static void usage() { @@ -24,56 +72,64 @@ public final class Server extends Ice.Application public static void main(String[] args) { + int status = 0; + Ice.StringSeqHolder argHolder = new Ice.StringSeqHolder(args); + Ice.InitializationData initData = new Ice.InitializationData(); initData.properties = Ice.Util.createProperties(); initData.properties.setProperty("Ice.Admin.DelayCreation", "1"); + ShutdownHook shutdownHook = null; - Server server = new Server(); - System.exit(server.main("IceBox.Server", args, initData)); - } - - @Override - public int - run(String[] args) - { - final String prefix = "IceBox.Service."; - Ice.Properties properties = communicator().getProperties(); - java.util.Map<String, String> services = properties.getPropertiesForPrefix(prefix); - java.util.List<String> argSeq = new java.util.ArrayList<String>(args.length); - for(String s : args) + try(Ice.Communicator communicator = Ice.Util.initialize(argHolder, initData)) { - argSeq.add(s); - } + shutdownHook = new ShutdownHook(communicator); + Runtime.getRuntime().addShutdownHook(shutdownHook); - for(java.util.Map.Entry<String, String> entry : services.entrySet()) - { - String name = entry.getKey().substring(prefix.length()); - for(int i = 0; i < argSeq.size(); ++i) + final String prefix = "IceBox.Service."; + Ice.Properties properties = communicator.getProperties(); + java.util.Map<String, String> services = properties.getPropertiesForPrefix(prefix); + + for(String arg : argHolder.value) { - if(argSeq.get(i).startsWith("--" + name)) + boolean valid = false; + for(java.util.Map.Entry<String, String> entry : services.entrySet()) + { + String name = entry.getKey().substring(prefix.length()); + if(arg.startsWith("--" + name)) + { + valid = true; + break; + } + } + if(!valid) { - argSeq.remove(i); - i--; + if(arg.equals("-h") || arg.equals("--help")) + { + usage(); + status = 1; + break; + } + else + { + System.err.println("IceBox.Server: unknown option `" + arg + "'"); + usage(); + status = 1; + break; + } } } - } - for(String arg : argSeq) + ServiceManagerI serviceManagerImpl = new ServiceManagerI(communicator, argHolder.value); + status = serviceManagerImpl.run(); + } + finally { - if(arg.equals("-h") || arg.equals("--help")) - { - usage(); - return 0; - } - else + if(shutdownHook != null) { - System.err.println("Server: unknown option `" + arg + "'"); - usage(); - return 1; + shutdownHook.done(); } } - ServiceManagerI serviceManagerImpl = new ServiceManagerI(communicator(), args); - return serviceManagerImpl.run(); + System.exit(status); } } diff --git a/java-compat/src/IceBox/src/main/java/IceBox/ServiceManagerI.java b/java-compat/src/IceBox/src/main/java/IceBox/ServiceManagerI.java index 670a63f1909..42eedb3d490 100644 --- a/java-compat/src/IceBox/src/main/java/IceBox/ServiceManagerI.java +++ b/java-compat/src/IceBox/src/main/java/IceBox/ServiceManagerI.java @@ -430,14 +430,6 @@ public class ServiceManagerI extends _ServiceManagerDisp } // - // Don't move after the adapter activation. This allows - // applications to wait for the service manager to be - // reachable before sending a signal to shutdown the - // IceBox. - // - Ice.Application.shutdownOnInterrupt(); - - // // Register "this" as a facet to the Admin object and // create Admin object // @@ -471,7 +463,6 @@ public class ServiceManagerI extends _ServiceManagerDisp } _communicator.waitForShutdown(); - Ice.Application.defaultInterrupt(); } catch(FailureException ex) { diff --git a/java/src/IceBox/src/main/java/com/zeroc/IceBox/Admin.java b/java/src/IceBox/src/main/java/com/zeroc/IceBox/Admin.java index c7dec7f8945..7c5cff7088f 100644 --- a/java/src/IceBox/src/main/java/com/zeroc/IceBox/Admin.java +++ b/java/src/IceBox/src/main/java/com/zeroc/IceBox/Admin.java @@ -11,173 +11,175 @@ package com.zeroc.IceBox; public final class Admin { - private static class Client extends com.zeroc.Ice.Application + private static void usage() { - private void usage() - { - System.err.println( - "Usage: " + appName() + " [options] [command...]\n" + - "Options:\n" + - "-h, --help Show this message.\n" + - "\n" + - "Commands:\n" + - "start SERVICE Start a service.\n" + - "stop SERVICE Stop a service.\n" + - "shutdown Shutdown the server."); - } + System.err.println( + "Usage: com.zeroc.IceBox.Admin [options] [command...]\n" + + "Options:\n" + + "-h, --help Show this message.\n" + + "\n" + + "Commands:\n" + + "start SERVICE Start a service.\n" + + "stop SERVICE Stop a service.\n" + + "shutdown Shutdown the server."); + } + + public static void main(String[] args) + { + int status = 0; + java.util.List<String> commands = new java.util.ArrayList<String>(); - @Override - public int run(String[] args) + try(com.zeroc.Ice.Communicator communicator = com.zeroc.Ice.Util.initialize(args, commands)) { - java.util.List<String> commands = new java.util.ArrayList<>(); + Runtime.getRuntime().addShutdownHook(new Thread(() -> + { + communicator.destroy(); + })); - int idx = 0; - while(idx < args.length) + for(String command : commands) { - if(args[idx].equals("-h") || args[idx].equals("--help")) + if(command.equals("-h") || command.equals("--help")) { usage(); - return 1; + status = 1; + break; } - else if(args[idx].charAt(0) == '-') + else if(command.charAt(0) == '-') { - System.err.println(appName() + ": unknown option `" + args[idx] + "'"); + System.err.println("IceBox.Admin: unknown option `" + command + "'"); usage(); - return 1; - } - else - { - commands.add(args[idx]); - ++idx; + status = 1; + break; } } if(commands.isEmpty()) { usage(); - return 0; + status = 0; } + else + { + status = run(communicator, commands); + } + } - com.zeroc.Ice.ObjectPrx base = communicator().propertyToProxy("IceBoxAdmin.ServiceManager.Proxy"); + System.exit(status); + } - if(base == null) - { - // - // The old deprecated way to retrieve the service manager proxy - // + public static int run(com.zeroc.Ice.Communicator communicator, java.util.List<String> commands) + { + com.zeroc.Ice.ObjectPrx base = communicator.propertyToProxy("IceBoxAdmin.ServiceManager.Proxy"); + + if(base == null) + { + // + // The old deprecated way to retrieve the service manager proxy + // - com.zeroc.Ice.Properties properties = communicator().getProperties(); + com.zeroc.Ice.Properties properties = communicator.getProperties(); - com.zeroc.Ice.Identity managerIdentity = new com.zeroc.Ice.Identity(); - managerIdentity.category = properties.getPropertyWithDefault("IceBox.InstanceName", "IceBox"); - managerIdentity.name = "ServiceManager"; + com.zeroc.Ice.Identity managerIdentity = new com.zeroc.Ice.Identity(); + managerIdentity.category = properties.getPropertyWithDefault("IceBox.InstanceName", "IceBox"); + managerIdentity.name = "ServiceManager"; - String managerProxy; - if(properties.getProperty("Ice.Default.Locator").length() == 0) + String managerProxy; + if(properties.getProperty("Ice.Default.Locator").length() == 0) + { + String managerEndpoints = properties.getProperty("IceBox.ServiceManager.Endpoints"); + if(managerEndpoints.length() == 0) { - String managerEndpoints = properties.getProperty("IceBox.ServiceManager.Endpoints"); - if(managerEndpoints.length() == 0) - { - System.err.println(appName() + ": property `IceBoxAdmin.ServiceManager.Proxy' is not set"); - return 1; - } - - managerProxy = "\"" + communicator().identityToString(managerIdentity) + "\" :" + - managerEndpoints; + System.err.println("IceBox.Admin: property `IceBoxAdmin.ServiceManager.Proxy' is not set"); + return 1; } - else + + managerProxy = "\"" + communicator.identityToString(managerIdentity) + "\" :" + + managerEndpoints; + } + else + { + String managerAdapterId = properties.getProperty("IceBox.ServiceManager.AdapterId"); + if(managerAdapterId.length() == 0) { - String managerAdapterId = properties.getProperty("IceBox.ServiceManager.AdapterId"); - if(managerAdapterId.length() == 0) - { - System.err.println(appName() + ": property `IceBoxAdmin.ServiceManager.Proxy' is not set"); - return 1; - } - - managerProxy = "\"" + communicator().identityToString(managerIdentity) + "\" @" + - managerAdapterId; + System.err.println("IceBox.Admin: property `IceBoxAdmin.ServiceManager.Proxy' is not set"); + return 1; } - base = communicator().stringToProxy(managerProxy); + managerProxy = "\"" + communicator.identityToString(managerIdentity) + "\" @" + + managerAdapterId; } - com.zeroc.IceBox.ServiceManagerPrx manager = com.zeroc.IceBox.ServiceManagerPrx.checkedCast(base); - if(manager == null) + base = communicator.stringToProxy(managerProxy); + } + + com.zeroc.IceBox.ServiceManagerPrx manager = com.zeroc.IceBox.ServiceManagerPrx.checkedCast(base); + if(manager == null) + { + System.err.println("IceBox.Admin: `" + base.toString() + "' is not an IceBox::ServiceManager"); + return 1; + } + + for(int i = 0; i < commands.size(); i++) + { + String command = commands.get(i); + if(command.equals("shutdown")) { - System.err.println(appName() + ": `" + base.toString() + "' is not an IceBox::ServiceManager"); - return 1; + manager.shutdown(); } - - for(int i = 0; i < commands.size(); i++) + else if(command.equals("start")) { - String command = commands.get(i); - if(command.equals("shutdown")) + if(++i >= commands.size()) { - manager.shutdown(); + System.err.println("IceBox.Admin: no service name specified."); + return 1; } - else if(command.equals("start")) + + String service = commands.get(i); + try { - if(++i >= commands.size()) - { - System.err.println(appName() + ": no service name specified."); - return 1; - } - - String service = commands.get(i); - try - { - manager.startService(service); - } - catch(com.zeroc.IceBox.NoSuchServiceException ex) - { - System.err.println(appName() + ": unknown service `" + service + "'"); - return 1; - } - catch(com.zeroc.IceBox.AlreadyStartedException ex) - { - System.err.println(appName() + "service already started."); - } + manager.startService(service); } - else if(command.equals("stop")) + catch(com.zeroc.IceBox.NoSuchServiceException ex) { - if(++i >= commands.size()) - { - System.err.println(appName() + ": no service name specified."); - return 1; - } - - String service = commands.get(i); - try - { - manager.stopService(service); - } - catch(com.zeroc.IceBox.NoSuchServiceException ex) - { - System.err.println(appName() + ": unknown service `" + service + "'"); - return 1; - } - catch(com.zeroc.IceBox.AlreadyStoppedException ex) - { - System.err.println(appName() + "service already stopped."); - } + System.err.println("IceBox.Admin: unknown service `" + service + "'"); + return 1; } - else + catch(com.zeroc.IceBox.AlreadyStartedException ex) { - System.err.println(appName() + ": unknown command `" + command + "'"); - usage(); - return 1; + System.err.println("IceBox.Admin: service already started."); } } + else if(command.equals("stop")) + { + if(++i >= commands.size()) + { + System.err.println("IceBox.Admin: no service name specified."); + return 1; + } - return 0; + String service = commands.get(i); + try + { + manager.stopService(service); + } + catch(com.zeroc.IceBox.NoSuchServiceException ex) + { + System.err.println("IceBox.Admin: unknown service `" + service + "'"); + return 1; + } + catch(com.zeroc.IceBox.AlreadyStoppedException ex) + { + System.err.println("IceBox.Admin: service already stopped."); + } + } + else + { + System.err.println("IceBox.Admin: unknown command `" + command + "'"); + usage(); + return 1; + } } - } - - public static void main(String[] args) - { - Client app = new Client(); - int rc = app.main("IceBox.Admin", args); - System.exit(rc); + return 0; } } diff --git a/java/src/IceBox/src/main/java/com/zeroc/IceBox/Server.java b/java/src/IceBox/src/main/java/com/zeroc/IceBox/Server.java index 78d9b0bb366..3f0b2bac0d4 100644 --- a/java/src/IceBox/src/main/java/com/zeroc/IceBox/Server.java +++ b/java/src/IceBox/src/main/java/com/zeroc/IceBox/Server.java @@ -9,8 +9,54 @@ package com.zeroc.IceBox; -public final class Server extends com.zeroc.Ice.Application +public final class Server { + static class ShutdownHook extends Thread + { + private com.zeroc.Ice.Communicator _communicator; + private final java.lang.Object _doneMutex = new java.lang.Object(); + private boolean _done = false; + + ShutdownHook(com.zeroc.Ice.Communicator communicator) + { + _communicator = communicator; + } + + @Override + public void run() + { + _communicator.shutdown(); + + synchronized(_doneMutex) + { + // + // Wait on the server to finish shutting down before exiting the ShutdownHook. This ensures + // that all IceBox services have had a chance to shutdown cleanly before the JVM terminates. + // + while(!_done) + { + try + { + _doneMutex.wait(); + } + catch(InterruptedException ex) + { + break; + } + } + } + } + + public void done() + { + synchronized(_doneMutex) + { + _done = true; + _doneMutex.notify(); + } + } + } + private static void usage() { System.err.println("Usage: com.zeroc.IceBox.Server [options] --Ice.Config=<file>\n"); @@ -22,55 +68,64 @@ public final class Server extends com.zeroc.Ice.Application public static void main(String[] args) { + int status = 0; + java.util.List<String> argSeq = new java.util.ArrayList<String>(); + com.zeroc.Ice.InitializationData initData = new com.zeroc.Ice.InitializationData(); initData.properties = com.zeroc.Ice.Util.createProperties(); initData.properties.setProperty("Ice.Admin.DelayCreation", "1"); + ShutdownHook shutdownHook = null; - Server server = new Server(); - System.exit(server.main("IceBox.Server", args, initData)); - } - - @Override - public int run(String[] args) - { - final String prefix = "IceBox.Service."; - com.zeroc.Ice.Properties properties = communicator().getProperties(); - java.util.Map<String, String> services = properties.getPropertiesForPrefix(prefix); - java.util.List<String> argSeq = new java.util.ArrayList<>(args.length); - for(String s : args) + try(com.zeroc.Ice.Communicator communicator = com.zeroc.Ice.Util.initialize(args, initData, argSeq)) { - argSeq.add(s); - } + shutdownHook = new ShutdownHook(communicator); + Runtime.getRuntime().addShutdownHook(shutdownHook); - for(java.util.Map.Entry<String, String> entry : services.entrySet()) - { - String name = entry.getKey().substring(prefix.length()); - for(int i = 0; i < argSeq.size(); ++i) + final String prefix = "IceBox.Service."; + com.zeroc.Ice.Properties properties = communicator.getProperties(); + java.util.Map<String, String> services = properties.getPropertiesForPrefix(prefix); + + for(String arg : argSeq) { - if(argSeq.get(i).startsWith("--" + name)) + boolean valid = false; + for(java.util.Map.Entry<String, String> entry : services.entrySet()) + { + String name = entry.getKey().substring(prefix.length()); + if(arg.startsWith("--" + name)) + { + valid = true; + break; + } + } + if(!valid) { - argSeq.remove(i); - i--; + if(arg.equals("-h") || arg.equals("--help")) + { + usage(); + status = 1; + break; + } + else + { + System.err.println("IceBox.Server: unknown option `" + arg + "'"); + usage(); + status = 1; + break; + } } } - } - for(String arg : argSeq) + ServiceManagerI serviceManagerImpl = new ServiceManagerI(communicator, args); + status = serviceManagerImpl.run(); + } + finally { - if(arg.equals("-h") || arg.equals("--help")) - { - usage(); - return 0; - } - else + if(shutdownHook != null) { - System.err.println("Server: unknown option `" + arg + "'"); - usage(); - return 1; + shutdownHook.done(); } } - ServiceManagerI serviceManagerImpl = new ServiceManagerI(communicator(), args); - return serviceManagerImpl.run(); + System.exit(status); } } diff --git a/java/src/IceBox/src/main/java/com/zeroc/IceBox/ServiceManagerI.java b/java/src/IceBox/src/main/java/com/zeroc/IceBox/ServiceManagerI.java index b8a2bf9a10d..b03f32defcc 100644 --- a/java/src/IceBox/src/main/java/com/zeroc/IceBox/ServiceManagerI.java +++ b/java/src/IceBox/src/main/java/com/zeroc/IceBox/ServiceManagerI.java @@ -411,14 +411,6 @@ public class ServiceManagerI implements ServiceManager } // - // Don't move after the adapter activation. This allows - // applications to wait for the service manager to be - // reachable before sending a signal to shutdown the - // IceBox. - // - com.zeroc.Ice.Application.shutdownOnInterrupt(); - - // // Register "this" as a facet to the Admin object and // create Admin object // @@ -452,7 +444,6 @@ public class ServiceManagerI implements ServiceManager } _communicator.waitForShutdown(); - com.zeroc.Ice.Application.defaultInterrupt(); } catch(FailureException ex) { |