Changeset 31670 for main


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

Changing the EventListeners list in GShell over to be CopyOnWriteArrayList because it is threadsafe, as is needed here, though the change comes at the expense of reduced efficiency. Also removed Imagemagick test debug statements in Gatherer.

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

Legend:

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

    r31644 r31670  
    16761676                }
    16771677                DebugStream.println("***** Running ImageMagickTest command: " + cmd_str);
    1678                 System.err.println("***** Running ImageMagickTest command: " + cmd_str);
    16791678               
    16801679                SafeProcess image_magick_process = new SafeProcess(command_parts);
     
    16841683                String output = image_magick_process.getStdOutput().toLowerCase();
    16851684                if (output.indexOf("version") != -1 || output.indexOf("imagemagick") != -1) {
    1686                 System.err.println("*** ImageMagickTest output: " + output);
    16871685                found = true;
    16881686                } // else found var remains false   
  • main/trunk/gli/src/org/greenstone/gatherer/shell/GShell.java

    r31666 r31670  
    4141import java.util.ArrayList;
    4242import java.util.Enumeration;
     43import java.util.concurrent.CopyOnWriteArrayList;
    4344import java.util.regex.*;
    4445import javax.swing.*;
     
    6869    private boolean cancel = false;
    6970    private BufferedOutputStream buffered_output_stream = null;
    70     /** The list of listeners associated with this class. */
    71     private EventListenerList listeners = null;
     71    /** The list of listeners associated with this class.
     72     * Make it a CopyOnWriteArrayList to make it threadsafe at a cost of decreased efficiency.
     73     * see http://stackoverflow.com/questions/8259479/should-i-synchronize-listener-notifications-or-not
     74     */
     75    private final CopyOnWriteArrayList<GShellListener> listeners;
    7276    /** The current status of this shell process. */
    7377    private int status = -1;
     
    8589    private String commandOutput = null;
    8690
    87     /** The process safely run in this GShell. Make sure to set to null when done with. */
    88     SafeProcess prcs = null;
     91    /** The process safely run in this GShell Thread. Make sure to set to prcs to null when done with. */
     92    private SafeProcess prcs = null;
    8993
    9094    /** Elements in process type enumeration. */
     
    136140    // Lower this jobs priority
    137141    this.setPriority(Thread.MIN_PRIORITY);
    138     listeners = new EventListenerList();
    139     listeners.add(GShellListener.class, caller);
     142    listeners = new CopyOnWriteArrayList<GShellListener>();
     143    listeners.add(caller);
    140144    }
    141145    /** This method adds another shell listener to this process.
     
    143147     */
    144148    public void addGShellListener(GShellListener listener) {
    145     listeners.add(GShellListener.class, listener);
     149    listeners.add(listener);
    146150    }
    147151    /** This method removes a certain shell listener from this process.
     
    149153     */
    150154    /* private void removeGShellListener(GShellListener listener) {
    151        listeners.remove(GShellListener.class, listener);
     155       listeners.remove(listener);
    152156       } */
    153157
     
    549553        // If the event hasn't been vetoed, pass it on to other listeners
    550554        if(!current_event.isVetoed()) {
    551         Object[] concerned = listeners.getListenerList();
    552         for(int i = 0; i < concerned.length ; i++) {
    553             if(concerned[i] == GShellListener.class) {
    554             ((GShellListener)concerned[i+1]).message(current_event);
    555             }
    556         }
    557         concerned = null;
     555        // See http://stackoverflow.com/questions/8259479/should-i-synchronize-listener-notifications-or-not
     556        for (GShellListener l: this.listeners) {
     557            l.message(current_event);
     558        }
    558559        }
    559560    }
     
    585586    // Fire an event
    586587    GShellEvent event = new GShellEvent(this, 0, type, "", status);
    587     Object[] concerned = listeners.getListenerList();
    588     for(int i = 0; i < concerned.length ; i++) {
    589         if(concerned[i] == GShellListener.class) {
    590         ((GShellListener)concerned[i+1]).processBegun(event);
    591         }
     588    for (GShellListener l: this.listeners) {
     589        l.processBegun(event);
    592590    }
    593591    }
     
    607605    if(status == CANCELLED && Configuration.getMode() <= Configuration.LIBRARIAN_MODE) {
    608606        GShellEvent current_event = new GShellEvent(this, 0, type, Dictionary.get("GShell.Build.BuildCancelled"), status);
    609         Object[] concerned = listeners.getListenerList();
    610         for(int i = 0; i < concerned.length ; i++) {
    611         if(concerned[i] == GShellListener.class) {
    612             ((GShellListener)concerned[i+1]).message(current_event);
    613         }
    614         }
    615         concerned = null;
     607        for (GShellListener l: this.listeners) {
     608        l.message(current_event);
     609        }
    616610    }
    617611   
     
    623617    // And firing off an event
    624618    GShellEvent event = new GShellEvent(this, 0, type, msg, status);
    625     Object[] concerned = listeners.getListenerList();
    626     for(int i = 0; i < concerned.length ; i++) {
    627         if(concerned[i] == GShellListener.class) {
    628         ((GShellListener)concerned[i+1]).processComplete(event);
    629         }
     619    for (GShellListener l: this.listeners) {
     620        l.processComplete(event);
    630621    }
    631622    }
Note: See TracChangeset for help on using the changeset viewer.