diff options
author | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2009-02-04 01:31:18 +0000 |
---|---|---|
committer | rfeng <rfeng@13f79535-47bb-0310-9956-ffa450edef68> | 2009-02-04 01:31:18 +0000 |
commit | 54bec0b997ffd6d92efdd209071ce230b2e24099 (patch) | |
tree | 657bbfd2ff8ddb8994031c07f0995a7069655048 /java/sca/modules/node-launcher-equinox | |
parent | b6a7235c65cf4aed9dbb8037118e7839c689a070 (diff) |
Add command-line parsing to the node launcher
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@740567 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to '')
4 files changed, 108 insertions, 30 deletions
diff --git a/java/sca/modules/node-launcher-equinox/META-INF/MANIFEST.MF b/java/sca/modules/node-launcher-equinox/META-INF/MANIFEST.MF index bd930190dd..18e619c559 100644 --- a/java/sca/modules/node-launcher-equinox/META-INF/MANIFEST.MF +++ b/java/sca/modules/node-launcher-equinox/META-INF/MANIFEST.MF @@ -11,7 +11,8 @@ Bnd-LastModified: 1225397473703 Bundle-ManifestVersion: 2
Bundle-License: http://www.apache.org/licenses/LICENSE-2.0.txt
Bundle-Description: Apache Tuscany SCA Node Equinox OSGi Launcher
-Import-Package: org.apache.tuscany.sca.node.equinox.launcher;version="2.0.0",
+Import-Package: org.apache.commons.cli;resolution:=optional,
+ org.apache.tuscany.sca.node.equinox.launcher;version="2.0.0",
org.eclipse.core.runtime.adaptor,
org.osgi.framework;version="1.4"
Bundle-SymbolicName: org.apache.tuscany.sca.node.launcher.equinox
diff --git a/java/sca/modules/node-launcher-equinox/pom.xml b/java/sca/modules/node-launcher-equinox/pom.xml index 6acabf1136..608ba3ee51 100644 --- a/java/sca/modules/node-launcher-equinox/pom.xml +++ b/java/sca/modules/node-launcher-equinox/pom.xml @@ -62,7 +62,13 @@ <artifactId>app</artifactId> <version>1.0.0-v20070606</version> <scope>runtime</scope> - </dependency> + </dependency> + + <dependency> + <groupId>commons-cli</groupId> + <artifactId>commons-cli</artifactId> + <version>1.1</version> + </dependency> <dependency> <groupId>org.apache.tuscany.sca</groupId> 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 462246f37c..510fe62c71 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 @@ -21,10 +21,20 @@ package org.apache.tuscany.sca.node.equinox.launcher; import static org.apache.tuscany.sca.node.equinox.launcher.NodeLauncherUtil.node; +import java.io.File; import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.CommandLineParser; +import org.apache.commons.cli.HelpFormatter; +import org.apache.commons.cli.Option; +import org.apache.commons.cli.Options; +import org.apache.commons.cli.PosixParser; import org.osgi.framework.BundleContext; /** @@ -98,32 +108,72 @@ public class NodeLauncher { } public static void main(String[] args) throws Exception { - logger.info("Apache Tuscany SCA Node is starting..."); + CommandLineParser parser = new PosixParser(); + Options options = new Options(); + Option opt1 = new Option("c", "composite", true, "URI for the composite"); + opt1.setArgName("compositeURI"); + options.addOption(opt1); + Option opt2 = new Option("n", "node", true, "URI for the node configuration"); + opt2.setArgName("nodeConfigurationURI"); + options.addOption(opt2); + Option opt3 = new Option("config", "configuration", true, "Configuration"); + opt3.setArgName("equinoxConfiguration"); + options.addOption(opt3); + CommandLine cli = parser.parse(options, args); - // Create a node launcher - NodeLauncher launcher = newInstance(); - - EquinoxHost equinox = launcher.equinoxHost; Object node = null; ShutdownThread shutdown = null; + EquinoxHost equinox = null; try { - if (args.length ==1) { - + 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 = args[0]; + 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 = args[0]; - String contributionLocation = args[1]; + String compositeURI = cli.getOptionValue("composite"); + List<String> 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++; + } + } else { + HelpFormatter formatter = new HelpFormatter(); + formatter.setSyntaxPrefix("Usage: "); + formatter.printHelp("java " + NodeLauncher.class.getName() + + " -c <compositeURI> contributionURL1 ... contributionURLN", options); + return; + } + // Create a node launcher logger.info("SCA composite: " + compositeURI); - logger.info("SCA contribution: " + contributionLocation); - node = launcher.createNode(compositeURI, new Contribution("default", contributionLocation)); + NodeLauncher launcher = newInstance(); + equinox = launcher.equinoxHost; + node = launcher.createNode(compositeURI, contributions); } - + + logger.info("Apache Tuscany SCA Node is starting..."); + // Start the node try { node.getClass().getMethod("start").invoke(node); @@ -132,19 +182,19 @@ public class NodeLauncher { throw e; } logger.info("SCA Node is now started."); - + // 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) { - + // Wait forever Object lock = new Object(); - synchronized(lock) { + synchronized (lock) { lock.wait(); } } @@ -154,7 +204,7 @@ public class NodeLauncher { if (shutdown != null) { Runtime.getRuntime().removeShutdownHook(shutdown); } - + // Stop the node if (node != null) { destroyNode(node); @@ -164,7 +214,7 @@ public class NodeLauncher { } } } - + public void destroy() { if (equinoxHost != null) { equinoxHost.stop(); @@ -188,7 +238,7 @@ public class NodeLauncher { throw e; } } - + private static class ShutdownThread extends Thread { private Object node; private EquinoxHost equinox; diff --git a/java/sca/modules/node-launcher-equinox/src/main/java/org/apache/tuscany/sca/node/equinox/launcher/NodeMain.java b/java/sca/modules/node-launcher-equinox/src/main/java/org/apache/tuscany/sca/node/equinox/launcher/NodeMain.java index 17172dbd5e..44f92dee7d 100644 --- a/java/sca/modules/node-launcher-equinox/src/main/java/org/apache/tuscany/sca/node/equinox/launcher/NodeMain.java +++ b/java/sca/modules/node-launcher-equinox/src/main/java/org/apache/tuscany/sca/node/equinox/launcher/NodeMain.java @@ -19,6 +19,12 @@ package org.apache.tuscany.sca.node.equinox.launcher; +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.CommandLineParser; +import org.apache.commons.cli.Option; +import org.apache.commons.cli.Options; +import org.apache.commons.cli.PosixParser; + /** * Main class for this JAR. @@ -31,14 +37,29 @@ package org.apache.tuscany.sca.node.equinox.launcher; public class NodeMain { public static void main(String[] args) throws Exception { - if (args.length != 0) { - if (args[0].equals("domain")) { - DomainManagerLauncher.main(args); - } else { - NodeLauncher.main(args); - } - } else { + CommandLineParser parser = new PosixParser(); + Options options = new Options(); + options.addOption("dm", "domainManager", false, "Domain Manager"); + options.addOption("nd", "nodeDaemon", false, "Node Domain"); + + // Add options from NodeLauncher to avoid UnrecognizedOptionException + Option opt1 = new Option("c", "composite", true, "URI for the composite"); + opt1.setArgName("compositeURI"); + options.addOption(opt1); + Option opt2 = new Option("n", "node", true, "URI for the node configuration"); + opt2.setArgName("nodeConfigurationURI"); + options.addOption(opt2); + Option opt3 = new Option("config", "configuration", true, "Configuration"); + opt3.setArgName("equinoxConfiguration"); + options.addOption(opt3); + + CommandLine cli = parser.parse(options, args, false); + if (cli.hasOption("nd")) { NodeDaemonLauncher.main(args); + } else if (cli.hasOption("dm")) { + DomainManagerLauncher.main(args); + } else { + NodeLauncher.main(args); } } } |