Improved addClassPath and addJars behavior

+ Less code
+ Corrected javadoc
This commit is contained in:
Joakim Erdfelt 2022-10-17 15:22:16 -05:00
parent bb887c9c81
commit 62db79b791
No known key found for this signature in database
GPG Key ID: 2D0E1FB8FE4B68B4
1 changed files with 14 additions and 33 deletions

View File

@ -223,38 +223,20 @@ public class WebAppClassLoader extends URLClassLoader implements ClassVisibility
} }
/** /**
* @param resource Comma or semicolon separated path of filenames or URLs * @param resources The resources to add to the classpath
* pointing to directories or jar files. Directories should end
* with '/'.
* @throws IOException if unable to add classpath from resource * @throws IOException if unable to add classpath from resource
*/ */
public void addClassPath(Resource resource) public void addClassPath(Resource resources)
throws IOException throws IOException
{ {
if (resource instanceof ResourceCollection) for (Resource resource: resources)
{ {
for (Resource r : ((ResourceCollection)resource).getResources()) if (resource != null && resource.exists())
{
addClassPath(r);
}
}
else
{
// Resolve file path if possible
Path path = resource.getPath();
if (path != null)
{
URL url = resource.getURI().toURL();
addURL(url);
}
else if (resource.isDirectory())
{
addURL(resource.getURI().toURL()); addURL(resource.getURI().toURL());
}
else else
{ {
if (LOG.isDebugEnabled()) if (LOG.isDebugEnabled())
LOG.debug("Check file exists and is not nested jar: {}", resource); LOG.debug("Check resource exists and is not a nested jar: {}", resource);
throw new IllegalArgumentException("File not resolvable or incompatible with URLClassloader: " + resource); throw new IllegalArgumentException("File not resolvable or incompatible with URLClassloader: " + resource);
} }
} }
@ -273,11 +255,7 @@ public class WebAppClassLoader extends URLClassLoader implements ClassVisibility
return; return;
List<URI> uris = URIUtil.split(classPath); List<URI> uris = URIUtil.split(classPath);
ResourceCollection rc = _resourceFactory.newResource(uris); addClassPath(_resourceFactory.newResource(uris));
for (Resource resource : rc.getResources())
{
addClassPath(resource);
}
} }
/** /**
@ -298,13 +276,16 @@ public class WebAppClassLoader extends URLClassLoader implements ClassVisibility
* Add elements to the class path for the context from the jar and zip files found * Add elements to the class path for the context from the jar and zip files found
* in the specified resource. * in the specified resource.
* *
* @param lib the resource that contains the jar and/or zip files. * @param libs the directory resource that contains the jar and/or zip files.
*/ */
public void addJars(Resource lib) public void addJars(Resource libs)
{ {
if (lib.exists() && lib.isDirectory()) if (libs == null || !libs.exists() || !libs.isDirectory())
return;
for (Resource libDir: libs)
{ {
Path dir = lib.getPath(); Path dir = libDir.getPath();
try (Stream<Path> streamEntries = Files.list(dir)) try (Stream<Path> streamEntries = Files.list(dir))
{ {
@ -314,7 +295,7 @@ public class WebAppClassLoader extends URLClassLoader implements ClassVisibility
.sorted(Comparator.naturalOrder()) .sorted(Comparator.naturalOrder())
.toList(); .toList();
for (Path jar: jars) for (Path jar : jars)
{ {
try try
{ {