public void runAsProcess()

in uima-ducc-sm/src/main/java/org/apache/uima/ducc/sm/PingDriver.java [612:764]


    public void runAsProcess()
    {
        long tid = Thread.currentThread().getId();
        String methodName = "runAsProcess[" + tid + "]";

        String cp = classpath;
        String dh = System.getProperty("DUCC_HOME");

        // We need the sm & cli & common jars ... whatever version is installed.
        // Put them at the head of the classpath as the user may have compiled against an old version.

        File libdir = new File(dh + "/lib/uima-ducc");
        String[] jars = libdir.list();
        if ( jars != null ) {
            for ( String j : jars ) {
                if ( j.startsWith("uima-ducc-sm") ||  j.startsWith("uima-ducc-cli") ||  j.startsWith("uima-ducc-common")) {
                    cp = dh + "/lib/uima-ducc/" + j + ":" + cp;
                    continue;
                }
            }
        }

        try {
            pinger =  new PingThread();
        } catch ( Throwable t ) {
            logger.error(methodName, sset.getId(), "Cannot start listen socket, pinger not started.", t);
            pingState = ServiceState.Stopped;
            return;
        }
        int port = pinger.getPort();

        ping_thread = new Thread(pinger);
        ping_thread.setName("XTrnPingMonitor-"+ sset.getId());
        ping_thread.start();                            // sets up the listener, before we start the the external process

        Map<String, Object> initProps = new HashMap<String, Object>();
        String serprops = setCommonInitProperties(initProps);
        ArrayList<String> arglist = new ArrayList<String>();
        if ( ! test_mode ) {
            arglist.add(System.getProperty("ducc.agent.launcher.ducc_spawn_path"));
            arglist.add("-u");
            arglist.add(user);
            arglist.add("-w");
            arglist.add(working_directory);
            if ( do_log ) {
                arglist.add("-f");
                arglist.add(log_directory + "/services/ping/" + sset.getId());
            }
            arglist.add("--");
        }

        // Jira 4805 - run the pinger with the same environment as the service
        // Tokenize & unquote the assignments, and convert to a map of environment settings after any substitutions
        // Suntax errors usually caught when registered, but --modify does not check
        ArrayList<String> envVarList = QuotedOptions.tokenizeList(environment, true);
        Map<String, String> envMap;
        try {
        	envMap = QuotedOptions.parseAssignments(envVarList, +1);
        } catch (IllegalArgumentException e) {
            logger.error(methodName, sset.getId(), "Invalid environment:", e);
            pingState = ServiceState.Stopped;
            return;
        }

        // Jira 5002 - check for user-specified JVM
        String javaHome = envMap.get("JAVA_HOME");
        if (javaHome != null) {
        	arglist.add(javaHome + "/bin/java");
        } else {
        	arglist.add(System.getProperty("ducc.jvm"));
        }

        arglist.add("-DSM_MONITOR=T");
        if ( jvm_args != null ) {
            for ( String s : jvm_args) {
                arglist.add(s);
            }
        }
        arglist.add("-cp");
        arglist.add(cp);
        //arglist.add("-Xmx100M");
        arglist.add("-Dcom.sun.management.jmxremote");
        arglist.add("org.apache.uima.ducc.sm.ServicePingMain");
        arglist.add("--class");
        arglist.add(ping_class);
        arglist.add("--endpoint");
        arglist.add(endpoint);
        arglist.add("--port");
        arglist.add(Integer.toString(port));
        arglist.add("--initprops");
        arglist.add(serprops);

        if( ping_arguments != null ) {
            arglist.add("--arguments");
            arglist.add(ping_arguments);
        }

        int i = 0;
        for ( String s : arglist) {
            logger.debug(methodName, sset.getId(), "Args[", i++,"]:  ", s);
        }
        ProcessBuilder pb = new ProcessBuilder(arglist);

        //
        // Establish our pinger
        //
        InputStream stdout = null;
        InputStream stderr = null;
        try {
            Map<String, String> env = pb.environment();
            env.clear();
            env.putAll(envMap);

            ping_main = pb.start();
            stdout = ping_main.getInputStream();
            stderr = ping_main.getErrorStream();

            sin_listener = new StdioListener(1, stdout);
            ser_listener = new StdioListener(2, stderr);
            Thread sol = new Thread(sin_listener);
            Thread sel = new Thread(ser_listener);
            sol.start();
            sel.start();
        } catch (Throwable t) {
            logger.error(methodName, sset.getId(), "Cannot establish ping process:", t);
            pingState = ServiceState.Stopped;
            return;
        }

        int rc;
        while ( true ) {
            try {
                rc = ping_main.waitFor();
                ping_main = null;

                if ( pingStopper != null ) {
                    pingStopper.cancel();
                    pingStopper = null;
                    logger.info(methodName, sset.getId(), "Pinger returned, pingStopper is canceled.");
                }

                logger.info(methodName, sset.getId(), "Pinger returns rc ", rc);
                sset.pingExited(rc, this);
                break;
            } catch (InterruptedException e2) {
                // nothing
            }
        }

		// pinger.stop();
        sin_listener.stop();
        ser_listener.stop();
    }