Skip to content

Commit

Permalink
ResourceConfig is now properly using the ClassLoader specified in Pac…
Browse files Browse the repository at this point in the history
…kageNamesScanner to load classes

Signed-off-by: Dorian Heinrichs <[email protected]>
  • Loading branch information
d0x7 authored and jansupol committed Nov 5, 2019
1 parent 1504174 commit 72c27bc
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
* <p/>
* Package scanning ignores an inheritance and therefore {@link Path} annotation
* on parent classes and interfaces will be ignored.
* <p/>
* @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.
* <p/>
Expand Down Expand Up @@ -877,9 +898,19 @@ private Set<Class<?>> 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;

if (resourceFinder instanceof PackageNamesScanner) {
final ClassLoader classLoader = ((PackageNamesScanner) resourceFinder).getClassloader();
if (!getClassLoader().equals(classLoader)) {
afl = AnnotationAcceptingListener.newJaxrsResourceAndProviderListener(classLoader);
}
}

while (resourceFinder.hasNext()) {
final String next = resourceFinder.next();
if (afl.accept(next)) {
Expand All @@ -897,9 +928,13 @@ private Set<Class<?>> scanClasses() {
}
}
}

if (afl != parentAfl) {
result.addAll(afl.getAnnotatedClasses());
}
}

result.addAll(afl.getAnnotatedClasses());
result.addAll(parentAfl.getAnnotatedClasses());
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@ public void reset() {
init();
}

public ClassLoader getClassloader() {
return classloader;
}

private void init() {
compositeResourceFinder = new CompositeResourceFinder();

Expand Down

0 comments on commit 72c27bc

Please sign in to comment.