Ignore:
Timestamp:
2003-05-27T15:40:47+12:00 (21 years ago)
Author:
mdewsnip
Message:

Fixed tabbing.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/gli/src/org/greenstone/gatherer/shell/GShell.java

    r4317 r4364  
    4848 */
    4949public class GShell
    50     extends Thread {
    51     /** A flag used to determine if this process has been asked to cancel. */
    52     private boolean cancel = false;
    53     /** The list of listeners associated with this class. */
    54     private EventListenerList listeners = null;
    55     /** The current status of this shell process. */
    56     private int status = -1;
    57     /** The type of message being sent. */
    58     private int msg_type = -1;
    59     /** The type of shell process. */
    60     private int type = -1;
    61     /** The caller of this process, and thus the class most interested in messages. */
    62     private GShellListener caller = null;
    63     /** The progress monitor associated with this process. */
    64     private GShellProgressMonitor progress = null;
    65     /** Arguments to be given to the process (including the executable you are calling. */
    66     private String args[] = null;
    67     /** Element in process type enumeration. */
    68     static final public int BUILD = 0;
    69     /** Element in process type enumeration. */
    70     static final public int IMPORT = 1;
    71     /** Element in process type enumeration. */
    72     static final public int NEW = 2;
    73     /** Element in process type enumeration. */
    74     static final public int OTHER = 3;
    75     /** Element in status type enumeration. */
    76     static final public int ERROR = 0;
    77     /** Element in status type enumeration. */
    78     static final public int OK = 1;
    79     /** Element in process type name enumeration. */
    80     static public String GSHELL_BUILD = "gshell_build";
    81     /** Element in process type name enumeration. */
    82     static public String GSHELL_IMPORT = "gshell_import";
    83     /** Element in process type name enumeration. */
    84     static public String GSHELL_NEW = "gshell_new";
    85     /** Constructor gatherer all the data required to create a new process, and emit meaningfull messages.
    86       * @param args A <strong>String[]</strong> containing the arguments to the process thread, including the name of the executable.
    87       * @param type An <strong>int</strong> that indicates what group of processes this process belongs to, as some are treated slightly differently (i.e an IMPORT type process is always followed by a BUILD one).
    88       * @param msg_type As process threads may be background (like a makecol.pl call) or important processes in their own right (such as an IMPORT-BUILD) we must be able to set what level messages posted by this class will have by usings this <strong>int</strong>.
    89       * @param caller The default <i>GShellListener</i> that is interested in the progress of this process.
    90       * @param progress The <i>GShellProgressMonitor</i> associated with this process.
    91       * @param name A <strong>String</strong> identifier given to the process, for convience and debug reasons.
    92       */
    93     public GShell(String args[], int type, int msg_type, GShellListener caller, GShellProgressMonitor progress, String name) {
    94           super(name);
    95           this.args = args;
    96           this.msg_type = msg_type;
    97           this.type = type;
    98           this.caller = caller;
    99           this.progress = progress;
    100           this.status = 0;
    101           // Lower this jobs priority
    102           this.setPriority(Thread.MIN_PRIORITY);
    103           listeners = new EventListenerList();
    104           listeners.add(GShellListener.class, caller);
    105     }
    106     /** This method adds another shell listener to this process.
     50    extends Thread {
     51    /** A flag used to determine if this process has been asked to cancel. */
     52    private boolean cancel = false;
     53    /** The list of listeners associated with this class. */
     54    private EventListenerList listeners = null;
     55    /** The current status of this shell process. */
     56    private int status = -1;
     57    /** The type of message being sent. */
     58    private int msg_type = -1;
     59    /** The type of shell process. */
     60    private int type = -1;
     61    /** The caller of this process, and thus the class most interested in messages. */
     62    private GShellListener caller = null;
     63    /** The progress monitor associated with this process. */
     64    private GShellProgressMonitor progress = null;
     65    /** Arguments to be given to the process (including the executable you are calling. */
     66    private String args[] = null;
     67    /** Element in process type enumeration. */
     68    static final public int BUILD = 0;
     69    /** Element in process type enumeration. */
     70    static final public int IMPORT = 1;
     71    /** Element in process type enumeration. */
     72    static final public int NEW = 2;
     73    /** Element in process type enumeration. */
     74    static final public int OTHER = 3;
     75    /** Element in status type enumeration. */
     76    static final public int ERROR = 0;
     77    /** Element in status type enumeration. */
     78    static final public int OK = 1;
     79    /** Element in process type name enumeration. */
     80    static public String GSHELL_BUILD = "gshell_build";
     81    /** Element in process type name enumeration. */
     82    static public String GSHELL_IMPORT = "gshell_import";
     83    /** Element in process type name enumeration. */
     84    static public String GSHELL_NEW = "gshell_new";
     85    /** Constructor gatherer all the data required to create a new process, and emit meaningfull messages.
     86     * @param args A <strong>String[]</strong> containing the arguments to the process thread, including the name of the executable.
     87     * @param type An <strong>int</strong> that indicates what group of processes this process belongs to, as some are treated slightly differently (i.e an IMPORT type process is always followed by a BUILD one).
     88     * @param msg_type As process threads may be background (like a makecol.pl call) or important processes in their own right (such as an IMPORT-BUILD) we must be able to set what level messages posted by this class will have by usings this <strong>int</strong>.
     89     * @param caller The default <i>GShellListener</i> that is interested in the progress of this process.
     90     * @param progress The <i>GShellProgressMonitor</i> associated with this process.
     91     * @param name A <strong>String</strong> identifier given to the process, for convience and debug reasons.
     92     */
     93    public GShell(String args[], int type, int msg_type, GShellListener caller, GShellProgressMonitor progress, String name) {
     94    super(name);
     95    this.args = args;
     96    this.msg_type = msg_type;
     97    this.type = type;
     98    this.caller = caller;
     99    this.progress = progress;
     100    this.status = 0;
     101    // Lower this jobs priority
     102    this.setPriority(Thread.MIN_PRIORITY);
     103    listeners = new EventListenerList();
     104    listeners.add(GShellListener.class, caller);
     105    }
     106    /** This method adds another shell listener to this process.
    107107      * @param listener The new <i>GShellListener</i>.
    108108      */
    109     public void addGShellListener(GShellListener listener) {
    110           listeners.add(GShellListener.class, listener);
    111     }
    112     /** This method removes a certain shell listener from this process.
     109    public void addGShellListener(GShellListener listener) {
     110    listeners.add(GShellListener.class, listener);
     111    }
     112    /** This method removes a certain shell listener from this process.
    113113      * @param listener The <i>GShellListener</i> to be removed.
    114114      */
    115     public void removeGShellListener(GShellListener listener) {
    116           listeners.remove(GShellListener.class, listener);
    117     }
    118     /** Any threaded class must include this method to allow the thread body to be run.
     115    public void removeGShellListener(GShellListener listener) {
     116    listeners.remove(GShellListener.class, listener);
     117    }
     118    /** Any threaded class must include this method to allow the thread body to be run.
    119119      */
    120120    public void run() {
    121           // Setup
    122           if(progress != null) {
    123                 progress.start();
    124           }
    125           // Determine if the user has asked for an outfile.
    126           String out_name = null;
    127           FileOutputStream fos = null;
    128           BufferedOutputStream bos = null;
    129           if(type == IMPORT || type == BUILD) {
    130                 if(type == IMPORT) {
    131                      out_name = (String) Gatherer.c_man.getCollection().build_options.getImportValue("out");
    132                 }
    133                 else {
    134                      out_name = (String) Gatherer.c_man.getCollection().build_options.getBuildValue("out");
    135                 }
    136                 if(out_name != null && out_name.length() > 0) {
    137                      File out = new File(out_name);
    138                      try {
    139                           if(out.exists()) {
    140                                 boolean append = true;
    141                                 fos = new FileOutputStream(out, append);
    142                                 bos = new BufferedOutputStream(fos);
    143                           }
    144                      }
    145                      catch (Exception error) {
    146                           error.printStackTrace();
    147                      }
    148                 }
    149           }
    150           // Issue a processBegun event
    151           fireProcessBegun(type, status);
    152           try {
    153                 String command = "";
    154                 for(int i = 0; i < args.length; i++) {
    155                      command = command + args[i] + " ";
    156                 }
     121    // Setup
     122    if(progress != null) {
     123        progress.start();
     124    }
     125    // Determine if the user has asked for an outfile.
     126    String out_name = null;
     127    FileOutputStream fos = null;
     128    BufferedOutputStream bos = null;
     129    if(type == IMPORT || type == BUILD) {
     130        if(type == IMPORT) {
     131        out_name = (String) Gatherer.c_man.getCollection().build_options.getImportValue("out");
     132        }
     133        else {
     134        out_name = (String) Gatherer.c_man.getCollection().build_options.getBuildValue("out");
     135        }
     136        if(out_name != null && out_name.length() > 0) {
     137        File out = new File(out_name);
     138        try {
     139            if(out.exists()) {
     140            boolean append = true;
     141            fos = new FileOutputStream(out, append);
     142            bos = new BufferedOutputStream(fos);
     143            }
     144        }
     145        catch (Exception error) {
     146            error.printStackTrace();
     147        }
     148        }
     149    }
     150    // Issue a processBegun event
     151    fireProcessBegun(type, status);
     152    try {
     153        String command = "";
     154        for(int i = 0; i < args.length; i++) {
     155        command = command + args[i] + " ";
     156        }
    157157                ///ystem.err.println("Command: " + command);
    158                 fireMessage(type, get("Command") + ": " + command, status);
    159                 message(Message.EVENT, get("Command") + ": " + command);
     158        fireMessage(type, get("Command") + ": " + command, status);
     159        message(Message.EVENT, get("Command") + ": " + command);
    160160               
    161                 Runtime rt = Runtime.getRuntime();
    162                 Process prcs = rt.exec(args);
    163                 InputStreamReader eisr = new InputStreamReader( prcs.getErrorStream() );
    164                 InputStreamReader stdinisr = new InputStreamReader( prcs.getInputStream() );
    165                 BufferedReader ebr = new BufferedReader( eisr );
    166                 BufferedReader stdinbr = new BufferedReader( stdinisr );
     161        Runtime rt = Runtime.getRuntime();
     162        Process prcs = rt.exec(args);
     163        InputStreamReader eisr = new InputStreamReader( prcs.getErrorStream() );
     164        InputStreamReader stdinisr = new InputStreamReader( prcs.getInputStream() );
     165        BufferedReader ebr = new BufferedReader( eisr );
     166        BufferedReader stdinbr = new BufferedReader( stdinisr );
    167167                // Captures the std err of a program and pipes it into
    168168                // std in of java
    169                 String eline = null;
    170                 String stdinline = null;
    171                 while (((eline = ebr.readLine()) != null || (stdinline = stdinbr.readLine()) != null) && !hasSignalledStop()) {
    172                      if(eline != null) {
    173                           if(progress != null) {
    174                                 progress.parse(eline);
    175                           }
    176                           if(bos != null) {
    177                                 bos.write(eline.getBytes(), 0, eline.length());
    178                           }
    179                           ///ystem.err.println("stderr: " + eline);
    180                           fireMessage(type, typeAsString(type) + "> " + eline,
    181                                           status);
    182                           message(Message.INFO, eline);
    183                      }
    184                      if(stdinline != null) {
    185                           if(bos != null) {
    186                                 //bos.write(stdinline.getBytes(), 0, stdinline.length());
    187                           }
    188                           ///ystem.err.println("stdin: " + stdinline);
    189                           fireMessage(type, typeAsString(type) + "> " + stdinline,
    190                                           status);
    191                           message(Message.INFO, stdinline);
    192                      }
    193                 }
     169        String eline = null;
     170        String stdinline = null;
     171        while (((eline = ebr.readLine()) != null || (stdinline = stdinbr.readLine()) != null) && !hasSignalledStop()) {
     172        if(eline != null) {
     173            if(progress != null) {
     174            progress.parse(eline);
     175            }
     176            if(bos != null) {
     177            bos.write(eline.getBytes(), 0, eline.length());
     178            }
     179            ///ystem.err.println("stderr: " + eline);
     180            fireMessage(type, typeAsString(type) + "> " + eline,
     181                status);
     182            message(Message.INFO, eline);
     183        }
     184        if(stdinline != null) {
     185            if(bos != null) {
     186            //bos.write(stdinline.getBytes(), 0, stdinline.length());
     187            }
     188            ///ystem.err.println("stdin: " + stdinline);
     189            fireMessage(type, typeAsString(type) + "> " + stdinline,
     190                status);
     191            message(Message.INFO, stdinline);
     192        }
     193        }
    194194
    195                 if(!hasSignalledStop()) {
    196                      // Now display final message based on exit value
    197                      prcs.waitFor();
     195        if(!hasSignalledStop()) {
     196        // Now display final message based on exit value
     197        prcs.waitFor();
    198198                     
    199                      if(prcs.exitValue() == 0) {
    200                           status = OK;
    201                           fireMessage(type, typeAsString(type) + "> " + get("Success"), status);
    202                           message(Message.MAIN, get("Success"));
    203                      } else {
    204                           status = ERROR;
    205                           fireMessage(type, typeAsString(type) + "> " + get("Failure"), status);
    206                           message(Message.ERROR, get("Failure"));
    207                      }
    208                 }
    209                 else {
    210                      // I need to somehow kill the child process. Unfortunately Thread.stop() and Process.destroy() both fail to do this. But now, thankx to the magic of Michaels 'close the stream suggestion', it works fine (no it doesn't!)
    211                      prcs.getOutputStream().close();
    212                      prcs.destroy();
    213                      status = ERROR;
    214                 }
    215           }
    216           // Exception
    217           catch (Exception error) {
    218                 message(Message.ERROR, get("Failure"));
    219                 message(Message.ERROR, error.toString());
    220                 Gatherer.printStackTrace(error);
    221                 status = ERROR;
    222           }
    223           // If no error occured, and this was an import process we now extract any new metadata from the archive directory.
    224           if(status == OK && type == IMPORT) {
    225                 fireMessage(type, typeAsString(type) + "> " + get("Parsing_Metadata_Start"), status);
    226                 new GreenstoneArchiveParser(progress, this);
    227                 fireMessage(type, typeAsString(type) + "> " + get("Parsing_Metadata_Complete"), status);
    228           }
    229           // Tidy up.
    230           if(progress != null) {
    231                 progress.stop();
    232           }
    233           if(bos != null) {
    234                 try {
    235                      bos.close();
    236                 }
    237                 catch (Exception e) {
    238                 }
    239           }
    240           // We're done.
    241           fireProcessComplete(type, status);
    242     }
    243     /** Method for firing a message to all interested listeners.
     199        if(prcs.exitValue() == 0) {
     200            status = OK;
     201            fireMessage(type, typeAsString(type) + "> " + get("Success"), status);
     202            message(Message.MAIN, get("Success"));
     203        } else {
     204            status = ERROR;
     205            fireMessage(type, typeAsString(type) + "> " + get("Failure"), status);
     206            message(Message.ERROR, get("Failure"));
     207        }
     208        }
     209        else {
     210        // I need to somehow kill the child process. Unfortunately Thread.stop() and Process.destroy() both fail to do this. But now, thankx to the magic of Michaels 'close the stream suggestion', it works fine (no it doesn't!)
     211        prcs.getOutputStream().close();
     212        prcs.destroy();
     213        status = ERROR;
     214        }
     215    }
     216    // Exception
     217    catch (Exception error) {
     218        message(Message.ERROR, get("Failure"));
     219        message(Message.ERROR, error.toString());
     220        Gatherer.printStackTrace(error);
     221        status = ERROR;
     222    }
     223    // If no error occured, and this was an import process we now extract any new metadata from the archive directory.
     224    if(status == OK && type == IMPORT) {
     225        fireMessage(type, typeAsString(type) + "> " + get("Parsing_Metadata_Start"), status);
     226        new GreenstoneArchiveParser(progress, this);
     227        fireMessage(type, typeAsString(type) + "> " + get("Parsing_Metadata_Complete"), status);
     228    }
     229    // Tidy up.
     230    if(progress != null) {
     231        progress.stop();
     232    }
     233    if(bos != null) {
     234        try {
     235        bos.close();
     236        }
     237        catch (Exception e) {
     238        }
     239    }
     240    // We're done.
     241    fireProcessComplete(type, status);
     242    }
     243    /** Method for firing a message to all interested listeners.
    244244      * @param type An <strong>int</strong> indicating the process type.
    245245      * @param message The message as a <strong>String</strong>.
    246246      * @param status An <strong>int</strong> specifying the current status of the process.
    247247      */
    248     public void fireMessage(int type, String message, int status) {
    249           GShellEvent event = new GShellEvent(this, 0, type, message, status);
    250           Object[] concerned = listeners.getListenerList();
    251           for(int i = 0; i < concerned.length ; i++) {
    252                 if(concerned[i] == GShellListener.class) {
    253                      ((GShellListener)concerned[i+1]).message(event);
    254                 }
    255           }
    256     }
    257     /** Method for firing a process begun event which is called, strangly enough, when the process begins.
     248    public void fireMessage(int type, String message, int status) {
     249    GShellEvent event = new GShellEvent(this, 0, type, message, status);
     250    Object[] concerned = listeners.getListenerList();
     251    for(int i = 0; i < concerned.length ; i++) {
     252        if(concerned[i] == GShellListener.class) {
     253        ((GShellListener)concerned[i+1]).message(event);
     254        }
     255    }
     256    }
     257    /** Method for firing a process begun event which is called, strangly enough, when the process begins.
    258258      * @param type An <strong>int</strong> indicating the process type.
    259259      * @param status An <strong>int</strong> specifying the current status of the process.
    260260      */
    261     protected void fireProcessBegun(int type, int status) {
    262           GShellEvent event = new GShellEvent(this, 0, type, "", status);
    263           Object[] concerned = listeners.getListenerList();
    264           for(int i = 0; i < concerned.length ; i++) {
    265                 if(concerned[i] == GShellListener.class) {
    266                      ((GShellListener)concerned[i+1]).processBegun(event);
    267                 }
    268           }
    269     }
    270     /** Method for firing a process complete event which is called, no surprise here, when the process ends.
     261    protected void fireProcessBegun(int type, int status) {
     262    GShellEvent event = new GShellEvent(this, 0, type, "", status);
     263    Object[] concerned = listeners.getListenerList();
     264    for(int i = 0; i < concerned.length ; i++) {
     265        if(concerned[i] == GShellListener.class) {
     266        ((GShellListener)concerned[i+1]).processBegun(event);
     267        }
     268    }
     269    }
     270    /** Method for firing a process complete event which is called, no surprise here, when the process ends.
    271271      * @param type An <strong>int</strong> indicating the process type.
    272272      * @param status An <strong>int</strong> specifying the current status of the process.
    273273      */
    274     protected void fireProcessComplete(int type, int status) {
    275           GShellEvent event = new GShellEvent(this, 0, type, "", status);
    276           Object[] concerned = listeners.getListenerList();
    277           for(int i = 0; i < concerned.length ; i++) {
    278                 if(concerned[i] == GShellListener.class) {
    279                      ((GShellListener)concerned[i+1]).processComplete(event);
    280                 }
    281           }
    282     }
    283     /** Retrieve a phrase from the dictionary based on the given key.
     274    protected void fireProcessComplete(int type, int status) {
     275    GShellEvent event = new GShellEvent(this, 0, type, "", status);
     276    Object[] concerned = listeners.getListenerList();
     277    for(int i = 0; i < concerned.length ; i++) {
     278        if(concerned[i] == GShellListener.class) {
     279        ((GShellListener)concerned[i+1]).processComplete(event);
     280        }
     281    }
     282    }
     283    /** Retrieve a phrase from the dictionary based on the given key.
    284284      * @param key A <strong>String</strong> used to match against a phrase from the <strong>Dictionary</strong>.
    285285      * @return The phrase as a <strong>String</strong>.
    286286      */
    287     private String get(String key) {
    288           if(key.indexOf('.') == -1) {
    289                 key = "GShell." + key;
    290           }
    291           return Gatherer.dictionary.get(key);
    292     }
    293     /** Method to determine if the user, via the progress monitor, has signalled stop.
     287    private String get(String key) {
     288    if(key.indexOf('.') == -1) {
     289        key = "GShell." + key;
     290    }
     291    return Gatherer.dictionary.get(key);
     292    }
     293    /** Method to determine if the user, via the progress monitor, has signalled stop.
    294294      * @return A <strong>boolean</strong> indicating if the user wanted to stop.
    295295      */
    296     private boolean hasSignalledStop() {
    297           boolean has_signalled_stop = false;
    298           if(progress != null) {
    299                 return progress.hasSignalledStop();
    300           }
    301           return has_signalled_stop;
    302     }
    303     /** Creates and dispatches a message given the initial details.
     296    private boolean hasSignalledStop() {
     297    boolean has_signalled_stop = false;
     298    if(progress != null) {
     299        return progress.hasSignalledStop();
     300    }
     301    return has_signalled_stop;
     302    }
     303    /** Creates and dispatches a message given the initial details.
    304304      * @param level An <strong>int</strong> indicating the message level for this message.
    305305      * @param message A <strong>String</strong> which contains the payload of this message.
    306306      */
    307     private void message(int level, String message) {
    308           Message msg = new Message(msg_type, level, message);
    309           if(Gatherer.g_man != null) {
    310                 Gatherer.log.add(msg);
    311           }
    312     }
    313     /** Converts a type into a text representation.
     307    private void message(int level, String message) {
     308    Message msg = new Message(msg_type, level, message);
     309    if(Gatherer.g_man != null) {
     310        Gatherer.log.add(msg);
     311    }
     312    }
     313    /** Converts a type into a text representation.
    314314      * @param type An <strong>int</strong> which maps to a shell process type.
    315315      * @return A <strong>String</strong> which is the thread process's text name.
    316316      */
    317     public String typeAsString(int type) {
    318           String name = null;
    319           switch(type) {
    320           case BUILD:
    321                 name = get("Build");
    322                 break;
    323           case IMPORT:
    324                 name = get("Import");
    325                 break;
    326           case NEW:
    327                 name = get("New");
    328                 break;
    329           default:
    330                 name = get("Other");
    331           }
    332           return name;
    333     }
     317    public String typeAsString(int type) {
     318    String name = null;
     319    switch(type) {
     320    case BUILD:
     321        name = get("Build");
     322        break;
     323    case IMPORT:
     324        name = get("Import");
     325        break;
     326    case NEW:
     327        name = get("New");
     328        break;
     329    default:
     330        name = get("Other");
     331    }
     332    return name;
     333    }
    334334}
    335 
    336 
Note: See TracChangeset for help on using the changeset viewer.