Ignore:
Timestamp:
2017-05-08T21:18:21+12:00 (7 years ago)
Author:
ak19
Message:

GS3 source code now updated to use SafeProcess instead of Process (calling Runtime.exec() directly). The use of SafeProcess in RunTarget and BrowserLauncher has been tested on Linux. GDBMWrapper, MapRetrieve and admin/guiext's Command.java are not tested. For GDBMWrapper, because it's in a bit of code that works with txtgz databases. MapRetrive.java and Command.java are untested because I don't know how to test them.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone3/src/java/org/greenstone/admin/guiext/Command.java

    r25635 r31665  
    1212import javax.swing.JTextArea;
    1313import javax.swing.JOptionPane;
     14
     15import org.greenstone.util.SafeProcess;
    1416
    1517public class Command implements Runnable
     
    5759       
    5860    File workingDirectory = new File(_parent.getParent().getParent().getExtensionDirectory());
    59     Process commandLineProc = null;
     61    SafeProcess commandLineProc = null;
    6062    try{
    6163        commandLineProc = null;
     64        String[] args = new String[3];
    6265       
    6366        if(System.getProperty("os.name").contains("Windows")){
    64         String[] args = new String[3];
    6567        args[0] = "cmd.exe";
    6668        args[1] = "/C";
     
    7577        messageArea.append("\nExecuting \"" + allArgs + "\" on the command line\n");
    7678
    77         commandLineProc = Runtime.getRuntime().exec(args, null, workingDirectory);
     79        //commandLineProc = Runtime.getRuntime().exec(args, null, workingDirectory);
    7880        }
    7981        else{
    80         String[] args = new String[3];
    8182        args[0] = "sh";
    8283        args[1] = "-c";
     
    9091
    9192        messageArea.append("\nExecuting \"" + allArgs + "\" on the command line\n");
    92         commandLineProc = Runtime.getRuntime().exec(args, null, workingDirectory);
    93         }
    94        
     93        //commandLineProc = Runtime.getRuntime().exec(args, null, workingDirectory);
     94        }
     95
     96        commandLineProc = new SafeProcess(args, null, workingDirectory);
     97
     98        /*
    9599        BufferedReader stdInput = new BufferedReader(new InputStreamReader(commandLineProc.getInputStream()));
    96100
     
    104108       
    105109        int success = commandLineProc.waitFor();
    106        
     110        */
     111
     112        // Replacing the above and its use of the PrinterThread inner class with SafeProcess.java:
     113        SafeProcess.LineByLineHandler outLineHandler = new ProcessLineHandler(messageArea, SafeProcess.STDOUT);
     114        SafeProcess.LineByLineHandler errLineHandler = new ProcessLineHandler(messageArea, SafeProcess.STDERR);         
     115
     116        int success = commandLineProc.runProcess(outLineHandler, errLineHandler);
     117
    107118        if(success != 0){
    108119        System.err.println("Command line process \"" + command  + "\" returned unsuccessfully with the value \"" + success + "\"");
     
    148159    }
    149160
     161    /*
    150162    public class PrinterThread extends Thread
    151163    {
     
    173185        }
    174186    }
     187    }*/
     188
     189    public class ProcessLineHandler extends SafeProcess.LineByLineHandler
     190    {
     191    // These members need to be final in order to synchronize on them
     192    private final JTextArea _messageArea;
     193
     194    public ProcessLineHandler(JTextArea messageArea, int src)
     195    {
     196        super(src); // will set this.source to STDERR or STDOUT
     197        _messageArea = messageArea;
     198    }
     199
     200    public void gotLine(String line) { // first non-null line
     201
     202        // messageArea needs to be synchronized, since both the process'
     203        // stderr and stdout will be attempting to append to it
     204
     205        synchronized(_messageArea) {
     206        _messageArea.append(line + "\n");
     207        _messageArea.setSelectionEnd(_messageArea.getDocument().getLength());
     208        }
     209    }
     210    public void gotException(Exception e) {
     211        e.printStackTrace();
     212    }
     213
    175214    }
    176215}
Note: See TracChangeset for help on using the changeset viewer.