Ignore:
Timestamp:
2017-04-20T18:21:50+12:00 (7 years ago)
Author:
ak19
Message:

Adding a basic run process method, runBasicProcess(), one to be used when you don't work with the process' streams at all.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone3/src/java/org/greenstone/util/SafeProcess.java

    r31594 r31615  
    9090    }
    9191
    92 //***************** Copied from gli's gui/FormatConversionDialog.java *************//
     92    private Process doRuntimeExec() throws IOException {
     93    Process prcs = null;
     94    Runtime rt = Runtime.getRuntime();
     95   
     96    if(this.command != null) {
     97        prcs = rt.exec(this.command);
     98    }
     99    else { // at least command_args must be set now
     100       
     101        // http://stackoverflow.com/questions/5283444/convert-array-of-strings-into-a-string-in-java
     102        ///System.err.println("SafeProcess running: " + Arrays.toString(command_args));
     103        logger.info("SafeProcess running: " + Arrays.toString(command_args));
     104       
     105        if(this.envp == null) {
     106        prcs = rt.exec(this.command_args);
     107        } else { // launch process using cmd str with env params       
     108       
     109        if(this.dir == null) {
     110            ///logger.info("\twith: " + Arrays.toString(this.envp));
     111            ///System.err.println("\twith: " + Arrays.toString(this.envp));
     112            prcs = rt.exec(this.command_args, this.envp);
     113        } else {
     114            ///logger.info("\tfrom directory: " + this.dir);
     115            ///logger.info("\twith: " + Arrays.toString(this.envp));
     116            ///System.err.println("\tfrom directory: " + this.dir);
     117            ///System.err.println("\twith: " + Arrays.toString(this.envp));
     118            prcs = rt.exec(this.command_args, this.envp, this.dir);
     119        }
     120        }
     121    }
     122
     123    return prcs;
     124    }
     125
     126
     127    // no reading from or writing to Process' iostreams, just exec process and wait for it to return
     128    public int runBasicProcess() {
     129    Process prcs = null;
     130    try {
     131        prcs = doRuntimeExec();
     132        this.exitValue = prcs.waitFor();
     133
     134       
     135    } catch(IOException ioe) {
     136        if(exceptionHandler != null) {
     137        exceptionHandler.gotException(ioe);
     138        } else {
     139        logger.error("IOException: " + ioe.getMessage(), ioe);
     140        //System.err.println("IOException " + ioe.getMessage());
     141        //ioe.printStackTrace();
     142        }
     143    } catch(InterruptedException ie) {
     144
     145        if(exceptionHandler != null) {
     146        exceptionHandler.gotException(ie);
     147        } else {
     148        logger.error("Process InterruptedException: " + ie.getMessage(), ie);
     149        //System.err.println("Process InterruptedException " + ie.getMessage());
     150        ///ie.printStackTrace(); // an interrupt here is not an error, it can be a cancel action
     151        }
     152       
     153        Thread.currentThread().interrupt();
     154    } finally {
     155
     156        if( prcs != null ) {
     157        prcs.destroy(); // see runProcess() below
     158        }
     159    }
     160    return this.exitValue;
     161    }
    93162
    94163    public int runProcess() {
    95164    return runProcess(null, null, null); // use default processing of all 3 of the process' iostreams
    96165    }
     166
     167//***************** Copied from gli's gui/FormatConversionDialog.java *************//
    97168
    98169    public int runProcess(CustomProcessHandler procInHandler,
     
    105176    SafeProcess.InputStreamGobbler outputGobbler = null;
    106177
    107     try {       
    108        
    109         Runtime rt = Runtime.getRuntime();     
    110        
    111        
    112         if(this.command != null) {
    113         prcs = rt.exec(this.command);
    114         }
    115         else { // at least command_args must be set now
    116 
    117         // http://stackoverflow.com/questions/5283444/convert-array-of-strings-into-a-string-in-java
    118         ///System.err.println("SafeProcess running: " + Arrays.toString(command_args));
    119         logger.info("SafeProcess running: " + Arrays.toString(command_args));
    120 
    121         if(this.envp == null) {
    122             prcs = rt.exec(this.command_args);
    123         } else { // launch process using cmd str with env params       
    124            
    125             if(this.dir == null) {
    126             ///logger.info("\twith: " + Arrays.toString(this.envp));
    127             ///System.err.println("\twith: " + Arrays.toString(this.envp));
    128             prcs = rt.exec(this.command_args, this.envp);
    129             } else {
    130             ///logger.info("\tfrom directory: " + this.dir);
    131             ///logger.info("\twith: " + Arrays.toString(this.envp));
    132             ///System.err.println("\tfrom directory: " + this.dir);
    133             ///System.err.println("\twith: " + Arrays.toString(this.envp));
    134             prcs = rt.exec(this.command_args, this.envp, this.dir);
    135             }
    136         }
    137         }
     178    try {
     179        prcs = doRuntimeExec();
     180       
    138181
    139182        // Create the streamgobblers and set any specified handlers on them
     
    166209        }
    167210
    168        
     211
    169212            // kick off the stream gobblers
    170213            inputGobbler.start();
Note: See TracChangeset for help on using the changeset viewer.