Changeset 31583 for main/trunk/gli

Show
Ignore:
Timestamp:
06.04.2017 20:33:36 (3 years ago)
Author:
ak19
Message:

GS3ServerThread now uses SafeProcess?. And moved the code in Gatherer that stopped the GS3 server in the main thread into the GS3ServerThread as a static stopServer method. Hopefully the static is enough to indicate that it is not part of any specific thread other than the main GLI thread.

Location:
main/trunk/gli/src/org/greenstone/gatherer
Files:
3 modified

Legend:

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

    r30765 r31583  
    766766            //thread.start(); 
    767767 
    768             try { 
    769             String shellCommand = null; 
    770             Process p = null; 
    771             if (Utility.isWindows()) { 
    772                 // cmd /C "cd "C:\path\to\greenstone3" && ant stop" 
    773                 p = Runtime.getRuntime().exec("cmd /C \"cd \"" + Configuration.gsdl3_src_path + File.separator + "\" && ant stop\"");    
    774             } else { 
    775                 p = Runtime.getRuntime().exec(new String[]{"/bin/bash", "-c", "ant stop -f \"" + Configuration.gsdl3_src_path + File.separator + "build.xml\""}); 
    776             } 
    777             // doing a p.waitFor() without processing the Process' IOstreams causes blocking with Java 6  
    778             // (i.e. when JRE 6 included with GS binaries). However, p.waitFor() with Java 7 is fine. 
    779             /*if(p != null && p.waitFor() == 0) { 
    780                 DebugStream.println("********** SUCCESSFULLY stopped THE GS3 SERVER ON EXIT"); 
    781             } 
    782             else { 
    783                 System.err.println("********** FAILED TO SUCCESSFULLY stop THE GS3 SERVER ON EXIT"); 
    784                 //throw new Exception ("Failed to successfully stop the GS3 server on exit."); 
    785             }*/ 
    786             } catch(Exception e) { 
    787             System.err.println("Exception when trying to stop the tomcat web server: " + e); 
    788             DebugStream.printStackTrace(e); 
    789             } 
     768            GS3ServerThread.stopServer(); 
    790769             
    791770        } 
  • main/trunk/gli/src/org/greenstone/gatherer/util/GS3ServerThread.java

    r31582 r31583  
    3737 
    3838package org.greenstone.gatherer.util; 
     39 
     40import org.greenstone.gatherer.Configuration; 
     41import org.greenstone.gatherer.DebugStream; 
     42 
    3943import java.io.File; 
    4044 
     
    5155    } 
    5256     
    53     public void run() 
     57    public void old_run() 
    5458    { 
    5559    try 
     
    8791            // (For normal input and output stream handling using the Gobblers, see FormatConversionDialog.java) 
    8892            SafeProcess.OutputStreamGobbler inputGobbler 
    89                 = new SafeProcess.OutputStreamGobbler(p.getOutputStream(), null);            
     93                = new SafeProcess.OutputStreamGobbler(p.getOutputStream());          
    9094            SafeProcess.InputStreamGobbler errorGobbler 
    9195                = new SafeProcess.InputStreamGobbler(p.getErrorStream(), true); 
     
    116120        } 
    117121    } 
     122 
     123    public void run() 
     124    { 
     125 
     126    ///System.err.println("**** GS3 server : " + _ant_command); 
     127 
     128    SafeProcess p = null; 
     129    if (Utility.isWindows()) { 
     130        if(_ant_command.indexOf("start") != -1) { // running an "ant (re)start" command on windows, run start 
     131        _ant_command = "start"; 
     132        } 
     133         
     134        // The path in quotes, and the entire sequence of commands in quotes as well 
     135        // E.g. the following works in a Runtime.exec() call: 
     136        // cmd /C "cd "C:\path\to\greenstone3" && ant stop" 
     137        // and it preserves any spaces in the path to GSDL3SRCHOME (_gsdl3_src_path). 
     138        p = new SafeProcess("cmd /C \"cd \"" + _gsdl3_src_path + File.separator + "\" && ant " + _ant_command + "\""); 
     139    } 
     140    else { 
     141        if(_ant_command.indexOf("start") != -1) { // if running an "ant (re)start" command on non-Windows, run restart 
     142        _ant_command = "restart"; 
     143        } 
     144        p = new SafeProcess(new String[]{"/bin/bash", "-c", "ant " + _ant_command + " -f \"" + _gsdl3_src_path + File.separator + "build.xml\""}); 
     145    } 
     146     
     147     
     148    // in order for the process.waitFor() method to work with Java 6 (JRE 6 is included in GS binaries) 
     149    // need to make sure the IOstreams of the process are not blocked. For Java 7, this is not necessary 
     150    // and a waitFor() is sufficient. But with Java 6, the waitFor() causes the server to finally start 
     151    // after the user has quit GLI. 
     152    // Process takes no input, but we will still catch the process' instream too 
     153    // And we'll catch the error and output streams to prevent them from blocking during waitFor() 
     154    // (For normal input and output stream handling using the Gobblers, see FormatConversionDialog.java) 
     155 
     156 
     157    // prepare our SafeProcess object 
     158    p.setSplitStdErrorNewLines(true); 
     159 
     160    // run it 
     161    int result = p.runProcess(); // uses default process streamgobbler behaviours and 
     162    // does the important part: waitFor() the process (ant stop or start or re-start) to terminate. 
     163    // The int result returned is the exitvalue upon Process.waitFor() returning 
     164     
     165    if(result != 0) { 
     166        System.err.println("**** Failed to successfully " + _ant_command + " the GS3 server."); 
     167    } 
     168    ///else { 
     169    ///System.err.println("**** " + _ant_command + " of the GS3 server successful."); 
     170    ///} 
     171     
     172    } 
     173 
     174    public static void old_stopServer() { 
     175 
     176    System.err.println("Stopping GS3 Server"); 
     177    try { 
     178        String shellCommand = null; 
     179        Process p = null; 
     180        if (Utility.isWindows()) { 
     181        // cmd /C "cd "C:\path\to\greenstone3" && ant stop" 
     182        p = Runtime.getRuntime().exec("cmd /C \"cd \"" + Configuration.gsdl3_src_path + File.separator + "\" && ant stop\"");    
     183        } else { 
     184        p = Runtime.getRuntime().exec(new String[]{"/bin/bash", "-c", "ant stop -f \"" + Configuration.gsdl3_src_path + File.separator + "build.xml\""}); 
     185        } 
     186        // doing a p.waitFor() without processing the Process' IOstreams causes blocking with Java 6  
     187        // (i.e. when JRE 6 included with GS binaries). However, p.waitFor() with Java 7 is fine. 
     188        /*if(p != null && p.waitFor() == 0) { 
     189          DebugStream.println("********** SUCCESSFULLY stopped THE GS3 SERVER ON EXIT"); 
     190          } 
     191          else { 
     192          System.err.println("********** FAILED TO SUCCESSFULLY stop THE GS3 SERVER ON EXIT"); 
     193          //throw new Exception ("Failed to successfully stop the GS3 server on exit."); 
     194          }*/ 
     195    } catch(Exception e) { 
     196        System.err.println("Exception when trying to stop the tomcat web server: " + e); 
     197        DebugStream.printStackTrace(e); 
     198    } 
     199    } 
     200 
     201    // can't call ant stop from its own thread - what if GLI has exited by then? 
     202    // issue call to ant stop from the main GLI thread 
     203    //GS3ServerThread thread = new GS3ServerThread(Configuration.gsdl_path, "stop"); 
     204    //thread.start(); 
     205    // So, static function to issue the command to stop the server from GLI's own thread. 
     206    // This will block the main GLI thread until the server has stopped. 
     207    public static void stopServer() { 
     208 
     209    SafeProcess p = null; 
     210    if (Utility.isWindows()) { 
     211        // cmd /C "cd "C:\path\to\greenstone3" && ant stop" 
     212        p = new SafeProcess("cmd /C \"cd \"" + Configuration.gsdl3_src_path + File.separator + "\" && ant stop\"");  
     213    } else { 
     214        p = new SafeProcess(new String[]{"/bin/bash", "-c", "ant stop -f \"" + Configuration.gsdl3_src_path + File.separator + "build.xml\""}); 
     215    } 
     216 
     217    System.err.println("Issuing stop command to GS3 Server. Waiting for GS3 server to stop..."); 
     218    int result = p.runProcess(); 
     219    if(result == 0) { 
     220        System.err.println("Successfully stopped GS3 server."); 
     221        //DebugStream.println("********** SUCCESSFULLY stopped THE GS3 SERVER ON EXIT"); 
     222    } 
     223    else { 
     224        System.err.println("********** FAILED TO SUCCESSFULLY stop THE GS3 SERVER ON EXIT"); 
     225        //throw new Exception ("Failed to successfully stop the GS3 server on exit."); 
     226    } 
     227     
     228    // doing a p.waitFor() without processing the Process' IOstreams causes blocking with Java 6  
     229    // (i.e. when JRE 6 included with GS binaries). However, p.waitFor() with Java 7 is fine. 
     230    /*if(p != null && p.waitFor() == 0) { 
     231      DebugStream.println("********** SUCCESSFULLY stopped THE GS3 SERVER ON EXIT"); 
     232      } 
     233      else { 
     234      System.err.println("********** FAILED TO SUCCESSFULLY stop THE GS3 SERVER ON EXIT"); 
     235      //throw new Exception ("Failed to successfully stop the GS3 server on exit."); 
     236      }*/ 
     237     
     238    } 
    118239} 
  • main/trunk/gli/src/org/greenstone/gatherer/util/SafeProcess.java

    r31582 r31583  
    2323    //static Logger logger = Logger.getLogger(org.greenstone.util.SafeProcess.class.getName()); 
    2424 
     25    private String command = null; 
    2526    private String[] command_args = null; 
    2627    private String[] envp = null; 
     
    5152    } 
    5253 
     54    public SafeProcess(String cmdStr) 
     55    { 
     56    command = cmdStr; 
     57    } 
     58 
    5359    // cmd args with env version, launchDir can be null. 
    5460    public SafeProcess(String[] cmd_args, String[] envparams, File launchDir) 
     
    96102 
    97103//***************** Copied from gli's gui/FormatConversionDialog.java *************// 
    98     public void runProcess() { 
     104    public int runProcess() { 
    99105 
    100106    Process prcs = null; 
     
    105111    try {        
    106112        Runtime rt = Runtime.getRuntime();       
    107          
    108         // http://stackoverflow.com/questions/5283444/convert-array-of-strings-into-a-string-in-java 
    109         //System.err.println("Running process: " + Arrays.toString(command_args)); 
    110  
    111         if(this.envp == null) { 
    112         prcs = rt.exec(this.command_args); 
    113         } else { // launch process using cmd str with env params         
    114  
    115         if(this.dir == null) { 
    116             //System.err.println("\twith: " + Arrays.toString(this.envp)); 
    117             prcs = rt.exec(this.command_args, this.envp); 
    118         } else { 
    119             //System.err.println("\tfrom directory: " + this.dir); 
    120             //System.err.println("\twith: " + Arrays.toString(this.envp)); 
    121             prcs = rt.exec(this.command_args, this.envp, this.dir); 
     113        if(this.command != null) { 
     114        prcs = rt.exec(this.command); 
     115        } 
     116        else { // at least command_args must be set now 
     117 
     118        // http://stackoverflow.com/questions/5283444/convert-array-of-strings-into-a-string-in-java 
     119        ///System.err.println("SafeProcess running: " + Arrays.toString(command_args)); 
     120 
     121        if(this.envp == null) {  
     122            prcs = rt.exec(this.command_args); 
     123        } else { // launch process using cmd str with env params         
     124             
     125            if(this.dir == null) { 
     126            System.err.println("\twith: " + Arrays.toString(this.envp)); 
     127            prcs = rt.exec(this.command_args, this.envp); 
     128            } else { 
     129            System.err.println("\tfrom directory: " + this.dir); 
     130            System.err.println("\twith: " + Arrays.toString(this.envp)); 
     131            prcs = rt.exec(this.command_args, this.envp, this.dir); 
     132            } 
    122133        } 
    123134        } 
     
    219230    } 
    220231 
     232    return this.exitValue; 
    221233    } 
    222234     
     
    321333    String inputstr = ""; 
    322334    ExceptionHandler exceptionHandler = null; 
     335 
     336    public OutputStreamGobbler(OutputStream os) { 
     337    this.os = os; 
     338    } 
    323339 
    324340    public OutputStreamGobbler(OutputStream os, String inputstr)