Ignore:
Timestamp:
2017-04-06T19:15:55+12:00 (7 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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.