Search in sources :

Example 1 with ContextClassLoaderInterceptor

use of org.jboss.invocation.ContextClassLoaderInterceptor in project wildfly by wildfly.

the class ManagedBeanAnnotationProcessor method deploy.

/**
     * Check the deployment annotation index for all classes with the @ManagedBean annotation.  For each class with the
     * annotation, collect all the required information to create a managed bean instance, and attach it to the context.
     *
     * @param phaseContext the deployment unit context
     * @throws DeploymentUnitProcessingException
     *
     */
public void deploy(DeploymentPhaseContext phaseContext) throws DeploymentUnitProcessingException {
    final DeploymentUnit deploymentUnit = phaseContext.getDeploymentUnit();
    final EEResourceReferenceProcessorRegistry registry = deploymentUnit.getAttachment(org.jboss.as.ee.component.Attachments.RESOURCE_REFERENCE_PROCESSOR_REGISTRY);
    final EEModuleDescription moduleDescription = deploymentUnit.getAttachment(org.jboss.as.ee.component.Attachments.EE_MODULE_DESCRIPTION);
    final CompositeIndex compositeIndex = deploymentUnit.getAttachment(Attachments.COMPOSITE_ANNOTATION_INDEX);
    final PropertyReplacer replacer = EJBAnnotationPropertyReplacement.propertyReplacer(deploymentUnit);
    if (compositeIndex == null) {
        return;
    }
    final List<AnnotationInstance> instances = compositeIndex.getAnnotations(MANAGED_BEAN_ANNOTATION_NAME);
    if (instances == null || instances.isEmpty()) {
        return;
    }
    for (AnnotationInstance instance : instances) {
        AnnotationTarget target = instance.target();
        if (!(target instanceof ClassInfo)) {
            throw EeLogger.ROOT_LOGGER.classOnlyAnnotation("@ManagedBean", target);
        }
        final ClassInfo classInfo = (ClassInfo) target;
        // skip if it's not a valid managed bean class
        if (!assertManagedBeanClassValidity(classInfo)) {
            continue;
        }
        final String beanClassName = classInfo.name().toString();
        // Get the managed bean name from the annotation
        final AnnotationValue nameValue = instance.value();
        final String beanName = (nameValue == null || nameValue.asString().isEmpty()) ? beanClassName : replacer.replaceProperties(nameValue.asString());
        final ManagedBeanComponentDescription componentDescription = new ManagedBeanComponentDescription(beanName, beanClassName, moduleDescription, deploymentUnit.getServiceName());
        // Add the view
        ViewDescription viewDescription = new ViewDescription(componentDescription, beanClassName);
        viewDescription.getConfigurators().addFirst(new ViewConfigurator() {

            public void configure(final DeploymentPhaseContext context, final ComponentConfiguration componentConfiguration, final ViewDescription description, final ViewConfiguration configuration) throws DeploymentUnitProcessingException {
                // Add MB association interceptors
                configuration.addClientPostConstructInterceptor(ManagedBeanCreateInterceptor.FACTORY, InterceptorOrder.ClientPostConstruct.INSTANCE_CREATE);
                final ClassLoader classLoader = componentConfiguration.getModuleClassLoader();
                configuration.addViewInterceptor(AccessCheckingInterceptor.getFactory(), InterceptorOrder.View.CHECKING_INTERCEPTOR);
                configuration.addViewInterceptor(new ImmediateInterceptorFactory(new ContextClassLoaderInterceptor(classLoader)), InterceptorOrder.View.TCCL_INTERCEPTOR);
            }
        });
        viewDescription.getBindingNames().addAll(Arrays.asList("java:module/" + beanName, "java:app/" + moduleDescription.getModuleName() + "/" + beanName));
        componentDescription.getViews().add(viewDescription);
        moduleDescription.addComponent(componentDescription);
        // register an EEResourceReferenceProcessor which can process @Resource references to this managed bean.
        registry.registerResourceReferenceProcessor(new ManagedBeanResourceReferenceProcessor(beanClassName));
    }
}
Also used : AnnotationTarget(org.jboss.jandex.AnnotationTarget) ViewConfigurator(org.jboss.as.ee.component.ViewConfigurator) DeploymentUnitProcessingException(org.jboss.as.server.deployment.DeploymentUnitProcessingException) ManagedBeanComponentDescription(org.jboss.as.ee.managedbean.component.ManagedBeanComponentDescription) ViewDescription(org.jboss.as.ee.component.ViewDescription) CompositeIndex(org.jboss.as.server.deployment.annotation.CompositeIndex) ContextClassLoaderInterceptor(org.jboss.invocation.ContextClassLoaderInterceptor) ManagedBeanResourceReferenceProcessor(org.jboss.as.ee.managedbean.component.ManagedBeanResourceReferenceProcessor) DeploymentPhaseContext(org.jboss.as.server.deployment.DeploymentPhaseContext) EEResourceReferenceProcessorRegistry(org.jboss.as.ee.component.deployers.EEResourceReferenceProcessorRegistry) ComponentConfiguration(org.jboss.as.ee.component.ComponentConfiguration) ViewConfiguration(org.jboss.as.ee.component.ViewConfiguration) EEModuleDescription(org.jboss.as.ee.component.EEModuleDescription) ImmediateInterceptorFactory(org.jboss.invocation.ImmediateInterceptorFactory) AnnotationValue(org.jboss.jandex.AnnotationValue) PropertyReplacer(org.jboss.metadata.property.PropertyReplacer) DeploymentUnit(org.jboss.as.server.deployment.DeploymentUnit) AnnotationInstance(org.jboss.jandex.AnnotationInstance) ClassInfo(org.jboss.jandex.ClassInfo)

