Changeset 31630


Ignore:
Timestamp:
2017-04-21T19:59:03+12:00 (7 years ago)
Author:
ak19
Message:

Further changes to SafeProcess to turn some interfaces to abstract classes.

File:
1 edited

Legend:

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

    r31620 r31630  
    2121public class SafeProcess {
    2222
     23    public static final int STDERR = 0;
     24    public static final int STDOUT = 1;
     25    public static final int STDIN = 2;   
     26
    2327    ///static Logger logger = Logger.getLogger(org.greenstone.util.SafeProcess.class.getName());
    2428
     
    9599   
    96100    if(this.command != null) {
     101        //logger.info("SafeProcess running: " + command);
     102        System.err.println("SafeProcess running: " + command_args);
    97103        prcs = rt.exec(this.command);
    98104    }
     
    204210    }
    205211
    206     // Runs a process with default stream processing
     212    // Runs a process with default stream processing. Returns the exitValue
    207213    public int runProcess() {
    208214    return runProcess(null, null, null); // use default processing of all 3 of the process' iostreams
     
    210216
    211217    // Run a process with custom stream processing (any custom handlers passed in that are null
    212     // will use the default stream processing)
     218    // will use the default stream processing).
     219    // Returns the exitValue from running the Process
    213220    public int runProcess(CustomProcessHandler procInHandler,
    214221               CustomProcessHandler procOutHandler,
     
    239246        if(procErrHandler == null) {
    240247        errorGobbler // ReaderFromProcessOutputStream
    241             = new SafeProcess.InputStreamGobbler(prcs.getErrorStream(), splitStdErrorNewLines);     
     248            = new SafeProcess.InputStreamGobbler(prcs.getErrorStream(), splitStdErrorNewLines);
    242249        } else {
    243250        errorGobbler
     
    414421
    415422
    416 //******************** Inner class interface definitions ********************//
     423//******************** Inner class and interface definitions ********************//
    417424// Static inner classes can be instantiated without having to instantiate an object of the outer class first
    418425
     
    423430public static interface ExceptionHandler {
    424431
    425     // SHOULD I DECLARE THIS SYNCHRONIZED?
    426     // It ends up being thread safe for the particular instance I'm using it for, but that doesn't
    427     // make it future proof...
    428     public void gotException(Exception e);
     432    // when implementing ExceptionHandler.gotException(), if it manipulates anything that's
     433    // not threadsafe, declare gotException() as a synchronized method to ensure thread safety
     434    public void gotException(Exception e); // can't declare as synchronized in interface method declaration
    429435}
    430436
    431437// Write your own run() body for any StreamGobbler. You need to create an instance of a class
    432 // implementing CustomProcessHandler for EACH IOSTREAM of the process that you want to handle.
     438// extending CustomProcessHandler for EACH IOSTREAM of the process that you want to handle.
    433439// Do not create a single CustomProcessHandler instance and reuse it for all three streams,
    434440// i.e. don't call SafeProcess' runProcess(x, x, x); It should be runProcess(x, y, z).
    435441// Make sure your implementation is threadsafe if you're sharing immutable objects between the threaded streams
    436442// example implementation is in the GS2PerlConstructor.SynchronizedProcessHandler class.
    437 public static interface CustomProcessHandler {
    438     public void run(Closeable stream); //InputStream or OutputStream
     443// CustomProcessHandler is made an abstract class instead of an interface to force classes that want
     444// to use a CustomProcessHandler to create a separate class that extends CustomProcessHandler, rather than
     445// that the classes that wish to use it "implementing" the CustomProcessHandler interface itself: the
     446// CustomProcessHandler.run() method may then be called in the major thread from which the Process is being
     447// executed, rather than from the individual threads that deal with each iostream of the Process.
     448public static abstract class CustomProcessHandler {
     449
     450    protected final int source;
     451
     452    protected CustomProcessHandler(int src) {
     453    this.source = src; // STDERR or STDOUT or STDIN
     454    }
     455   
     456    public abstract void run(Closeable stream); //InputStream or OutputStream
    439457}
    440458
    441 // When using the default stream processing to read from a process' stdout or stderr stream,
    442 // you can create a LineByLineHandler for the process' err and out streams
     459// When using the default stream processing to read from a process' stdout or stderr stream, you can
     460// create a class extending LineByLineHandler for the process' err stream and one for its output stream
    443461// to do something on a line by line basis, such as sending the line to a log
    444 public static interface LineByLineHandler {
    445     public void gotLine(String line);
    446     public void gotException(Exception e); // for when an exception occurs instead of getting a line
     462public static abstract class LineByLineHandler {
     463    protected final int source;
     464
     465    protected LineByLineHandler(int src) {
     466    this.source = src; // STDERR or STDOUT
     467    }
     468
     469    public abstract void gotLine(String line); // first non-null line
     470    public abstract void gotException(Exception e); // for when an exception occurs instead of getting a line
    447471}
     472
    448473
    449474//**************** StreamGobbler Inner class definitions (stream gobblers copied from GLI) **********//
Note: See TracChangeset for help on using the changeset viewer.