1 21 22 package com.izforge.izpack.util; 23 24 import java.io.BufferedReader ; 25 import java.io.BufferedWriter ; 26 import java.io.File ; 27 import java.io.IOException ; 28 import java.io.InputStreamReader ; 29 import java.io.Reader ; 30 import java.io.StringWriter ; 31 import java.io.Writer ; 32 import java.util.ArrayList ; 33 import java.util.Collection ; 34 import java.util.Iterator ; 35 import java.util.List ; 36 37 import com.izforge.izpack.ExecutableFile; 38 39 47 public class FileExecutor 48 { 49 50 56 private static class MonitorInputStream implements Runnable 57 { 58 59 private BufferedReader reader; 60 61 private BufferedWriter writer; 62 63 private boolean shouldStop = false; 64 65 public MonitorInputStream(Reader in, Writer out) 66 { 67 reader = new BufferedReader (in); 68 writer = new BufferedWriter (out); 69 } 70 71 public void doStop() 72 { 73 shouldStop = true; 74 } 75 76 public void run() 77 { 78 try 79 { 80 String line; 81 while ((line = reader.readLine()) != null) 82 { 83 writer.write(line); 84 writer.newLine(); 85 writer.flush(); 86 if (shouldStop) return; 87 } 88 } 89 catch (IOException ioe) 90 { 91 ioe.printStackTrace(System.out); 92 } 93 } 94 } 95 96 private boolean stopThread(Thread t, MonitorInputStream m) 97 { 98 m.doStop(); 99 long softTimeout = 1000; 100 try 101 { 102 t.join(softTimeout); 103 } 104 catch (InterruptedException e) 105 {} 106 107 if (!t.isAlive()) return true; 108 109 t.interrupt(); 110 long hardTimeout = 1000; 111 try 112 { 113 t.join(hardTimeout); 114 } 115 catch (InterruptedException e) 116 {} 117 return !t.isAlive(); 118 } 119 120 126 public FileExecutor(Collection files) 127 { 128 this.files = files; 129 } 130 131 134 public FileExecutor() 135 { 136 this.files = null; 137 } 138 139 145 public static String getExecOutput(String [] aCommandLine) 146 { 147 return getExecOutput(aCommandLine, false); 148 149 } 150 151 158 public static String getExecOutput(String [] aCommandLine, boolean forceToGetStdOut) 159 { 160 FileExecutor fe = new FileExecutor(); 161 162 String [] execOut = new String [2]; 163 164 int execResult = fe.executeCommand(aCommandLine, execOut); 165 166 if (execResult == 0) 167 168 return execOut[0]; 169 170 else if (forceToGetStdOut == true) 171 return execOut[0]; 172 else 173 return execOut[1]; 174 } 175 176 184 public int executeCommand(String [] params, String [] output) 185 { 186 StringBuffer retval = new StringBuffer (); 187 retval.append("executeCommand\n"); 188 if (params != null) 189 { 190 for (int i = 0; i < params.length; i++) 191 { 192 retval.append("\tparams: ").append(params[i]); 193 retval.append("\n"); 194 } 195 } 196 Process process = null; 197 MonitorInputStream outMonitor = null; 198 MonitorInputStream errMonitor = null; 199 Thread t1 = null; 200 Thread t2 = null; 201 int exitStatus = -1; 202 203 Debug.trace(retval); 204 205 try 206 { 207 process = Runtime.getRuntime().exec(params); 209 210 boolean console = false; if (console) 214 { 215 Console c = new Console(process); 216 output[0] = c.getOutputData(); 218 output[1] = c.getErrorData(); 219 exitStatus = process.exitValue(); 220 } 221 else 222 { 223 StringWriter outWriter = new StringWriter (); 224 StringWriter errWriter = new StringWriter (); 225 226 InputStreamReader or = new InputStreamReader (process.getInputStream()); 227 InputStreamReader er = new InputStreamReader (process.getErrorStream()); 228 outMonitor = new MonitorInputStream(or, outWriter); 229 errMonitor = new MonitorInputStream(er, errWriter); 230 t1 = new Thread (outMonitor); 231 t2 = new Thread (errMonitor); 232 t1.setDaemon(true); 233 t2.setDaemon(true); 234 t1.start(); 235 t2.start(); 236 237 exitStatus = process.waitFor(); 239 t1.join(); 240 t2.join(); 241 242 output[0] = outWriter.toString(); 244 Debug.trace("stdout:"); 245 Debug.trace(output[0]); 246 output[1] = errWriter.toString(); 247 Debug.trace("stderr:"); 248 Debug.trace(output[1]); 249 } 250 Debug.trace("exit status: " + Integer.toString(exitStatus)); 251 } 252 catch (InterruptedException e) 253 { 254 if (Debug.tracing()) e.printStackTrace(System.err); 255 stopThread(t1, outMonitor); 256 stopThread(t2, errMonitor); 257 output[0] = ""; 258 output[1] = e.getMessage() + "\n"; 259 process.destroy(); 260 } 261 catch (IOException e) 262 { 263 if (Debug.tracing()) e.printStackTrace(System.err); 264 output[0] = ""; 265 output[1] = e.getMessage() + "\n"; 266 } 267 return exitStatus; 268 } 269 270 278 public int executeFiles(int currentStage, AbstractUIHandler handler) 279 { 280 int exitStatus = 0; 281 String [] output = new String [2]; 282 String permissions = "a+x"; 285 286 Iterator efileIterator = files.iterator(); 288 while (exitStatus == 0 && efileIterator.hasNext()) 289 { 290 ExecutableFile efile = (ExecutableFile) efileIterator.next(); 291 boolean deleteAfterwards = !efile.keepFile; 292 File file = new File (efile.path); 293 Debug.trace("handeling executable file " + efile); 294 295 if (!OsConstraint.oneMatchesCurrentSystem(efile.osList)) continue; 298 299 if (currentStage != ExecutableFile.UNINSTALL && OsVersion.IS_UNIX) 300 { 301 Debug.trace("making file executable (setting executable flag)"); 303 String [] params = { "/bin/chmod", permissions, file.toString()}; 304 exitStatus = executeCommand(params, output); 305 if (exitStatus != 0) 306 { 307 handler.emitError("file execution error", "Error executing \n" + params[0] 308 + " " + params[1] + " " + params[2]); 309 continue; 310 } 311 } 312 313 if ((exitStatus == 0) 315 && ((currentStage == ExecutableFile.POSTINSTALL && efile.executionStage == ExecutableFile.POSTINSTALL) || (currentStage == ExecutableFile.UNINSTALL && efile.executionStage == ExecutableFile.UNINSTALL))) 316 { 317 List paramList = new ArrayList (); 318 if (ExecutableFile.BIN == efile.type) 319 paramList.add(file.toString()); 320 321 else if (ExecutableFile.JAR == efile.type && null == efile.mainClass) 322 { 323 paramList.add(System.getProperty("java.home") + "/bin/java"); 324 paramList.add("-jar"); 325 paramList.add(file.toString()); 326 } 327 else if (ExecutableFile.JAR == efile.type && null != efile.mainClass) 328 { 329 paramList.add(System.getProperty("java.home") + "/bin/java"); 330 paramList.add("-cp"); 331 paramList.add(file.toString()); 332 paramList.add(efile.mainClass); 333 } 334 335 if (null != efile.argList && !efile.argList.isEmpty()) 336 paramList.addAll(efile.argList); 337 338 String [] params = new String [paramList.size()]; 339 for (int i = 0; i < paramList.size(); i++) 340 params[i] = (String ) paramList.get(i); 341 342 exitStatus = executeCommand(params, output); 343 344 if (exitStatus != 0) 346 { 347 deleteAfterwards = false; 348 String message = output[0] + "\n" + output[1]; 349 if (message.length() == 1) 350 message = "Failed to execute " + file.toString() + "."; 351 352 if (efile.onFailure == ExecutableFile.ABORT) 353 { 354 handler.emitError("file execution error", message); 356 } 357 else if (efile.onFailure == ExecutableFile.WARN) 358 { 359 handler.emitWarning("file execution error", message); 361 exitStatus = 0; 362 } 363 else 364 { 365 if (handler 366 .askQuestion(null, "Continue?", AbstractUIHandler.CHOICES_YES_NO) == AbstractUIHandler.ANSWER_YES) 367 exitStatus = 0; 368 } 369 370 } 371 372 } 373 374 if (efile.executionStage == ExecutableFile.POSTINSTALL && deleteAfterwards) 376 { 377 if (file.canWrite()) file.delete(); 378 } 379 380 } 381 return exitStatus; 382 } 383 384 385 private Collection files; 386 } 387 | Popular Tags |