1 21 22 package org.opensubsystems.core.util; 23 24 import java.io.File ; 25 import java.io.IOException ; 26 import java.util.logging.Level ; 27 import java.util.logging.Logger ; 28 29 import org.opensubsystems.core.error.OSSException; 30 31 40 public final class ProcessExec 41 { 42 44 47 public static final int INTERRUPTED_PROCESS_RETURN_VALUE = -1; 48 49 52 public static final Integer INTERRUPTED_PROCESS_RETURN_VALUE_OBJ 53 = new Integer (INTERRUPTED_PROCESS_RETURN_VALUE); 54 55 57 60 private static Logger s_logger = Log.getInstance(ProcessExec.class); 61 62 64 67 private ProcessExec( 68 ) 69 { 70 } 72 73 75 89 public static Object [] executeAndWait( 90 String commandline, 91 boolean bCapture 92 ) throws IOException , 93 OSSException 94 { 95 Object [] returnValue; 96 97 s_logger.log(Level.FINEST, "Executing command " + commandline); 98 returnValue = execute(commandline, bCapture); 99 int exitVal; 101 102 try 103 { 104 exitVal = ((Process )returnValue[0]).waitFor(); 105 returnValue[0] = new Integer (exitVal); 106 } 107 catch (InterruptedException ieExc) 108 { 109 returnValue[0] = INTERRUPTED_PROCESS_RETURN_VALUE_OBJ; 111 } 112 113 return returnValue; 114 } 115 116 136 public static Object [] executeAndWait( 137 String [] commandline, 138 String [] environment, 139 File workingDir, 140 boolean bCapture, 141 long lMaxCaptureSize 142 ) throws IOException 143 { 144 Object [] returnValue; 145 146 returnValue = execute(commandline, environment, workingDir, bCapture, 147 lMaxCaptureSize); 148 149 int exitVal; 151 152 try 153 { 154 exitVal = ((Process )returnValue[0]).waitFor(); 155 returnValue[0] = new Integer (exitVal); 156 } 157 catch (InterruptedException ieExc) 158 { 159 returnValue[0] = INTERRUPTED_PROCESS_RETURN_VALUE_OBJ; 161 } 162 163 return returnValue; 164 } 165 166 180 public static Object [] execute( 181 String commandline, 182 boolean bCapture 183 ) throws IOException , 184 OSSException 185 { 186 Object [] returnValue; 187 188 s_logger.log(Level.FINEST, "Executing command " + commandline); 189 returnValue = execute(StringUtils.parseQuotedStringToStringArray( 190 commandline, " ", true, true), 191 null, null, bCapture, Long.MAX_VALUE); 192 193 return returnValue; 194 } 195 196 197 217 public static Object [] execute( 218 String [] commandline, 219 String [] environment, 220 File workingDir, 221 boolean bCapture, 222 long lMaxCaptureSize 223 ) throws IOException 224 { 225 String [] cmd = new String [3]; 226 Object [] returnValue = new Object [3]; 227 228 if (GlobalConstants.isWindows9x()) 229 { 230 cmd = new String [3]; 231 cmd[0] = "command.com"; 232 cmd[1] = "/C"; 233 cmd[2] = StringUtils.concat(commandline, " ", null); 234 } 235 else if (GlobalConstants.isWindows()) 236 { 237 cmd = new String [3]; 238 cmd[0] = "cmd.exe"; 239 cmd[1] = "/C"; 240 cmd[2] = StringUtils.concat(commandline, " ", null); 241 } 242 else 243 { 244 cmd = commandline; 245 } 246 247 Runtime rt = Runtime.getRuntime(); 248 s_logger.log(Level.FINEST, "Executing command " 249 + StringUtils.concat(cmd, " ", null)); 250 Process proc = rt.exec(cmd); 251 252 StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(), 254 "ERROR", bCapture, 255 lMaxCaptureSize); 256 257 StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(), 259 "OUTPUT", bCapture, 260 lMaxCaptureSize); 261 262 errorGobbler.start(); 264 outputGobbler.start(); 265 266 returnValue[0] = proc; 267 returnValue[1] = outputGobbler; 268 returnValue[2] = errorGobbler; 269 270 return returnValue; 271 } 272 } 273 | Popular Tags |