From a454150e7f9cb3aa2ea1ae1b4365ac7de475865e Mon Sep 17 00:00:00 2001 From: Joakim Erdfelt Date: Mon, 17 Oct 2022 15:19:32 -0500 Subject: [PATCH] Improved addClassPath and addJars behavior + Less code + Corrected javadoc --- .../jetty/ee9/webapp/WebAppClassLoader.java | 45 ++++++------------- 1 file changed, 13 insertions(+), 32 deletions(-) diff --git a/jetty-ee9/jetty-ee9-webapp/src/main/java/org/eclipse/jetty/ee9/webapp/WebAppClassLoader.java b/jetty-ee9/jetty-ee9-webapp/src/main/java/org/eclipse/jetty/ee9/webapp/WebAppClassLoader.java index fd2f858f44d..a6c221aec13 100644 --- a/jetty-ee9/jetty-ee9-webapp/src/main/java/org/eclipse/jetty/ee9/webapp/WebAppClassLoader.java +++ b/jetty-ee9/jetty-ee9-webapp/src/main/java/org/eclipse/jetty/ee9/webapp/WebAppClassLoader.java @@ -223,38 +223,20 @@ public class WebAppClassLoader extends URLClassLoader implements ClassVisibility } /** - * @param resource Comma or semicolon separated path of filenames or URLs - * pointing to directories or jar files. Directories should end - * with '/'. + * @param resources The resources to add to the classpath * @throws IOException if unable to add classpath from resource */ - public void addClassPath(Resource resource) + public void addClassPath(Resource resources) throws IOException { - if (resource instanceof ResourceCollection) + for (Resource resource: resources) { - for (Resource r : ((ResourceCollection)resource).getResources()) - { - 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()) - { + if (resource.exists()) addURL(resource.getURI().toURL()); - } else { 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); } } @@ -273,11 +255,7 @@ public class WebAppClassLoader extends URLClassLoader implements ClassVisibility return; List uris = URIUtil.split(classPath); - ResourceCollection rc = _resourceFactory.newResource(uris); - for (Resource resource : rc.getResources()) - { - addClassPath(resource); - } + addClassPath(_resourceFactory.newResource(uris)); } /** @@ -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 * in the specified resource. * - * @param lib the resource that contains the jar and/or zip files. + * @param libs the 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 streamEntries = Files.list(dir)) {