Changeset 31587 for main/trunk


Ignore:
Timestamp:
2017-04-06T21:19:10+12:00 (7 years ago)
Author:
ak19
Message:

Changes to GS3 src SafeProcess.java to bring it up to speed with GLI's version (sadly can't be identical because of the lack of logger in GLI). 1. Exceptions are now no longer handled by SafeProcess at all times, but only if no exception hander was registered. 2. cmd string also accepted by SafeProcess constructor. And a new single arg constructor to OutputStreamGobbler.

File:
1 edited

Legend:

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

    r31579 r31587  
    2323    static Logger logger = Logger.getLogger(org.greenstone.util.SafeProcess.class.getName());
    2424
     25    // input to SafeProcess and initialising it
     26    private String command = null;
    2527    private String[] command_args = null;
    2628    private String[] envp = null;
     
    2830    private String inputStr = null;
    2931
     32    // output from running SafeProcess.runProcess()
    3033    private String outputStr = "";
    3134    private String errorStr = "";
    32 
    3335    private int exitValue = -1;
    3436
     
    5153    }
    5254
     55    // cmd string version
     56    public SafeProcess(String cmdStr)
     57    {
     58    command = cmdStr;
     59    }
     60
    5361    // cmd args with env version, launchDir can be null.
    5462    public SafeProcess(String[] cmd_args, String[] envparams, File launchDir)
     
    96104
    97105//***************** Copied from gli's gui/FormatConversionDialog.java *************//
    98     public void runProcess() {
     106    public int runProcess() {
    99107
    100108    Process prcs = null;
     
    105113    try {       
    106114        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            }
    122135        }
    123136        }
     
    171184       
    172185    } 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        }
    177193    } 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
    182202
    183203        // propagate interrupts to worker threads here?
     
    218238    }
    219239
     240    return this.exitValue;
    220241    }
    221242   
     
    286307        //System.out.println("@@@ GOT LINE: " + line);
    287308        outputstr.append(line);
     309       
    288310        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")
    290312        }
    291313
     
    295317        }
    296318    } 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       
    300326    } finally {
    301327        SafeProcess.closeResource(br);
     
    318344    ExceptionHandler exceptionHandler = null;
    319345
     346    public OutputStreamGobbler(OutputStream os) {
     347    this.os = os;
     348    }
     349
    320350    public OutputStreamGobbler(OutputStream os, String inputstr)
    321351    {
     
    355385        */
    356386    } 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        }
    361393       
    362394    } finally {
Note: See TracChangeset for help on using the changeset viewer.