001: /*
002: * <copyright>
003: *
004: * Copyright 2003-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026: package org.cougaar.tools.server.system;
027:
028: import java.io.ByteArrayInputStream;
029: import java.io.ByteArrayOutputStream;
030: import java.io.InputStream;
031: import java.io.OutputStream;
032:
033: /**
034: * Simple utilities for spawning a <code>Process</code> and
035: * capturing the full result to an <code>InputStream</code>.
036: * <p>
037: * This is not appropriate for long-running or interactive
038: * commands, but can be helpful for executing simple comands,
039: * such as "ls".
040: */
041: public final class InvokeUtility {
042:
043: private InvokeUtility() {
044: // just utility functions!
045: }
046:
047: public static final InputStream invokeCommand(String fullCmd)
048: throws Exception {
049: // spawn
050: Process proc = Runtime.getRuntime().exec(fullCmd);
051: // capture output
052: return captureOutput(proc);
053: }
054:
055: public static final InputStream invokeCommand(String[] cmd)
056: throws Exception {
057: // spawn
058: Process proc = Runtime.getRuntime().exec(cmd);
059: // capture output
060: return captureOutput(proc);
061: }
062:
063: //
064: // could all all the other "Runtime.exec(*)" methods here...
065: //
066:
067: private static final InputStream captureOutput(Process proc)
068: throws Exception {
069: // capture the output
070: ByteArrayOutputStream baos = new ByteArrayOutputStream();
071: IOPipe iop = new IOPipe(proc.getInputStream(), baos);
072: Thread tiop = new Thread(iop);
073: tiop.start();
074:
075: tiop.join();
076:
077: // wait for command to complete
078: int exCode = proc.waitFor();
079: if (exCode < 0) {
080: throw new RuntimeException(
081: "Process returned an error response: " + exCode);
082: }
083:
084: // capture the response
085: ByteArrayInputStream bais = new ByteArrayInputStream(baos
086: .toByteArray());
087:
088: return bais;
089: }
090:
091: /**
092: * Simple utility class for piping the contents of an
093: * <code>InputStream</code> to an <code>OutputStream</code>.
094: * <p>
095: * For example, this can be used to capture the output of
096: * a stream to a <code>java.io.ByteArrayOutputStream</code>.
097: */
098: public static final class IOPipe implements Runnable {
099:
100: private final InputStream in;
101: private final OutputStream out;
102:
103: public IOPipe(InputStream in, OutputStream out) {
104: this .in = in;
105: this .out = out;
106: }
107:
108: public void run() {
109: try {
110: byte[] buf = new byte[1024];
111: while (true) {
112: int len = in.read(buf);
113: if (len <= 0) {
114: return; // End of file or error
115: }
116: out.write(buf, 0, len);
117: }
118: } catch (Exception e) {
119: }
120: }
121: }
122:
123: }
|