ant-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bode...@apache.org
Subject svn commit: r830244 - in /ant/core/trunk/src: main/org/apache/tools/ant/types/resources/AbstractClasspathResource.java main/org/apache/tools/ant/types/resources/JavaResource.java tests/junit/org/apache/tools/ant/types/resources/JavaResourceTest.java
Date Tue, 27 Oct 2009 16:44:56 GMT
Author: bodewig
Date: Tue Oct 27 16:44:56 2009
New Revision: 830244

URL: http://svn.apache.org/viewvc?rev=830244&view=rev
Log:
make javaresource an URLProvider

Modified:
    ant/core/trunk/src/main/org/apache/tools/ant/types/resources/AbstractClasspathResource.java
    ant/core/trunk/src/main/org/apache/tools/ant/types/resources/JavaResource.java
    ant/core/trunk/src/tests/junit/org/apache/tools/ant/types/resources/JavaResourceTest.java

Modified: ant/core/trunk/src/main/org/apache/tools/ant/types/resources/AbstractClasspathResource.java
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/types/resources/AbstractClasspathResource.java?rev=830244&r1=830243&r2=830244&view=diff
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/types/resources/AbstractClasspathResource.java
(original)
+++ ant/core/trunk/src/main/org/apache/tools/ant/types/resources/AbstractClasspathResource.java
Tue Oct 27 16:44:56 2009
@@ -172,6 +172,31 @@
             return ((Resource) getCheckedRef()).getInputStream();
         }
         dieOnCircularReference();
+
+        final ClassLoaderWithFlag classLoader = getClassLoader();
+        return !classLoader.needsCleanup()
+            ? openInputStream(classLoader.getLoader())
+            : new FilterInputStream(openInputStream(classLoader.getLoader())) {
+                    public void close() throws IOException {
+                        FileUtils.close(in);
+                        classLoader.cleanup();
+                    }
+                    protected void finalize() throws Throwable {
+                        try {
+                            close();
+                        } finally {
+                            super.finalize();
+                        }
+                    }
+                };
+    }
+
+    /**
+     * combines the various ways that could specify a ClassLoader and
+     * potentially creates one that needs to be cleaned up when it is
+     * no longer needed so that classes can get garbage collected.
+     */
+    protected ClassLoaderWithFlag getClassLoader() {
         ClassLoader cl = null;
         boolean clNeedsCleanup = false;
         if (loader != null) {
@@ -196,23 +221,7 @@
                 getProject().addReference(loader.getRefId(), cl);
             }
         }
-
-        final ClassLoader classLoader = cl;
-        return !clNeedsCleanup
-            ? openInputStream(cl)
-            : new FilterInputStream(openInputStream(cl)) {
-                    public void close() throws IOException {
-                        FileUtils.close(in);
-                        ((AntClassLoader) classLoader).cleanup();
-                    }
-                    protected void finalize() throws Throwable {
-                        try {
-                            close();
-                        } finally {
-                            super.finalize();
-                        }
-                    }
-                };
+        return new ClassLoaderWithFlag(cl, clNeedsCleanup);
     }
 
     /**
@@ -237,4 +246,20 @@
         }
     }
 
+    public static class ClassLoaderWithFlag {
+        private final ClassLoader loader;
+        private final boolean cleanup;
+
+        ClassLoaderWithFlag(ClassLoader l, boolean needsCleanup) {
+            loader = l;
+            cleanup = needsCleanup && l instanceof AntClassLoader;
+        }
+        public ClassLoader getLoader() { return loader; }
+        public boolean needsCleanup() { return cleanup; }
+        public void cleanup() {
+            if (cleanup) {
+                ((AntClassLoader) loader).cleanup();
+            }
+        }
+    }
 }

Modified: ant/core/trunk/src/main/org/apache/tools/ant/types/resources/JavaResource.java
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/types/resources/JavaResource.java?rev=830244&r1=830243&r2=830244&view=diff
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/types/resources/JavaResource.java (original)
+++ ant/core/trunk/src/main/org/apache/tools/ant/types/resources/JavaResource.java Tue Oct
27 16:44:56 2009
@@ -20,6 +20,7 @@
 import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.InputStream;
+import java.net.URL;
 
 import org.apache.tools.ant.types.Path;
 
@@ -27,7 +28,8 @@
  * A Resource representation of something loadable via a Java classloader.
  * @since Ant 1.7
  */
-public class JavaResource extends AbstractClasspathResource {
+public class JavaResource extends AbstractClasspathResource
+    implements URLProvider {
 
     /**
      * Default constructor.
@@ -48,8 +50,9 @@
     }
 
     /**
-     * open the inpout stream from a specific classloader
-     * @param cl the classloader to use. Will be null if the system classloader is used
+     * open the input stream from a specific classloader
+     * @param cl the classloader to use. Will be null if the system
+     * classloader is used
      * @return an open input stream for the resource
      * @throws IOException if an error occurs.
      */
@@ -72,6 +75,27 @@
     }
 
     /**
+     * Get the URL represented by this Resource.
+     * @since Ant 1.8.0
+     */
+    public URL getURL() {
+        if (isReference()) {
+            return ((JavaResource) getCheckedRef()).getURL();
+        }
+        AbstractClasspathResource.ClassLoaderWithFlag classLoader =
+            getClassLoader();
+        if (classLoader.getLoader() == null) {
+            return ClassLoader.getSystemResource(getName());
+        } else {
+            try {
+                return classLoader.getLoader().getResource(getName());
+            } finally {
+                classLoader.cleanup();
+            }
+        }
+    }
+
+    /**
      * Compare this JavaResource to another Resource.
      * @param another the other Resource against which to compare.
      * @return a negative integer, zero, or a positive integer as this

Modified: ant/core/trunk/src/tests/junit/org/apache/tools/ant/types/resources/JavaResourceTest.java
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/tests/junit/org/apache/tools/ant/types/resources/JavaResourceTest.java?rev=830244&r1=830243&r2=830244&view=diff
==============================================================================
--- ant/core/trunk/src/tests/junit/org/apache/tools/ant/types/resources/JavaResourceTest.java
(original)
+++ ant/core/trunk/src/tests/junit/org/apache/tools/ant/types/resources/JavaResourceTest.java
Tue Oct 27 16:44:56 2009
@@ -38,4 +38,15 @@
         assertTrue(getProject().getProperty("manifest")
                    .startsWith("Manifest-Version:"));
     }
+
+    public void testIsURLProvider() {
+        JavaResource r = new JavaResource();
+        assertSame(r, r.as(URLProvider.class));
+    }
+
+    public void testGetURLOfManifest() {
+        JavaResource r = new JavaResource();
+        r.setName("META-INF/MANIFEST.MF");
+        assertNotNull(r.getURL());
+    }
 }



Mime
View raw message