1 64 package com.jcorporate.expresso.core.misc; 65 66 import com.jcorporate.expresso.core.db.DBException; 67 import org.apache.log4j.Logger; 68 69 import java.io.BufferedReader ; 70 import java.io.IOException ; 71 import java.io.InputStreamReader ; 72 import java.util.Enumeration ; 73 import java.util.Vector ; 74 75 76 97 public class OSProcess 98 extends Thread { 99 private static final String thisClass = OSProcess.class.getName() + "."; 100 private String myCommandLine = null; 101 private long sleepInterval; 102 private int maxSleeps; 103 private int exitValue; 104 private Vector stderr = new Vector (); 105 private Vector stdout = new Vector (); 106 private DBException nested = null; 107 private static Logger log = Logger.getLogger(OSProcess.class); 108 109 114 public OSProcess(String commandLine) { 115 myCommandLine = commandLine; 116 } 117 118 123 public DBException getException() { 124 return nested; 125 } 126 127 132 public int getExitValue() 133 throws DBException { 134 return exitValue; 135 } 136 137 138 143 public Vector getStderr() { 144 synchronized (stderr) { 145 return (Vector ) stderr.clone(); 146 } 147 } 148 149 150 155 public Vector getStdout() { 156 synchronized (stdout) { 157 return (Vector ) stdout.clone(); 158 } 159 } 160 161 162 168 public static void main(String [] args) { 169 System.out.println("OSProcess Test"); 170 171 BufferedReader ds = new BufferedReader (new InputStreamReader (System.in)); 172 173 try { 174 System.out.print("Command Line:"); 175 176 String commandLine = ds.readLine(); 177 System.out.print("Timeout interval (milliseconds) (default 5000):"); 178 179 String timeOut = ds.readLine(); 180 181 if (timeOut.equals("")) { 182 timeOut = ("5000"); 183 } 184 185 System.out.print("Retry count (default 1):"); 186 187 String retries = ds.readLine(); 188 189 if (retries.equals("")) { 190 retries = ("1"); 191 } 192 193 OSProcess tp = new OSProcess(commandLine); 194 long sleep = new Long (timeOut).longValue(); 195 int sleepCount = new Integer (retries).intValue(); 196 tp.setTimeOut(sleep, sleepCount); 197 tp.runProcess(); 198 System.out.println("Command '" + commandLine + "' completed."); 199 System.out.println("Exit value:" + tp.getExitValue()); 200 System.out.println("Standard Output:"); 201 202 for (Enumeration e = tp.getStdout().elements(); 203 e.hasMoreElements();) { 204 System.out.println((String ) e.nextElement()); 205 } 206 207 System.out.println("Standard Error:"); 208 209 for (Enumeration e2 = tp.getStderr().elements(); 210 e2.hasMoreElements();) { 211 System.out.println((String ) e2.nextElement()); 212 } 213 } catch (Exception e) { 214 e.printStackTrace(); 215 } 216 } 217 218 222 public void run() { 223 String myName = (thisClass + "run()"); 224 225 226 Runtime rt = Runtime.getRuntime(); 227 228 try { 229 Process myProcess = rt.exec(myCommandLine); 230 231 232 myProcess.waitFor(); 233 234 BufferedReader i = new BufferedReader (new InputStreamReader (myProcess.getInputStream())); 235 BufferedReader ie = new BufferedReader (new InputStreamReader (myProcess.getErrorStream())); 236 String inLine = i.readLine(); 237 238 while (inLine != null) { 239 synchronized (stdout) { 240 stdout.addElement(inLine); 241 } 242 inLine = i.readLine(); 243 } 244 245 inLine = ie.readLine(); 246 247 while (inLine != null) { 248 synchronized (stderr) { 249 stderr.addElement(inLine); 250 } 251 inLine = ie.readLine(); 252 } 253 254 exitValue = myProcess.exitValue(); 255 } catch (InterruptedException ie) { 256 nested = new DBException(myName, 257 "Process '" + myCommandLine + 258 "' was interrupted before it completed successfully:" + 259 ie.getMessage()); 260 } catch (IOException ie) { 261 nested = new DBException(myName, 262 "Unable to launch '" + myCommandLine + 263 "' on server:" + ie.getMessage()); 264 } 265 } 266 267 272 public void runProcess() 273 throws DBException { 274 String myName = (thisClass + "runProcess()"); 275 OSProcess myProcess = new OSProcess(myCommandLine); 276 int currentSleep = 1; 277 278 try { 279 myProcess.start(); 280 281 while (currentSleep < maxSleeps) { 282 sleep(sleepInterval); 283 284 if (myProcess.isAlive()) { 285 if (log.isInfoEnabled()) { 286 log.info("Note: Process '" + myCommandLine + 287 "' has taken " + 288 (sleepInterval * currentSleep) / 1000 + 289 " seconds and is still running"); 290 } 291 } else { 292 break; 293 } 294 295 currentSleep++; 296 } 297 } catch (InterruptedException ie) { 298 throw new DBException(myName + ":Process '" + myCommandLine + 299 "' interrupted", ie); 300 } 301 if (myProcess.isAlive()) { 302 throw new DBException(myName + ":Process '" + myCommandLine + 303 "' did not complete within specified timeout interval"); 304 } 305 306 stderr = myProcess.getStderr(); 307 stdout = myProcess.getStdout(); 308 exitValue = myProcess.getExitValue(); 309 310 if (myProcess.getException() != null) { 311 throw myProcess.getException(); 312 } 313 } 314 315 316 322 public void setTimeOut(long newSleepInterval, int newMaxSleeps) { 323 sleepInterval = newSleepInterval; 324 maxSleeps = newMaxSleeps; 325 } 326 327 } 328 | Popular Tags |