io.quarkus.arc.runtime.BeanContainerImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of quarkus-arc Show documentation
Show all versions of quarkus-arc Show documentation
Build time CDI dependency injection
package io.quarkus.arc.runtime;
import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.function.Supplier;
import org.jboss.logging.Logger;
import io.quarkus.arc.ArcContainer;
import io.quarkus.arc.InstanceHandle;
import io.quarkus.arc.ManagedContext;
class BeanContainerImpl implements BeanContainer {
private static final Logger LOGGER = Logger.getLogger(BeanContainerImpl.class.getName());
private final ArcContainer container;
BeanContainerImpl(ArcContainer container) {
this.container = container;
}
@Override
public T beanInstance(Class beanType, Annotation... beanQualifiers) {
return container.select(beanType, beanQualifiers).get();
}
@Override
public Factory beanInstanceFactory(Class type, Annotation... qualifiers) {
Supplier> handleSupplier = container.beanInstanceSupplier(type, qualifiers);
return createFactory(handleSupplier, null, type, qualifiers);
}
@Override
public Factory beanInstanceFactory(Supplier> fallbackSupplier, Class type,
Annotation... qualifiers) {
Supplier> handleSupplier = container.beanInstanceSupplier(type, qualifiers);
return createFactory(handleSupplier, fallbackSupplier, type, qualifiers);
}
private Factory createFactory(Supplier> handleSupplier, Supplier> fallbackSupplier,
Class type, Annotation... qualifiers) {
if (handleSupplier == null) {
LOGGER.debugf(
"No matching bean found for type %s and qualifiers %s. The bean might have been marked as unused and removed during build.",
type, Arrays.toString(qualifiers));
if (fallbackSupplier != null) {
return fallbackSupplier.get();
} else {
// by default, if there is no bean, return factory that tries to instantiate non-cdi object
return new DefaultInstanceFactory<>(type);
}
}
return new Factory() {
@Override
public Instance create() {
InstanceHandle handle = handleSupplier.get();
return new Instance() {
@Override
public T get() {
return handle.get();
}
@Override
public void close() {
handle.close();
}
};
}
};
}
@Override
public ManagedContext requestContext() {
return container.requestContext();
}
/**
* A default fallback {@link Factory} implementation used by
* {@link BeanContainer#beanInstanceFactory(Class, Annotation...)}.
*
* This factory attempts to create instances of given class by calling their no-arg constructor. Any exceptions
* related to lack of such constructor of failure to invoke it are simply re-thrown.
*
* @param represents the type that this factory can create
*/
private final class DefaultInstanceFactory implements BeanContainer.Factory {
private final Class type;
DefaultInstanceFactory(Class type) {
this.type = type;
}
@Override
public BeanContainer.Instance create() {
try {
T instance = type.getDeclaredConstructor().newInstance();
return new BeanContainer.Instance() {
@Override
public T get() {
return instance;
}
};
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
throw new RuntimeException(e);
}
}
}
}