KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > boot > CommandRunner


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.boot;
21
22 import java.io.BufferedReader JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.InputStream JavaDoc;
25 import java.io.InputStreamReader JavaDoc;
26 import java.util.Vector JavaDoc;
27
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30
31 /**
32  * Executes a system command give a list of arguments and gather the output in a
33  * string that be accessed when the command is complete.
34  *
35  * @author Brett Smith <a HREF="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
36  */

37 public class CommandRunner extends Thread JavaDoc {
38     final static Log log = LogFactory.getLog(CommandRunner.class);
39
40     // Private instance variables
41

42     private String JavaDoc[] arguments;
43     private StringBuffer JavaDoc outBuf = new StringBuffer JavaDoc();
44     private Process JavaDoc process;
45     private boolean includeOutputOnException;
46
47     /**
48      * Constructor. The first argument supplied must be the name of the command
49      * (if it is on the system path) or the full path the command. Any command
50      * arguments should be supplied as further elements.
51      *
52      * @param arguments
53      */

54     public CommandRunner(String JavaDoc[] arguments) {
55         this.arguments = arguments;
56     }
57
58     /**
59      * Constructor. The first argument supplied must be the name of the command
60      * (if it is on the system path) or the full path the command. Any command
61      * arguments should be supplied as further elements.
62      *
63      * @param arguments
64      */

65     public CommandRunner(Vector JavaDoc arguments) {
66         this.arguments = new String JavaDoc[arguments.size()];
67         arguments.copyInto(this.arguments);
68     }
69
70     /**
71      * Get the output of the command.
72      *
73      * @return output
74      */

75     public String JavaDoc getOutput() {
76         return outBuf.toString();
77     }
78
79     /**
80      * Run the command. Any output will then be availabie from
81      * {@link #getOutput()}. An exception will be thrown if the command returns
82      * a non-zero exit value.
83      *
84      * @throws Exception if any error occurs running the command.
85      */

86     public void runCommand() throws Exception JavaDoc {
87         try {
88
89             if (log.isDebugEnabled() || "true".equals(System.getProperty("sslexplorer.debugSystemCommands", "false"))) {
90                 
91                 StringBuffer JavaDoc debug = new StringBuffer JavaDoc("Running command '");
92                 for (int i = 0; i < arguments.length; i++) {
93                     if (i > 0) {
94                         debug.append(" ");
95                     }
96                     debug.append("\"");
97                     debug.append(arguments[i]);
98                     debug.append("\"");
99                 }
100     
101                 if(log.isDebugEnabled()) {
102                     log.debug(debug.toString());
103                 }
104                 
105                 if("true".equals(System.getProperty("sslexplorer.debugSystemCommands", "false"))) {
106                     log.error("CMD " + debug.toString());
107                 }
108             }
109
110             process = Runtime.getRuntime().exec(arguments);
111             InputStream JavaDoc in = process.getInputStream();
112             start();
113             BufferedReader JavaDoc reader = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(in));
114             String JavaDoc line = null;
115             while ((line = reader.readLine()) != null) {
116                 if ("true".equals(System.getProperty("sslexplorer.debugSystemCommands", "false"))) {
117                     log.error("STDOUT " + line);
118                 }
119                 synchronized (outBuf) {
120                     if (outBuf.length() > 0) {
121                         outBuf.append("\n");
122                     }
123                     outBuf.append(line);
124                 }
125             }
126             process.waitFor();
127             if (process.exitValue() != 0) {
128                 log.error("Command '" + arguments[0] + "' failed with exit code " + process.exitValue());
129                 log.error("Start of command output ---->");
130                 log.error(outBuf.toString());
131                 log.error("<---- End of command output");
132                 throw new Exception JavaDoc("Command returned exit code " + process.exitValue() + ". "
133                                 + (includeOutputOnException ? outBuf.toString() : "Check the logs for more detail."));
134             }
135         } finally {
136         }
137     }
138
139     /**
140      * Set whther the output should be included in the exception text should an
141      * error occur.
142      *
143      * @param includeOutputOnException include output on exception
144      */

145     public void setIncludeOutputOnException(boolean includeOutputOnException) {
146         this.includeOutputOnException = includeOutputOnException;
147
148     }
149
150     public void run() {
151         try {
152             BufferedReader JavaDoc reader = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(process.getErrorStream()));
153             String JavaDoc line = null;
154             while ((line = reader.readLine()) != null) {
155                 if ("true".equals(System.getProperty("sslexplorer.debugSystemCommands", "false"))) {
156                     log.error("STDERR " + line);
157                 }
158                 synchronized (outBuf) {
159                     if (outBuf.length() > 0) {
160                         outBuf.append("\n");
161                     }
162                     outBuf.append(line);
163                 }
164             }
165         } catch (IOException JavaDoc ioe) {
166         }
167     }
168 }
Popular Tags