Changeset 31582 for main/trunk/gli


Ignore:
Timestamp:
04/06/17 19:15:55 (4 years ago)
Author:
ak19
Message:

In place of the Input- and OutputStreamGobbler classes, am now shifting GLI code to use SafeProcess too, copied across from GS3 src code. Class SafeProcess includes the two streamgobblers as static inner classes and some more functionality with safely running an external process from Java.

Location:
main/trunk/gli/src/org/greenstone/gatherer
Files:
1 added
2 deleted
5 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/gli/src/org/greenstone/gatherer/gui/ConfigFileEditor.java

    r30959 r31582  
    2929import org.greenstone.gatherer.Dictionary;
    3030import org.greenstone.gatherer.Gatherer;
    31 import org.greenstone.gatherer.cdm.CollectionConfigXMLReadWrite;
    32 import org.greenstone.gatherer.util.Codec;
    33 import org.greenstone.gatherer.util.InputStreamGobbler;
    34 import org.greenstone.gatherer.util.OutputStreamGobbler;
    3531import org.greenstone.gatherer.util.StaticStrings;
    3632import org.greenstone.gatherer.util.Utility;
  • main/trunk/gli/src/org/greenstone/gatherer/gui/DownloadPane.java

    r29974 r31582  
    5252import org.greenstone.gatherer.file.WorkspaceTree;
    5353import org.greenstone.gatherer.greenstone.LocalGreenstone;
     54import org.greenstone.gatherer.util.SafeProcess;
    5455import org.greenstone.gatherer.util.StaticStrings;
    5556import org.greenstone.gatherer.util.Utility;
     
    281282    }
    282283    finally {
    283         Utility.closeProcess(process);
     284        SafeProcess.closeProcess(process);
    284285    }
    285286
  • main/trunk/gli/src/org/greenstone/gatherer/gui/FormatConversionDialog.java

    r30870 r31582  
    3131import org.greenstone.gatherer.cdm.CollectionConfigXMLReadWrite;
    3232import org.greenstone.gatherer.util.Codec;
    33 import org.greenstone.gatherer.util.InputStreamGobbler;
    34 import org.greenstone.gatherer.util.OutputStreamGobbler;
     33import org.greenstone.gatherer.util.SafeProcess;
    3534import org.greenstone.gatherer.util.StaticStrings;
    3635import org.greenstone.gatherer.util.XMLTools;
     
    317316      }*/
    318317
     318    SafeProcess prcs = new SafeProcess(command_args);
     319    prcs.setInputString(inputstr);
     320    prcs.setSplitStdErrorNewLines(true);
     321    prcs.runProcess();
     322    // process done, now get process output
     323    outputstr = prcs.getStdOutput();
     324    String errmsg = prcs.getStdError();
     325    if(!errmsg.equals("")) {
     326        System.err.println("*** Process errorstream: \n" + errmsg + "\n****");
     327    }
     328
     329    ///System.err.println("#### Got output: " + outputstr);
     330
     331    /*
    319332    try {       
    320333       
     
    363376        //ie.printStackTrace();
    364377    }
    365 
     378    */
    366379    return outputstr;
    367380    }
  • main/trunk/gli/src/org/greenstone/gatherer/util/GS3ServerThread.java

    r29361 r31582  
    8686            // And we'll catch the error and output streams to prevent them from blocking during waitFor()
    8787            // (For normal input and output stream handling using the Gobblers, see FormatConversionDialog.java)
    88             OutputStreamGobbler inputGobbler = new OutputStreamGobbler(p.getOutputStream(), null);         
    89             InputStreamGobbler errorGobbler = new InputStreamGobbler(p.getErrorStream(), true);
    90             InputStreamGobbler outputGobbler = new InputStreamGobbler(p.getInputStream());
     88            SafeProcess.OutputStreamGobbler inputGobbler
     89                = new SafeProcess.OutputStreamGobbler(p.getOutputStream(), null);           
     90            SafeProcess.InputStreamGobbler errorGobbler
     91                = new SafeProcess.InputStreamGobbler(p.getErrorStream(), true);
     92            SafeProcess.InputStreamGobbler outputGobbler
     93                = new SafeProcess.InputStreamGobbler(p.getInputStream());
    9194           
    9295            errorGobbler.start();
  • main/trunk/gli/src/org/greenstone/gatherer/util/Utility.java

    r30702 r31582  
    4141// Don't even think about adding import java.awt.* here!
    4242// The functions in this class should not use any graphical classes. Put your function somewhere else buster!
    43 import java.io.Closeable;
    4443import java.io.*;
    4544import java.net.*;
     
    5049// Don't even think about adding import org.greenstone.gatherer.Gatherer in here!
    5150// The functions in this class should be independent of the Gatherer class. Put your function somewhere else buster!
    52 
     51import org.greenstone.gatherer.util.SafeProcess; // for the closeResource() static method
    5352
    5453/** To provide a library of common methods, in a static context, for use in the Gatherer.
     
    7473    static final public String PERL_EXECUTABLE_WINDOWS = "Perl.exe";
    7574
     75    /** Platform independent NEWLINE character */
     76    public static final String NEWLINE;   
     77   
     78    // NEWLINE related code copied across from GS3 src code
     79    // Before Java 7, no System.lineSeparator() or System.getProperty("line.separator")
     80    // And on local linux, am compiling with JDK 6, so need this.
     81    // http://stackoverflow.com/questions/207947/how-do-i-get-a-platform-dependent-new-line-character
     82    // http://stackoverflow.com/questions/2591083/getting-java-version-at-runtime
     83    // https://www.tutorialspoint.com/java/lang/package_getspecificationversion.htm
     84    // https://docs.oracle.com/javase/tutorial/essential/environment/sysprop.html
     85    // Can initialise static final vars on declaration or in static initialisation code block
     86    // http://stackoverflow.com/questions/2339932/java-can-final-variables-be-initialized-in-static-initialization-block
     87    // Initialise object member final vars on declaration or in constructors
     88    static {   
     89    double java_version = Double.parseDouble(System.getProperty("java.specification.version"));
     90    if(java_version >= 1.7) {
     91        NEWLINE = System.getProperty("line.separator");
     92    } else {
     93        NEWLINE = isWindows() ? "\r\n" : "\n";
     94    }   
     95    }
    7696
    7797    /**
     
    93113        System.err.println("*** Exception occurred: " + e.getMessage());
    94114    } finally {
    95         closeResource(fin);     
     115        SafeProcess.closeResource(fin);     
    96116    }
    97117
     
    575595        System.err.println("Exception occurred: " + e.getMessage());
    576596    } finally {
    577         closeResource(fin);
    578         closeResource(fout);       
     597        SafeProcess.closeResource(fin);
     598        SafeProcess.closeResource(fout);       
    579599       
    580600    }
    581601    return oldValue;
    582602    }
    583 
    584     // For safely closing streams/handles/resources.
    585     // For examples of use look in the Input- and OutputStreamGobbler classes.
    586     // http://docs.oracle.com/javase/tutorial/essential/exceptions/finally.html
    587     // http://stackoverflow.com/questions/481446/throws-exception-in-finally-blocks
    588     public static void closeResource(Closeable resourceHandle) {
    589     try {
    590         if(resourceHandle != null) {
    591         resourceHandle.close();
    592         resourceHandle = null;
    593         }
    594     } catch(Exception e) {
    595         System.err.println("Exception closing resource: " + e.getMessage());
    596         e.printStackTrace();
    597         resourceHandle = null;
    598     }
    599     }
    600 
    601     public static void closeProcess(Process prcs) {
    602     if( prcs != null ) {
    603         closeResource(prcs.getErrorStream());
    604         closeResource(prcs.getOutputStream());
    605         closeResource(prcs.getInputStream());
    606         prcs.destroy();
    607     }
    608     }
    609603}
Note: See TracChangeset for help on using the changeset viewer.