Example 2 with ContextClassLoaderInterceptor

use of org.jboss.invocation.ContextClassLoaderInterceptor in project wildfly by wildfly.

the class EJBComponentDescription method setupViewInterceptors.

protected void setupViewInterceptors(final EJBViewDescription view) {
    // add a logging interceptor (to take care of EJB3 spec, section 14.3 logging requirements)
    view.getConfigurators().add(new ViewConfigurator() {

        @Override
        public void configure(DeploymentPhaseContext context, ComponentConfiguration componentConfiguration, ViewDescription description, ViewConfiguration viewConfiguration) throws DeploymentUnitProcessingException {
            viewConfiguration.addViewInterceptor(LoggingInterceptor.FACTORY, InterceptorOrder.View.EJB_EXCEPTION_LOGGING_INTERCEPTOR);
            final ClassLoader classLoader = componentConfiguration.getModuleClassLoader();
            viewConfiguration.addViewInterceptor(AccessCheckingInterceptor.getFactory(), InterceptorOrder.View.CHECKING_INTERCEPTOR);
            viewConfiguration.addViewInterceptor(new ImmediateInterceptorFactory(new ContextClassLoaderInterceptor(classLoader)), InterceptorOrder.View.TCCL_INTERCEPTOR);
            //If this is the EJB 2.x local or home view add the exception transformer interceptor
            if (view.getMethodIntf() == MethodIntf.LOCAL && EJBLocalObject.class.isAssignableFrom(viewConfiguration.getViewClass())) {
                viewConfiguration.addViewInterceptor(EjbExceptionTransformingInterceptorFactories.LOCAL_INSTANCE, InterceptorOrder.View.REMOTE_EXCEPTION_TRANSFORMER);
            } else if (view.getMethodIntf() == MethodIntf.LOCAL_HOME) {
                viewConfiguration.addViewInterceptor(EjbExceptionTransformingInterceptorFactories.LOCAL_INSTANCE, InterceptorOrder.View.REMOTE_EXCEPTION_TRANSFORMER);
            }
            final List<SetupAction> ejbSetupActions = context.getDeploymentUnit().getAttachmentList(Attachments.OTHER_EE_SETUP_ACTIONS);
            if (!ejbSetupActions.isEmpty()) {
                viewConfiguration.addViewInterceptor(AdditionalSetupInterceptor.factory(ejbSetupActions), InterceptorOrder.View.EE_SETUP);
            }
            viewConfiguration.addViewInterceptor(WaitTimeInterceptor.FACTORY, InterceptorOrder.View.EJB_WAIT_TIME_INTERCEPTOR);
            viewConfiguration.addViewInterceptor(shutDownInterceptorFactory, InterceptorOrder.View.SHUTDOWN_INTERCEPTOR);
        }
    });
    this.addCurrentInvocationContextFactory(view);
    this.setupSecurityInterceptors(view);
    this.setupRemoteViewInterceptors(view);
    view.getConfigurators().addFirst(new NamespaceViewConfigurator());
}
Also used : NamespaceViewConfigurator(org.jboss.as.ee.component.NamespaceViewConfigurator) EJBRemoteTransactionsViewConfigurator(org.jboss.as.ejb3.remote.EJBRemoteTransactionsViewConfigurator) EJBSecurityViewConfigurator(org.jboss.as.ejb3.security.EJBSecurityViewConfigurator) ViewConfigurator(org.jboss.as.ee.component.ViewConfigurator) ComponentConfiguration(org.jboss.as.ee.component.ComponentConfiguration) DeploymentUnitProcessingException(org.jboss.as.server.deployment.DeploymentUnitProcessingException) ViewConfiguration(org.jboss.as.ee.component.ViewConfiguration) ViewDescription(org.jboss.as.ee.component.ViewDescription) ImmediateInterceptorFactory(org.jboss.invocation.ImmediateInterceptorFactory) ContextClassLoaderInterceptor(org.jboss.invocation.ContextClassLoaderInterceptor) NamespaceViewConfigurator(org.jboss.as.ee.component.NamespaceViewConfigurator) ArrayList(java.util.ArrayList) List(java.util.List) DeploymentPhaseContext(org.jboss.as.server.deployment.DeploymentPhaseContext)

