Browse Source

Implement ClassLoader#findResource(String)

master
Basil Crow 3 years ago
parent
commit
ef85f491ee
3 changed files with 41 additions and 9 deletions
  1. +1
    -0
      CONTRIBUTORS
  2. +4
    -0
      contributors.xml
  3. +36
    -9
      src/main/org/apache/tools/ant/AntClassLoader.java

+ 1
- 0
CONTRIBUTORS View File

@@ -43,6 +43,7 @@ Avik Sengupta
Balazs Fejes 2
barney2k7
Bart Vanhaute
Basil Crow
Ben Galbraith
Ben Gertzfield
Benjamin Burgess


+ 4
- 0
contributors.xml View File

@@ -199,6 +199,10 @@
<first>Bart</first>
<last>Vanhaute</last>
</name>
<name>
<first>Basil</first>
<last>Crow</last>
</name>
<name>
<first>Benjamin</first>
<last>Burgess</last>


+ 36
- 9
src/main/org/apache/tools/ant/AntClassLoader.java View File

@@ -889,15 +889,7 @@ public class AntClassLoader extends ClassLoader implements SubBuildListener, Clo
if (url != null) {
log("Resource " + name + " loaded from parent loader", Project.MSG_DEBUG);
} else {
// try and load from this loader if the parent either didn't find
// it or wasn't consulted.
for (final File pathComponent : pathComponents) {
url = getResourceURL(pathComponent, name);
if (url != null) {
log("Resource " + name + " loaded from ant loader", Project.MSG_DEBUG);
break;
}
}
url = getUrl(name);
}
if (url == null && !isParentFirst(name)) {
// this loader was first but it didn't find it - try the parent
@@ -916,6 +908,29 @@ public class AntClassLoader extends ClassLoader implements SubBuildListener, Clo
return url;
}

/**
* Finds a matching file by iterating through path components.
*
* @param name File to find
* @return A <code>URL</code> object for reading the resource, or <code>null</code> if the
* resource could not be found
*/
private URL getUrl(String name) {
URL url = null;

// try and load from this loader if the parent either didn't find
// it or wasn't consulted.
for (final File pathComponent : pathComponents) {
url = getResourceURL(pathComponent, name);
if (url != null) {
log("Resource " + name + " loaded from ant loader", Project.MSG_DEBUG);
break;
}
}

return url;
}

/**
* Finds all the resources with the given name. A resource is some
* data (images, audio, text, etc) that can be accessed by class
@@ -935,6 +950,18 @@ public class AntClassLoader extends ClassLoader implements SubBuildListener, Clo
return findResources(name, false);
}

/**
* Finds the resource with the given name.
*
* @param name The resource name
* @return A <code>URL</code> object for reading the resource, or <code>null</code> if the
* resource could not be found
*/
@Override
protected URL findResource(final String name) {
return getUrl(name);
}

/**
* Returns an enumeration of URLs representing all the resources with the
* given name by searching the class loader's classpath.


Loading…
Cancel
Save