Changeset 31636


Ignore:
Timestamp:
04/21/17 21:09:08 (4 years ago)
Author:
ak19
Message:

First phase of shifting gli code to use SafeProcess instead of Java Process. This phase takes care of all the easy cases.

Location:
main/trunk/gli/src/org/greenstone/gatherer
Files:
5 edited

Legend:

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

    r31583 r31636  
    6060import org.greenstone.gatherer.util.GS3ServerThread;
    6161import org.greenstone.gatherer.util.JarTools;
     62import org.greenstone.gatherer.util.SafeProcess;
    6263import org.greenstone.gatherer.util.StaticStrings;
    6364import org.greenstone.gatherer.util.Utility;
     
    14331434
    14341435   
    1435     /** This private class contains an instance of an external application running within a JVM shell. It is important that this process sits in its own thread, but its more important that when we exit the Gatherer we don't actually System.exit(0) the Gatherer object until the user has volunteerily ended all of these child processes. Otherwise when we quit the Gatherer any changes the users may have made in external programs will be lost and the child processes are automatically deallocated. */
     1436    /** This private class contains an instance of an external application running within a JVM shell. It is important that this process sits in its own thread, but its more important that when we exit the Gatherer we don't actually System.exit(0) the Gatherer object until the user has voluntarily ended all of these child processes. Otherwise when we quit the Gatherer any changes the users may have made in external programs will be lost and the child processes are automatically deallocated. */
    14361437    static private class ExternalApplication
    14371438    extends Thread {
     
    16291630            boolean found = false;
    16301631
    1631             try {
     1632           
    16321633                // run the command `/path/to/perl -S gs-magick.pl identify -version`
    16331634                ArrayList cmd_list = new ArrayList();
     
    16561657                }
    16571658                DebugStream.println("***** Running ImageMagickTest command: " + cmd_str);
    1658 
    1659                 Process image_magick_process = Runtime.getRuntime().exec(command_parts);
    1660                 image_magick_process.waitFor();
    1661 
    1662                 //new way of detection of ImageMagick
    1663                 InputStreamReader isr = new InputStreamReader(image_magick_process.getInputStream());
    1664 
    1665                 BufferedReader br = new BufferedReader(isr);
    1666                 // Capture the standard output stream and seach for two particular occurrences: Version and ImageMagick.
    1667 
    1668                 String line = br.readLine();
    1669                 if (line != null) {
    1670                     String lc_line = line.toLowerCase();
    1671                     if (lc_line.indexOf("version") != -1 || lc_line.indexOf("imagemagick") != -1) {
    1672                     //System.err.println("*** ImageMagickTest Line: " + line);
    1673                     found = true;
    1674                     } // else found var remains false
    1675                 }
    1676 
    1677                 // Maybe put the close in a finally (but note that it can throw and IOex too)? See
    1678                 // http://download.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html
    1679                 br.close();
    1680                 return found;
    1681                 //return (image_magick_process.exitValue() == 0);
    1682             }
    1683             catch (Exception exception) {   
    1684                 exception.printStackTrace();
     1659                System.err.println("***** Running ImageMagickTest command: " + cmd_str);
     1660               
     1661                SafeProcess image_magick_process = new SafeProcess(command_parts);
     1662                int exitValue = image_magick_process.runProcess(); // default process iostream handling
     1663                //new way of detection of ImageMagick
     1664                // Inspect the standard output of the process and seach for two particular occurrences: Version and ImageMagick.
     1665                String output = image_magick_process.getStdOutput().toLowerCase();
     1666                if (output.indexOf("version") != -1 || output.indexOf("imagemagick") != -1) {
     1667                System.err.println("*** ImageMagickTest output: " + output);
     1668                found = true;
     1669                } // else found var remains false   
     1670               
    16851671                return found;
    1686             }
     1672                //return (image_magick_process.exitValue() == 0);
     1673               
    16871674        }
    16881675    }
     
    17021689        {
    17031690            try {
    1704                 Process perl_process = Runtime.getRuntime().exec(command);
    1705                 perl_process.waitFor();
    1706                 return (perl_process.exitValue() == 0);
     1691                SafeProcess perl_process = new SafeProcess(command);
     1692                int exitValue = perl_process.runBasicProcess();
     1693                return (exitValue == 0);
    17071694            }
    17081695            catch (Exception exception) {
  • main/trunk/gli/src/org/greenstone/gatherer/collection/ScriptOptions.java

    r20924 r31636  
    1111import org.greenstone.gatherer.util.ArrayTools;
    1212import org.greenstone.gatherer.util.Codec;
     13import org.greenstone.gatherer.util.SafeProcess;
    1314import org.greenstone.gatherer.util.Utility;
    1415import org.greenstone.gatherer.util.XMLTools;
     
    238239        args[5] = lang;
    239240
    240 
    241241        // Create the process.
    242         Runtime runtime = Runtime.getRuntime();
    243         Process process = runtime.exec(args);
    244 
    245        
     242        SafeProcess process = new SafeProcess(args);
     243
    246244        //for (int i=0; i<args.length; i++) {
    247245        //   System.err.print(args[i] + " ");
     
    249247        //System.err.println("");
    250248       
    251 
    252 
    253         document = XMLTools.parseXML(process.getErrorStream());
     249        // run the SafeProcess
     250        int exitVal = process.runProcess();
     251        if(exitVal != 0) {
     252            throw new Exception("*** Error running ScriptOptions process, process exited with: "
     253                    + exitVal);
     254        }       
    254255       
     256        // get the result and process it.
     257        // We expect XML to have come out of the process std error stream.
     258        String errStreamOutput = process.getStdError();
     259        ///System.err.println("*********\nScriptOptions data, got:\n" + errStreamOutput + "\n**********\n");
     260        StringReader xmlStrReader = new StringReader(errStreamOutput);
     261        document = XMLTools.parseXML(xmlStrReader);
     262        xmlStrReader.close();
     263
     264
    255265        if (document == null) {
    256266            // command has not generated XML, script has probably failed in some way
  • main/trunk/gli/src/org/greenstone/gatherer/download/ServerInfoDialog.java

    r31629 r31636  
    1515import org.greenstone.gatherer.greenstone.LocalGreenstone;
    1616import org.greenstone.gatherer.gui.*;
     17import org.greenstone.gatherer.util.SafeProcess;
    1718import org.greenstone.gatherer.util.Utility;
    1819
     
    105106    try {
    106107        String [] env = null;
    107         Process prcs = null;
    108         Runtime rt = Runtime.getRuntime();
     108        SafeProcess prcs = null;
    109109     
    110110        if (Utility.isWindows()) {
    111                 prcs = rt.exec(command);   
     111        prcs = new SafeProcess(command);
    112112        }
    113113        else {
     
    121121            env[2] = "GSDLHOME=" + Configuration.gsdl_path;
    122122            env[3] = "GSDLOS=" + Gatherer.client_operating_system;
    123             prcs = rt.exec(command, env);
     123            prcs = new SafeProcess(command, env, null);
    124124        }
    125125        else {
    126126            // Will inherit the GLI's environment, with GSDLHOME and GSDLOS set
    127             prcs = rt.exec(command);
     127            prcs = new SafeProcess(command);
    128128        }
    129129        }
    130 
    131         InputStreamReader isr = new InputStreamReader(prcs.getErrorStream());
    132         BufferedReader br = new BufferedReader(isr);
    133         String line;
    134         // Capture the standard error stream and seach for two particular occurences.
    135         while ((line = br.readLine()) != null && !line.trim().equals("<<Finished>>")) {
    136          
    137         JLabel a_label = new JLabel(line);
    138         a_label.setAlignmentX(Component.LEFT_ALIGNMENT);
    139         info_list_pane.add(a_label);
    140         }
     130       
     131        // special processing of Process' stderr stream:
     132        // Search the process standard error stream and seach for two particular occurences.
     133        int exitVal = prcs.runProcess(null, new ProcErrStreamLineByLineHandler());
    141134       
    142135    } catch (Exception ioe) {
     
    146139    }
    147140
     141    private class ProcErrStreamLineByLineHandler extends SafeProcess.LineByLineHandler
     142    {
     143    public ProcErrStreamLineByLineHandler() {
     144        super(SafeProcess.STDERR); // sets this.source
     145    }
     146
     147    public void gotLine(String line) {
     148        // Check the current line from the standard error stream and seach for two particular occurences.
     149        if (!line.trim().equals("<<Finished>>")) {
     150         
     151        JLabel a_label = new JLabel(line);
     152        a_label.setAlignmentX(Component.LEFT_ALIGNMENT);
     153        ServerInfoDialog.this.info_list_pane.add(a_label);
     154        }
     155    }
     156    public void gotException(Exception e) {
     157        e.printStackTrace();
     158    }
     159    }
    148160}
  • main/trunk/gli/src/org/greenstone/gatherer/greenstone/Classifiers.java

    r20924 r31636  
    3737import org.greenstone.gatherer.cdm.Classifier;
    3838import org.greenstone.gatherer.remote.RemoteGreenstoneServer;
     39import org.greenstone.gatherer.util.SafeProcess;
    3940import org.greenstone.gatherer.util.StaticStrings;
    4041import org.greenstone.gatherer.util.Utility;
     
    150151        args.add(classifier.getName());
    151152
    152         // Run the classinfo.pl process
    153         Runtime runtime = Runtime.getRuntime();
    154         Process process = runtime.exec((String[]) args.toArray(new String[] { }));
    155         InputStream input_stream = process.getErrorStream();
    156         StringBuffer classinfo_xml_buffer = XMLTools.readXMLStream(input_stream);
    157         if (classinfo_xml_buffer != null) {
    158             classinfo_xml = classinfo_xml_buffer.toString();
    159         }
     153
     154        // Run the classinfo.pl process:
     155        // Create the process.
     156        SafeProcess process = new SafeProcess((String[]) args.toArray(new String[] { }));
     157
     158        // run the SafeProcess
     159        int exitVal = process.runProcess();
     160        if(exitVal != 0) {
     161            throw new Exception("*** Error running classify.pl loadClassifierInfo, process exited with: "
     162                    + exitVal);
     163        }
     164        // get the result: We expect XML to have come out of the process std error stream.
     165        classinfo_xml = process.getStdError();
     166        ///System.err.println("*********\nClassifierInfo, got:\n" + classinfo_xml + "\n**********\n");
    160167        }
    161168
     
    208215        args.add("-xml");
    209216
    210         // Run the classinfo.pl process
    211         Runtime runtime = Runtime.getRuntime();
    212         Process process = runtime.exec((String[]) args.toArray(new String[] { }));
    213         InputStream input_stream = process.getErrorStream();
    214         xml = XMLTools.readXMLStream(input_stream);
     217        // Run the classinfo.pl process:
     218        // Create the process.
     219        SafeProcess process = new SafeProcess((String[]) args.toArray(new String[] { }));
     220
     221        // run the SafeProcess
     222        int exitVal = process.runProcess();
     223        if(exitVal != 0) {
     224            throw new Exception("*** Error running classify.pl loadClassifiersList, process exited with: "
     225                    + exitVal);
     226        }
     227        // get the result: We expect XML to have come out of the process std error stream.
     228        xml = new StringBuffer(process.getStdError());
     229        ///System.err.println("*********\nClassifierList, got:\n" + xml + "\n**********\n");
    215230        }
    216231
  • main/trunk/gli/src/org/greenstone/gatherer/greenstone/Plugins.java

    r26225 r31636  
    3737import org.greenstone.gatherer.cdm.Plugin;
    3838import org.greenstone.gatherer.remote.RemoteGreenstoneServer;
     39import org.greenstone.gatherer.util.SafeProcess;
    3940import org.greenstone.gatherer.util.StaticStrings;
    4041import org.greenstone.gatherer.util.Utility;
     
    160161        args.add(Configuration.getLanguage());
    161162        args.add(plugin.getName());
    162         // Run the pluginfo.pl process
    163         Runtime runtime = Runtime.getRuntime();
    164         Process process = runtime.exec((String[]) args.toArray(new String[] { }));
    165         InputStream input_stream = process.getErrorStream();
    166         StringBuffer pluginfo_xml_buffer = XMLTools.readXMLStream(input_stream);
    167         if (pluginfo_xml_buffer != null) {
    168             pluginfo_xml = pluginfo_xml_buffer.toString();
    169         }
     163
     164        // Run the pluginfo.pl process:
     165        // Create the process.
     166        SafeProcess process = new SafeProcess((String[]) args.toArray(new String[] { }));
     167
     168        // run the SafeProcess
     169        int exitVal = process.runProcess();
     170        if(exitVal != 0) {
     171            throw new Exception("*** Error running pluginfo.pl loadPlugInfo, process exited with: "
     172                    + exitVal);
     173        }
     174        // get the result: We expect XML to have come out of the process std error stream.
     175        pluginfo_xml = process.getStdError();
     176        ///System.err.println("*********\nPluginInfo, got:\n" + pluginfo_xml + "\n**********\n");
    170177        }
    171178
     
    230237        args.add("-xml");
    231238
    232         // Run the pluginfo.pl process
    233         Runtime runtime = Runtime.getRuntime();
    234         Process process = runtime.exec((String[]) args.toArray(new String[] { }));
    235         InputStream input_stream = process.getErrorStream();
    236         xml = XMLTools.readXMLStream(input_stream);
     239        // Run the pluginfo.pl process:
     240        // Create the process.
     241        SafeProcess process = new SafeProcess((String[]) args.toArray(new String[] { }));
     242
     243        // run the SafeProcess
     244        int exitVal = process.runProcess();
     245        if(exitVal != 0) {
     246            throw new Exception("*** Error running pluginfo.pl loadPluginsList, process exited with: "
     247                    + exitVal);
     248        }
     249        // get the result: We expect XML to have come out of the process std error stream.
     250        xml = new StringBuffer(process.getStdError());
     251        ///System.err.println("*********\nPluginsList, got:\n" + xml + "\n**********\n");
    237252        }
    238253
Note: See TracChangeset for help on using the changeset viewer.