From 9bc87487f853cc8ae512ed840556325f3a08115e Mon Sep 17 00:00:00 2001 From: rfeng Date: Wed, 4 Feb 2009 18:31:32 +0000 Subject: Add a new option "ttl" to control when to stop the node git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@740819 13f79535-47bb-0310-9956-ffa450edef68 --- .../sca/node/equinox/launcher/NodeLauncher.java | 169 ++++++++++++++------- .../tuscany/sca/node/launcher/NodeLauncher.java | 31 +++- 2 files changed, 136 insertions(+), 64 deletions(-) (limited to 'java/sca') 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 6ce2efda72..04d77bcdd5 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 @@ -43,6 +43,7 @@ import org.osgi.framework.BundleContext; * Agruments: * [-config ]: The configuration folder for Equinox * [-c ]: The composite URI + * [-t ]: Time to live in milliseconds before the node is started * contribution1 ... contributionN: A list of contribution files or URLs * * @version $Rev$ $Date$ @@ -125,74 +126,103 @@ public class NodeLauncher { if (cli.hasOption("config")) { System.setProperty("osgi.configuration.area", cli.getOptionValue("config")); } - if (cli.hasOption("node")) { - // Create a node from a configuration URI - String configurationURI = cli.getOptionValue("node"); - logger.info("SCA Node configuration: " + configurationURI); - - // Create a node launcher - NodeLauncher launcher = newInstance(); - equinox = launcher.equinoxHost; - - node = launcher.createNode(configurationURI); - } else { - // Create a node from a composite URI and a contribution location - String compositeURI = cli.getOptionValue("composite"); - List contribs = cli.getArgList(); - Contribution[] contributions = null; - if (!contribs.isEmpty()) { - contributions = new Contribution[contribs.size()]; - int index = 0; - for (String contrib : contribs) { - logger.info("SCA contribution: " + contrib); - URL url = null; - try { - url = new URL(contrib); - } catch(MalformedURLException e) { - url = new File(contrib).toURI().toURL(); + while (true) { + if (cli.hasOption("node")) { + // Create a node from a configuration URI + String configurationURI = cli.getOptionValue("node"); + logger.info("SCA Node configuration: " + configurationURI); + + // Create a node launcher + NodeLauncher launcher = newInstance(); + equinox = launcher.equinoxHost; + + node = launcher.createNode(configurationURI); + } else { + // Create a node from a composite URI and a contribution location + String compositeURI = cli.getOptionValue("composite"); + List contribs = cli.getArgList(); + Contribution[] contributions = null; + if (!contribs.isEmpty()) { + contributions = new Contribution[contribs.size()]; + int index = 0; + for (String contrib : contribs) { + logger.info("SCA contribution: " + contrib); + URL url = null; + try { + url = new URL(contrib); + } catch (MalformedURLException e) { + url = new File(contrib).toURI().toURL(); + } + contributions[index] = new Contribution("contribution-" + index, url.toString()); + index++; } - contributions[index] = new Contribution("contribution-" + index, url.toString()); - index++; + } else { + HelpFormatter formatter = new HelpFormatter(); + formatter.setSyntaxPrefix("Usage: "); + formatter.printHelp("java " + NodeLauncher.class.getName() + + " [-config ]" + + " [-c ]" + + " [-t ]" + + " contribution1 ... contributionN", options); + return; } - } else { - HelpFormatter formatter = new HelpFormatter(); - formatter.setSyntaxPrefix("Usage: "); - formatter.printHelp("java " + NodeLauncher.class.getName() - + " [-config ] [-c ] contribution1 ... contributionN", options); - return; + // Create a node launcher + logger.info("SCA composite: " + compositeURI); + NodeLauncher launcher = newInstance(); + equinox = launcher.equinoxHost; + node = launcher.createNode(compositeURI, contributions); } - // Create a node launcher - logger.info("SCA composite: " + compositeURI); - NodeLauncher launcher = newInstance(); - equinox = launcher.equinoxHost; - node = launcher.createNode(compositeURI, contributions); - } - logger.info("Apache Tuscany SCA Node is starting..."); + logger.info("Apache Tuscany SCA Node is starting..."); - // Start the node - try { - node.getClass().getMethod("start").invoke(node); - } catch (Exception e) { - logger.log(Level.SEVERE, "SCA Node could not be started", e); - throw e; - } - logger.info("SCA Node is now started."); + // Start the node + try { + node.getClass().getMethod("start").invoke(node); + } catch (Exception e) { + logger.log(Level.SEVERE, "SCA Node could not be started", e); + throw e; + } + logger.info("SCA Node is now started."); - // Install a shutdown hook - shutdown = new ShutdownThread(node, equinox); - Runtime.getRuntime().addShutdownHook(shutdown); + // Install a shutdown hook + shutdown = new ShutdownThread(node, equinox); + Runtime.getRuntime().addShutdownHook(shutdown); - logger.info("Press enter to shutdown."); - try { - System.in.read(); - } catch (IOException e) { + long ttl = Long.parseLong(cli.getOptionValue("ttl", "-1")); + if (ttl >= 0) { + logger.info("Waiting for " + ttl + " milliseconds ..."); + Thread.sleep(ttl); + break; // Exit + } + + // Wait until the "Enter" is pressed + logger.info("Press 'q' to quit, 'r' to restart."); + int k = 0; + try { + while ((k != 'q') && (k != 'r')) { + k = System.in.read(); + } + } catch (IOException e) { + + // Wait forever + Object lock = new Object(); + synchronized (lock) { + lock.wait(); + } + } + + // Stop the node + if (node != null) { + Object n = node; + node = null; + stopNode(n); + } - // Wait forever - Object lock = new Object(); - synchronized (lock) { - lock.wait(); + // Quit + if (k == 'q') { + break; } + } } finally { @@ -222,6 +252,11 @@ public class NodeLauncher { Option opt3 = new Option("config", "configuration", true, "Configuration"); opt3.setArgName("equinoxConfiguration"); options.addOption(opt3); + Option opt4 = new Option("t", "ttl", true, "Time to live"); + opt4.setArgName("timeToLiveInMilliseconds"); + // opt4.setType(long.class); + options.addOption(opt4); + return options; } @@ -273,4 +308,20 @@ public class NodeLauncher { } } } + + /** + * 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; + } + } } diff --git a/java/sca/modules/node-launcher/src/main/java/org/apache/tuscany/sca/node/launcher/NodeLauncher.java b/java/sca/modules/node-launcher/src/main/java/org/apache/tuscany/sca/node/launcher/NodeLauncher.java index 1e42347d32..e2c8550f09 100644 --- a/java/sca/modules/node-launcher/src/main/java/org/apache/tuscany/sca/node/launcher/NodeLauncher.java +++ b/java/sca/modules/node-launcher/src/main/java/org/apache/tuscany/sca/node/launcher/NodeLauncher.java @@ -37,9 +37,12 @@ import org.apache.commons.cli.Options; import org.apache.commons.cli.PosixParser; /** - * A launcher for SCA nodes. - * - * @version $Rev$ $Date$ + * A launcher for SCA nodes in JSE. + * + * Agruments: + * [-c ]: The composite URI + * [-t ]: Time to live in milliseconds before the node is started + * contribution1 ... contributionN: A list of contribution files or URLs * @version $Rev$ $Date$ */ public class NodeLauncher { @@ -127,6 +130,10 @@ public class NodeLauncher { Option opt2 = new Option("n", "node", true, "URI for the node configuration"); opt2.setArgName("nodeConfigurationURI"); options.addOption(opt2); + Option opt3 = new Option("t", "ttl", true, "Time to live"); + opt3.setArgName("timeToLiveInMilliseconds"); + // opt4.setType(long.class); + options.addOption(opt3); return options; } @@ -171,8 +178,9 @@ public class NodeLauncher { HelpFormatter formatter = new HelpFormatter(); formatter.setSyntaxPrefix("Usage: "); formatter.printHelp("java " + NodeLauncher.class.getName() - + " [-c ] contribution1 ... contributionN", options); - return; + + " [-c ]" + + " [-t ]" + + " contribution1 ... contributionN", options); return; } // Create a node launcher logger.info("SCA composite: " + compositeURI); @@ -195,6 +203,19 @@ public class NodeLauncher { // Install a shutdown hook shutdown = new ShutdownThread(node); Runtime.getRuntime().addShutdownHook(shutdown); + + long ttl = Long.parseLong(cli.getOptionValue("ttl", "-1")); + if (ttl >= 0) { + logger.info("Waiting for " + ttl + " milliseconds ..."); + Thread.sleep(ttl); + // Stop the node + if (node != null) { + Object n = node; + node = null; + stopNode(n); + } + break; // Exit + } logger.info("Press 'q' to quit, 'r' to restart."); int k = 0; -- cgit v1.2.3