/*
 * ################################################################
 *
 * ProActive Parallel Suite(TM): The Java(TM) library for
 *    Parallel, Distributed, Multi-Core Computing for
 *    Enterprise Grids & Clouds
 *
 * Copyright (C) 1997-2011 INRIA/University of
 *                 Nice-Sophia Antipolis/ActiveEon
 * Contact: proactive@ow2.org or contact@activeeon.com
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Affero General Public License
 * as published by the Free Software Foundation; version 3 of
 * the License.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 *
 * If needed, contact us to obtain a release under GPL Version 2 or 3
 * or a different license than the AGPL.
 *
 *  Initial developer(s):               The ProActive Team
 *                        http://proactive.inria.fr/team_members.htm
 *  Contributor(s):
 *
 * ################################################################
 * $$PROACTIVE_INITIAL_DEV$$
 */
package org.objectweb.proactive.extensions.gcmdeployment.GCMDeployment;

import static org.objectweb.proactive.extensions.gcmdeployment.GCMDeploymentLoggers.GCMD_LOGGER;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.objectweb.proactive.core.config.CentralPAPropertyRepository;
import org.objectweb.proactive.core.util.log.Loggers;
import org.objectweb.proactive.core.util.log.ProActiveLogger;
import org.objectweb.proactive.utils.OperatingSystem;


public class Executor {
    final static private Executor singleton = new Executor();
    private List<Thread> threads;
    private long jobId;

    private Executor() {
        GCMD_LOGGER.trace("Executor started");
        threads = new ArrayList<Thread>();
        jobId = 0;
    }

    static public synchronized Executor getExecutor() {
        return singleton;
    }

    public void submit(String command) {
        Logger logger = ProActiveLogger.getLogger(Loggers.DEPLOYMENT + ".job." + jobId);
        jobId++;

        logger.debug("Command submited: " + command);
        try {
            logger.info("executing command=" + command);

            Process p = null;
            switch (OperatingSystem.getOperatingSystem()) {
                case unix:
                    p = Runtime.getRuntime().exec(
                            new String[] { CentralPAPropertyRepository.PA_GCMD_UNIX_SHELL.getValue(), "-c",
                                    command });
                    break;
                case windows:
                    p = Runtime.getRuntime().exec(command);
                    break;
            }

            InputStreamMonitor stdoutM = new InputStreamMonitor(MonitorType.STDOUT, p.getInputStream(),
                command, logger);
            InputStreamMonitor stderrM = new InputStreamMonitor(MonitorType.STDERR, p.getErrorStream(),
                command, logger);
            stderrM.start();
            stdoutM.start();
            threads.add(stdoutM);
            threads.add(stderrM);
        } catch (IOException e) {
            logger.warn("Cannot execute: " + command, e);
        }
    }

    private enum MonitorType {
        STDOUT, STDERR;
    }

    static private class InputStreamMonitor extends Thread {
        MonitorType type;
        InputStream stream;
        String cmd;
        Logger logger;

        public InputStreamMonitor(MonitorType type, InputStream stream, String cmd, Logger logger) {
            this.logger = logger;
            logger.trace("Monitor started: " + type.name() + " " + cmd);
            this.type = type;
            this.stream = stream;
            this.cmd = cmd;
            setDaemon(true);
            setName("GCM Deployment" + type.toString() + " Monitor for " + cmd.subSequence(0, 100));
        }

        @Override
        public void run() {
            try {
                BufferedReader br;
                String line;

                br = new BufferedReader(new InputStreamReader(stream));
                while ((line = br.readLine()) != null) {
                    logger.info(line);
                }
                logger.trace("Monitor exited: " + type.name() + " " + cmd);
            } catch (IOException e) {
                // TODO: handle exception
                e.printStackTrace();
            }
        }
    }
}

@@ -2717,18 +2717,26 @@
  Process
+Builder
  p
+b
  = null;
@@ -2848,81 +2848,31 @@
    p
+b
  = 
-Runtime.getRuntime().exec(%0A                            new String%5B%5D %7B 
+new ProcessBuilder(
 Cent
@@ -2960,29 +2960,15 @@
     
-      
-      
 command
- %7D
 );%0A 
@@ -3048,36 +3048,30 @@
    p
+b
  = 
-Runtime.getRuntime().exec
+new ProcessBuilder
 (com
@@ -3135,131 +3135,71 @@
     
-InputStreamMonitor stdoutM = new InputStreamMonitor(MonitorType.STDOUT, p.getInputStream(),%0A                command, logger
+pb.redirectErrorStream(true);%0A            Process p = pb.start(
 );%0A 
@@ -3230,21 +3230,27 @@
 nitor st
-derrM
+reamMonitor
  = new I
@@ -3271,205 +3271,118 @@
 tor(
-MonitorType.STDERR, p.getErrorStream(),%0A                command, logger);%0A            stderrM.start();%0A            stdoutM.start();%0A            threads.add(stdoutM);%0A            threads.add(stderrM
+p.getInputStream(), command, logger);%0A            streamMonitor.start();%0A            threads.add(streamMonitor
 );%0A 
@@ -3493,70 +3493,8 @@
  %7D%0A%0A
-    private enum MonitorType %7B%0A        STDOUT, STDERR;%0A    %7D%0A%0A
     
@@ -3554,34 +3554,8 @@
 d %7B%0A
-        MonitorType type;%0A
     
@@ -3660,26 +3660,8 @@
 tor(
-MonitorType type, 
 Inpu
@@ -3789,65 +3789,15 @@
  %22 +
- type.name() + %22 %22 +
  cmd);%0A
-            this.type = type;%0A
     
@@ -3922,31 +3922,8 @@
 ment
-%22 + type.toString() + %22
  Mon
@@ -4340,28 +4340,8 @@
  %22 +
- type.name() + %22 %22 +
  cmd

Merge stdout and stderr to reduce thread usage