Search in sources :

Example 6 with InterceptorDescription

use of org.jboss.as.ee.component.InterceptorDescription 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)

Example 7 with InterceptorDescription

use of org.jboss.as.ee.component.InterceptorDescription in project wildfly by wildfly.

the class EJBContainerInterceptorsViewConfigurator method doConfigure.

private void doConfigure(final DeploymentPhaseContext context, final EJBComponentDescription ejbComponentDescription, final ViewConfiguration viewConfiguration) throws DeploymentUnitProcessingException {
    final DeploymentUnit deploymentUnit = context.getDeploymentUnit();
    final EEApplicationClasses applicationClasses = deploymentUnit.getAttachment(Attachments.EE_APPLICATION_CLASSES_DESCRIPTION);
    final Module module = deploymentUnit.getAttachment(org.jboss.as.server.deployment.Attachments.MODULE);
    final Map<String, List<InterceptorFactory>> userAroundInvokesByInterceptorClass = new HashMap<String, List<InterceptorFactory>>();
    final Map<String, List<InterceptorFactory>> userAroundTimeoutsByInterceptorClass;
    if (ejbComponentDescription.isTimerServiceRequired()) {
        userAroundTimeoutsByInterceptorClass = new HashMap<String, List<InterceptorFactory>>();
    } else {
        userAroundTimeoutsByInterceptorClass = null;
    }
    // info
    for (final InterceptorDescription interceptorDescription : ejbComponentDescription.getAllContainerInterceptors()) {
        final String interceptorClassName = interceptorDescription.getInterceptorClassName();
        final Class<?> intereptorClass;
        try {
            intereptorClass = ClassLoadingUtils.loadClass(interceptorClassName, module);
        } catch (ClassNotFoundException e) {
            throw EeLogger.ROOT_LOGGER.cannotLoadInterceptor(e, interceptorClassName);
        }
        // run the interceptor class (and its super class hierarchy) through the InterceptorClassDescriptionTraversal so that it can
        // find the relevant @AroundInvoke/@AroundTimeout methods
        final InterceptorClassDescriptionTraversal interceptorClassDescriptionTraversal = new InterceptorClassDescriptionTraversal(intereptorClass, applicationClasses, deploymentUnit, ejbComponentDescription);
        interceptorClassDescriptionTraversal.run();
        // now that the InterceptorClassDescriptionTraversal has done the relevant processing, keep track of the @AroundInvoke and
        // @AroundTimeout methods applicable for this interceptor class, within a map
        final List<InterceptorFactory> aroundInvokeInterceptorFactories = interceptorClassDescriptionTraversal.getAroundInvokeInterceptorFactories();
        if (aroundInvokeInterceptorFactories != null) {
            userAroundInvokesByInterceptorClass.put(interceptorClassName, aroundInvokeInterceptorFactories);
        }
        if (ejbComponentDescription.isTimerServiceRequired()) {
            final List<InterceptorFactory> aroundTimeoutInterceptorFactories = interceptorClassDescriptionTraversal.getAroundTimeoutInterceptorFactories();
            if (aroundTimeoutInterceptorFactories != null) {
                userAroundTimeoutsByInterceptorClass.put(interceptorClassName, aroundTimeoutInterceptorFactories);
            }
        }
    }
    // At this point we have each interceptor class mapped against their corresponding @AroundInvoke/@AroundTimeout InterceptorFactory(s)
    // Let's now iterate over all the methods of the EJB view and apply the relevant InterceptorFactory(s) to that method
    final List<InterceptorDescription> classLevelContainerInterceptors = ejbComponentDescription.getClassLevelContainerInterceptors();
    final Map<MethodIdentifier, List<InterceptorDescription>> methodLevelContainerInterceptors = ejbComponentDescription.getMethodLevelContainerInterceptors();
    final List<Method> viewMethods = viewConfiguration.getProxyFactory().getCachedMethods();
    for (final Method method : viewMethods) {
        final MethodIdentifier methodIdentifier = MethodIdentifier.getIdentifier(method.getReturnType(), method.getName(), method.getParameterTypes());
        final List<InterceptorFactory> aroundInvokesApplicableForMethod = new ArrayList<InterceptorFactory>();
        final List<InterceptorFactory> aroundTimeoutsApplicableForMethod = new ArrayList<InterceptorFactory>();
        // first add the default interceptors (if not excluded) to the deque
        if (!ejbComponentDescription.isExcludeDefaultContainerInterceptors() && !ejbComponentDescription.isExcludeDefaultContainerInterceptors(methodIdentifier)) {
            for (final InterceptorDescription interceptorDescription : ejbComponentDescription.getDefaultContainerInterceptors()) {
                String interceptorClassName = interceptorDescription.getInterceptorClassName();
                final List<InterceptorFactory> aroundInvokesOnInterceptor = userAroundInvokesByInterceptorClass.get(interceptorClassName);
                if (aroundInvokesOnInterceptor != null) {
                    aroundInvokesApplicableForMethod.addAll(aroundInvokesOnInterceptor);
                }
                if (ejbComponentDescription.isTimerServiceRequired()) {
                    final List<InterceptorFactory> aroundTimeoutsOnInterceptor = userAroundTimeoutsByInterceptorClass.get(interceptorClassName);
                    if (aroundTimeoutsOnInterceptor != null) {
                        aroundTimeoutsApplicableForMethod.addAll(aroundTimeoutsOnInterceptor);
                    }
                }
            }
        }
        // now add class level interceptors (if not excluded) to the deque
        if (!ejbComponentDescription.isExcludeClassLevelContainerInterceptors(methodIdentifier)) {
            for (final InterceptorDescription interceptorDescription : classLevelContainerInterceptors) {
                String interceptorClassName = interceptorDescription.getInterceptorClassName();
                final List<InterceptorFactory> aroundInvokesOnInterceptor = userAroundInvokesByInterceptorClass.get(interceptorClassName);
                if (aroundInvokesOnInterceptor != null) {
                    aroundInvokesApplicableForMethod.addAll(aroundInvokesOnInterceptor);
                }
                if (ejbComponentDescription.isTimerServiceRequired()) {
                    final List<InterceptorFactory> aroundTimeoutsOnInterceptor = userAroundTimeoutsByInterceptorClass.get(interceptorClassName);
                    if (aroundTimeoutsOnInterceptor != null) {
                        aroundTimeoutsApplicableForMethod.addAll(aroundTimeoutsOnInterceptor);
                    }
                }
            }
        }
        // now add method level interceptors for to the deque so that they are triggered after the class interceptors
        final List<InterceptorDescription> interceptorsForMethod = methodLevelContainerInterceptors.get(methodIdentifier);
        if (interceptorsForMethod != null) {
            for (final InterceptorDescription methodLevelInterceptor : interceptorsForMethod) {
                String interceptorClassName = methodLevelInterceptor.getInterceptorClassName();
                final List<InterceptorFactory> aroundInvokesOnInterceptor = userAroundInvokesByInterceptorClass.get(interceptorClassName);
                if (aroundInvokesOnInterceptor != null) {
                    aroundInvokesApplicableForMethod.addAll(aroundInvokesOnInterceptor);
                }
                if (ejbComponentDescription.isTimerServiceRequired()) {
                    final List<InterceptorFactory> aroundTimeoutsOnInterceptor = userAroundTimeoutsByInterceptorClass.get(interceptorClassName);
                    if (aroundTimeoutsOnInterceptor != null) {
                        aroundTimeoutsApplicableForMethod.addAll(aroundTimeoutsOnInterceptor);
                    }
                }
            }
        }
        // apply the interceptors to the view's method.
        viewConfiguration.addViewInterceptor(method, new UserInterceptorFactory(weaved(aroundInvokesApplicableForMethod), weaved(aroundTimeoutsApplicableForMethod)), InterceptorOrder.View.USER_APP_SPECIFIC_CONTAINER_INTERCEPTORS);
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) MethodIdentifier(org.jboss.invocation.proxy.MethodIdentifier) Method(java.lang.reflect.Method) InterceptorDescription(org.jboss.as.ee.component.InterceptorDescription) InterceptorFactory(org.jboss.invocation.InterceptorFactory) UserInterceptorFactory(org.jboss.as.ee.component.interceptors.UserInterceptorFactory) EEApplicationClasses(org.jboss.as.ee.component.EEApplicationClasses) UserInterceptorFactory(org.jboss.as.ee.component.interceptors.UserInterceptorFactory) ArrayList(java.util.ArrayList) List(java.util.List) Module(org.jboss.modules.Module) DeploymentUnit(org.jboss.as.server.deployment.DeploymentUnit)

Example 8 with InterceptorDescription

use of org.jboss.as.ee.component.InterceptorDescription in project wildfly by wildfly.

the class EJBContainerInterceptorsViewConfigurator method configure.

@Override
public void configure(DeploymentPhaseContext deploymentPhaseContext, ComponentConfiguration componentConfiguration, ViewDescription viewDescription, ViewConfiguration viewConfiguration) throws DeploymentUnitProcessingException {
    final ComponentDescription componentDescription = componentConfiguration.getComponentDescription();
    // ideally it should always be an EJBComponentDescription when this view configurator is invoked, but let's just make sure
    if (!(componentDescription instanceof EJBComponentDescription)) {
        return;
    }
    final EJBComponentDescription ejbComponentDescription = (EJBComponentDescription) componentDescription;
    // we don't want to waste time processing if there are no container interceptors applicable for the EJB
    final Set<InterceptorDescription> allContainerInterceptors = ejbComponentDescription.getAllContainerInterceptors();
    if (allContainerInterceptors == null || allContainerInterceptors.isEmpty()) {
        return;
    }
    // do the processing
    this.doConfigure(deploymentPhaseContext, ejbComponentDescription, viewConfiguration);
}
Also used : InterceptorDescription(org.jboss.as.ee.component.InterceptorDescription) ComponentDescription(org.jboss.as.ee.component.ComponentDescription)

Aggregations

InterceptorDescription (org.jboss.as.ee.component.InterceptorDescription)8 DeploymentUnit (org.jboss.as.server.deployment.DeploymentUnit)6 Module (org.jboss.modules.Module)6 Method (java.lang.reflect.Method)4 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 HashSet (java.util.HashSet)4 List (java.util.List)4 ComponentDescription (org.jboss.as.ee.component.ComponentDescription)4 EEModuleDescription (org.jboss.as.ee.component.EEModuleDescription)4 MethodIdentifier (org.jboss.invocation.proxy.MethodIdentifier)4 Map (java.util.Map)3 EEApplicationClasses (org.jboss.as.ee.component.EEApplicationClasses)3 ServiceName (org.jboss.msc.service.ServiceName)3 BindingConfiguration (org.jboss.as.ee.component.BindingConfiguration)2 ComponentConfiguration (org.jboss.as.ee.component.ComponentConfiguration)2 ViewConfiguration (org.jboss.as.ee.component.ViewConfiguration)2 UserInterceptorFactory (org.jboss.as.ee.component.interceptors.UserInterceptorFactory)2 EJBComponentDescription (org.jboss.as.ejb3.component.EJBComponentDescription)2 ContextNames (org.jboss.as.naming.deployment.ContextNames)2