Changeset 31587 for main/trunk/greenstone3/src/java
- Timestamp:
- 2017-04-06T21:19:10+12:00 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
main/trunk/greenstone3/src/java/org/greenstone/util/SafeProcess.java
r31579 r31587 23 23 static Logger logger = Logger.getLogger(org.greenstone.util.SafeProcess.class.getName()); 24 24 25 // input to SafeProcess and initialising it 26 private String command = null; 25 27 private String[] command_args = null; 26 28 private String[] envp = null; … … 28 30 private String inputStr = null; 29 31 32 // output from running SafeProcess.runProcess() 30 33 private String outputStr = ""; 31 34 private String errorStr = ""; 32 33 35 private int exitValue = -1; 34 36 … … 51 53 } 52 54 55 // cmd string version 56 public SafeProcess(String cmdStr) 57 { 58 command = cmdStr; 59 } 60 53 61 // cmd args with env version, launchDir can be null. 54 62 public SafeProcess(String[] cmd_args, String[] envparams, File launchDir) … … 96 104 97 105 //***************** Copied from gli's gui/FormatConversionDialog.java *************// 98 public voidrunProcess() {106 public int runProcess() { 99 107 100 108 Process prcs = null; … … 105 113 try { 106 114 Runtime rt = Runtime.getRuntime(); 107 108 // http://stackoverflow.com/questions/5283444/convert-array-of-strings-into-a-string-in-java 109 //logger.info("Running process: " + Arrays.toString(command_args)); 110 111 if(this.envp == null) { 112 prcs = rt.exec(this.command_args); 113 } else { // launch process using cmd str with env params 114 115 if(this.dir == null) { 116 //logger.info("\twith: " + Arrays.toString(this.envp)); 117 prcs = rt.exec(this.command_args, this.envp); 118 } else { 119 //logger.info("\tfrom directory: " + this.dir); 120 //logger.info("\twith: " + Arrays.toString(this.envp)); 121 prcs = rt.exec(this.command_args, this.envp, this.dir); 115 if(this.command != null) { 116 prcs = rt.exec(this.command); 117 } 118 else { // at least command_args must be set now 119 120 // http://stackoverflow.com/questions/5283444/convert-array-of-strings-into-a-string-in-java 121 ///logger.info("SafeProcess running: " + Arrays.toString(command_args)); 122 123 if(this.envp == null) { 124 prcs = rt.exec(this.command_args); 125 } else { // launch process using cmd str with env params 126 127 if(this.dir == null) { 128 logger.info("\twith: " + Arrays.toString(this.envp)); 129 prcs = rt.exec(this.command_args, this.envp); 130 } else { 131 logger.info("\tfrom directory: " + this.dir); 132 logger.info("\twith: " + Arrays.toString(this.envp)); 133 prcs = rt.exec(this.command_args, this.envp, this.dir); 134 } 122 135 } 123 136 } … … 171 184 172 185 } catch(IOException ioe) { 173 logger.error("IOexception: " + ioe.getMessage(), ioe); 174 //System.err.println("IOexception " + ioe.getMessage()); 175 //ioe.printStackTrace(); 176 if(exceptionHandler != null) exceptionHandler.gotException(ioe); 186 if(exceptionHandler != null) { 187 exceptionHandler.gotException(ioe); 188 } else { 189 logger.error("IOexception: " + ioe.getMessage(), ioe); 190 //System.err.println("IOexception " + ioe.getMessage()); 191 //ioe.printStackTrace(); 192 } 177 193 } catch(InterruptedException ie) { 178 logger.error("Process InterruptedException: " + ie.getMessage(), ie); 179 //System.err.println("Process InterruptedException " + ie.getMessage()); 180 //ie.printStackTrace(); 181 if(exceptionHandler != null) exceptionHandler.gotException(ie); 194 if(exceptionHandler != null) { 195 exceptionHandler.gotException(ie); 196 } else { 197 logger.error("Process InterruptedException: " + ie.getMessage(), ie); 198 //System.err.println("Process InterruptedException " + ie.getMessage()); 199 //ie.printStackTrace(); // an interrupt here is not an error, it can be a cancel action 200 } 201 182 202 183 203 // propagate interrupts to worker threads here? … … 218 238 } 219 239 240 return this.exitValue; 220 241 } 221 242 … … 286 307 //System.out.println("@@@ GOT LINE: " + line); 287 308 outputstr.append(line); 309 288 310 if(split_newlines) { 289 outputstr.append( Misc.NEWLINE); // "\n" is system dependent (Win must be "\r\n")311 outputstr.append(Utility.NEWLINE); // "\n" is system dependent (Win must be "\r\n") 290 312 } 291 313 … … 295 317 } 296 318 } catch (IOException ioe) { 297 logger.error("Exception when reading from a process' stdout/stderr stream: ", ioe); 298 if(lineByLineHandler != null) lineByLineHandler.gotException(ioe); 299 //ioe.printStackTrace(); 319 if(lineByLineHandler != null) { 320 lineByLineHandler.gotException(ioe); 321 } else { 322 logger.error("Exception when reading from a process' stdout/stderr stream: ", ioe); 323 //ioe.printStackTrace(); 324 } 325 300 326 } finally { 301 327 SafeProcess.closeResource(br); … … 318 344 ExceptionHandler exceptionHandler = null; 319 345 346 public OutputStreamGobbler(OutputStream os) { 347 this.os = os; 348 } 349 320 350 public OutputStreamGobbler(OutputStream os, String inputstr) 321 351 { … … 355 385 */ 356 386 } catch (IOException ioe) { 357 logger.error("Exception writing to SafeProcess' inputstream: ", ioe); 358 //ioe.printStackTrace(); 359 360 if (this.exceptionHandler != null) this.exceptionHandler.gotException(ioe); 387 if (this.exceptionHandler != null) { 388 this.exceptionHandler.gotException(ioe); 389 } else { 390 logger.error("Exception writing to SafeProcess' inputstream: ", ioe); 391 //ioe.printStackTrace(); 392 } 361 393 362 394 } finally {
Note:
See TracChangeset
for help on using the changeset viewer.