/**
 * Copyright (c) 2011, 2014 Eurotech and/or its affiliates
 *
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License v1.0
 *  which accompanies this distribution, and is available at
 *  http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   Eurotech
 */
package org.eclipse.kura.core.util;

import java.io.IOException;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ProcessUtil 
{
	private static final Logger s_logger = LoggerFactory.getLogger(ProcessUtil.class);

	private static final ExecutorService s_processExecutor = Executors.newSingleThreadExecutor();

	private static final String BASH      = "/bin/bash";
    private static final String BASH_FLAG = "-c";
    
	public static SafeProcess exec(String command)
		throws IOException
	{
		return exec( new String[] {command});
	}

	public static SafeProcess exec(String[] cmdarray)
		throws IOException
	{
		final String[] newCmdArray = new String[cmdarray.length + 2];
		newCmdArray[0] = BASH;
		newCmdArray[1] = BASH_FLAG;
		System.arraycopy(cmdarray, 0, newCmdArray, 2, cmdarray.length);
		
		// Serialize process executions. One at a time so we can consume all streams.
        Future<SafeProcess> futureSafeProcess = s_processExecutor.submit( new Callable<SafeProcess>() {
            @Override
            public SafeProcess call() throws Exception {
                Thread.currentThread().setName("SafeProcessExecutor");
                SafeProcess safeProcess = new SafeProcess();
                safeProcess.exec(newCmdArray);
                return safeProcess;
            }           
        });
        
        try {
            return futureSafeProcess.get();
        } 
        catch (Exception e) {
            s_logger.error("Error waiting from SafeProcess ooutput", e);
            throw new IOException(e);
        }
	}

	/**
	 * @deprecated  The method does nothing
	 */
	@Deprecated
	public static void close(SafeProcess proc)
	{
	}
	
	public static void destroy(SafeProcess proc)
	{
		proc.destroy();	
	}
}



    
@@ -412,16 +412,50 @@
 eption;%0A
+import java.util.StringTokenizer;%0A
 import j
@@ -875,317 +875,244 @@
 ();%0A
-%0A%09private static final String BASH      = %22/bin/bash%22;%0A    private static final String BASH_FLAG = %22-c%22;%0A    %0A%09public static SafeProcess exec(String command)%0A%09%09throws IOException%0A%09%7B%0A%09%09return exec( new String%5B%5D %7Bcommand%7D);%0A%09%7D%0A%0A%09public static SafeProcess exec(String%5B%5D cmdarray)%0A%09%09throws IOException%0A%09%7B%0A%09%09final 
+    %0A%09public static SafeProcess exec(String command)%0A%09%09throws IOException%0A%09%7B%0A%09%09// Use StringTokenizer since this is the method documented by Runtime%0A%09%09StringTokenizer st = new StringTokenizer(command);%0A%09%09int count = st.countTokens();%0A%09%09
 Stri
@@ -1116,20 +1116,17 @@
 tring%5B%5D 
-newC
+c
 mdArray 
@@ -1143,43 +1143,68 @@
 ng%5Bc
-mdarray.length + 2%5D;%0A%09%09newC
+ount%5D;%0A%09%09%09%09%0A%09%09for (int i = 0; i %3C count; i++) %7B%0A%09%09%09c
 mdArray%5B
 0%5D =
@@ -1203,115 +1203,145 @@
 ray%5B
-0
+i
 %5D = 
-BASH;%0A%09%09newCmdArray%5B1%5D = BASH_FLAG;%0A%09%09System.arraycopy(cmdarray, 0, newCmdArray, 2, cmdarray.length);%0A
+st.nextToken();%0A%09%09%7D%0A%09%09%0A%09%09return exec(cmdArray);%0A%09%7D%0A%0A%09public static SafeProcess exec(final String%5B%5D cmdarray)%0A%09%09throws IOException%0A%09%7B
 %09%09%0A%09
@@ -1492,17 +1492,16 @@
 .submit(
- 
 new Call
@@ -1770,15 +1770,12 @@
 xec(
-newCmdA
+cmda
 rray
@@ -1826,27 +1826,16 @@
        %7D
-           
 %0A       
@@ -2006,17 +2006,16 @@
 Process 
-o
 output%22,
@@ -2260,15 +2260,12 @@
 ;%09%0A%09%7D%0A%7D%0A
-%0A%0A%0A
     

Revert to running processes without /bin/bash -c