tuscany-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rf...@apache.org
Subject svn commit: r740819 - in /tuscany/java/sca/modules: node-launcher-equinox/src/main/java/org/apache/tuscany/sca/node/equinox/launcher/NodeLauncher.java node-launcher/src/main/java/org/apache/tuscany/sca/node/launcher/NodeLauncher.java
Date Wed, 04 Feb 2009 18:31:33 GMT
Author: rfeng
Date: Wed Feb  4 18:31:32 2009
New Revision: 740819

URL: http://svn.apache.org/viewvc?rev=740819&view=rev
Log:
Add a new option "ttl" to control when to stop the node

Modified:
    tuscany/java/sca/modules/node-launcher-equinox/src/main/java/org/apache/tuscany/sca/node/equinox/launcher/NodeLauncher.java
    tuscany/java/sca/modules/node-launcher/src/main/java/org/apache/tuscany/sca/node/launcher/NodeLauncher.java

Modified: tuscany/java/sca/modules/node-launcher-equinox/src/main/java/org/apache/tuscany/sca/node/equinox/launcher/NodeLauncher.java
URL: http://svn.apache.org/viewvc/tuscany/java/sca/modules/node-launcher-equinox/src/main/java/org/apache/tuscany/sca/node/equinox/launcher/NodeLauncher.java?rev=740819&r1=740818&r2=740819&view=diff
==============================================================================
--- tuscany/java/sca/modules/node-launcher-equinox/src/main/java/org/apache/tuscany/sca/node/equinox/launcher/NodeLauncher.java
(original)
+++ tuscany/java/sca/modules/node-launcher-equinox/src/main/java/org/apache/tuscany/sca/node/equinox/launcher/NodeLauncher.java
Wed Feb  4 18:31:32 2009
@@ -43,6 +43,7 @@
  * Agruments:
  * [-config <equinoxConfiguration>]: The configuration folder for Equinox 
  * [-c <compositeURI>]: The composite URI
+ * [-t <ttl>]: 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 @@
             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<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();
+            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<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++;
                         }
-                        contributions[index] = new Contribution("contribution-" + index,
url.toString());
-                        index++;
+                    } else {
+                        HelpFormatter formatter = new HelpFormatter();
+                        formatter.setSyntaxPrefix("Usage: ");
+                        formatter.printHelp("java " + NodeLauncher.class.getName()
+                            + " [-config <equinoxConfiguration>]"
+                            + " [-c <compositeURI>]"
+                            + " [-t <ttl>]"
+                            + " contribution1 ... contributionN", options);
+                        return;
                     }
-                } else {
-                    HelpFormatter formatter = new HelpFormatter();
-                    formatter.setSyntaxPrefix("Usage: ");
-                    formatter.printHelp("java " + NodeLauncher.class.getName()
-                        + " [-config <equinoxConfiguration>] [-c <compositeURI>]
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);
+
+                long ttl = Long.parseLong(cli.getOptionValue("ttl", "-1"));
+                if (ttl >= 0) {
+                    logger.info("Waiting for " + ttl + " milliseconds ...");
+                    Thread.sleep(ttl);
+                    break; // Exit
+                }
 
-            logger.info("Press enter to shutdown.");
-            try {
-                System.in.read();
-            } catch (IOException e) {
+                // 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 @@
         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 @@
             }
         }
     }
+
+    /**
+     * 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;
+        }
+    }
 }

Modified: tuscany/java/sca/modules/node-launcher/src/main/java/org/apache/tuscany/sca/node/launcher/NodeLauncher.java
URL: http://svn.apache.org/viewvc/tuscany/java/sca/modules/node-launcher/src/main/java/org/apache/tuscany/sca/node/launcher/NodeLauncher.java?rev=740819&r1=740818&r2=740819&view=diff
==============================================================================
--- tuscany/java/sca/modules/node-launcher/src/main/java/org/apache/tuscany/sca/node/launcher/NodeLauncher.java
(original)
+++ tuscany/java/sca/modules/node-launcher/src/main/java/org/apache/tuscany/sca/node/launcher/NodeLauncher.java
Wed Feb  4 18:31:32 2009
@@ -37,9 +37,12 @@
 import org.apache.commons.cli.PosixParser;
 
 /**
- * A launcher for SCA nodes.
- *  
- * @version $Rev$ $Date$
+ * A launcher for SCA nodes in JSE.
+ * 
+ * Agruments:
+ * [-c <compositeURI>]: The composite URI
+ * [-t <ttl>]: 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 @@
         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 @@
                         HelpFormatter formatter = new HelpFormatter();
                         formatter.setSyntaxPrefix("Usage: ");
                         formatter.printHelp("java " + NodeLauncher.class.getName()
-                            + " [-c <compositeURI>] contribution1 ... contributionN",
options);
-                        return;
+                                            + " [-c <compositeURI>]"
+                                            + " [-t <ttl>]"
+                                            + " contribution1 ... contributionN", options);
                       return;
                     }
                     // Create a node launcher
                     logger.info("SCA composite: " + compositeURI);
@@ -195,6 +203,19 @@
                 // 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;



Mime
View raw message