/*
* Copyright 2002-2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
 * Created on Mar 9, 2004
 */
package org.apache.forrest.forrestbot.webapp.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.forrest.forrestbot.webapp.Config;
import org.apache.log4j.Logger;

class ExecutorThread extends Thread {
	private Process proc;
	private Logger log;
	private boolean debug; 
	
	public ExecutorThread(String id, Process p) {
		super(id);
		proc = p;
		log = Logger.getLogger(Executor.class + " " + id);
		debug = Boolean.valueOf(Config.getProperty("debug-exec")).booleanValue();
	}

	public void run() {
		BufferedReader br = new BufferedReader(new InputStreamReader(proc.getInputStream()));
		BufferedReader brErr = new BufferedReader(new InputStreamReader(proc.getErrorStream()));

		try {
			String line;
			while ((line = brErr.readLine()) != null) {
				if (debug)
					log.error(line);
			}
			while ((line = br.readLine()) != null) {
				if (debug)
					log.debug(line);
			}
		} catch (IOException e) {
			log.warn("error reading from process output", e);
			return;
		}
	}

}

public class Executor {
	private static Logger log = Logger.getLogger(Executor.class);

	private static void run(String target, String project) throws IOException {
		String command = Config.getProperty("forrest-exec") + " -f " + project + ".xml " + target;
		File workingDir = new File(Config.getProperty("config-dir"));

		log.debug("executing '" + command + "' in " + workingDir);

		Runtime rt = Runtime.getRuntime();
		Process proc = rt.exec(command, null, workingDir);
		ExecutorThread execThread = new ExecutorThread(project, proc);
		execThread.start();
		// don't wait for it to finish
	}

	public static void build(String project) throws IOException {
		run(Config.getProperty("targets.build"), project);

	}

	public static void deploy(String project) throws IOException {
		run(Config.getProperty("targets.deploy"), project);
	}

}

@@ -763,16 +763,44 @@
 eption;%0A
+import java.io.InputStream;%0A
 import j
@@ -914,68 +914,215 @@
 er;%0A
-%0Aclass ExecutorThread extends Thread %7B%0A%09private Process proc
+import org.apache.log4j.Priority;%0A%0A// based on class from http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html%0Aclass StreamGobbler extends Thread %7B%0A%09private InputStream is;%0A%09private Priority type
 ;%0A%09p
@@ -1167,82 +1167,93 @@
 bug;
- %0A%09%0A%09public ExecutorThread(String id
+%0A%0A%0A%09StreamGobbler(InputStream is
 , Pr
-ocess p) %7B%0A%09%09super(id);%0A%09%09proc = p
+iority type) %7B%0A%09%09this.is = is;%0A%09%09this.type = type
 ;%0A%09%09
@@ -1298,18 +1298,31 @@
 + %22 %22 + 
-id
+type.toString()
 );%0A%09%09deb
@@ -1401,118 +1401,175 @@
 %7D%0A%0A%09
-public void run() %7B%0A%09%09BufferedReader br = new BufferedReader(new InputStreamReader(proc.getInputStream()));
+// we have to read from the buffer whether we're going to debug or not; on some systems things will freeze up if the output isn't read%0A%09public void run() %7B%0A%09%09try %7B
 %0A%09%09
+%09
 Buff
@@ -1581,19 +1581,16 @@
 eader br
-Err
  = new B
@@ -1629,41 +1629,13 @@
 der(
-proc.getErrorStream()));%0A%0A%09%09try %7B
+is));
 %0A%09%09%09
@@ -1645,16 +1645,23 @@
 ing line
+ = null
 ;%0A%09%09%09whi
@@ -1678,11 +1678,8 @@
 = br
-Err
 .rea
@@ -1726,14 +1726,18 @@
 log.
-error(
+log(type, 
 line
@@ -1750,187 +1750,470 @@
 %7D%0A%09%09
-%09while ((line = br.readLine()) != null) %7B%0A%09%09%09%09if (debug)%0A%09%09%09%09%09log.debug(line
+%7D catch (IOException ioe) %7B%0A%09%09%09ioe.printStackTrace(
 );%0A%09%09
+%7D%0A
 %09%7D%0A
-%09%09%7D catch (IOException e) %7B%0A%09%09%09log.warn(%22error reading from process output%22, e);%0A%09%09%09return;%0A%09%09%7D
+%7D%0A%0Aclass ExecutorThread extends Thread %7B%0A%09private Process proc;%0A%0A%09public ExecutorThread(String id, Process p) %7B%0A%09%09super(id);%0A%09%09proc = p;%0A%09%7D%0A%0A%09public void run() %7B%0A        StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(), Priority.ERROR);%0A        errorGobbler.start();%0A%09%09StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(), Priority.DEBUG);%0A%09%09outputGobbler.start();
 %0A%09%7D%0A

read from stdout & stderr simultaneously