package org.oneliveweb.live.streams; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.Writer; import java.util.ArrayList; import java.util.Collections; import java.util.List; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.openedit.OpenEditException; import org.openedit.util.Exec; import org.openedit.util.ExecutorManager; import org.openedit.util.OutputFiller; import org.openedit.util.StreamCopyRunner; public class LongRunningProcess { private static final Log log = LogFactory.getLog(Exec.class); protected OutputFiller fieldFiller; protected ExecutorManager fieldExecutorManager; protected Process fieldProcess; Writer sendtoprocesswriter; BufferedReader readfromprocess; StreamCopyRunner streamcopy; String fieldCommandName; public StreamCopyRunner getStreamcopy() { return streamcopy; } public ExecutorManager getExecutorManager() { return fieldExecutorManager; } public void setExecutorManager(ExecutorManager inExecutorManager) { fieldExecutorManager = inExecutorManager; } public Process getProcess() { return fieldProcess; } public void setProcess(Process inProcess) { fieldProcess = inProcess; } public void destroyProcess() { if(fieldProcess != null) { fieldProcess.destroy(); } } public void forceDestroyProcess() { if(fieldProcess != null) { fieldProcess.destroyForcibly(); } } public boolean isRunning() { return fieldProcess != null && fieldProcess.isAlive(); } public OutputFiller getFiller() { if (fieldFiller == null) { fieldFiller = new OutputFiller(); } return fieldFiller; } //Called one time public void start(String inCommandKey) { start(inCommandKey,Collections.EMPTY_LIST); } public void start(String inCommandKey, List args) { fieldCommandName = inCommandKey; List com = new ArrayList(args.size() + 1); com.add(inCommandKey); com.addAll(args); log.info("Starting: " + com); if(isRunning()) { log.info("NGINX already running. Stop and restart if needed"); } try { ProcessBuilder processBuilder = new ProcessBuilder(); processBuilder.redirectErrorStream(true); processBuilder.command(com); //processBuilder. fieldProcess = processBuilder.start(); sendtoprocesswriter = new OutputStreamWriter( fieldProcess.getOutputStream(), "UTF-8" ); //Send to process //Start param thread readfromprocess = new BufferedReader( new InputStreamReader(fieldProcess.getInputStream(),"UTF-8") ); //int returnVal = process.waitFor(); streamcopy = new StreamCopyRunner(readfromprocess, getExecutorManager()); synchronized (streamcopy) { streamcopy.startCopy(); streamcopy.wait(2000); //needed? } } catch (Exception ex) { throw new OpenEditException("Could not start " + fieldCommandName ,ex); } } public String runExecStream(String inToSendToProces) throws OpenEditException { return runExecStream(inToSendToProces, -1); } public synchronized String runExecStream(String inToSendToProces, long timeout) throws OpenEditException { try { long start = System.currentTimeMillis(); if( inToSendToProces.contains("\n") && inToSendToProces.indexOf("\n") < inToSendToProces.length() - 1 ) { throw new OpenEditException("Cannot contain new lines " + inToSendToProces); } sendtoprocesswriter.write(inToSendToProces + "\n"); sendtoprocesswriter.flush(); // return null; String returned = getStreamcopy().getNextResult(timeout); long time = System.currentTimeMillis() - start; log.info("ran " + fieldCommandName + " for " + time/1000L + " seconds:"); //log.info("Complete " + fieldCommandName + " with:" + inToSendToProces); return returned; //get the last result from the other thread? } catch (IOException e) { log.debug("Failed to gobble stream", e); } return null; } }