ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bode...@apache.org
Subject cvs commit: ant/src/main/org/apache/tools/ant Project.java
Date Mon, 25 Aug 2003 08:28:27 GMT
bodewig     2003/08/25 01:28:27

  Modified:    src/main/org/apache/tools/ant Project.java
  Log:
  Switch BuildListener management from "copy on each event" to "copy on
  add/remove".
  
  Suggested by:	Sylvain Wallez <sylvain dot wallez at anyware dash tech dot com>
  
  Revision  Changes    Path
  1.150     +35 -33    ant/src/main/org/apache/tools/ant/Project.java
  
  Index: Project.java
  ===================================================================
  RCS file: /home/cvs/ant/src/main/org/apache/tools/ant/Project.java,v
  retrieving revision 1.149
  retrieving revision 1.150
  diff -u -r1.149 -r1.150
  --- Project.java	24 Jul 2003 14:05:49 -0000	1.149
  +++ Project.java	25 Aug 2003 08:28:27 -0000	1.150
  @@ -62,6 +62,7 @@
   import java.lang.reflect.Modifier;
   import java.util.Enumeration;
   import java.util.Hashtable;
  +import java.util.Iterator;
   import java.util.Properties;
   import java.util.Stack;
   import java.util.Vector;
  @@ -371,8 +372,12 @@
        * @param listener The listener to add to the list.
        *                 Must not be <code>null</code>.
        */
  -    public void addBuildListener(BuildListener listener) {
  -        listeners.addElement(listener);
  +    public synchronized void addBuildListener(BuildListener listener) {
  +        // create a new Vector to avoid ConcurrentModificationExc when
  +        // the listeners get added/removed while we are in fire
  +        Vector newListeners = getBuildListeners();
  +        newListeners.addElement(listener);
  +        listeners = newListeners;
       }
   
       /**
  @@ -383,11 +388,15 @@
        *                 Should not be <code>null</code>.
        */
       public void removeBuildListener(BuildListener listener) {
  -        listeners.removeElement(listener);
  +        // create a new Vector to avoid ConcurrentModificationExc when
  +        // the listeners get added/removed while we are in fire
  +        Vector newListeners = getBuildListeners();
  +        newListeners.removeElement(listener);
  +        listeners = newListeners;
       }
   
       /**
  -     * Returns a list of build listeners for the project.
  +     * Returns a copy of the list of build listeners for the project.
        *
        * @return a list of build listeners for the project
        */
  @@ -1800,10 +1809,9 @@
        */
       public void fireBuildStarted() {
           BuildEvent event = new BuildEvent(this);
  -        Vector listeners = getBuildListeners();
  -        int size = listeners.size();
  -        for (int i = 0; i < size; i++) {
  -            BuildListener listener = (BuildListener) listeners.elementAt(i);
  +        Iterator iter = listeners.iterator();
  +        while (iter.hasNext()) {
  +            BuildListener listener = (BuildListener) iter.next();
               listener.buildStarted(event);
           }
       }
  @@ -1817,10 +1825,9 @@
       public void fireBuildFinished(Throwable exception) {
           BuildEvent event = new BuildEvent(this);
           event.setException(exception);
  -        Vector listeners = getBuildListeners();
  -        int size = listeners.size();
  -        for (int i = 0; i < size; i++) {
  -            BuildListener listener = (BuildListener) listeners.elementAt(i);
  +        Iterator iter = listeners.iterator();
  +        while (iter.hasNext()) {
  +            BuildListener listener = (BuildListener) iter.next();
               listener.buildFinished(event);
           }
       }
  @@ -1834,10 +1841,9 @@
        */
       protected void fireTargetStarted(Target target) {
           BuildEvent event = new BuildEvent(target);
  -        Vector listeners = getBuildListeners();
  -        int size = listeners.size();
  -        for (int i = 0; i < size; i++) {
  -            BuildListener listener = (BuildListener) listeners.elementAt(i);
  +        Iterator iter = listeners.iterator();
  +        while (iter.hasNext()) {
  +            BuildListener listener = (BuildListener) iter.next();
               listener.targetStarted(event);
           }
       }
  @@ -1855,10 +1861,9 @@
       protected void fireTargetFinished(Target target, Throwable exception) {
           BuildEvent event = new BuildEvent(target);
           event.setException(exception);
  -        Vector listeners = getBuildListeners();
  -        int size = listeners.size();
  -        for (int i = 0; i < size; i++) {
  -            BuildListener listener = (BuildListener) listeners.elementAt(i);
  +        Iterator iter = listeners.iterator();
  +        while (iter.hasNext()) {
  +            BuildListener listener = (BuildListener) iter.next();
               listener.targetFinished(event);
           }
       }
  @@ -1873,10 +1878,9 @@
           // register this as the current task on the current thread.
           registerThreadTask(Thread.currentThread(), task);
           BuildEvent event = new BuildEvent(task);
  -        Vector listeners = getBuildListeners();
  -        int size = listeners.size();
  -        for (int i = 0; i < size; i++) {
  -            BuildListener listener = (BuildListener) listeners.elementAt(i);
  +        Iterator iter = listeners.iterator();
  +        while (iter.hasNext()) {
  +            BuildListener listener = (BuildListener) iter.next();
               listener.taskStarted(event);
           }
       }
  @@ -1897,10 +1901,9 @@
           System.err.flush();
           BuildEvent event = new BuildEvent(task);
           event.setException(exception);
  -        Vector listeners = getBuildListeners();
  -        int size = listeners.size();
  -        for (int i = 0; i < size; i++) {
  -            BuildListener listener = (BuildListener) listeners.elementAt(i);
  +        Iterator iter = listeners.iterator();
  +        while (iter.hasNext()) {
  +            BuildListener listener = (BuildListener) iter.next();
               listener.taskFinished(event);
           }
       }
  @@ -1924,7 +1927,6 @@
           } else {
               event.setMessage(message, priority);
           }
  -        Vector listeners = getBuildListeners();
           synchronized (this) {
               if (loggingMessage) {
                   throw new BuildException("Listener attempted to access "
  @@ -1933,9 +1935,9 @@
               }
               try {
                   loggingMessage = true;
  -                int size = listeners.size();
  -                for (int i = 0; i < size; i++) {
  -                    BuildListener listener = (BuildListener) listeners.elementAt(i);
  +                Iterator iter = listeners.iterator();
  +                while (iter.hasNext()) {
  +                    BuildListener listener = (BuildListener) iter.next();
                       listener.messageLogged(event);
                   }
               } finally {
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org


Mime
View raw message