Example 3 with ContextClassLoaderInterceptor

use of org.jboss.invocation.ContextClassLoaderInterceptor in project wildfly by wildfly.

the class DefaultComponentConfigurator method configure.

public void configure(final DeploymentPhaseContext context, final ComponentDescription description, final ComponentConfiguration configuration) throws DeploymentUnitProcessingException {
    final DeploymentUnit deploymentUnit = context.getDeploymentUnit();
    final DeploymentReflectionIndex deploymentReflectionIndex = deploymentUnit.getAttachment(REFLECTION_INDEX);
    final Module module = deploymentUnit.getAttachment(org.jboss.as.server.deployment.Attachments.MODULE);
    final EEApplicationClasses applicationClasses = deploymentUnit.getAttachment(Attachments.EE_APPLICATION_CLASSES_DESCRIPTION);
    final EEModuleDescription moduleDescription = deploymentUnit.getAttachment(Attachments.EE_MODULE_DESCRIPTION);
    final boolean metadataComplete = MetadataCompleteMarker.isMetadataComplete(deploymentUnit);
    // Module stuff
    final Deque<InterceptorFactory> injectors = new ArrayDeque<>();
    final Deque<InterceptorFactory> uninjectors = new ArrayDeque<>();
    final Deque<InterceptorFactory> destructors = new ArrayDeque<>();
    final List<InterceptorFactory> componentUserAroundInvoke = new ArrayList<>();
    final List<InterceptorFactory> componentUserAroundTimeout;
    final List<InterceptorFactory> userPostConstruct = new ArrayList<>();
    final List<InterceptorFactory> userPreDestroy = new ArrayList<>();
    final List<InterceptorFactory> componentUserPrePassivate;
    final List<InterceptorFactory> componentUserPostActivate;
    final Set<MethodIdentifier> timeoutMethods = description.getTimerMethods();
    if (description.isTimerServiceRequired()) {
        componentUserAroundTimeout = new ArrayList<>();
    } else {
        componentUserAroundTimeout = null;
    }
    if (description.isPassivationApplicable()) {
        componentUserPrePassivate = new ArrayList<>();
        componentUserPostActivate = new ArrayList<>();
    } else {
        componentUserPrePassivate = null;
        componentUserPostActivate = null;
    }
    destructors.add(new ImmediateInterceptorFactory(new ManagedReferenceReleaseInterceptor(BasicComponentInstance.INSTANCE_KEY)));
    new ClassDescriptionTraversal(configuration.getComponentClass(), applicationClasses) {

        @Override
        public void handle(Class<?> clazz, EEModuleClassDescription classDescription) throws DeploymentUnitProcessingException {
            mergeInjectionsForClass(clazz, configuration.getComponentClass(), classDescription, moduleDescription, deploymentReflectionIndex, description, configuration, context, injectors, BasicComponentInstance.INSTANCE_KEY, uninjectors, metadataComplete);
        }
    }.run();
    new ClassDescriptionTraversal(configuration.getComponentClass(), applicationClasses) {

        @Override
        public void handle(final Class<?> clazz, EEModuleClassDescription classDescription) throws DeploymentUnitProcessingException {
            final InterceptorClassDescription interceptorConfig = InterceptorClassDescription.merge(ComponentDescription.mergeInterceptorConfig(clazz, classDescription, description, metadataComplete), moduleDescription.getInterceptorClassOverride(clazz.getName()));
            handleClassMethod(clazz, interceptorConfig.getAroundInvoke(), componentUserAroundInvoke, false, false, configuration);
            if (description.isTimerServiceRequired()) {
                handleClassMethod(clazz, interceptorConfig.getAroundTimeout(), componentUserAroundTimeout, false, false, configuration);
            }
            if (!description.isIgnoreLifecycleInterceptors()) {
                handleClassMethod(clazz, interceptorConfig.getPostConstruct(), userPostConstruct, true, true, configuration);
                handleClassMethod(clazz, interceptorConfig.getPreDestroy(), userPreDestroy, true, true, configuration);
                if (description.isPassivationApplicable()) {
                    handleClassMethod(clazz, interceptorConfig.getPrePassivate(), componentUserPrePassivate, false, true, configuration);
                    handleClassMethod(clazz, interceptorConfig.getPostActivate(), componentUserPostActivate, false, true, configuration);
                }
            }
        }

        private void handleClassMethod(final Class<?> clazz, final MethodIdentifier methodIdentifier, final List<InterceptorFactory> interceptors, boolean changeMethod, boolean lifecycleMethod, ComponentConfiguration configuration) throws DeploymentUnitProcessingException {
            if (methodIdentifier != null) {
                final Method method = ClassReflectionIndexUtil.findRequiredMethod(deploymentReflectionIndex, clazz, methodIdentifier);
                if (isNotOverriden(clazz, method, configuration.getComponentClass(), deploymentReflectionIndex)) {
                    InterceptorFactory interceptorFactory = new ImmediateInterceptorFactory(new ManagedReferenceLifecycleMethodInterceptor(BasicComponentInstance.INSTANCE_KEY, method, changeMethod, lifecycleMethod));
                    interceptors.add(interceptorFactory);
                    if (lifecycleMethod) {
                        configuration.addLifecycleMethod(method);
                    }
                }
            }
        }
    }.run();
    final ClassLoader classLoader = module.getClassLoader();
    final InterceptorFactory tcclInterceptor = new ImmediateInterceptorFactory(new ContextClassLoaderInterceptor(classLoader));
    if (!injectors.isEmpty()) {
        configuration.addPostConstructInterceptors(new ArrayList<>(injectors), InterceptorOrder.ComponentPostConstruct.COMPONENT_RESOURCE_INJECTION_INTERCEPTORS);
    }
    // Apply post-construct
    if (!userPostConstruct.isEmpty()) {
        configuration.addPostConstructInterceptors(userPostConstruct, InterceptorOrder.ComponentPostConstruct.COMPONENT_USER_INTERCEPTORS);
    }
    configuration.addPostConstructInterceptor(Interceptors.getTerminalInterceptorFactory(), InterceptorOrder.ComponentPostConstruct.TERMINAL_INTERCEPTOR);
    configuration.addPostConstructInterceptor(tcclInterceptor, InterceptorOrder.ComponentPostConstruct.TCCL_INTERCEPTOR);
    // Apply pre-destroy
    if (!uninjectors.isEmpty()) {
        configuration.addPreDestroyInterceptors(new ArrayList<>(uninjectors), InterceptorOrder.ComponentPreDestroy.COMPONENT_UNINJECTION_INTERCEPTORS);
    }
    if (!destructors.isEmpty()) {
        configuration.addPreDestroyInterceptors(new ArrayList<>(destructors), InterceptorOrder.ComponentPreDestroy.COMPONENT_DESTRUCTION_INTERCEPTORS);
    }
    if (!userPreDestroy.isEmpty()) {
        configuration.addPreDestroyInterceptors(userPreDestroy, InterceptorOrder.ComponentPreDestroy.COMPONENT_USER_INTERCEPTORS);
    }
    configuration.addPreDestroyInterceptor(Interceptors.getTerminalInterceptorFactory(), InterceptorOrder.ComponentPreDestroy.TERMINAL_INTERCEPTOR);
    configuration.addPreDestroyInterceptor(tcclInterceptor, InterceptorOrder.ComponentPreDestroy.TCCL_INTERCEPTOR);
    if (description.isPassivationApplicable()) {
        if (!componentUserPrePassivate.isEmpty()) {
            configuration.addPrePassivateInterceptors(componentUserPrePassivate, InterceptorOrder.ComponentPassivation.COMPONENT_USER_INTERCEPTORS);
        }
        configuration.addPrePassivateInterceptor(Interceptors.getTerminalInterceptorFactory(), InterceptorOrder.ComponentPassivation.TERMINAL_INTERCEPTOR);
        configuration.addPrePassivateInterceptor(tcclInterceptor, InterceptorOrder.ComponentPassivation.TCCL_INTERCEPTOR);
        if (!componentUserPostActivate.isEmpty()) {
            configuration.addPostActivateInterceptors(componentUserPostActivate, InterceptorOrder.ComponentPassivation.COMPONENT_USER_INTERCEPTORS);
        }
        configuration.addPostActivateInterceptor(Interceptors.getTerminalInterceptorFactory(), InterceptorOrder.ComponentPassivation.TERMINAL_INTERCEPTOR);
        configuration.addPostActivateInterceptor(tcclInterceptor, InterceptorOrder.ComponentPassivation.TCCL_INTERCEPTOR);
    }
    // @AroundInvoke interceptors
    if (description.isIntercepted()) {
        for (final Method method : configuration.getDefinedComponentMethods()) {
            //now add the interceptor that initializes and the interceptor that actually invokes to the end of the interceptor chain
            configuration.addComponentInterceptor(method, Interceptors.getInitialInterceptorFactory(), InterceptorOrder.Component.INITIAL_INTERCEPTOR);
            configuration.addComponentInterceptor(method, new ImmediateInterceptorFactory(new ManagedReferenceMethodInterceptor(BasicComponentInstance.INSTANCE_KEY, method)), InterceptorOrder.Component.TERMINAL_INTERCEPTOR);
            final MethodIdentifier identifier = MethodIdentifier.getIdentifier(method.getReturnType(), method.getName(), method.getParameterTypes());
            // first add the default interceptors (if not excluded) to the deque
            final boolean requiresTimerChain = description.isTimerServiceRequired() && timeoutMethods.contains(identifier);
            if (requiresTimerChain) {
                configuration.addComponentInterceptor(method, new UserInterceptorFactory(weaved(componentUserAroundInvoke), weaved(componentUserAroundTimeout)), InterceptorOrder.Component.COMPONENT_USER_INTERCEPTORS);
            } else {
                configuration.addComponentInterceptors(method, componentUserAroundInvoke, InterceptorOrder.Component.COMPONENT_USER_INTERCEPTORS);
            }
        }
    }
}
Also used : DeploymentUnitProcessingException(org.jboss.as.server.deployment.DeploymentUnitProcessingException) ArrayList(java.util.ArrayList) ContextClassLoaderInterceptor(org.jboss.invocation.ContextClassLoaderInterceptor) InterceptorClassDescription(org.jboss.as.ee.component.interceptors.InterceptorClassDescription) ImmediateInterceptorFactory(org.jboss.invocation.ImmediateInterceptorFactory) MethodIdentifier(org.jboss.invocation.proxy.MethodIdentifier) Method(java.lang.reflect.Method) ArrayDeque(java.util.ArrayDeque) InterceptorFactory(org.jboss.invocation.InterceptorFactory) UserInterceptorFactory(org.jboss.as.ee.component.interceptors.UserInterceptorFactory) ImmediateInterceptorFactory(org.jboss.invocation.ImmediateInterceptorFactory) UserInterceptorFactory(org.jboss.as.ee.component.interceptors.UserInterceptorFactory) Module(org.jboss.modules.Module) DeploymentUnit(org.jboss.as.server.deployment.DeploymentUnit) DeploymentReflectionIndex(org.jboss.as.server.deployment.reflect.DeploymentReflectionIndex)

