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

[MNG-8220] Fix loading DI-powered beans from extensions #1683

Merged
merged 1 commit into from
Aug 29, 2024
Merged
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 @@ -55,13 +55,16 @@
import org.codehaus.plexus.component.repository.exception.ComponentLookupException;

@Named
class SisuDiBridgeModule extends AbstractModule {
public class SisuDiBridgeModule extends AbstractModule {

InjectorImpl injector;
final Set<String> loaded = new HashSet<>();

@Override
protected void configure() {
Provider<PlexusContainer> containerProvider = getProvider(PlexusContainer.class);

InjectorImpl injector = new InjectorImpl() {
injector = new InjectorImpl() {
@Override
public <Q> Supplier<Q> getCompiledBinding(Key<Q> key) {
Set<Binding<Q>> res = getBindings(key);
Expand Down Expand Up @@ -142,38 +145,12 @@ public <Q> Supplier<Q> getCompiledBinding(Key<Q> key) {
});
injector.bindInstance(Injector.class, injector);
bind(Injector.class).toInstance(injector);
bind(SisuDiBridgeModule.class).toInstance(this);
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
if (classLoader == null) {
classLoader = getClass().getClassLoader();
}
try {
for (Iterator<URL> it = classLoader
.getResources("META-INF/maven/org.apache.maven.api.di.Inject")
.asIterator();
it.hasNext(); ) {
URL url = it.next();
List<String> lines;
try (InputStream is = url.openStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8))) {
lines = reader.lines()
.map(String::trim)
.filter(s -> !s.isEmpty() && !s.startsWith("#"))
.toList();
}
for (String className : lines) {
try {
Class<?> clazz = classLoader.loadClass(className);
injector.bindImplicit(clazz);
} catch (ClassNotFoundException e) {
// ignore
e.printStackTrace();
}
}
}

} catch (IOException e) {
throw new MavenException(e);
}
loadFromClassLoader(classLoader);
injector.getBindings().keySet().stream()
.filter(k -> k.getQualifier() != null)
.sorted(Comparator.comparing(k -> k.getRawType().getName()))
Expand All @@ -185,7 +162,7 @@ public <Q> Supplier<Q> getCompiledBinding(Key<Q> key) {
: (Class<Object>) (clazz.getInterfaces().length > 0 ? clazz.getInterfaces()[0] : clazz));
if (itf != null) {
AnnotatedBindingBuilder<Object> binder = bind(itf);
if (key.getQualifier() instanceof String s) {
if (key.getQualifier() instanceof String s && !s.isEmpty()) {
binder.annotatedWith(Names.named(s));
} else if (key.getQualifier() instanceof Annotation a) {
binder.annotatedWith(a);
Expand All @@ -194,4 +171,37 @@ public <Q> Supplier<Q> getCompiledBinding(Key<Q> key) {
}
});
}

public void loadFromClassLoader(ClassLoader classLoader) {
try {
for (Iterator<URL> it = classLoader
.getResources("META-INF/maven/org.apache.maven.api.di.Inject")
.asIterator();
it.hasNext(); ) {
URL url = it.next();
if (loaded.add(url.toExternalForm())) {
List<String> lines;
try (InputStream is = url.openStream();
BufferedReader reader =
new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8))) {
lines = reader.lines()
.map(String::trim)
.filter(s -> !s.isEmpty() && !s.startsWith("#"))
.toList();
}
for (String className : lines) {
try {
Class<?> clazz = classLoader.loadClass(className);
injector.bindImplicit(clazz);
} catch (ClassNotFoundException e) {
// ignore
e.printStackTrace();
}
}
}
}
} catch (IOException e) {
throw new MavenException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ private Class<?>[] getInterfaces(Class<?> superType) {
} else {
for (Annotation a : superType.getAnnotations()) {
Class<? extends Annotation> annotationType = a.annotationType();
if ("org.eclipse.sisu.Typed".equals(annotationType.getName())
if ("org.apache.maven.api.di.Typed".equals(annotationType.getName())
|| "org.eclipse.sisu.Typed".equals(annotationType.getName())
|| "javax.enterprise.inject.Typed".equals(annotationType.getName())
|| "jakarta.enterprise.inject.Typed".equals(annotationType.getName())) {
try {
Expand Down
13 changes: 10 additions & 3 deletions maven-di/src/main/java/org/apache/maven/di/Key.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,15 @@ public <U> Key<U> getTypeParameter(int index) {
* and prepended qualifier display string if this key has a qualifier.
*/
public String getDisplayString() {
return (qualifier != null ? Utils.getDisplayString(qualifier) + " " : "")
+ ReflectionUtils.getDisplayName(type);
return (qualifier != null ? getQualifierDisplayString() + " " : "") + ReflectionUtils.getDisplayName(type);
}

private String getQualifierDisplayString() {
if (qualifier instanceof String s) {
return s.isEmpty() ? "@Named" : "@Named(\"" + s + "\")";
}
String s = Utils.getDisplayString(qualifier);
return s;
}

@Override
Expand All @@ -155,6 +162,6 @@ public int hashCode() {

@Override
public String toString() {
return (qualifier != null ? qualifier + " " : "") + type.getTypeName();
return getDisplayString();
}
}
17 changes: 10 additions & 7 deletions maven-di/src/main/java/org/apache/maven/di/impl/InjectorImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,15 +137,18 @@ private Injector doBind(Key<?> key, Binding<?> binding) {
current.add(key);
throw new DIException("Circular references: " + current);
}
doBindImplicit(key, binding);
Class<?> cls = key.getRawType().getSuperclass();
while (cls != Object.class && cls != null) {
key = Key.of(cls, key.getQualifier());
try {
doBindImplicit(key, binding);
cls = cls.getSuperclass();
Class<?> cls = key.getRawType().getSuperclass();
while (cls != Object.class && cls != null) {
key = Key.of(cls, key.getQualifier());
doBindImplicit(key, binding);
cls = cls.getSuperclass();
}
return this;
} finally {
current.remove(key);
}
current.remove(key);
return this;
}

protected <U> Injector bind(Key<U> key, Binding<U> b) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
import org.apache.maven.execution.scope.internal.MojoExecutionScopeModule;
import org.apache.maven.extension.internal.CoreExports;
import org.apache.maven.extension.internal.CoreExtensionEntry;
import org.apache.maven.internal.impl.SisuDiBridgeModule;
import org.apache.maven.jline.JLineMessageBuilderFactory;
import org.apache.maven.jline.MessageUtils;
import org.apache.maven.lifecycle.LifecycleExecutionException;
Expand Down Expand Up @@ -715,6 +716,7 @@ public Object getValue(String expression) {
new SessionScopeModule(container.lookup(SessionScope.class)),
new MojoExecutionScopeModule(container.lookup(MojoExecutionScope.class)),
new ExtensionConfigurationModule(extension, extensionSource));
container.lookup(SisuDiBridgeModule.class).loadFromClassLoader(extension.getClassRealm());
}

customizeContainer(container);
Expand Down