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

Reduce usage of Commons Discovery #582

Merged
merged 1 commit into from
Aug 30, 2024
Merged
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
32 changes: 4 additions & 28 deletions core/src/main/java/org/kohsuke/stapler/Facet.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,14 @@
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.ServiceLoader;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import org.apache.commons.discovery.ResourceNameIterator;
import org.apache.commons.discovery.resource.ClassLoaders;
import org.apache.commons.discovery.resource.names.DiscoverServiceNames;
import org.kohsuke.MetaInfServices;
import org.kohsuke.stapler.event.FilteredDispatchTriggerListener;
import org.kohsuke.stapler.lang.Klass;
Expand Down Expand Up @@ -86,31 +82,11 @@ public static <T> List<T> discoverExtensions(Class<T> type, ClassLoader... cls)
Set<String> classNames = new HashSet<>();

for (ClassLoader cl : cls) {
ClassLoaders classLoaders = new ClassLoaders();
classLoaders.put(cl);
DiscoverServiceNames dc = new DiscoverServiceNames(classLoaders);
ResourceNameIterator itr = dc.findResourceNames(type.getName());
while (itr.hasNext()) {
String name = itr.nextResourceName();
if (!classNames.add(name)) {
for (T extension : ServiceLoader.load(type, cl)) {
if (!classNames.add(extension.getClass().getName())) {
continue; // avoid duplication
}

Class<? extends T> c;
try {
c = cl.loadClass(name).asSubclass(type);
} catch (ClassNotFoundException e) {
LOGGER.log(Level.WARNING, "Failed to load " + name, e);
continue;
}
try {
r.add(c.getDeclaredConstructor().newInstance());
} catch (NoSuchMethodException
| InstantiationException
| IllegalAccessException
| InvocationTargetException e) {
LOGGER.log(Level.WARNING, "Failed to instantiate " + c, e);
}
r.add(extension);
}
}
return r;
Expand Down
Loading