From b1e04bf8d778cd7cf454411bdad6a1f294bab160 Mon Sep 17 00:00:00 2001 From: Dorian Heinrichs Date: Mon, 28 Oct 2019 14:50:19 +0100 Subject: [PATCH 1/2] ResourceConfig is now properly using the ClassLoader specified in PackageNamesScanner to load classes --- .../jersey/server/ResourceConfig.java | 36 +++++++++++++++++-- .../scanning/PackageNamesScanner.java | 4 +++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/core-server/src/main/java/org/glassfish/jersey/server/ResourceConfig.java b/core-server/src/main/java/org/glassfish/jersey/server/ResourceConfig.java index 669e9ef23c..53d63ee450 100644 --- a/core-server/src/main/java/org/glassfish/jersey/server/ResourceConfig.java +++ b/core-server/src/main/java/org/glassfish/jersey/server/ResourceConfig.java @@ -655,6 +655,27 @@ public final ResourceConfig packages(final boolean recursive, final String... pa return registerFinder(new PackageNamesScanner(packages, recursive)); } + /** + * Adds array of package names which will be used to scan for components. + *

+ * Package scanning ignores an inheritance and therefore {@link Path} annotation + * on parent classes and interfaces will be ignored. + *

+ * @param recursive defines whether any nested packages in the collection of specified + * package names should be recursively scanned (value of {@code true}) + * as part of the package scanning or not (value of {@code false}). + * @param classLoader defines the classloader used for scanning the packages and loading the classes. + * @param packages array of package names. + * @return updated resource configuration instance. + * @see #packages(String...) + */ + public final ResourceConfig packages(final boolean recursive, final ClassLoader classLoader, final String... packages) { + if (packages == null || packages.length == 0) { + return this; + } + return registerFinder(new PackageNamesScanner(classLoader, packages, recursive)); + } + /** * Adds array of file and directory names to scan for components. *

@@ -877,11 +898,21 @@ private Set> scanClasses() { rfs.add(new FilesScanner(classPathElements, true)); } - final AnnotationAcceptingListener afl = + final AnnotationAcceptingListener parentAfl = AnnotationAcceptingListener.newJaxrsResourceAndProviderListener(_state.getClassLoader()); + for (final ResourceFinder resourceFinder : rfs) { + AnnotationAcceptingListener afl = parentAfl; + + // Workaround because otherwise classes from a different classloader can't be loaded + if (resourceFinder instanceof PackageNamesScanner) { + final ClassLoader classLoader = ((PackageNamesScanner) resourceFinder).getClassloader(); + afl = AnnotationAcceptingListener.newJaxrsResourceAndProviderListener(classLoader); + } + while (resourceFinder.hasNext()) { final String next = resourceFinder.next(); + if (afl.accept(next)) { final InputStream in = resourceFinder.open(); try { @@ -896,10 +927,11 @@ private Set> scanClasses() { } } } + + result.addAll(afl.getAnnotatedClasses()); } } - result.addAll(afl.getAnnotatedClasses()); return result; } diff --git a/core-server/src/main/java/org/glassfish/jersey/server/internal/scanning/PackageNamesScanner.java b/core-server/src/main/java/org/glassfish/jersey/server/internal/scanning/PackageNamesScanner.java index edabd3dc6d..c2c3f7bf6e 100644 --- a/core-server/src/main/java/org/glassfish/jersey/server/internal/scanning/PackageNamesScanner.java +++ b/core-server/src/main/java/org/glassfish/jersey/server/internal/scanning/PackageNamesScanner.java @@ -164,6 +164,10 @@ public void reset() { init(); } + public ClassLoader getClassloader() { + return classloader; + } + private void init() { compositeResourceFinder = new CompositeResourceFinder(); From 1a11574e9933bdcce12eec277bec361dcb53e12c Mon Sep 17 00:00:00 2001 From: Dorian Heinrichs Date: Tue, 29 Oct 2019 12:13:23 +0100 Subject: [PATCH 2/2] Updated ResourceConfig according to requested changes (#4305) --- .../java/org/glassfish/jersey/server/ResourceConfig.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/core-server/src/main/java/org/glassfish/jersey/server/ResourceConfig.java b/core-server/src/main/java/org/glassfish/jersey/server/ResourceConfig.java index 53d63ee450..bb87f597c5 100644 --- a/core-server/src/main/java/org/glassfish/jersey/server/ResourceConfig.java +++ b/core-server/src/main/java/org/glassfish/jersey/server/ResourceConfig.java @@ -907,7 +907,9 @@ private Set> scanClasses() { // Workaround because otherwise classes from a different classloader can't be loaded if (resourceFinder instanceof PackageNamesScanner) { final ClassLoader classLoader = ((PackageNamesScanner) resourceFinder).getClassloader(); - afl = AnnotationAcceptingListener.newJaxrsResourceAndProviderListener(classLoader); + if (!getClassLoader().equals(classLoader)) { + afl = AnnotationAcceptingListener.newJaxrsResourceAndProviderListener(classLoader); + } } while (resourceFinder.hasNext()) { @@ -928,10 +930,13 @@ private Set> scanClasses() { } } - result.addAll(afl.getAnnotatedClasses()); + if (parentAfl != afl) { + result.addAll(afl.getAnnotatedClasses()); + } } } + result.addAll(parentAfl.getAnnotatedClasses()); return result; }