1 17 20 package org.apache.forrest.forrestbot.webapp.util; 21 22 import java.io.BufferedReader ; 23 import java.io.File ; 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import java.io.InputStreamReader ; 27 28 import org.apache.forrest.forrestbot.webapp.Config; 29 import org.apache.log4j.Logger; 30 import org.apache.log4j.Priority; 31 32 class StreamGobbler extends Thread { 34 private InputStream is; 35 private Priority type; 36 private Logger log; 37 private boolean debug; 38 39 40 StreamGobbler(InputStream is, Priority type) { 41 this.is = is; 42 this.type = type; 43 log = Logger.getLogger(Executor.class + " " + type.toString()); 44 debug = Boolean.valueOf(Config.getProperty("debug-exec")).booleanValue(); 45 } 46 47 public void run() { 49 BufferedReader br = null; 50 try { 51 br = new BufferedReader (new InputStreamReader (is)); 52 String line = null; 53 while ((line = br.readLine()) != null) { 54 if (debug) 55 log.log(type, line); 56 } 57 } catch (IOException ioe) { 58 log.error("error reading from process", ioe); 59 } finally { 60 if (br != null) { 61 try { 62 br.close(); 63 } catch (IOException ioe2) { 64 log.error("couldn't cleanup after process IO exception", ioe2); 65 } 66 } 67 } 68 } 69 } 70 71 class ExecutorThread extends Thread { 72 private Process proc; 73 private Logger log; 74 75 public ExecutorThread(String id, Process p) { 76 super(id); 77 proc = p; 78 log = Logger.getLogger(Executor.class); 79 } 80 81 public void run() { 82 StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(), Priority.ERROR); 83 errorGobbler.start(); 84 StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(), Priority.DEBUG); 85 outputGobbler.start(); 86 try { 87 proc.getInputStream().close(); 88 proc.getErrorStream().close(); 89 proc.getOutputStream().close(); 90 } catch (IOException ioe) { 91 log.error("couldn't close process's input/output streams", ioe); 92 } 93 } 94 95 } 96 97 public class Executor { 98 private static Logger log = Logger.getLogger(Executor.class); 99 100 private static void run(String target, String project) throws IOException { 101 String command = Config.getProperty("forrest-exec") + " -f " + project + ".xml " + target; 102 File workingDir = new File (Config.getProperty("config-dir")); 103 104 log.debug("executing '" + command + "' in " + workingDir); 105 106 Runtime rt = Runtime.getRuntime(); 107 Process proc = rt.exec(command, null, workingDir); 108 ExecutorThread execThread = new ExecutorThread(project, proc); 109 execThread.start(); 110 } 112 113 public static void build(String project) throws IOException { 114 run(Config.getProperty("targets.build"), project); 115 116 } 117 118 public static void deploy(String project) throws IOException { 119 run(Config.getProperty("targets.deploy"), project); 120 } 121 122 } 123 | Popular Tags |