Skip to content

Go back to regular ServiceLoader #339

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

Merged
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
34 changes: 2 additions & 32 deletions inject/src/main/java/io/avaje/inject/DBeanScopeBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,10 @@
import io.avaje.lang.Nullable;
import jakarta.inject.Provider;

import java.io.*;
import java.lang.reflect.Type;
import java.net.URL;
import java.util.*;
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static java.lang.System.Logger.Level.DEBUG;

Expand Down Expand Up @@ -191,7 +187,7 @@ private void initClassLoader() {

private void initPropertyPlugin() {
propertyRequiresPlugin =
serviceLoad(PropertyRequiresPlugin.class)
ServiceLoader.load(PropertyRequiresPlugin.class, classLoader)
.findFirst()
.orElse(defaultPropertyPlugin());
}
Expand All @@ -218,7 +214,7 @@ public BeanScope build() {
initPropertyPlugin();
}

serviceLoad(Plugin.class).forEach(plugin -> plugin.apply(this));
ServiceLoader.load(Plugin.class, classLoader).forEach(plugin -> plugin.apply(this));
// sort factories by dependsOn
FactoryOrder factoryOrder = new FactoryOrder(parent, includeModules, !suppliedBeans.isEmpty());
if (factoryOrder.isEmpty()) {
Expand All @@ -245,32 +241,6 @@ public BeanScope build() {
return builder.build(shutdownHook, start);
}

private <P> Stream<P> serviceLoad(Class<P> pluginClass) {
return classLoader
.resources("META-INF/services/" + pluginClass.getCanonicalName())
.flatMap(this::resourceLines)
.map(this::serviceInstance);
}

@SuppressWarnings("unchecked")
private <P> P serviceInstance(String className) {
try {
final var clazz = classLoader.loadClass(className);
return (P) clazz.getDeclaredConstructor().newInstance();
} catch (Throwable e) {
throw new RuntimeException(e);
}
}

private Stream<String> resourceLines(URL url) {
try (InputStream is = url.openStream()) {
final var reader = new LineNumberReader(new InputStreamReader(is));
return reader.lines().collect(Collectors.toList()).stream();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

/**
* Return the type that we map the supplied bean to.
*/
Expand Down