KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > forrest > forrestbot > webapp > util > Executor


1 /*
2 * Copyright 2002-2004 The Apache Software Foundation or its licensors,
3 * as applicable.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */

17 /*
18  * Created on Mar 9, 2004
19  */

20 package org.apache.forrest.forrestbot.webapp.util;
21
22 import java.io.BufferedReader JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.io.InputStreamReader JavaDoc;
27
28 import org.apache.forrest.forrestbot.webapp.Config;
29 import org.apache.log4j.Logger;
30 import org.apache.log4j.Priority;
31
32 // based on class from http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html
33
class StreamGobbler extends Thread JavaDoc {
34     private InputStream JavaDoc is;
35     private Priority type;
36     private Logger log;
37     private boolean debug;
38
39
40     StreamGobbler(InputStream JavaDoc 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     // 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
48
public void run() {
49         BufferedReader JavaDoc br = null;
50         try {
51             br = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(is));
52             String JavaDoc line = null;
53             while ((line = br.readLine()) != null) {
54                 if (debug)
55                     log.log(type, line);
56             }
57         } catch (IOException JavaDoc ioe) {
58             log.error("error reading from process", ioe);
59         } finally {
60             if (br != null) {
61                 try {
62                     br.close();
63                 } catch (IOException JavaDoc ioe2) {
64                     log.error("couldn't cleanup after process IO exception", ioe2);
65                 }
66             }
67         }
68     }
69 }
70
71 class ExecutorThread extends Thread JavaDoc {
72     private Process JavaDoc proc;
73     private Logger log;
74
75     public ExecutorThread(String JavaDoc id, Process JavaDoc 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 JavaDoc 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 JavaDoc target, String JavaDoc project) throws IOException JavaDoc {
101         String JavaDoc command = Config.getProperty("forrest-exec") + " -f " + project + ".xml " + target;
102         File JavaDoc workingDir = new File JavaDoc(Config.getProperty("config-dir"));
103
104         log.debug("executing '" + command + "' in " + workingDir);
105
106         Runtime JavaDoc rt = Runtime.getRuntime();
107         Process JavaDoc proc = rt.exec(command, null, workingDir);
108         ExecutorThread execThread = new ExecutorThread(project, proc);
109         execThread.start();
110         // don't wait for it to finish
111
}
112
113     public static void build(String JavaDoc project) throws IOException JavaDoc {
114         run(Config.getProperty("targets.build"), project);
115
116     }
117
118     public static void deploy(String JavaDoc project) throws IOException JavaDoc {
119         run(Config.getProperty("targets.deploy"), project);
120     }
121
122 }
123
Popular Tags