KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cruisecontrol > builders > ScriptRunner


1 /********************************************************************************
2  * CruiseControl, a Continuous Integration Toolkit
3  * Copyright (c) 2001, ThoughtWorks, Inc.
4  * 651 W Washington Ave. Suite 600
5  * Chicago, IL 60661 USA
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * + Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * + Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  *
20  * + Neither the name of ThoughtWorks, Inc., CruiseControl, nor the
21  * names of its contributors may be used to endorse or promote
22  * products derived from this software without specific prior
23  * written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
29  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  ********************************************************************************/

37
38 package net.sourceforge.cruisecontrol.builders;
39
40 import java.io.File JavaDoc;
41 import java.io.IOException JavaDoc;
42
43 import net.sourceforge.cruisecontrol.CruiseControlException;
44 import net.sourceforge.cruisecontrol.util.Commandline;
45 import net.sourceforge.cruisecontrol.util.StreamConsumer;
46 import net.sourceforge.cruisecontrol.util.StreamPumper;
47
48 import org.apache.log4j.Logger;
49
50 /**
51  * Takes a script and runs it. Monitors how long the script takes to run, and
52  * returns whether the script completed or not.
53  */

54 public class ScriptRunner {
55     private static final Logger LOG = Logger.getLogger(ScriptRunner.class);
56     public static final long NO_TIMEOUT = -1;
57     
58     public static class AsyncKiller extends Thread JavaDoc {
59         private final Process JavaDoc p;
60         private final long timeout;
61         private boolean killed;
62
63         AsyncKiller(final Process JavaDoc p, final long timeout) {
64             this.p = p;
65             this.timeout = timeout;
66         }
67
68         public void run() {
69             try {
70                 sleep(timeout * 1000L);
71                 synchronized (this) {
72                     p.destroy();
73                     killed = true;
74                 }
75             } catch (InterruptedException JavaDoc expected) {
76             }
77         }
78
79         public synchronized boolean processKilled() {
80             return killed;
81         }
82     }
83
84     /**
85      * build and return the results via xml. debug status can be determined from
86      * log4j category once we get all the logging in place.
87      *
88      * @param workingDir The directory to run the script from.
89      * @param script The details on the script to be run.
90      */

91     public boolean runScript(File JavaDoc workingDir, Script script, long timeout) throws CruiseControlException {
92         Commandline commandline = script.buildCommandline();
93
94         commandline.setWorkingDir(workingDir);
95         
96         Process JavaDoc p;
97         int exitCode = -1;
98
99         try {
100             p = commandline.execute();
101         } catch (IOException JavaDoc e) {
102             throw new CruiseControlException("Encountered an IO exception while attempting to execute '"
103                     + script.toString() + "'. CruiseControl cannot continue.", e);
104         }
105
106         StreamPumper errorPumper;
107         StreamPumper outPumper;
108         if (script instanceof StreamConsumer) {
109             errorPumper = new StreamPumper(p.getErrorStream(), (StreamConsumer) script);
110             outPumper = new StreamPumper(p.getInputStream(), (StreamConsumer) script);
111         } else {
112             errorPumper = new StreamPumper(p.getErrorStream());
113             outPumper = new StreamPumper(p.getInputStream());
114         }
115         
116         
117         Thread JavaDoc stderr = new Thread JavaDoc(errorPumper);
118         stderr.start();
119         Thread JavaDoc stdout = new Thread JavaDoc(outPumper);
120         stdout.start();
121         AsyncKiller killer = new AsyncKiller(p, timeout);
122         if (timeout > 0) {
123             killer.start();
124         }
125
126         try {
127             exitCode = p.waitFor();
128             killer.interrupt();
129             stderr.join();
130             stdout.join();
131             p.getInputStream().close();
132             p.getOutputStream().close();
133             p.getErrorStream().close();
134         } catch (InterruptedException JavaDoc e) {
135             LOG.info("Was interrupted while waiting for script to finish."
136                     + " CruiseControl will continue, assuming that it completed");
137         } catch (IOException JavaDoc ie) {
138             LOG.info("Exception trying to close Process streams.", ie);
139         }
140
141         outPumper.flush();
142         errorPumper.flush();
143         
144         script.setExitCode(exitCode);
145         
146         return !killer.processKilled();
147
148     }
149 }
150
151
152
153
154
Popular Tags