Changeset 5152


Ignore:
Timestamp:
2003-08-18T13:54:57+12:00 (21 years ago)
Author:
jmt12
Message:

Slight fix to ensure messages are written to logs properly

File:
1 edited

Legend:

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

    r5038 r5152  
    117117    listeners.remove(GShellListener.class, listener);
    118118    }
    119     /** Any threaded class must include this method to allow the thread body to be run.
    120       */
    121     private MessageDispatcher dispatcher;
     119    /** Any threaded class must include this method to allow the thread body to be run. */
    122120    public void run() {
    123     // Create a new message dispatcher to merge then send off messages at a predetermined rate.
    124     dispatcher = new MessageDispatcher(250);
    125     dispatcher.start();
    126     // Setup
    127121    if(progress != null) {
    128122        progress.start();
     
    130124    // Determine if the user has asked for an outfile.
    131125    String out_name = null;
     126    BufferedOutputStream bos = null;
    132127    if(type == IMPORT || type == BUILD) {
    133128        if(type == IMPORT) {
     
    138133        }
    139134        if(out_name != null && out_name.length() > 0) {
    140         File out = new File(out_name);
    141135        try {
    142             if(out.exists()) {
    143             boolean append = true;
    144             dispatcher.setBOS(new BufferedOutputStream(new FileOutputStream(out, append)));
    145             }
     136            bos = new BufferedOutputStream(new FileOutputStream(new File(out_name), true));
    146137        }
    147138        catch (Exception error) {
    148             error.printStackTrace();
     139            Gatherer.printStackTrace(error);
    149140        }
    150141        }
     
    159150        }
    160151        ///ystem.err.println("Command: " + command);
    161         dispatcher.standardMessage(type, get("Command") + ": " + command, status);
     152        fireMessage(type, get("Command") + ": " + command, status);
    162153               
    163154        Runtime rt = Runtime.getRuntime();
     
    175166            progress.parse(eline);
    176167            }
    177             dispatcher.errorMessage(type, typeAsString(type) + "> " + eline, status);
     168            if(bos != null) {
     169            try {
     170                bos.write(eline.getBytes(), 0, eline.length());
     171            }
     172            catch(Exception error) {
     173                Gatherer.printStackTrace(error);
     174            }
     175            }
     176            fireMessage(type, typeAsString(type) + "> " + eline, status);
    178177        }
    179178        if(stdinline != null) {
    180             dispatcher.standardMessage(type, typeAsString(type) + "> " + stdinline, status);
     179            fireMessage(type, typeAsString(type) + "> " + stdinline, status);
    181180           
    182181        }
     
    189188        if(prcs.exitValue() == 0) {
    190189            status = OK;
    191             dispatcher.standardMessage(type, typeAsString(type) + "> " + get("Success"), status);
     190            fireMessage(type, typeAsString(type) + "> " + get("Success"), status);
    192191           
    193192        } else {
    194193            status = ERROR;
    195             dispatcher.standardMessage(type, typeAsString(type) + "> " + get("Failure"), status);
     194            fireMessage(type, typeAsString(type) + "> " + get("Failure"), status);
    196195           
    197196        }
     
    199198        else {
    200199        // 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!)
     200        prcs.getInputStream().close();
    201201        prcs.getOutputStream().close();
    202202        prcs.destroy();
     
    211211    // If no error occured, and this was an import process we now extract any new metadata from the archive directory.
    212212    if(status == OK && type == IMPORT) {
    213         dispatcher.standardMessage(type, typeAsString(type) + "> " + get("Parsing_Metadata_Start"), status);
     213        fireMessage(type, typeAsString(type) + "> " + get("Parsing_Metadata_Start"), status);
    214214        new GreenstoneArchiveParser(progress, this);
    215         dispatcher.standardMessage(type, typeAsString(type) + "> " + get("Parsing_Metadata_Complete"), status);
     215        fireMessage(type, typeAsString(type) + "> " + get("Parsing_Metadata_Complete"), status);
    216216    }
    217217    // Tidy up.
     
    221221    // We're done.
    222222    fireProcessComplete(type, status);
    223     dispatcher.processComplete();
    224     dispatcher = null;
     223    // Close bos
     224    if(bos != null) {
     225        try {
     226        bos.close();
     227        bos = null;
     228        }
     229        catch(Exception error) {
     230        Gatherer.printStackTrace(error);
     231        }
     232    }
    225233    }
    226234    /** Method for firing a message to all interested listeners.
     
    236244        ((GShellListener)concerned[i+1]).message(event);
    237245        }
    238     }
    239     }
    240 
    241     public void message(int type, String message, int status) {
    242     if(dispatcher != null) {
    243         dispatcher.standardMessage(type, message, status);
    244246    }
    245247    }
     
    313315    return name;
    314316    }
    315 
    316     /** Rather than bombard the progress logs with thousands of individual lines, which can actually slow build times by 200-1000%, we instead fire off quarter second updates with all of the strings retrieved in that time, if any. */
    317     private class MessageDispatcher
    318     extends Thread {
    319     private boolean process_complete;
    320     private BufferedOutputStream bos;
    321     private int last_status;
    322     private int last_type;
    323     private int wait_time;
    324     private StringBuffer bos_buffer;
    325     private StringBuffer buffer;
    326     public MessageDispatcher(int wait_time) {
    327         this.bos = bos;
    328         this.bos_buffer = new StringBuffer();
    329         this.buffer = new StringBuffer();
    330         this.process_complete = false;
    331         this.wait_time = wait_time;
    332     }
    333 
    334     public synchronized void errorMessage(int type, String message, int status) {
    335         buffer.append(message);
    336         buffer.append(StaticStrings.NEW_LINE_CHARACTER);
    337         if(bos != null) {
    338         bos_buffer.append(message);
    339         bos_buffer.append(StaticStrings.NEW_LINE_CHARACTER);
    340         }
    341         last_status = status;
    342         last_type = type;
    343     }
    344 
    345     public void processComplete() {
    346         process_complete = true;
    347     }
    348 
    349     public void run() {
    350         while(!process_complete) {
    351         try {
    352             wait(wait_time);
    353         }
    354         catch(Exception error) {
    355         }
    356         message();
    357         }
    358         // If there is still something in buffer send it
    359         message();
    360         // Close bos if it exists
    361         if(bos != null) {
    362         try {
    363             bos.close();
    364         }
    365         catch (Exception e) {
    366         }
    367         }
    368     }
    369 
    370     public void setBOS(BufferedOutputStream bos) {
    371         this.bos = bos;
    372     }
    373 
    374     public synchronized void standardMessage(int type, String message, int status) {
    375         buffer.append(message);
    376         buffer.append(StaticStrings.NEW_LINE_CHARACTER);
    377         last_status = status;
    378         last_type = type;
    379     }
    380 
    381     private synchronized void message() {
    382         if(bos != null && bos_buffer.length() > 0) {
    383         try {
    384             bos.write(bos_buffer.toString().getBytes(), 0, bos_buffer.length());
    385         }
    386         catch(Exception error) {
    387             Gatherer.printStackTrace(error);
    388         }
    389         bos_buffer.delete(0, bos_buffer.length());
    390         }
    391         if(buffer.length() > 0) {
    392         fireMessage(last_type, buffer.toString(), last_status);
    393         buffer.delete(0, buffer.length());
    394         }
    395     }
    396     }
    397317}
Note: See TracChangeset for help on using the changeset viewer.