Skip to content

Commit

Permalink
Rename affected methods and introduce their deprecated variants with …
Browse files Browse the repository at this point in the history
…original syntax to give users time to adapt
  • Loading branch information
manovotn committed Oct 10, 2022
1 parent 7db25a1 commit 28b74fe
Show file tree
Hide file tree
Showing 21 changed files with 114 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,11 @@ public void setHandlerClass(Class<? extends RequestHandler<?, ?>> handler, BeanC
*/
public static void handle(InputStream inputStream, OutputStream outputStream, Context context) throws IOException {
if (streamHandlerClass != null) {
RequestStreamHandler handler = beanContainer.instance(streamHandlerClass);
RequestStreamHandler handler = beanContainer.beanInstance(streamHandlerClass);
handler.handleRequest(inputStream, outputStream, context);
} else {
Object request = objectReader.readValue(inputStream);
RequestHandler handler = beanContainer.instance(handlerClass);
RequestHandler handler = beanContainer.beanInstance(handlerClass);
Object response = handler.handleRequest(request, context);
objectWriter.writeValue(outputStream, response);
}
Expand Down Expand Up @@ -166,7 +166,7 @@ public void startPollLoop(ShutdownContext context, LaunchMode launchMode) {

@Override
protected Object processRequest(Object input, AmazonLambdaContext context) throws Exception {
RequestHandler handler = beanContainer.instance(handlerClass);
RequestHandler handler = beanContainer.beanInstance(handlerClass);
return handler.handleRequest(input, context);
}

Expand All @@ -188,7 +188,7 @@ protected boolean isStream() {
@Override
protected void processRequest(InputStream input, OutputStream output, AmazonLambdaContext context)
throws Exception {
RequestStreamHandler handler = beanContainer.instance(streamHandlerClass);
RequestStreamHandler handler = beanContainer.beanInstance(streamHandlerClass);
handler.handleRequest(input, output, context);

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public static class TestRecorder {

public void test(BeanContainer beanContainer) {
// This should trigger the warning - Gama was removed
Gama gama = beanContainer.instance(Gama.class);
Gama gama = beanContainer.beanInstance(Gama.class);
// Test that fallback was used - no injection was performed
Assertions.assertNull(gama.beanManager);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,33 @@
public interface BeanContainer {

/**
* Returns a bean instance for given bean type and qualifiers.
* <p/>
* This method follows standard CDI rules meaning that if there are two or more eligible beans, an ambiguous
* dependency exception is thrown.
* Note that the method is allowed to return {@code null} if there is no matching bean which allows
* for fallback implementations.
*
* @param type
* @param qualifiers
* @return a bean instance or {@code null} if no matching bean is found
*/
default <T> T beanInstance(Class<T> type, Annotation... qualifiers) {
return beanInstanceFactory(type, qualifiers).create().get();
}

/**
* This method is deprecated and will be removed in future versions.
* Use {@link #beanInstance(Class, Annotation...)} instead.
* </p>
* As opposed to {@link #beanInstance(Class, Annotation...)}, this method does <b>NOT</b> follow CDI
* resolution rules and in case of ambiguous resolution performs a choice based on the class type parameter.
*
* @param type
* @param qualifiers
* @return a bean instance or {@code null} if no matching bean is found
*/
@Deprecated
default <T> T instance(Class<T> type, Annotation... qualifiers) {
return instanceFactory(type, qualifiers).create().get();
}
Expand All @@ -31,6 +53,20 @@ default <T> T instance(Class<T> type, Annotation... qualifiers) {
* @param qualifiers
* @return a bean instance factory, never {@code null}
*/
<T> Factory<T> beanInstanceFactory(Class<T> type, Annotation... qualifiers);

/**
* This method is deprecated and will be removed in future versions.
* Use {@link #beanInstanceFactory(Class, Annotation...)} instead.
* </p>
* As opposed to {@link #beanInstanceFactory(Class, Annotation...)}, this method does <b>NOT</b> follow CDI
* resolution rules and in case of ambiguous resolution performs a choice based on the class type parameter.
*
* @param type
* @param qualifiers
* @return a bean instance factory, never {@code null}
*/
@Deprecated
<T> Factory<T> instanceFactory(Class<T> type, Annotation... qualifiers);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,19 @@ public BeanContainerImpl(ArcContainer container) {
this.container = container;
}

@Override
public <T> Factory<T> beanInstanceFactory(Class<T> type, Annotation... qualifiers) {
Supplier<InstanceHandle<T>> handleSupplier = container.beanInstanceSupplier(type, qualifiers);
return createFactory(handleSupplier, type, qualifiers);
}

@Override
public <T> Factory<T> instanceFactory(Class<T> type, Annotation... qualifiers) {
Supplier<InstanceHandle<T>> handleSupplier = container.instanceSupplier(type, qualifiers);
return createFactory(handleSupplier, type, qualifiers);
}

private <T> Factory<T> createFactory(Supplier<InstanceHandle<T>> handleSupplier, Class<T> 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.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public void runLoadTask(Runnable runnable) {
}

public void setDomainForIdentityProvider(BeanContainer bc, RuntimeValue<SecurityDomain> domain) {
bc.instance(ElytronSecurityDomainManager.class).setDomain(domain.getValue());
bc.beanInstance(ElytronSecurityDomainManager.class).setDomain(domain.getValue());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class FunqyCloudFunctionsBindingRecorder {

public void init(BeanContainer bc) {
beanContainer = bc;
objectMapper = beanContainer.instance(ObjectMapper.class);
objectMapper = beanContainer.beanInstance(ObjectMapper.class);

for (FunctionInvoker invoker : FunctionRecorder.registry.invokers()) {
if (invoker.hasInput()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public FunctionConstructor(Class<T> cls) {

public T construct() {
if (factory == null)
factory = CONTAINER.instanceFactory(cls);
factory = CONTAINER.beanInstanceFactory(cls);
return factory.create().get();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public DataSourceTenantConnectionResolver get() {
}

public void startAllPersistenceUnits(BeanContainer beanContainer) {
beanContainer.instance(JPAConfig.class).startAll();
beanContainer.beanInstance(JPAConfig.class).startAll();
}

public Supplier<SessionFactory> sessionFactorySupplier(String persistenceUnitName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class InfinispanRecorder {

public BeanContainerListener configureInfinispan(@RelaxedValidation Properties properties) {
return container -> {
InfinispanClientProducer instance = container.instance(InfinispanClientProducer.class);
InfinispanClientProducer instance = container.beanInstance(InfinispanClientProducer.class);
instance.configure(properties);
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public Object construct(boolean unwrapAsync) {
if (factory != null) {
return factory.get().get();
}
factory = Arc.container().instanceSupplier(this.ctor.getDeclaringClass());
factory = Arc.container().beanInstanceSupplier(this.ctor.getDeclaringClass());
if (factory == null) {
return delegate.construct(unwrapAsync);
}
Expand All @@ -45,7 +45,7 @@ public Object construct(HttpRequest request, HttpResponse response, boolean unwr
if (factory != null) {
return factory.get().get();
}
factory = Arc.container().instanceSupplier(this.ctor.getDeclaringClass());
factory = Arc.container().beanInstanceSupplier(this.ctor.getDeclaringClass());
if (factory == null) {
return delegate.construct(request, response, unwrapAsync);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class ArcBeanFactory<T> implements BeanFactory<T> {

public ArcBeanFactory(Class<T> target, BeanContainer beanContainer) {
targetClassName = target.getName();
factory = beanContainer.instanceFactory(target);
factory = beanContainer.beanInstanceFactory(target);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public RuntimeValue<Deployment> createDeployment(DeploymentInfo info,
}

CurrentRequestManager
.setCurrentRequestInstance(new QuarkusCurrentRequest(beanContainer.instance(CurrentVertxRequest.class)));
.setCurrentRequestInstance(new QuarkusCurrentRequest(beanContainer.beanInstance(CurrentVertxRequest.class)));

BlockingOperationSupport.setIoThreadDetector(new BlockingOperationSupport.IOThreadDetector() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ public WebAuthnRecorder(RuntimeValue<HttpConfiguration> httpConfiguration, Runti
}

public void setupRoutes(BeanContainer beanContainer, RuntimeValue<Router> routerValue, String prefix) {
WebAuthnSecurity security = beanContainer.instance(WebAuthnSecurity.class);
WebAuthnAuthenticationMechanism authMech = beanContainer.instance(WebAuthnAuthenticationMechanism.class);
IdentityProviderManager identityProviderManager = beanContainer.instance(IdentityProviderManager.class);
WebAuthnSecurity security = beanContainer.beanInstance(WebAuthnSecurity.class);
WebAuthnAuthenticationMechanism authMech = beanContainer.beanInstance(WebAuthnAuthenticationMechanism.class);
IdentityProviderManager identityProviderManager = beanContainer.beanInstance(IdentityProviderManager.class);
WebAuthnController controller = new WebAuthnController(security, config.getValue(), identityProviderManager, authMech);
Router router = routerValue.getValue();
BodyHandler bodyHandler = BodyHandler.create();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
public class SmallRyeGraphQLRecorder {

public RuntimeValue<Boolean> createExecutionService(BeanContainer beanContainer, Schema schema) {
GraphQLProducer graphQLProducer = beanContainer.instance(GraphQLProducer.class);
GraphQLProducer graphQLProducer = beanContainer.beanInstance(GraphQLProducer.class);
GraphQLSchema graphQLSchema = graphQLProducer.initialize(schema);
return new RuntimeValue<>(graphQLSchema != null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ public void createRegistries(BeanContainer container) {
//HACK: registration is done via statics, but cleanup is done via pre destroy
//however if the bean is not used it will not be created, so no cleanup will be done
//we force bean creation here to make sure the container can restart correctly
container.instance(MetricRegistries.class).getApplicationRegistry();
container.beanInstance(MetricRegistries.class).getApplicationRegistry();
}

public void dropRegistriesAtShutdown(ShutdownContext shutdownContext) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ public RuntimeValue<ServletInfo> registerServlet(RuntimeValue<DeploymentInfo> de
InstanceFactory<? extends Servlet> instanceFactory) throws Exception {

InstanceFactory<? extends Servlet> factory = instanceFactory != null ? instanceFactory
: new QuarkusInstanceFactory(beanContainer.instanceFactory(servletClass));
: new QuarkusInstanceFactory(beanContainer.beanInstanceFactory(servletClass));
ServletInfo servletInfo = new ServletInfo(name, (Class<? extends Servlet>) servletClass,
factory);
deploymentInfo.getValue().addServlet(servletInfo);
Expand Down Expand Up @@ -304,7 +304,7 @@ public RuntimeValue<FilterInfo> registerFilter(RuntimeValue<DeploymentInfo> info
InstanceFactory<? extends Filter> instanceFactory) throws Exception {

InstanceFactory<? extends Filter> factory = instanceFactory != null ? instanceFactory
: new QuarkusInstanceFactory(beanContainer.instanceFactory(filterClass));
: new QuarkusInstanceFactory(beanContainer.beanInstanceFactory(filterClass));
FilterInfo filterInfo = new FilterInfo(name, (Class<? extends Filter>) filterClass, factory);
info.getValue().addFilter(filterInfo);
filterInfo.setAsyncSupported(asyncSupported);
Expand All @@ -329,7 +329,7 @@ public void registerListener(RuntimeValue<DeploymentInfo> info, Class<?> listene
info.getValue()
.addListener(new ListenerInfo((Class<? extends EventListener>) listenerClass,
(InstanceFactory<? extends EventListener>) new QuarkusInstanceFactory<>(
factory.instanceFactory(listenerClass))));
factory.beanInstanceFactory(listenerClass))));
}

public void addMimeMapping(RuntimeValue<DeploymentInfo> info, String extension,
Expand Down Expand Up @@ -457,7 +457,7 @@ public DeploymentManager bootServletContainer(RuntimeValue<DeploymentInfo> info,
info.getValue().setClassIntrospecter(new ClassIntrospecter() {
@Override
public <T> InstanceFactory<T> createInstanceFactory(Class<T> clazz) throws NoSuchMethodException {
BeanContainer.Factory<T> res = beanContainer.instanceFactory(clazz);
BeanContainer.Factory<T> res = beanContainer.beanInstanceFactory(clazz);
if (res == null) {
return defaultVal.createInstanceFactory(clazz);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ public BeanContainerListener initPermissions(HttpBuildTimeConfig permissions,
return new BeanContainerListener() {
@Override
public void created(BeanContainer container) {
container.instance(PathMatchingHttpSecurityPolicy.class).init(permissions, policies);
container.beanInstance(PathMatchingHttpSecurityPolicy.class).init(permissions, policies);
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public RuntimeValue<ServerWebSocketContainer> createServerContainer(BeanContaine
ServerWebSocketContainer container = serverContainerFactory.create(new ObjectIntrospecter() {
@Override
public <T> ObjectFactory<T> createInstanceFactory(Class<T> clazz) {
BeanContainer.Factory<T> factory = beanContainer.instanceFactory(clazz);
BeanContainer.Factory<T> factory = beanContainer.beanInstanceFactory(clazz);
return new ObjectFactory<T>() {
@Override
public ObjectHandle<T> createInstance() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,21 @@ public interface ArcContainer {
* @param <T>
* @return
*/
<T> Supplier<InstanceHandle<T>> beanInstanceSupplier(Class<T> type, Annotation... qualifiers);

/**
* This method is deprecated and will be removed in future versions.
* Use {@link #beanInstanceSupplier(Class, Annotation...)} instead.
* </p>
* As opposed to {@link #beanInstanceSupplier(Class, Annotation...)}, this method does <b>NOT</b> follow CDI
* resolution rules and in case of ambiguous resolution performs a choice based on the class type parameter.
*
* @param type
* @param qualifiers
* @return
* @param <T>
*/
@Deprecated
<T> Supplier<InstanceHandle<T>> instanceSupplier(Class<T> type, Annotation... qualifiers);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,17 +235,41 @@ public <X> InstanceHandle<X> instance(Type type, Annotation... qualifiers) {
}

@SuppressWarnings("unchecked")
@Override
public <T> Supplier<InstanceHandle<T>> beanInstanceSupplier(Class<T> type, Annotation... qualifiers) {
return createInstanceSupplier(false, type, qualifiers);
}

@Override
public <T> Supplier<InstanceHandle<T>> instanceSupplier(Class<T> type, Annotation... qualifiers) {
return createInstanceSupplier(true, type, qualifiers);
}

private <T> Supplier<InstanceHandle<T>> createInstanceSupplier(boolean resolveAmbiguities, Class<T> type,
Annotation... qualifiers) {
if (qualifiers == null || qualifiers.length == 0) {
qualifiers = new Annotation[] { Default.Literal.INSTANCE };
}
Set<InjectableBean<?>> resolvedBeans = resolved.getValue(new Resolvable(type, qualifiers));
Set<InjectableBean<?>> filteredBean = resolvedBeans;
if (resolvedBeans.size() > 1) {
throw new AmbiguousResolutionException("Beans: " + resolvedBeans);
if (resolveAmbiguities) {
// this is non-standard CDI behavior that we momentarily keep to retain compatibility
// if there are multiple beans we look for an exact match
// this method is only called with the exact type required
// so ignoring subclasses is the correct behaviour
filteredBean = new HashSet<>();
for (InjectableBean<?> i : resolvedBeans) {
if (i.getBeanClass().equals(type)) {
filteredBean.add(i);
}
}
} else {
throw new AmbiguousResolutionException("Beans: " + resolvedBeans);
}
}
InjectableBean<T> bean = resolvedBeans.size() != 1 ? null
: (InjectableBean<T>) resolvedBeans.iterator().next();
InjectableBean<T> bean = filteredBean.size() != 1 ? null
: (InjectableBean<T>) filteredBean.iterator().next();
if (bean == null) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void startRuntimeService(ShutdownContext shutdownContext, RuntimeValue<Ru
* @param beanContainer - CDI bean container
*/
public void loadDSAPublicKeyProducer(DSAPublicKey publicKey, BeanContainer beanContainer) {
PublicKeyProducer keyProducer = beanContainer.instance(PublicKeyProducer.class);
PublicKeyProducer keyProducer = beanContainer.beanInstance(PublicKeyProducer.class);
keyProducer.setPublicKey(publicKey);
}

Expand Down

0 comments on commit 28b74fe

Please sign in to comment.