Skip to content

Commit

Permalink
[WFLY-11381] Refactoring WeldComponentService to use non deprecated S…
Browse files Browse the repository at this point in the history
…erviceBuilder methods.
  • Loading branch information
ropalka committed Nov 20, 2018
1 parent 64137ef commit 79be914
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 26 deletions.
Expand Up @@ -85,6 +85,7 @@
* to components that are part of a bean archive.
*
* @author Stuart Douglas
* @author <a href="mailto:ropalka@redhat.com">Richard Opalka</a>
*/
public class WeldComponentIntegrationProcessor implements DeploymentUnitProcessor {

Expand Down Expand Up @@ -176,13 +177,8 @@ private boolean isComponentWithView(ComponentDescription component, Iterable<Com
* As the weld based instantiator needs access to the bean manager it is installed as a service.
*/
private void addWeldIntegration(final Iterable<ComponentIntegrator> componentIntegrators, final ComponentInterceptorSupport componentInterceptorSupport, final ServiceTarget target, final ComponentConfiguration configuration, final ComponentDescription description, final Class<?> componentClass, final String beanName, final ServiceName weldServiceName, final ServiceName weldStartService, final ServiceName beanManagerService, final Set<Class<?>> interceptorClasses, final ClassLoader classLoader, final String beanDeploymentArchiveId) {

final ServiceName serviceName = configuration.getComponentDescription().getServiceName().append("WeldInstantiator");

final WeldComponentService weldComponentService = new WeldComponentService(componentClass, beanName, interceptorClasses, classLoader,
beanDeploymentArchiveId, description.isCDIInterceptorEnabled(), description, isComponentWithView(description, componentIntegrators));
final ServiceBuilder<WeldComponentService> builder = target.addService(serviceName, weldComponentService)
.addDependency(weldServiceName, WeldBootstrapService.class, weldComponentService.getWeldContainer());
final ServiceBuilder<?> builder = target.addService(serviceName);
builder.requires(weldStartService);

configuration.setInstanceFactory(WeldManagedReferenceFactory.INSTANCE);
Expand Down Expand Up @@ -221,6 +217,13 @@ public void configureDependency(final ServiceBuilder<?> serviceBuilder, Componen
}
}

final Supplier<WeldBootstrapService> weldContainerSupplier = builder.requires(weldServiceName);
final WeldComponentService weldComponentService =
new WeldComponentService(weldContainerSupplier, componentClass, beanName, interceptorClasses, classLoader,
beanDeploymentArchiveId, description.isCDIInterceptorEnabled(),
description, isComponentWithView(description, componentIntegrators));
builder.setInstance(weldComponentService);

if (!isComponentIntegrationPerformed) {
description.setIgnoreLifecycleInterceptors(true); //otherwise they will be called twice
// for components with no view register interceptors that delegate to InjectionTarget lifecycle methods to trigger lifecycle interception
Expand Down Expand Up @@ -256,15 +259,15 @@ private static ServiceName addWeldInterceptorBindingService(final ServiceTarget
return bindingServiceName;
}

private static void addJsr299BindingsCreateInterceptor(final ComponentConfiguration configuration, final ComponentDescription description, final String beanName, final ServiceName weldServiceName, ServiceBuilder<WeldComponentService> builder, final ServiceName bindingServiceName, final ComponentInterceptorSupport componentInterceptorSupport) {
private static void addJsr299BindingsCreateInterceptor(final ComponentConfiguration configuration, final ComponentDescription description, final String beanName, final ServiceName weldServiceName, ServiceBuilder<?> builder, final ServiceName bindingServiceName, final ComponentInterceptorSupport componentInterceptorSupport) {
//add the create interceptor that creates the CDI interceptors
final Jsr299BindingsCreateInterceptor createInterceptor = new Jsr299BindingsCreateInterceptor(description.getBeanDeploymentArchiveId(), beanName, componentInterceptorSupport);
configuration.addPostConstructInterceptor(new ImmediateInterceptorFactory(createInterceptor), InterceptorOrder.ComponentPostConstruct.CREATE_CDI_INTERCEPTORS);
builder.addDependency(weldServiceName, WeldBootstrapService.class, createInterceptor.getWeldContainer());
builder.addDependency(bindingServiceName, InterceptorBindings.class, createInterceptor.getInterceptorBindings());
}

private static void addCommonLifecycleInterceptionSupport(final ComponentConfiguration configuration, ServiceBuilder<WeldComponentService> builder, final ServiceName bindingServiceName, final ServiceName beanManagerServiceName, final ComponentInterceptorSupport componentInterceptorSupport) {
private static void addCommonLifecycleInterceptionSupport(final ComponentConfiguration configuration, ServiceBuilder<?> builder, final ServiceName bindingServiceName, final ServiceName beanManagerServiceName, final ComponentInterceptorSupport componentInterceptorSupport) {
configuration.addPreDestroyInterceptor(factory(InterceptionType.PRE_DESTROY, builder, bindingServiceName, componentInterceptorSupport), InterceptorOrder.ComponentPreDestroy.CDI_INTERCEPTORS);
configuration.addAroundConstructInterceptor(factory(InterceptionType.AROUND_CONSTRUCT, builder, bindingServiceName, componentInterceptorSupport), InterceptorOrder.AroundConstruct.WELD_AROUND_CONSTRUCT_INTERCEPTORS);
configuration.addPostConstructInterceptor(factory(InterceptionType.POST_CONSTRUCT, builder, bindingServiceName, componentInterceptorSupport), InterceptorOrder.ComponentPostConstruct.CDI_INTERCEPTORS);
Expand Down
Expand Up @@ -25,6 +25,7 @@
import java.util.Map;
import java.util.ServiceLoader;
import java.util.Set;
import java.util.function.Supplier;

import javax.enterprise.inject.spi.AnnotatedType;
import javax.enterprise.inject.spi.Bean;
Expand All @@ -33,11 +34,10 @@
import org.jboss.as.ee.component.ComponentDescription;
import org.jboss.as.weld.WeldBootstrapService;
import org.jboss.as.weld.spi.ComponentSupport;
import org.jboss.msc.service.Service;
import org.jboss.msc.Service;
import org.jboss.msc.service.StartContext;
import org.jboss.msc.service.StartException;
import org.jboss.msc.service.StopContext;
import org.jboss.msc.value.InjectedValue;
import org.jboss.weld.bean.SessionBean;
import org.jboss.weld.ejb.spi.EjbDescriptor;
import org.jboss.weld.injection.producer.InjectionTargetService;
Expand All @@ -49,14 +49,15 @@
* Interceptor that attaches all the nessesary information for weld injection to the interceptor context
*
* @author Stuart Douglas
* @author <a href="mailto:ropalka@redhat.com">Richard Opalka</a>
*/
public class WeldComponentService implements Service<WeldComponentService> {
public class WeldComponentService implements Service {

private final Class<?> componentClass;
private final InjectedValue<WeldBootstrapService> weldContainer;
private final Supplier<WeldBootstrapService> weldContainerSupplier;
private final String ejbName;
private final Set<Class<?>> interceptorClasses;
private final Map<Class<?>, InjectionTarget> interceptorInjections = new HashMap<Class<?>, InjectionTarget>();
private final Map<Class<?>, InjectionTarget> interceptorInjections = new HashMap<>();
private final ClassLoader classLoader;
private final String beanDeploymentArchiveId;
private final ComponentDescription componentDescription;
Expand All @@ -73,19 +74,19 @@ public class WeldComponentService implements Service<WeldComponentService> {
private Bean<?> bean;
private BeanManagerImpl beanManager;

public WeldComponentService(Class<?> componentClass, String ejbName, final Set<Class<?>> interceptorClasses, final ClassLoader classLoader, final String beanDeploymentArchiveId, final boolean delegateProduce, ComponentDescription componentDescription, boolean isComponentWithView) {
public WeldComponentService(final Supplier<WeldBootstrapService> weldContainerSupplier, final Class<?> componentClass, String ejbName, final Set<Class<?>> interceptorClasses, final ClassLoader classLoader, final String beanDeploymentArchiveId, final boolean delegateProduce, ComponentDescription componentDescription, final boolean isComponentWithView) {
this.weldContainerSupplier = weldContainerSupplier;
this.componentClass = componentClass;
this.ejbName = ejbName;
this.beanDeploymentArchiveId = beanDeploymentArchiveId;
this.delegateProduce = delegateProduce;
this.weldContainer = new InjectedValue<WeldBootstrapService>();
this.interceptorClasses = interceptorClasses;
this.classLoader = classLoader;
this.componentDescription = componentDescription;
this.isComponentWithView = isComponentWithView;
}

public WeldInjectionContext createInjectionContext() {
WeldInjectionContext createInjectionContext() {
return new WeldInjectionContext(beanManager.createCreationalContext(bean), bean, delegateProduce, injectionTarget, interceptorInjections);
}

Expand All @@ -94,7 +95,7 @@ public synchronized void start(final StartContext context) throws StartException
final ClassLoader cl = WildFlySecurityManager.getCurrentContextClassLoaderPrivileged();
try {
WildFlySecurityManager.setCurrentContextClassLoaderPrivileged(classLoader);
beanManager = weldContainer.getValue().getBeanManager(beanDeploymentArchiveId);
beanManager = weldContainerSupplier.get().getBeanManager(beanDeploymentArchiveId);

for (final Class<?> interceptor : interceptorClasses) {
AnnotatedType<?> type = beanManager.createAnnotatedType(interceptor);
Expand Down Expand Up @@ -144,16 +145,8 @@ public synchronized void stop(final StopContext context) {
bean = null;
}

@Override
public synchronized WeldComponentService getValue() throws IllegalStateException, IllegalArgumentException {
return this;
}

public InjectedValue<WeldBootstrapService> getWeldContainer() {
return weldContainer;
}

public InjectionTarget getInjectionTarget() {
return injectionTarget;
}

}

0 comments on commit 79be914

Please sign in to comment.