Changeset 31718 for main


Ignore:
Timestamp:
2017-05-31T18:05:11+12:00 (7 years ago)
Author:
ak19
Message:

Two more instances in GLI's Gatherer.java were using Java's Process instead of going through SafeProcess. I had a note in the tickets about this, but it was obscured by being an item in my todo list that was ticked off, because I hadn't originally intended to change these 2 instances to use SafeProcess, because SafeProcess didn't have a cancel feature back then. With the recent addition of the cancel feature, the changes became straightforward. Both remaining instances in Gatherer have been changed to use SafeProcess and it's tested on Linux. Updated the SafeProcess README.

Location:
main/trunk
Files:
4 edited

Legend:

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

    r31670 r31718  
    14561456    static private class ExternalApplication
    14571457    extends Thread {
    1458         private Process process = null;
     1458        private SafeProcess process = null;
    14591459        /** The initial command string given to this sub-process. */
    14601460        private String command = null;
     
    15151515                    }
    15161516                    DebugStream.println("Running " + whole_command.toString());
    1517                     Runtime rt = Runtime.getRuntime();
    1518                     process = rt.exec(commands);
    1519                     process.waitFor();
     1517                    process = new SafeProcess(commands);
    15201518                }
    15211519                else {
    15221520                    DebugStream.println("Running " + command);
    1523                     Runtime rt = Runtime.getRuntime();
    1524                     process = rt.exec(command);
    1525                     process.waitFor();
    1526                 }
     1521                    process = new SafeProcess(command);
     1522                }
     1523                process.runProcess();
    15271524            }
    15281525            catch (Exception exception) {
     
    15481545        public void stopExternalApplication() {
    15491546            if(process != null) {
    1550                 process.destroy();
     1547                SafeProcess.log("*** stopExternalApplication called.");
     1548                process.cancelRunningProcess();
    15511549            }
    15521550        }
     
    15551553    static private class BrowserApplication
    15561554    extends Thread {
    1557         private Process process = null;
     1555        private SafeProcess process = null;
    15581556        /** The initial command string given to this sub-process. */
    15591557        private String command = null;
     
    15951593                    printArray(new_commands);
    15961594
    1597                     Runtime rt = Runtime.getRuntime();
    1598                     process = rt.exec(new_commands);
    1599                     int exitCode = process.waitFor();
     1595                    process = new SafeProcess(new_commands);
     1596                    int exitCode = process.runProcess();
    16001597                    if (exitCode != 0) { // if Netscape or mozilla was not open
    16011598                        DebugStream.println("couldn't do remote, trying original command");
    16021599                        printArray(commands);
    1603                         process = rt.exec(commands); // try the original command
     1600                        process = null;
     1601                        process = new SafeProcess(commands); // try the original command
     1602                        process.runProcess();
    16041603                    }
    16051604                } else {
     
    16111610                    }
    16121611                    DebugStream.println("Running " + whole_command.toString());
    1613                     Runtime rt = Runtime.getRuntime();
    1614                     process = rt.exec(commands);
    1615                     process.waitFor();
     1612                    process = new SafeProcess(commands);
     1613                    process.runProcess();
    16161614                }
    16171615            }
     
    16351633        public void stopBrowserApplication() {
    16361634            if(process != null) {
    1637                 process.destroy();
     1635                SafeProcess.log("*** stopBrowserApplication called.");
     1636                process.cancelRunningProcess();
    16381637            }
    16391638        }
  • main/trunk/gli/src/org/greenstone/gatherer/util/Readme_Using_SafeProcess.txt

    r31717 r31718  
    163163
    164164
     165All the runProcess() variants do a Process.waitFor(). This means all runProcess() variants block the primary thread, which is the thread in which they're executing, until the internal Process has completed.
    165166
    166167If you want to completely override the default behaviour of any of SafeProcess' iostream related worker threads (such as if you want to read a char at a time from the stderr stream and do something, instead of the default behaviour of reading a line at a time from it), then call this method.
     
    756757    + src/org/greenstone/gatherer/gui/DownloadPane.java
    757758    + src/org/greenstone/gatherer/util/GS3ServerThread.java
    758     + 2 src/org/greenstone/gatherer/Gatherer.java [2 MORE but don't want to mess with those]
     759    + 4 src/org/greenstone/gatherer/Gatherer.java [All 4 instances that used Process now go through SafeProcess instead]
    759760    + 1 src/org/greenstone/gatherer/download/ServerInfoDialog.java
    760761    + 2 src/org/greenstone/gatherer/greenstone/Classifiers.java
  • main/trunk/gli/src/org/greenstone/gatherer/util/SafeProcess.java

    r31716 r31718  
    162162     * This method returns a boolean that you can call sentInterrupt. 
    163163     */
    164     public boolean cancelRunningProcess() { 
     164    public boolean cancelRunningProcess() {
    165165
    166166    boolean forceWaitUntilInterruptible = true;
     
    921921    */
    922922   
    923     if(pid == -1) {
     923    if(pid == -1) { // if the process has already terminated, or we can't get the pid for any reason:
    924924        p.destroy(); // at minimum. Will have no effect if the process had already terminated
    925925    } else {
     
    929929        if(!killUnixProcessWithID(pid, !forceKill, killEntireProcessTree)) { // send sig TERM (kill -15 or kill -TERM)
    930930        killUnixProcessWithID(pid, forceKill, killEntireProcessTree); // send sig KILL (kill -9 or kill -KILL)
    931         }       
     931        }
     932        // if both kill commands failed for whatever reason, can still at least end the top level process:
     933        p.destroy(); // no effect if the process has already terminated.
    932934    }
    933935   
  • main/trunk/greenstone3/src/java/org/greenstone/util/SafeProcess.java

    r31716 r31718  
    163163     * This method returns a boolean that you can call sentInterrupt. 
    164164     */
    165     public boolean cancelRunningProcess() { 
     165    public boolean cancelRunningProcess() {
    166166
    167167    boolean forceWaitUntilInterruptible = true;
     
    922922    */
    923923   
    924     if(pid == -1) {
     924    if(pid == -1) { // if the process has already terminated, or we can't get the pid for any reason:
    925925        p.destroy(); // at minimum. Will have no effect if the process had already terminated
    926926    } else {
     
    930930        if(!killUnixProcessWithID(pid, !forceKill, killEntireProcessTree)) { // send sig TERM (kill -15 or kill -TERM)
    931931        killUnixProcessWithID(pid, forceKill, killEntireProcessTree); // send sig KILL (kill -9 or kill -KILL)
    932         }       
     932        }
     933        // if both kill commands failed for whatever reason, can still at least end the top level process:
     934        p.destroy(); // no effect if the process has already terminated.   
    933935    }
    934936   
Note: See TracChangeset for help on using the changeset viewer.