Aggregations

DeploymentUnitProcessingException (org.jboss.as.server.deployment.DeploymentUnitProcessingException)3 ContextClassLoaderInterceptor (org.jboss.invocation.ContextClassLoaderInterceptor)3 ImmediateInterceptorFactory (org.jboss.invocation.ImmediateInterceptorFactory)3 ArrayList (java.util.ArrayList)2 ComponentConfiguration (org.jboss.as.ee.component.ComponentConfiguration)2 ViewConfiguration (org.jboss.as.ee.component.ViewConfiguration)2 ViewConfigurator (org.jboss.as.ee.component.ViewConfigurator)2 ViewDescription (org.jboss.as.ee.component.ViewDescription)2 DeploymentPhaseContext (org.jboss.as.server.deployment.DeploymentPhaseContext)2 DeploymentUnit (org.jboss.as.server.deployment.DeploymentUnit)2 Method (java.lang.reflect.Method)1 ArrayDeque (java.util.ArrayDeque)1 List (java.util.List)1 EEModuleDescription (org.jboss.as.ee.component.EEModuleDescription)1 NamespaceViewConfigurator (org.jboss.as.ee.component.NamespaceViewConfigurator)1 EEResourceReferenceProcessorRegistry (org.jboss.as.ee.component.deployers.EEResourceReferenceProcessorRegistry)1 InterceptorClassDescription (org.jboss.as.ee.component.interceptors.InterceptorClassDescription)1 UserInterceptorFactory (org.jboss.as.ee.component.interceptors.UserInterceptorFactory)1 ManagedBeanComponentDescription (org.jboss.as.ee.managedbean.component.ManagedBeanComponentDescription)1 ManagedBeanResourceReferenceProcessor (org.jboss.as.ee.managedbean.component.ManagedBeanResourceReferenceProcessor)1