001: /********************************************************************************
002: * CruiseControl, a Continuous Integration Toolkit
003: * Copyright (c) 2006, ThoughtWorks, Inc.
004: * 200 E. Randolph, 25th Floor
005: * Chicago, IL 60601 USA
006: * All rights reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: *
012: * + Redistributions of source code must retain the above copyright
013: * notice, this list of conditions and the following disclaimer.
014: *
015: * + Redistributions in binary form must reproduce the above
016: * copyright notice, this list of conditions and the following
017: * disclaimer in the documentation and/or other materials provided
018: * with the distribution.
019: *
020: * + Neither the name of ThoughtWorks, Inc., CruiseControl, nor the
021: * names of its contributors may be used to endorse or promote
022: * products derived from this software without specific prior
023: * written permission.
024: *
025: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
026: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
027: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
028: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
029: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
030: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
031: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
032: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
033: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
034: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
035: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
036: ********************************************************************************/package net.sourceforge.cruisecontrol.util;
037:
038: import java.io.IOException;
039: import org.apache.log4j.Logger;
040:
041: /**
042: * Utility methods for interacting with Java processes.
043: *
044: * @see Process
045: */
046: public final class Processes {
047: private static final Logger LOG = Logger.getLogger(Processes.class);
048: private static Executor runtime = new RuntimeExecutor();
049:
050: private Processes() {
051: //utility methods only.
052: }
053:
054: public static void executeFully(Commandline c) throws IOException,
055: InterruptedException {
056: Process p = execute(c);
057: p.waitFor();
058: IO.close(p);
059: }
060:
061: public static Process execute(Commandline c) throws IOException {
062: Process p = runtime.exec(c);
063: StreamPumper errorPumper = StreamLogger.getWarnPumper(LOG, p);
064: new Thread(errorPumper, "ProcessThread_" + c.getExecutable())
065: .start();
066: return p;
067: }
068:
069: /**
070: * Waits for a process to finish executing and logs the output.
071: *
072: * @param proc the process.
073: * @param log where to log both standard and error output.
074: * @return the process' exit value
075: */
076: public static int waitFor(Process proc, Logger log)
077: throws IOException, InterruptedException {
078: return waitFor(proc, StreamLogger.getInfoLogger(log),
079: StreamLogger.getWarnLogger(log));
080: }
081:
082: /**
083: * Waits for a process to finish executing.
084: * @param proc the process.
085: * @param output consumes the process' standard output.
086: * @param error consumes the process' error output.
087: * @return the process' exit value
088: */
089: public static int waitFor(Process proc, StreamConsumer output,
090: StreamConsumer error) throws IOException,
091: InterruptedException {
092: proc.getOutputStream().close();
093:
094: Thread stderr = new Thread(new StreamPumper(proc
095: .getErrorStream(), error));
096: stderr.start();
097:
098: new StreamPumper(proc.getInputStream(), output).run();
099:
100: int exitValue = proc.waitFor();
101: stderr.join();
102: return exitValue;
103: }
104:
105: static void setRuntime(Executor e) {
106: runtime = e;
107: }
108: }
|