diff options
author | jsdelfino <jsdelfino@13f79535-47bb-0310-9956-ffa450edef68> | 2008-09-05 02:07:43 +0000 |
---|---|---|
committer | jsdelfino <jsdelfino@13f79535-47bb-0310-9956-ffa450edef68> | 2008-09-05 02:07:43 +0000 |
commit | a8fa99e74baf16f8f5f83f0770d354523c9df6cd (patch) | |
tree | cc2c47b81b94e135aa51f44901695df5eb466b36 /java/sca/modules/node-launcher-equinox/src | |
parent | 6059c887cf7e4880a2de03959cf574a3d1f193cd (diff) |
Minor fixes to the JSE and OSGi Equinox launchers. Added shutdown hooks, some try/catch, cleaned up the logs a bit.
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@692319 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'java/sca/modules/node-launcher-equinox/src')
4 files changed, 232 insertions, 51 deletions
diff --git a/java/sca/modules/node-launcher-equinox/src/main/java/org/apache/tuscany/sca/node/equinox/launcher/DomainManagerLauncher.java b/java/sca/modules/node-launcher-equinox/src/main/java/org/apache/tuscany/sca/node/equinox/launcher/DomainManagerLauncher.java index d2cda21de4..388f42bbb3 100644 --- a/java/sca/modules/node-launcher-equinox/src/main/java/org/apache/tuscany/sca/node/equinox/launcher/DomainManagerLauncher.java +++ b/java/sca/modules/node-launcher-equinox/src/main/java/org/apache/tuscany/sca/node/equinox/launcher/DomainManagerLauncher.java @@ -20,6 +20,8 @@ package org.apache.tuscany.sca.node.equinox.launcher; import static org.apache.tuscany.sca.node.equinox.launcher.NodeLauncherUtil.domainManager; +import static org.apache.tuscany.sca.node.equinox.launcher.NodeLauncherUtil.startOSGi; +import static org.apache.tuscany.sca.node.equinox.launcher.NodeLauncherUtil.stopOSGi; import java.io.IOException; import java.util.logging.Level; @@ -32,7 +34,7 @@ import java.util.logging.Logger; */ public class DomainManagerLauncher { - private static final Logger logger = Logger.getLogger(DomainManagerLauncher.class.getName()); + static final Logger logger = Logger.getLogger(DomainManagerLauncher.class.getName()); /** * Constructs a new DomainManagerLauncher. @@ -72,39 +74,100 @@ public class DomainManagerLauncher { } public static void main(String[] args) throws Exception { - logger.info("Apache Tuscany SCA Domain Manager starting..."); + logger.info("Apache Tuscany SCA Domain Manager is starting..."); - // Create a domain manager + // Create a launcher DomainManagerLauncher launcher = newInstance(); - OSGiHost host = NodeLauncherUtil.startOSGi(); + + OSGiHost osgiHost = null; + Object domainManager = null; + ShutdownThread shutdown = null; try { - Object domainManager = launcher.createDomainManager(); + // Start the OSGi host + osgiHost = startOSGi(); // Start the domain manager + domainManager = launcher.createDomainManager(); try { domainManager.getClass().getMethod("start").invoke(domainManager); } catch (Exception e) { logger.log(Level.SEVERE, "SCA Domain Manager could not be started", e); throw e; } - logger.info("SCA Domain Manager started."); + logger.info("SCA Domain Manager is now started."); + + // Install a shutdown hook + ShutdownThread hook = new ShutdownThread(domainManager, osgiHost); + Runtime.getRuntime().addShutdownHook(hook); logger.info("Press enter to shutdown."); try { System.in.read(); } catch (IOException e) { + + // Wait forever + Object lock = new Object(); + synchronized(lock) { + lock.wait(); + } } - // Stop the domain manager + } finally { + + // Remove the shutdown hook + if (shutdown != null) { + Runtime.getRuntime().removeShutdownHook(shutdown); + } + + // Stop the domain manager and OSGi host + if (domainManager != null) { + stopDomainManager(domainManager); + } + if (osgiHost != null) { + stopOSGi(osgiHost); + } + } + } + + + /** + * Stop the given domain manager. + * + * @param domainManager + * @throws Exception + */ + private static void stopDomainManager(Object domainManager) throws Exception { + try { + domainManager.getClass().getMethod("stop").invoke(domainManager); + logger.info("SCA Domain Manager is now stopped."); + } catch (Exception e) { + logger.log(Level.SEVERE, "SCA Domain Manager could not be stopped", e); + throw e; + } + } + + private static class ShutdownThread extends Thread { + private Object domainManager; + private OSGiHost osgiHost; + + public ShutdownThread(Object domainManager, OSGiHost osgiHost) { + super(); + this.domainManager = domainManager; + this.osgiHost = osgiHost; + } + + public void run() { try { - domainManager.getClass().getMethod("stop").invoke(domainManager); + stopDomainManager(domainManager); } catch (Exception e) { - logger.log(Level.SEVERE, "SCA Domain Manager could not be stopped", e); - throw e; + // Ignore + } + try { + stopOSGi(osgiHost); + } catch (Exception e) { + // Ignore } - } finally { - NodeLauncherUtil.stopOSGi(host); } } } diff --git a/java/sca/modules/node-launcher-equinox/src/main/java/org/apache/tuscany/sca/node/equinox/launcher/NodeDaemonLauncher.java b/java/sca/modules/node-launcher-equinox/src/main/java/org/apache/tuscany/sca/node/equinox/launcher/NodeDaemonLauncher.java index fb2534485b..2eb4609da1 100644 --- a/java/sca/modules/node-launcher-equinox/src/main/java/org/apache/tuscany/sca/node/equinox/launcher/NodeDaemonLauncher.java +++ b/java/sca/modules/node-launcher-equinox/src/main/java/org/apache/tuscany/sca/node/equinox/launcher/NodeDaemonLauncher.java @@ -20,6 +20,8 @@ package org.apache.tuscany.sca.node.equinox.launcher; import static org.apache.tuscany.sca.node.equinox.launcher.NodeLauncherUtil.nodeDaemon; +import static org.apache.tuscany.sca.node.equinox.launcher.NodeLauncherUtil.startOSGi; +import static org.apache.tuscany.sca.node.equinox.launcher.NodeLauncherUtil.stopOSGi; import java.io.IOException; import java.util.logging.Level; @@ -32,7 +34,7 @@ import java.util.logging.Logger; */ public class NodeDaemonLauncher { - private static final Logger logger = Logger.getLogger(NodeDaemonLauncher.class.getName()); + static final Logger logger = Logger.getLogger(NodeDaemonLauncher.class.getName()); /** * Constructs a new node daemon launcher. @@ -61,40 +63,98 @@ public class NodeDaemonLauncher { } public static void main(String[] args) throws Exception { - logger.info("Apache Tuscany SCA Node Daemon starting..."); + logger.info("Apache Tuscany SCA Node Daemon is starting..."); - // Create a node daemon + // Create a node launcher NodeDaemonLauncher launcher = newInstance(); - OSGiHost host = NodeLauncherUtil.startOSGi(); + OSGiHost osgiHost = null; + Object node = null; + ShutdownThread shutdown = null; try { - Object daemon = launcher.createNodeDaemon(); - // Start the node daemon + // Start the OSGi host + osgiHost = startOSGi(); + + // Start the node + node = launcher.createNodeDaemon(); try { - daemon.getClass().getMethod("start").invoke(daemon); + node.getClass().getMethod("start").invoke(node); } catch (Exception e) { logger.log(Level.SEVERE, "SCA Node Daemon could not be started", e); throw e; } - logger.info("SCA Node Daemon started."); - + logger.info("SCA Node Daemon is now started."); + + // Install a shutdown hook + shutdown = new ShutdownThread(node, osgiHost); + Runtime.getRuntime().addShutdownHook(shutdown); + logger.info("Press enter to shutdown."); try { System.in.read(); } catch (IOException e) { + + // Wait forever + Object lock = new Object(); + synchronized(lock) { + lock.wait(); + } + } + } finally { + + // Remove the shutdown hook + if (shutdown != null) { + Runtime.getRuntime().removeShutdownHook(shutdown); + } + + // Stop the node + if (node != null) { + stopNode(node); } + if (osgiHost != null) { + stopOSGi(osgiHost); + } + } + } + + /** + * Stop the given node. + * + * @param node + * @throws Exception + */ + private static void stopNode(Object node) throws Exception { + try { + node.getClass().getMethod("stop").invoke(node); + logger.info("SCA Node Daemon is now stopped."); + } catch (Exception e) { + logger.log(Level.SEVERE, "SCA Node Daemon could not be stopped", e); + throw e; + } + } + + private static class ShutdownThread extends Thread { + private Object node; + private OSGiHost osgiHost; + + public ShutdownThread(Object node, OSGiHost osgiHost) { + super(); + this.node = node; + this.osgiHost = osgiHost; + } - // Stop the node daemon + public void run() { try { - daemon.getClass().getMethod("stop").invoke(daemon); + stopNode(node); } catch (Exception e) { - logger.log(Level.SEVERE, "SCA Node Daemon could not be stopped", e); - throw e; + // Ignore + } + try { + stopOSGi(osgiHost); + } catch (Exception e) { + // Ignore } - } finally { - NodeLauncherUtil.stopOSGi(host); } } - } diff --git a/java/sca/modules/node-launcher-equinox/src/main/java/org/apache/tuscany/sca/node/equinox/launcher/NodeLauncher.java b/java/sca/modules/node-launcher-equinox/src/main/java/org/apache/tuscany/sca/node/equinox/launcher/NodeLauncher.java index c363de0e71..870a5d5e10 100644 --- a/java/sca/modules/node-launcher-equinox/src/main/java/org/apache/tuscany/sca/node/equinox/launcher/NodeLauncher.java +++ b/java/sca/modules/node-launcher-equinox/src/main/java/org/apache/tuscany/sca/node/equinox/launcher/NodeLauncher.java @@ -20,6 +20,8 @@ package org.apache.tuscany.sca.node.equinox.launcher; import static org.apache.tuscany.sca.node.equinox.launcher.NodeLauncherUtil.node; +import static org.apache.tuscany.sca.node.equinox.launcher.NodeLauncherUtil.startOSGi; +import static org.apache.tuscany.sca.node.equinox.launcher.NodeLauncherUtil.stopOSGi; import java.io.IOException; import java.util.logging.Level; @@ -110,56 +112,113 @@ public class NodeLauncher { } public static void main(String[] args) throws Exception { - logger.info("Apache Tuscany SCA OSGi Node is starting..."); + logger.info("Apache Tuscany SCA Node is starting..."); - // Create a node + // Create a node launcher NodeLauncher launcher = newInstance(); - OSGiHost host = NodeLauncherUtil.startOSGi(); + OSGiHost osgiHost = null; + Object node = null; + ShutdownThread shutdown = null; try { - Object node; - if (args.length == 1) { + // Start the OSGi host + osgiHost = startOSGi(); - // Create from a configuration URI + if (args.length ==1) { + + // Create a node from a configuration URI String configurationURI = args[0]; - logger.info("SCA OSGi Node configuration: " + configurationURI); + logger.info("SCA Node configuration: " + configurationURI); node = launcher.createNodeFromURL(configurationURI); } else { - - // Create from a composite URI and a contribution location + + // Create a node from a composite URI and a contribution location String compositeURI = args[0]; String contributionLocation = args[1]; logger.info("SCA composite: " + compositeURI); logger.info("SCA contribution: " + contributionLocation); node = launcher.createNode(compositeURI, new Contribution("default", contributionLocation)); } - + // Start the node try { node.getClass().getMethod("start").invoke(node); } catch (Exception e) { - logger.log(Level.SEVERE, "SCA OSGi Node could not be started", e); + logger.log(Level.SEVERE, "SCA Node could not be started", e); throw e; } - logger.info("SCA OSGi Node is now started."); - - logger.info("Press Enter to shutdown..."); + logger.info("SCA Node is now started."); + + // Install a shutdown hook + shutdown = new ShutdownThread(node, osgiHost); + Runtime.getRuntime().addShutdownHook(shutdown); + + logger.info("Press enter to shutdown."); try { System.in.read(); } catch (IOException e) { + + // Wait forever + Object lock = new Object(); + synchronized(lock) { + lock.wait(); + } } + } finally { + // Remove the shutdown hook + if (shutdown != null) { + Runtime.getRuntime().removeShutdownHook(shutdown); + } + // Stop the node + if (node != null) { + stopNode(node); + } + if (osgiHost != null) { + stopOSGi(osgiHost); + } + } + } + + /** + * Stop the given node. + * + * @param node + * @throws Exception + */ + private static void stopNode(Object node) throws Exception { + try { + node.getClass().getMethod("stop").invoke(node); + logger.info("SCA Node is now stopped."); + } catch (Exception e) { + logger.log(Level.SEVERE, "SCA Node could not be stopped", e); + throw e; + } + } + + private static class ShutdownThread extends Thread { + private Object node; + private OSGiHost osgiHost; + + public ShutdownThread(Object node, OSGiHost osgiHost) { + super(); + this.node = node; + this.osgiHost = osgiHost; + } + + public void run() { try { - node.getClass().getMethod("stop").invoke(node); + stopNode(node); } catch (Exception e) { - logger.log(Level.SEVERE, "SCA OSGi Node could not be stopped", e); - throw e; + // Ignore + } + try { + stopOSGi(osgiHost); + } catch (Exception e) { + // Ignore } - } finally { - NodeLauncherUtil.stopOSGi(host); } } - } diff --git a/java/sca/modules/node-launcher-equinox/src/main/java/org/apache/tuscany/sca/node/equinox/launcher/NodeLauncherUtil.java b/java/sca/modules/node-launcher-equinox/src/main/java/org/apache/tuscany/sca/node/equinox/launcher/NodeLauncherUtil.java index 6b3981ed53..89d87b21d1 100644 --- a/java/sca/modules/node-launcher-equinox/src/main/java/org/apache/tuscany/sca/node/equinox/launcher/NodeLauncherUtil.java +++ b/java/sca/modules/node-launcher-equinox/src/main/java/org/apache/tuscany/sca/node/equinox/launcher/NodeLauncherUtil.java @@ -28,7 +28,6 @@ import java.util.logging.Level; * @version $Rev$ $Date$ */ final class NodeLauncherUtil { - // private static final Logger logger = Logger.getLogger(NodeLauncherUtil.class.getName()); private static final String DOMAIN_MANAGER_LAUNCHER_BOOTSTRAP = "org.apache.tuscany.sca.domain.manager.launcher.DomainManagerLauncherBootstrap"; @@ -138,7 +137,7 @@ final class NodeLauncherUtil { return nodeDaemon; } catch (Exception e) { - NodeLauncher.logger.log(Level.SEVERE, "SCA Node Daemon could not be created", e); + NodeDaemonLauncher.logger.log(Level.SEVERE, "SCA Node Daemon could not be created", e); throw new LauncherException(e); } finally { Thread.currentThread().setContextClassLoader(tccl); @@ -166,7 +165,7 @@ final class NodeLauncherUtil { return domainManager; } catch (Exception e) { - NodeLauncher.logger.log(Level.SEVERE, "SCA Domain Manager could not be created", e); + DomainManagerLauncher.logger.log(Level.SEVERE, "SCA Domain Manager could not be created", e); throw new LauncherException(e); } finally { Thread.currentThread().setContextClassLoader(tccl); |