Search in sources :

Example 1 with ComponentIntegrator

use of org.jboss.as.weld.spi.ComponentIntegrator in project wildfly by wildfly.

the class WeldComponentIntegrationProcessor method addWeldIntegration.

/**
 * 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 ServiceBuilder<?> builder = target.addService(serviceName);
    builder.requires(weldStartService);
    configuration.setInstanceFactory(WeldManagedReferenceFactory.INSTANCE);
    configuration.getStartDependencies().add(new DependencyConfigurator<ComponentStartService>() {

        @Override
        public void configureDependency(final ServiceBuilder<?> serviceBuilder, ComponentStartService service) throws DeploymentUnitProcessingException {
            serviceBuilder.requires(serviceName);
        }
    });
    boolean isComponentIntegrationPerformed = false;
    for (ComponentIntegrator componentIntegrator : componentIntegrators) {
        Supplier<ServiceName> bindingServiceNameSupplier = () -> {
            if (componentInterceptorSupport == null) {
                throw WeldLogger.DEPLOYMENT_LOGGER.componentInterceptorSupportNotAvailable(componentClass);
            }
            return addWeldInterceptorBindingService(target, configuration, componentClass, beanName, weldServiceName, weldStartService, beanDeploymentArchiveId, componentInterceptorSupport);
        };
        DefaultInterceptorIntegrationAction integrationAction = (bindingServiceName) -> {
            if (componentInterceptorSupport == null) {
                throw WeldLogger.DEPLOYMENT_LOGGER.componentInterceptorSupportNotAvailable(componentClass);
            }
            addJsr299BindingsCreateInterceptor(configuration, description, beanName, weldServiceName, builder, bindingServiceName, componentInterceptorSupport);
            addCommonLifecycleInterceptionSupport(configuration, builder, bindingServiceName, beanManagerService, componentInterceptorSupport);
            configuration.addComponentInterceptor(new UserInterceptorFactory(factory(InterceptionType.AROUND_INVOKE, builder, bindingServiceName, componentInterceptorSupport), factory(InterceptionType.AROUND_TIMEOUT, builder, bindingServiceName, componentInterceptorSupport)), InterceptorOrder.Component.CDI_INTERCEPTORS, false);
        };
        if (componentIntegrator.integrate(beanManagerService, configuration, description, builder, bindingServiceNameSupplier, integrationAction, componentInterceptorSupport)) {
            isComponentIntegrationPerformed = true;
            break;
        }
    }
    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) {
        // otherwise they will be called twice
        description.setIgnoreLifecycleInterceptors(true);
        // for components with no view register interceptors that delegate to InjectionTarget lifecycle methods to trigger lifecycle interception
        configuration.addPostConstructInterceptor(new ImmediateInterceptorFactory(new AbstractInjectionTargetDelegatingInterceptor() {

            @Override
            protected void run(Object instance) {
                weldComponentService.getInjectionTarget().postConstruct(instance);
            }
        }), InterceptorOrder.ComponentPostConstruct.CDI_INTERCEPTORS);
        configuration.addPreDestroyInterceptor(new ImmediateInterceptorFactory(new AbstractInjectionTargetDelegatingInterceptor() {

            @Override
            protected void run(Object instance) {
                weldComponentService.getInjectionTarget().preDestroy(instance);
            }
        }), InterceptorOrder.ComponentPreDestroy.CDI_INTERCEPTORS);
    }
    builder.install();
    configuration.addPostConstructInterceptor(new ImmediateInterceptorFactory(new WeldInjectionContextInterceptor(weldComponentService)), InterceptorOrder.ComponentPostConstruct.WELD_INJECTION_CONTEXT_INTERCEPTOR);
    configuration.addPostConstructInterceptor(new ImmediateInterceptorFactory(new WeldInterceptorInjectionInterceptor(interceptorClasses)), InterceptorOrder.ComponentPostConstruct.INTERCEPTOR_WELD_INJECTION);
    configuration.addPostConstructInterceptor(WeldInjectionInterceptor.FACTORY, InterceptorOrder.ComponentPostConstruct.COMPONENT_WELD_INJECTION);
}
Also used : DeploymentUnitProcessingException(org.jboss.as.server.deployment.DeploymentUnitProcessingException) WeldInterceptorInjectionInterceptor(org.jboss.as.weld.injection.WeldInterceptorInjectionInterceptor) DeploymentPhaseContext(org.jboss.as.server.deployment.DeploymentPhaseContext) UserInterceptorFactory(org.jboss.as.ee.component.interceptors.UserInterceptorFactory) DeploymentUnit(org.jboss.as.server.deployment.DeploymentUnit) InterceptorDescription(org.jboss.as.ee.component.InterceptorDescription) WeldDeploymentMarker(org.jboss.as.weld._private.WeldDeploymentMarker) Jsr299BindingsInterceptor.factory(org.jboss.as.weld.interceptors.Jsr299BindingsInterceptor.factory) WeldManagedReferenceFactory(org.jboss.as.weld.injection.WeldManagedReferenceFactory) ManagedReference(org.jboss.as.naming.ManagedReference) ServiceNames(org.jboss.as.weld.ServiceNames) InterceptorOrder(org.jboss.as.ee.component.interceptors.InterceptorOrder) ServiceTarget(org.jboss.msc.service.ServiceTarget) WeldLogger(org.jboss.as.weld.logging.WeldLogger) ComponentStartService(org.jboss.as.ee.component.ComponentStartService) WeldComponentService(org.jboss.as.weld.injection.WeldComponentService) Set(java.util.Set) ServiceLoader(java.util.ServiceLoader) WeldBootstrapService(org.jboss.as.weld.WeldBootstrapService) InterceptorContext(org.jboss.invocation.InterceptorContext) ComponentDescription(org.jboss.as.ee.component.ComponentDescription) WeldInterceptorBindingsService(org.jboss.as.weld.ejb.WeldInterceptorBindingsService) WildFlySecurityManager(org.wildfly.security.manager.WildFlySecurityManager) Module(org.jboss.modules.Module) ServiceName(org.jboss.msc.service.ServiceName) WeldInjectionInterceptor(org.jboss.as.weld.injection.WeldInjectionInterceptor) ComponentIntegrator(org.jboss.as.weld.spi.ComponentIntegrator) Interceptor(org.jboss.invocation.Interceptor) DependencyConfigurator(org.jboss.as.ee.component.DependencyConfigurator) WeldConstructionStartInterceptor(org.jboss.as.weld.injection.WeldConstructionStartInterceptor) Supplier(java.util.function.Supplier) Utils.getRootDeploymentUnit(org.jboss.as.weld.util.Utils.getRootDeploymentUnit) HashSet(java.util.HashSet) DefaultInterceptorIntegrationAction(org.jboss.as.weld.spi.ComponentIntegrator.DefaultInterceptorIntegrationAction) ComponentInterceptorSupport(org.jboss.as.weld.spi.ComponentInterceptorSupport) InterceptorBindings(org.jboss.weld.ejb.spi.InterceptorBindings) WeldStartService(org.jboss.as.weld.WeldStartService) DeploymentUnitProcessor(org.jboss.as.server.deployment.DeploymentUnitProcessor) EjbRequestScopeActivationInterceptor(org.jboss.as.weld.ejb.EjbRequestScopeActivationInterceptor) ImmediateInterceptorFactory(org.jboss.invocation.ImmediateInterceptorFactory) DeploymentUnitProcessingException(org.jboss.as.server.deployment.DeploymentUnitProcessingException) ServiceLoaders(org.jboss.as.weld.util.ServiceLoaders) Jsr299BindingsCreateInterceptor(org.jboss.as.weld.interceptors.Jsr299BindingsCreateInterceptor) WeldClassIntrospector(org.jboss.as.weld.deployment.WeldClassIntrospector) EEModuleDescription(org.jboss.as.ee.component.EEModuleDescription) ServiceBuilder(org.jboss.msc.service.ServiceBuilder) WeldInjectionContextInterceptor(org.jboss.as.weld.injection.WeldInjectionContextInterceptor) Consumer(java.util.function.Consumer) ComponentInstance(org.jboss.as.ee.component.ComponentInstance) ClassLoadingUtils(org.jboss.as.ee.utils.ClassLoadingUtils) ComponentConfiguration(org.jboss.as.ee.component.ComponentConfiguration) BasicComponentInstance(org.jboss.as.ee.component.BasicComponentInstance) ComponentConfigurator(org.jboss.as.ee.component.ComponentConfigurator) ViewConfiguration(org.jboss.as.ee.component.ViewConfiguration) Attachments(org.jboss.as.server.deployment.Attachments) ModuleClassLoader(org.jboss.modules.ModuleClassLoader) InterceptionType(javax.enterprise.inject.spi.InterceptionType) WeldInjectionContextInterceptor(org.jboss.as.weld.injection.WeldInjectionContextInterceptor) ServiceName(org.jboss.msc.service.ServiceName) ComponentIntegrator(org.jboss.as.weld.spi.ComponentIntegrator) UserInterceptorFactory(org.jboss.as.ee.component.interceptors.UserInterceptorFactory) ImmediateInterceptorFactory(org.jboss.invocation.ImmediateInterceptorFactory) DefaultInterceptorIntegrationAction(org.jboss.as.weld.spi.ComponentIntegrator.DefaultInterceptorIntegrationAction) WeldBootstrapService(org.jboss.as.weld.WeldBootstrapService) ComponentStartService(org.jboss.as.ee.component.ComponentStartService) WeldComponentService(org.jboss.as.weld.injection.WeldComponentService) WeldInterceptorInjectionInterceptor(org.jboss.as.weld.injection.WeldInterceptorInjectionInterceptor)

Example 2 with ComponentIntegrator

use of org.jboss.as.weld.spi.ComponentIntegrator in project wildfly by wildfly.

the class WeldComponentIntegrationProcessor method deploy.

@Override
public void deploy(DeploymentPhaseContext phaseContext) throws DeploymentUnitProcessingException {
    final DeploymentUnit deploymentUnit = phaseContext.getDeploymentUnit();
    if (!WeldDeploymentMarker.isWeldDeployment(deploymentUnit)) {
        return;
    }
    final DeploymentUnit topLevelDeployment = getRootDeploymentUnit(deploymentUnit);
    final EEModuleDescription eeModuleDescription = deploymentUnit.getAttachment(org.jboss.as.ee.component.Attachments.EE_MODULE_DESCRIPTION);
    final ServiceName weldBootstrapService = topLevelDeployment.getServiceName().append(WeldBootstrapService.SERVICE_NAME);
    final ServiceName weldStartService = topLevelDeployment.getServiceName().append(WeldStartService.SERVICE_NAME);
    final ServiceName beanManagerService = ServiceNames.beanManagerServiceName(deploymentUnit);
    final Iterable<ComponentIntegrator> componentIntegrators = ServiceLoader.load(ComponentIntegrator.class, WildFlySecurityManager.getClassLoaderPrivileged(WeldComponentIntegrationProcessor.class));
    final ComponentInterceptorSupport componentInterceptorSupport = ServiceLoaders.loadSingle(ComponentInterceptorSupport.class, WeldComponentIntegrationProcessor.class).orElse(null);
    WeldClassIntrospector.install(deploymentUnit, phaseContext.getServiceTarget());
    eeModuleDescription.setDefaultClassIntrospectorServiceName(WeldClassIntrospector.serviceName(deploymentUnit));
    for (ComponentDescription component : eeModuleDescription.getComponentDescriptions()) {
        final String beanName;
        if (isBeanNameRequired(component, componentIntegrators)) {
            beanName = component.getComponentName();
        } else {
            beanName = null;
        }
        component.getConfigurators().add((context, description, configuration) -> {
            // add interceptor to activate the request scope if required
            final EjbRequestScopeActivationInterceptor.Factory requestFactory = new EjbRequestScopeActivationInterceptor.Factory(beanManagerService);
            for (ViewConfiguration view : configuration.getViews()) {
                view.addViewInterceptor(requestFactory, InterceptorOrder.View.CDI_REQUEST_SCOPE);
            }
            configuration.addTimeoutViewInterceptor(requestFactory, InterceptorOrder.View.CDI_REQUEST_SCOPE);
        });
        component.getConfigurators().addFirst(new ComponentConfigurator() {

            @Override
            public void configure(final DeploymentPhaseContext context, final ComponentDescription description, final ComponentConfiguration configuration) throws DeploymentUnitProcessingException {
                final Class<?> componentClass = configuration.getComponentClass();
                final DeploymentUnit deploymentUnit = context.getDeploymentUnit();
                final Module module = deploymentUnit.getAttachment(Attachments.MODULE);
                final ModuleClassLoader classLoader = module.getClassLoader();
                // get the interceptors so they can be injected as well
                final Set<Class<?>> interceptorClasses = new HashSet<Class<?>>();
                for (InterceptorDescription interceptorDescription : description.getAllInterceptors()) {
                    try {
                        interceptorClasses.add(ClassLoadingUtils.loadClass(interceptorDescription.getInterceptorClassName(), module));
                    } catch (ClassNotFoundException e) {
                        throw WeldLogger.ROOT_LOGGER.couldNotLoadInterceptorClass(interceptorDescription.getInterceptorClassName(), e);
                    }
                }
                addWeldIntegration(componentIntegrators, componentInterceptorSupport, context.getServiceTarget(), configuration, description, componentClass, beanName, weldBootstrapService, weldStartService, beanManagerService, interceptorClasses, classLoader, description.getBeanDeploymentArchiveId());
            }
        });
    }
}
Also used : DeploymentUnitProcessingException(org.jboss.as.server.deployment.DeploymentUnitProcessingException) ComponentDescription(org.jboss.as.ee.component.ComponentDescription) Set(java.util.Set) HashSet(java.util.HashSet) ComponentConfigurator(org.jboss.as.ee.component.ComponentConfigurator) UserInterceptorFactory(org.jboss.as.ee.component.interceptors.UserInterceptorFactory) WeldManagedReferenceFactory(org.jboss.as.weld.injection.WeldManagedReferenceFactory) ImmediateInterceptorFactory(org.jboss.invocation.ImmediateInterceptorFactory) DeploymentPhaseContext(org.jboss.as.server.deployment.DeploymentPhaseContext) ViewConfiguration(org.jboss.as.ee.component.ViewConfiguration) InterceptorDescription(org.jboss.as.ee.component.InterceptorDescription) EEModuleDescription(org.jboss.as.ee.component.EEModuleDescription) ComponentIntegrator(org.jboss.as.weld.spi.ComponentIntegrator) ComponentInterceptorSupport(org.jboss.as.weld.spi.ComponentInterceptorSupport) ModuleClassLoader(org.jboss.modules.ModuleClassLoader) ComponentConfiguration(org.jboss.as.ee.component.ComponentConfiguration) ServiceName(org.jboss.msc.service.ServiceName) EjbRequestScopeActivationInterceptor(org.jboss.as.weld.ejb.EjbRequestScopeActivationInterceptor) Module(org.jboss.modules.Module) DeploymentUnit(org.jboss.as.server.deployment.DeploymentUnit) Utils.getRootDeploymentUnit(org.jboss.as.weld.util.Utils.getRootDeploymentUnit)

Aggregations

HashSet (java.util.HashSet)2 Set (java.util.Set)2 ComponentConfiguration (org.jboss.as.ee.component.ComponentConfiguration)2 ComponentConfigurator (org.jboss.as.ee.component.ComponentConfigurator)2 ComponentDescription (org.jboss.as.ee.component.ComponentDescription)2 EEModuleDescription (org.jboss.as.ee.component.EEModuleDescription)2 InterceptorDescription (org.jboss.as.ee.component.InterceptorDescription)2 ViewConfiguration (org.jboss.as.ee.component.ViewConfiguration)2 UserInterceptorFactory (org.jboss.as.ee.component.interceptors.UserInterceptorFactory)2 DeploymentPhaseContext (org.jboss.as.server.deployment.DeploymentPhaseContext)2 DeploymentUnit (org.jboss.as.server.deployment.DeploymentUnit)2 DeploymentUnitProcessingException (org.jboss.as.server.deployment.DeploymentUnitProcessingException)2 EjbRequestScopeActivationInterceptor (org.jboss.as.weld.ejb.EjbRequestScopeActivationInterceptor)2 WeldManagedReferenceFactory (org.jboss.as.weld.injection.WeldManagedReferenceFactory)2 ServiceLoader (java.util.ServiceLoader)1 Consumer (java.util.function.Consumer)1 Supplier (java.util.function.Supplier)1 InterceptionType (javax.enterprise.inject.spi.InterceptionType)1 BasicComponentInstance (org.jboss.as.ee.component.BasicComponentInstance)1 ComponentInstance (org.jboss.as.ee.component.ComponentInstance)1