001: /*
002: * BEGIN_HEADER - DO NOT EDIT
003: *
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017: * If applicable add the following below this CDDL HEADER,
018: * with the fields enclosed by brackets "[]" replaced with
019: * your own identifying information: Portions Copyright
020: * [year] [name of copyright owner]
021: */
022:
023: /*
024: * @(#)ProcessExecutor.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package com.sun.jbi.installer;
030:
031: import java.io.File;
032: import java.io.InputStream;
033: import java.io.InputStreamReader;
034: import java.io.BufferedReader;
035: import java.io.OutputStream;
036: import java.io.FileOutputStream;
037:
038: /**
039: *
040: *This class is used to execute external commands and is used
041: * during installation/uninstallation
042: */
043:
044: /**
045: * This is the inner class that consumes the error and output stream
046: */
047: class StreamConsumer extends Thread {
048: /**
049: * The inputstream from the process
050: */
051: InputStream is;
052:
053: /**
054: * The boolean that denotes if this is installation
055: * this is true if this is installation
056: */
057:
058: /**
059: * the logFile
060: */
061: File logFile = null;
062:
063: boolean isInstall;
064:
065: StreamConsumer(InputStream is, File logFile) {
066: this .is = is;
067: this .logFile = logFile;
068: }
069:
070: /**
071: * the thread execution method. This method consumes the input from the
072: * given stream and write it in the given resultStore. If no resultStore is given
073: * then it is written into the debug file. Installer uses resultStore for commands
074: * where the output has to be analysed and is predictably small. For example
075: * output of asadmin start-domain. But for cases like invoking ant commands to
076: * install files into the domain/or to install sample components, installer does not
077: * supply result store and thus the output goes to the debug files.
078: */
079: public void run() {
080: try {
081: InputStreamReader isr = new InputStreamReader(is);
082: BufferedReader br = new BufferedReader(isr);
083: String line = null;
084: while ((line = br.readLine()) != null) {
085: try {
086: writeToLog(line);
087:
088: } catch (Exception e) {
089: //try writing into the installer debug file.
090: //if not possible simply ignore
091: }
092: }
093: } catch (Exception ioe) {
094: ioe.printStackTrace();
095: }
096: }
097:
098: /**
099: *This method is used to dump the contents of the Stream
100: *consumers to a log file
101: */
102: public void writeToLog(String line) {
103: try {
104: FileOutputStream out = new FileOutputStream(logFile, true);
105: out.write(line.getBytes());
106: out.write("\n".getBytes());
107: out.close();
108: } catch (Exception e) {
109:
110: }
111:
112: }
113: }
114:
115: /**
116: * This class executes the given external command and returns the exitValue
117: * output and error.
118: */
119: public class ProcessExecutor {
120:
121: /**
122: * This method is used to execute a given external command
123: * @return int the execute value
124: * @param output after the command is executed
125: * @param error after the command is executed
126: * @param timeout the timeout for the executed process
127: */
128: public int execute(String cmd, File logFile) {
129: try {
130: Runtime rt = Runtime.getRuntime();
131: Process proc = rt.exec(cmd);
132:
133: StreamConsumer errorConsumer = new StreamConsumer(proc
134: .getErrorStream(), logFile);
135: StreamConsumer outputConsumer = new StreamConsumer(proc
136: .getInputStream(), logFile);
137: errorConsumer.start();
138: outputConsumer.start();
139: int exitVal = proc.waitFor();
140: return exitVal;
141: } catch (Exception e) {
142: return -1;
143: }
144: }
145:
146: }
|