Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #4304: ResourceConfig not properly using specified ClassLoader #4305

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,11 +898,23 @@ 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;

// Workaround because otherwise classes from a different classloader can't be loaded
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)) {
final InputStream in = resourceFinder.open();
try {
Expand All @@ -896,10 +929,14 @@ private Set<Class<?>> scanClasses() {
}
}
}

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

result.addAll(afl.getAnnotatedClasses());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+result.addAll(parentAfl.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