Changeset 31665 for main/trunk


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.

Location:
main/trunk/greenstone3/src/java/org/greenstone
Files:
7 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}
  • main/trunk/greenstone3/src/java/org/greenstone/gsdl3/service/MapRetrieve.java

    r28966 r31665  
    2323import org.greenstone.gsdl3.util.GSXML;
    2424import org.greenstone.gsdl3.util.XMLConverter;
     25import org.greenstone.util.SafeProcess;
    2526
    2627import org.w3c.dom.Document;
     
    459460                    // get the map size
    460461                    String get_size[] = { "identify", "-size", "10000", temp_image_file };
     462                    /*
    461463                    Process proc;
    462464                    proc = Runtime.getRuntime().exec(get_size);
     
    464466                    img_size = br.readLine();
    465467                    proc.waitFor();
     468                    */
     469                    SafeProcess proc = new SafeProcess(get_size);
     470                    proc.runProcess();
     471                    img_size = proc.getStdOutput();
     472                    proc = null;
     473
    466474                    img_size = img_size.substring(img_size.indexOf("JPEG") + 5, img_size.indexOf(" ", img_size.indexOf("JPEG") + 5));
    467475                    width = Integer.parseInt(img_size.substring(0, img_size.indexOf("x")));
     
    541549                    BufferedWriter bw = new BufferedWriter(new FileWriter(this.temp_files_dir + "add_x_" + uid));
    542550                    ;
    543                     Process proc;
     551                    SafeProcess proc;
    544552
    545553                    // if a new search
     
    547555                    {
    548556                        // copy requested map to temp.jpg
    549                         proc = Runtime.getRuntime().exec("cp " + this.files_home_dir + "maps" + File.separator + img_num + ".jpg " + temp_image_file);
    550                         proc.waitFor();
     557                        /*proc = Runtime.getRuntime().exec("cp " + this.files_home_dir + "maps" + File.separator + img_num + ".jpg " + temp_image_file);
     558                          proc.waitFor();*/
     559                        proc = new SafeProcess("cp " + this.files_home_dir + "maps" + File.separator + img_num + ".jpg " + temp_image_file);
     560                        proc.runProcess();
     561                        proc = null;                       
    551562                    }
    552563
    553564                    //get the image size
    554565                    String get_size[] = { "identify", "-size", "10000", temp_image_file };
    555                     proc = Runtime.getRuntime().exec(get_size);
     566                    /*proc = Runtime.getRuntime().exec(get_size);               
    556567                    BufferedReader br = new BufferedReader(new InputStreamReader(proc.getInputStream()));
    557568                    img_size = br.readLine();
    558569                    proc.waitFor();
     570                    */
     571                    proc = new SafeProcess(get_size);
     572                    proc.runProcess();
     573                    img_size = proc.getStdOutput();
     574                    proc = null;
     575
    559576                    img_size = img_size.substring(img_size.indexOf("JPEG") + 5, img_size.indexOf(" ", img_size.indexOf("JPEG") + 5));
    560577                    if (img_size.indexOf("+") != -1)
     
    754771                            buf.close();
    755772                            // execute the command for the legend image
    756                             proc = Runtime.getRuntime().exec("sh " + this.temp_files_dir + "add_l_" + uid);
    757                             proc.waitFor();
     773                            /*proc = Runtime.getRuntime().exec("sh " + this.temp_files_dir + "add_l_" + uid);
     774                              proc.waitFor();*/
     775                            proc = new SafeProcess("sh " + this.temp_files_dir + "add_l_" + uid);
     776                            proc.runProcess();
     777                            proc = null;
    758778                        }
    759779                        inType.close();
     
    763783
    764784                    // execute the convert commands etc.
    765                     proc = Runtime.getRuntime().exec("sh " + this.temp_files_dir + "add_x_" + uid);
     785                    /*proc = Runtime.getRuntime().exec("sh " + this.temp_files_dir + "add_x_" + uid);
    766786                    proc.waitFor();
     787                    */
     788                    proc = new SafeProcess("sh " + this.temp_files_dir + "add_x_" + uid);
     789                    proc.runProcess();
     790                    proc = null;
    767791
    768792                }
  • main/trunk/greenstone3/src/java/org/greenstone/gsdl3/util/GDBMWrapper.java

    r31230 r31665  
    2929import java.util.ArrayList;
    3030
     31import org.greenstone.util.SafeProcess;
     32
    3133/**
    3234 * java wrapper class for gdbm - uses Java-GDBM written by Martin Pool replaces
     
    113115                                String cmdTest = "perl -v 2>&1";
    114116                                //String cmdTest = "echo %PATH%";
    115                                 int returnValue = Processing.runProcess(cmdTest);
     117                               
     118                                //int returnValue = Processing.runProcess(cmdTest);
     119
     120                                // replace Processing.java with SafeProcess.java
     121                                // so retain the same behaviour (logging)
     122                                SafeProcess process = new SafeProcess(cmdTest);
     123                                logger.error("executing command "+cmdTest);
     124                                int returnValue = process.runProcess();
     125                                // do something with the messages
     126                                logger.error("err>"+process.getStdError());
     127                                logger.error("out>"+process.getStdOutput());
     128                                process = null;
     129                               
    116130                                if (returnValue != 0)
    117131                                {
     
    122136
    123137                                String cmd = "perl -S txtgz-to-gdbm.pl \"" + txtgzFilename + "\" \"" + filename + "\"";
    124                                 returnValue = Processing.runProcess(cmd);
     138                                //returnValue = Processing.runProcess(cmd);
     139
     140                                // replace Processing.java with SafeProcess.java
     141                                // so retain the same behaviour (logging)
     142                                process = new SafeProcess(cmd);
     143                                logger.error("executing command "+cmd);
     144                                returnValue = process.runProcess();
     145                                // do something with the messages
     146                                logger.error("err>"+process.getStdError());
     147                                logger.error("out>"+process.getStdOutput());
     148                                process = null;
     149
    125150                                // For some reason, launching this command with gsdl_system() still returns 1
    126151                                // even when it returns 0 when run from the command-line. We can check whether
  • main/trunk/greenstone3/src/java/org/greenstone/server/BaseServer.java

    r24479 r31665  
    8686        if (configure_required_){
    8787        server_control_.displayMessage(dictionary.get("ServerControl.Configuring"));
    88         int state = runTarget(CONFIGURE_CMD);
     88        int state = run(CONFIGURE_CMD);
    8989       
    9090        if (state != RunTarget.SUCCESS){
     
    114114
    115115    protected abstract int runTarget(String cmd);
     116
     117    protected int run(String cmd) {
     118    int result = runTarget(cmd);
     119    server_control_.repaint();
     120    // from Java 7, can just call revalidate() instead of invalidate() and validate():
     121    server_control_.invalidate();
     122    server_control_.validate();
     123    return result;
     124    }
     125
    116126    public abstract String getBrowserURL();
    117127    public abstract void reload(); // reload properties, since they may have changed
     
    136146        if (configure_required_){
    137147        server_control_.displayMessage(dictionary.get("ServerControl.Configuring"));
    138         state = runTarget(CONFIGURE_CMD);
     148        state = run(CONFIGURE_CMD);
    139149       
    140150       if (state != RunTarget.SUCCESS){
     
    153163        logger_.error("Exception trying to sleep: " + e);
    154164    }
    155         state = runTarget(START_CMD);
     165        state = run(START_CMD);
    156166   
    157167    if (state != RunTarget.SUCCESS){
     
    239249        server_control_.displayMessage(dictionary.get("ServerControl.Stopping"));
    240250    }
    241     int state = runTarget(STOP_CMD);
     251    int state = run(STOP_CMD);
    242252   
    243253        if (state != RunTarget.SUCCESS){
  • main/trunk/greenstone3/src/java/org/greenstone/util/BrowserLauncher.java

    r22085 r31665  
    77import org.greenstone.server.BaseProperty;
    88import org.greenstone.util.Misc;
     9import org.greenstone.util.SafeProcess;
    910import org.apache.log4j.*;
    1011
     
    5051         // we try to look for a browser
    5152        for (int i=0; i<default_browsers.length; i++) {
    52         if (isAvailable(default_browsers[i])) {
     53        if (SafeProcess.isAvailable(default_browsers[i])) {
    5354            this.command = default_browsers[i] + " %1";
    5455            break;
     
    6061    }
    6162
     63    // Replaced by SafeProcess.isAvailable(program)
     64    /*
    6265    protected boolean isAvailable(String program) {
    6366    try {
     
    7679        return false;
    7780    }
    78     }
     81    }*/
    7982
    8083    public  int getBrowserState(){
     
    100103        logger.info(new_command);
    101104        Runtime rt = Runtime.getRuntime();
    102         Process process = rt.exec(new_command);
     105        //Process process = rt.exec(new_command);
    103106                state = LAUNCHSUCCESS;
    104         exitCode = process.waitFor();
     107        //exitCode = process.waitFor();
     108        SafeProcess process = new SafeProcess(new_command);
     109        exitCode = process.runProcess();
     110        process = null;
    105111        logger.info("ExitCode:" + exitCode);             
    106112        if (exitCode != 0) { // if Netscape or mozilla was not open
    107113            logger.info("couldn't do remote, trying original command");
    108114                    logger.info(this.command);
    109             process = rt.exec(this.command); // try the original command
     115            //process = rt.exec(this.command); // try the original command
    110116                    state = LAUNCHSUCCESS;
    111117            //for some reason the following part is not executed sometimes.
    112                     exitCode = process.waitFor();
     118                    //exitCode = process.waitFor();
     119            process = new SafeProcess(this.command);
     120            exitCode = process.runProcess();
     121            process = null;
    113122        }
    114123        } else {
    115124        logger.info(this.command);
    116125                Runtime rt = Runtime.getRuntime();
    117         Process process = rt.exec(this.command);
     126        //Process process = rt.exec(this.command);
    118127                state = LAUNCHSUCCESS;
    119128                //for some reason the following part is not executed sometimes.
    120                 exitCode = process.waitFor();
     129                //exitCode = process.waitFor();
     130        SafeProcess process = new SafeProcess(this.command);
     131        exitCode = process.runProcess();
    121132        }
    122133
  • main/trunk/greenstone3/src/java/org/greenstone/util/RunTarget.java

    r29946 r31665  
    33import java.io.IOException;
    44import java.io.BufferedReader;
     5import java.io.Closeable;
    56import java.io.InputStream;
    67import java.io.InputStreamReader;
     
    2829     try {
    2930         state = -1;
    30          Runtime run = Runtime.getRuntime();
    31 
    3231         String targetCmd = getTargetCmd();
    3332         logger.info("Target: " + targetCmd);
    3433
     34         /*
     35         Runtime run = Runtime.getRuntime();
    3536             Process process = run.exec(targetCmd);
    3637         BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
     
    5556         
    5657         br.close();
     58         */
     59         
     60         
     61         SafeProcess process = new SafeProcess(targetCmd);
     62         process.setSplitStdOutputNewLines(true);
     63         process.runProcess();       
     64         String output = process.getStdOutput();
     65         String[] lines = output.split("[\\r\\n]+"); // http://stackoverflow.com/questions/454908/split-java-string-by-new-line
     66         for(int i = 0; i < lines.length; i++) {
     67         //System.err.println("*** Got line:|" + lines[i] + "|***");
     68         String line = lines[i].trim();
     69         if (line.equals(targetSuccess)){
     70             state = 0;
     71         }
     72         
     73         if (line.equals(targetFailed)){
     74             state = 1;
     75         }
    5776
     77         if(line.startsWith(targetFinished)){
     78             break;     
     79         }
     80         }
     81         //System.err.println("\n\n");
     82
     83         
    5884         if(state < 0) {
    5985         logger.info("Unexpected end of input when running target: " + targetCmd);
  • main/trunk/greenstone3/src/java/org/greenstone/util/SafeProcess.java

    r31663 r31665  
    3333
    3434public class SafeProcess {
    35     public static int DEBUG = 0;
     35    public static int DEBUG = 1;
    3636
    3737    public static final int STDERR = 0;
     
    741741    SafeProcess prcs = new SafeProcess("which " + program);     
    742742    prcs.runProcess();
    743     String output = prcs.getStdOutput();
     743    String output = prcs.getStdOutput().trim();
     744    ///System.err.println("*** 'which " + program + "' returned: |" + output + "|");
    744745    if(output.equals("")) {
     746        return false;
     747    } else if(output.indexOf("no "+program) !=-1) { // from GS3's org.greenstone.util.BrowserLauncher.java's isAvailable(program)
     748        log("@@@ SafeProcess.isAvailable(): " + program + "is not available");
    745749        return false;
    746750    }
     
    10101014    logger.info(msg);
    10111015
    1012     //System.err.println(msg);
     1016    System.err.println(msg);
    10131017
    10141018    //DebugStream.println(msg);
     
    10191023    logger.error(msg, e);
    10201024
    1021     //System.err.println(msg);
    1022     //e.printStackTrace();
     1025    System.err.println(msg);
     1026    e.printStackTrace();
    10231027
    10241028    //DebugStream.println(msg);
     
    10301034    logger.error(e);
    10311035
    1032     //e.printStackTrace();
     1036    e.printStackTrace();
    10331037
    10341038    //DebugStream.printStackTrace(e);
Note: See TracChangeset for help on using the changeset viewer.