ant-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bode...@apache.org
Subject svn commit: r810790 - /ant/core/trunk/src/main/org/apache/tools/ant/filters/util/ChainReaderHelper.java
Date Thu, 03 Sep 2009 07:06:59 GMT
Author: bodewig
Date: Thu Sep  3 07:06:58 2009
New Revision: 810790

URL: http://svn.apache.org/viewvc?rev=810790&view=rev
Log:
clean up classloaders if things go wrong as well

Modified:
    ant/core/trunk/src/main/org/apache/tools/ant/filters/util/ChainReaderHelper.java

Modified: ant/core/trunk/src/main/org/apache/tools/ant/filters/util/ChainReaderHelper.java
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/filters/util/ChainReaderHelper.java?rev=810790&r1=810789&r2=810790&view=diff
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/filters/util/ChainReaderHelper.java (original)
+++ ant/core/trunk/src/main/org/apache/tools/ant/filters/util/ChainReaderHelper.java Thu Sep
 3 07:06:58 2009
@@ -24,6 +24,7 @@
 import java.lang.reflect.InvocationTargetException;
 import java.util.ArrayList;
 import java.util.Iterator;
+import java.util.List;
 import java.util.Vector;
 import org.apache.tools.ant.AntClassLoader;
 import org.apache.tools.ant.BuildException;
@@ -138,6 +139,8 @@
         final int filtersCount = finalFilters.size();
 
         if (filtersCount > 0) {
+            boolean success = false;
+            try {
             for (int i = 0; i < filtersCount; i++) {
                 Object o = finalFilters.elementAt(i);
 
@@ -155,8 +158,8 @@
                             } else {
                                 AntClassLoader al
                                     = pro.createClassLoader(classpath);
-                                clazz = Class.forName(className, true, al);
                                 classLoadersToCleanUp.add(al);
+                                clazz = Class.forName(className, true, al);
                             }
                             if (clazz != null) {
                                 if (!FilterReader.class.isAssignableFrom(clazz)) {
@@ -208,16 +211,19 @@
                     setProjectOnObject(instream);
                 }
             }
+            success = true;
+            } finally {
+                if (!success && classLoadersToCleanUp.size() > 0) {
+                    cleanUpClassLoaders(classLoadersToCleanUp);
+                }
+            }
         }
         final Reader finalReader = instream;
         return classLoadersToCleanUp.size() == 0 ? finalReader
             : new FilterReader(finalReader) {
                     public void close() throws IOException {
                         FileUtils.close(in);
-                        for (Iterator it = classLoadersToCleanUp.iterator();
-                             it.hasNext(); ) {
-                            ((AntClassLoader) it.next()).cleanup();
-                        }
+                        cleanUpClassLoaders(classLoadersToCleanUp);
                     }
                     protected void finalize() throws Throwable {
                         try {
@@ -246,6 +252,15 @@
     }
 
     /**
+     * Deregisters Classloaders from the project so GC can remove them later.
+     */
+    private static void cleanUpClassLoaders(List/*<AntClassLoader>*/ loaders) {
+        for (Iterator it = loaders.iterator(); it.hasNext(); ) {
+            ((AntClassLoader) it.next()).cleanup();
+        }
+    }
+
+    /**
      * Read data from the reader and return the
      * contents as a string.
      * @param rdr the reader object



Mime
View raw message