> Bug: 749
> The ClassLoader that is being used for Ant's java task does not appear to
> implement the getResource method correctly.
The getResource method is not implemented at all. Attached is a patch that
implements this method. It works with ant 1.2 and
jdk 1.3.0
Markus
=============================== snip
=================================================
--- //D/java/jakarta-ant/src/org/apache/tools/ant/AntClassLoader.java Tue Oct
24 12:50:23 2000
+++ ../ant-patch/javasrc/org/apache/tools/ant/AntClassLoader.java Wed Apr
18 15:09:40 2001
@@ -57,6 +57,7 @@
import java.util.*;
import java.util.zip.*;
import java.io.*;
+import java.net.*;
import org.apache.tools.ant.types.Path;
/**
@@ -279,6 +280,90 @@
return null;
}
+
+
+
+ /**
+ * Finds the resource with the given name. A resource is some data (images,
audio, text, etc)
+ * that can be accessed by class
+ * code in a way that is independent of the location of the code.
+ *
+ * @param name the name of the resource for which a stream is required.
+ *
+ * @return a URL for reading the resource, or null if the resource could
not be found or the caller
+ * doesn't have adequate privileges to get the resource.
+ *
+ */
+ public URL getResource(String name) {
+ // we need to search the components of the path to see if we can find
the
+ // class we want.
+ URL url = null;
+
+ String[] pathElements = classpath.list();
+ for (int i = 0; i < pathElements.length && url == null; ++i) {
+ File pathComponent = project.resolveFile((String)pathElements[i]);
+ url = getResourceURL(pathComponent, name);
+ }
+
+ return url;
+ }
+
+ /**
+ * Get an inputstream to a given resource in the given file which may
+ * either be a directory or a zip file.
+ *
+ * @param file the file (directory or jar) in which to search for the
resource.
+ * @param resourceName the name of the resource for which a stream is
required.
+ *
+ * @return a stream to the required resource or null if the resource cannot
be
+ * found in the given file object
+ */
+ private URL getResourceURL(File file, String resourceName) {
+ try {
+ if (!file.exists()) {
+ return null;
+ }
+
+ if (file.isDirectory()) {
+ File resource = new File(file, resourceName);
+
+ if (resource.exists()) {
+ try {
+ return new URL("file:"+resource.toString());
+ } catch (MalformedURLException ex) {
+ return null;
+ }
+ }
+ }
+ else {
+ ZipFile zipFile = null;
+ try {
+ zipFile = new ZipFile(file);
+
+ ZipEntry entry = zipFile.getEntry(resourceName);
+ if (entry != null) {
+ try {
+ return new URL("jar:file:"+file.toString()+"!/"+entry);
+ } catch (MalformedURLException ex) {
+ return null;
+ }
+ }
+ }
+ finally {
+ if (zipFile != null) {
+ zipFile.close();
+ }
+ }
+ }
+ }
+ catch (Exception e) {
+ e.printStackTrace();
+ }
+
+ return null;
+ }
+
+
/**
* Load a class with this class loader.