public static void main()

in modules/node-launcher/src/main/java/org/apache/tuscany/sca/node/launcher/NodeLauncher.java [145:292]


    public static void main(String[] args) throws Exception {
        CommandLineParser parser = new PosixParser();
        Options options = getCommandLineOptions();
        CommandLine cli = parser.parse(options, args);
        
        Object node = null;
        ShutdownThread shutdown = null;
        try {
            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();
                    node = launcher.createNodeFromURL(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++;
                        }
                    } else {
                        HelpFormatter formatter = new HelpFormatter();
                        formatter.setSyntaxPrefix("Usage: ");
                        formatter.printHelp("java " + NodeLauncher.class.getName()
                                            + " [-c <compositeURI>]"
                                            + " [-s <service>]"
                                            + " [-t <ttl>]"
                                            + " contribution1 ... contributionN", options);                        return;
                    }
                    // Create a node launcher
                    logger.info("SCA composite: " + compositeURI);
                    NodeLauncher launcher = newInstance();
                    
                    node = launcher.createNode(compositeURI, contributions);
                }
                
                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.");
                
                String service = cli.getOptionValue("service");
                String regex = "(#|\\(|,|\\))";
                if (service != null) {
                    // componentName/serviceName/bindingName#methodName(arg0, ..., agrN)
                    String tokens[] = service.split(regex);
                    String serviceName = tokens[0];
                    String operationName = tokens[1];
                    String params[] = new String[tokens.length - 2];
                    System.arraycopy(tokens, 2, params, 0, params.length);
                    logger.info("Invoking service: " + service);
                    Method method = node.getClass().getMethod("getService", Class.class, String.class);
                    Object proxy = method.invoke(node, null, serviceName);

                    Object result = invoke(proxy, operationName, params);
                    if (result != null) {
                        logger.info("Result is: " + result);
                    }
                    break;
                }
                
                // 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;
                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);
                }
                
                // Quit
                if (k == 'q' ) {
                    break;
                }
            }
        } catch (Exception e) {
            // Stop the node
            if (node != null) {
                try {
                    Object n = node;
                    node = null;
                    stopNode(n);
                } catch (Exception e2) {
                }
            }
            throw e;
            
        } finally {

            // Remove the shutdown hook
            if (shutdown != null) {
                Runtime.getRuntime().removeShutdownHook(shutdown);
            }
        }
    }