Skip to content

Commit 4c49cd5

Browse files
authored
Merge pull request #150 from basil/findResource
Implement `ClassLoader#findResource(String)`
2 parents 431f973 + ef85f49 commit 4c49cd5

3 files changed

Lines changed: 41 additions & 9 deletions

File tree

CONTRIBUTORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ Avik Sengupta
4343
Balazs Fejes 2
4444
barney2k7
4545
Bart Vanhaute
46+
Basil Crow
4647
Ben Galbraith
4748
Ben Gertzfield
4849
Benjamin Burgess

contributors.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,10 @@
199199
<first>Bart</first>
200200
<last>Vanhaute</last>
201201
</name>
202+
<name>
203+
<first>Basil</first>
204+
<last>Crow</last>
205+
</name>
202206
<name>
203207
<first>Benjamin</first>
204208
<last>Burgess</last>

src/main/org/apache/tools/ant/AntClassLoader.java

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -889,15 +889,7 @@ public URL getResource(final String name) {
889889
if (url != null) {
890890
log("Resource " + name + " loaded from parent loader", Project.MSG_DEBUG);
891891
} else {
892-
// try and load from this loader if the parent either didn't find
893-
// it or wasn't consulted.
894-
for (final File pathComponent : pathComponents) {
895-
url = getResourceURL(pathComponent, name);
896-
if (url != null) {
897-
log("Resource " + name + " loaded from ant loader", Project.MSG_DEBUG);
898-
break;
899-
}
900-
}
892+
url = getUrl(name);
901893
}
902894
if (url == null && !isParentFirst(name)) {
903895
// this loader was first but it didn't find it - try the parent
@@ -916,6 +908,29 @@ public URL getResource(final String name) {
916908
return url;
917909
}
918910

911+
/**
912+
* Finds a matching file by iterating through path components.
913+
*
914+
* @param name File to find
915+
* @return A <code>URL</code> object for reading the resource, or <code>null</code> if the
916+
* resource could not be found
917+
*/
918+
private URL getUrl(String name) {
919+
URL url = null;
920+
921+
// try and load from this loader if the parent either didn't find
922+
// it or wasn't consulted.
923+
for (final File pathComponent : pathComponents) {
924+
url = getResourceURL(pathComponent, name);
925+
if (url != null) {
926+
log("Resource " + name + " loaded from ant loader", Project.MSG_DEBUG);
927+
break;
928+
}
929+
}
930+
931+
return url;
932+
}
933+
919934
/**
920935
* Finds all the resources with the given name. A resource is some
921936
* data (images, audio, text, etc) that can be accessed by class
@@ -935,6 +950,18 @@ public Enumeration<URL> getNamedResources(final String name)
935950
return findResources(name, false);
936951
}
937952

953+
/**
954+
* Finds the resource with the given name.
955+
*
956+
* @param name The resource name
957+
* @return A <code>URL</code> object for reading the resource, or <code>null</code> if the
958+
* resource could not be found
959+
*/
960+
@Override
961+
protected URL findResource(final String name) {
962+
return getUrl(name);
963+
}
964+
938965
/**
939966
* Returns an enumeration of URLs representing all the resources with the
940967
* given name by searching the class loader's classpath.

0 commit comments

Comments
 (0)