Search in sources :

Example 1 with InterceptorDescription

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

the class InterceptorAnnotationProcessor method handleAnnotations.

private void handleAnnotations(final EEApplicationClasses applicationClasses, final DeploymentReflectionIndex deploymentReflectionIndex, final Class<?> componentClass, final ComponentDescription description) {
    final List<Class> heirachy = new ArrayList<Class>();
    Class c = componentClass;
    while (c != Object.class && c != null) {
        heirachy.add(c);
        c = c.getSuperclass();
    }
    Collections.reverse(heirachy);
    final RuntimeAnnotationInformation<Boolean> excludeDefaultInterceptors = MethodAnnotationAggregator.runtimeAnnotationInformation(componentClass, applicationClasses, deploymentReflectionIndex, ExcludeDefaultInterceptors.class);
    if (excludeDefaultInterceptors.getClassAnnotations().containsKey(componentClass.getName())) {
        description.setExcludeDefaultInterceptors(true);
    }
    for (final Map.Entry<Method, List<Boolean>> entry : excludeDefaultInterceptors.getMethodAnnotations().entrySet()) {
        description.excludeDefaultInterceptors(MethodIdentifier.getIdentifierForMethod(entry.getKey()));
    }
    final RuntimeAnnotationInformation<Boolean> excludeClassInterceptors = MethodAnnotationAggregator.runtimeAnnotationInformation(componentClass, applicationClasses, deploymentReflectionIndex, ExcludeClassInterceptors.class);
    for (final Map.Entry<Method, List<Boolean>> entry : excludeClassInterceptors.getMethodAnnotations().entrySet()) {
        description.excludeClassInterceptors(MethodIdentifier.getIdentifierForMethod(entry.getKey()));
    }
    final RuntimeAnnotationInformation<String[]> interceptors = MethodAnnotationAggregator.runtimeAnnotationInformation(componentClass, applicationClasses, deploymentReflectionIndex, Interceptors.class);
    //walk the class heirachy in reverse
    for (final Class<?> clazz : heirachy) {
        final List<String[]> classInterceptors = interceptors.getClassAnnotations().get(clazz.getName());
        if (classInterceptors != null) {
            for (final String interceptor : classInterceptors.get(0)) {
                description.addClassInterceptor(new InterceptorDescription(interceptor));
            }
        }
    }
    for (final Map.Entry<Method, List<String[]>> entry : interceptors.getMethodAnnotations().entrySet()) {
        final MethodIdentifier method = MethodIdentifier.getIdentifierForMethod(entry.getKey());
        for (final String interceptor : entry.getValue().get(0)) {
            description.addMethodInterceptor(method, new InterceptorDescription(interceptor));
        }
    }
}
Also used : ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) MethodIdentifier(org.jboss.invocation.proxy.MethodIdentifier) InterceptorDescription(org.jboss.as.ee.component.InterceptorDescription) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map)

Example 2 with InterceptorDescription

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

the class ModuleJndiBindingProcessor method deploy.

public void deploy(final DeploymentPhaseContext phaseContext) throws DeploymentUnitProcessingException {
    final DeploymentUnit deploymentUnit = phaseContext.getDeploymentUnit();
    final EEApplicationClasses applicationClasses = deploymentUnit.getAttachment(Attachments.EE_APPLICATION_CLASSES_DESCRIPTION);
    final EEModuleConfiguration moduleConfiguration = deploymentUnit.getAttachment(Attachments.EE_MODULE_CONFIGURATION);
    final EEModuleDescription eeModuleDescription = deploymentUnit.getAttachment(Attachments.EE_MODULE_DESCRIPTION);
    final Module module = deploymentUnit.getAttachment(org.jboss.as.server.deployment.Attachments.MODULE);
    if (moduleConfiguration == null || DeploymentUtils.skipRepeatedActivation(deploymentUnit, 0)) {
        return;
    }
    final List<ServiceName> dependencies = deploymentUnit.getAttachmentList(org.jboss.as.server.deployment.Attachments.JNDI_DEPENDENCIES);
    final Map<ServiceName, BindingConfiguration> deploymentDescriptorBindings = new HashMap<ServiceName, BindingConfiguration>();
    final List<BindingConfiguration> bindingConfigurations = eeModuleDescription.getBindingConfigurations();
    //we need to make sure that java:module/env and java:comp/env are always available
    if (!DeploymentTypeMarker.isType(DeploymentType.EAR, deploymentUnit)) {
        bindingConfigurations.add(new BindingConfiguration("java:module/env", new ContextInjectionSource("env", "java:module/env")));
    }
    if (deploymentUnit.getParent() == null) {
        bindingConfigurations.add(new BindingConfiguration("java:app/env", new ContextInjectionSource("env", "java:app/env")));
    }
    for (BindingConfiguration binding : bindingConfigurations) {
        final ContextNames.BindInfo bindInfo = ContextNames.bindInfoForEnvEntry(moduleConfiguration.getApplicationName(), moduleConfiguration.getModuleName(), null, false, binding.getName());
        deploymentDescriptorBindings.put(bindInfo.getBinderServiceName(), binding);
        addJndiBinding(moduleConfiguration, binding, phaseContext, dependencies);
    }
    // these are bindings that have been added via a deployment descriptor
    for (final ComponentConfiguration componentConfiguration : moduleConfiguration.getComponentConfigurations()) {
        // handle these duplicates?
        for (BindingConfiguration binding : componentConfiguration.getComponentDescription().getBindingConfigurations()) {
            final String bindingName = binding.getName();
            final boolean compBinding = bindingName.startsWith("java:comp") || !bindingName.startsWith("java:");
            if (componentConfiguration.getComponentDescription().getNamingMode() == ComponentNamingMode.CREATE && compBinding) {
                //components with there own comp context do their own binding
                continue;
            }
            final ContextNames.BindInfo bindInfo = ContextNames.bindInfoForEnvEntry(moduleConfiguration.getApplicationName(), moduleConfiguration.getModuleName(), null, false, binding.getName());
            deploymentDescriptorBindings.put(bindInfo.getBinderServiceName(), binding);
            addJndiBinding(moduleConfiguration, binding, phaseContext, dependencies);
        }
    }
    //now add all class level bindings
    final Set<String> handledClasses = new HashSet<String>();
    for (final ComponentConfiguration componentConfiguration : moduleConfiguration.getComponentConfigurations()) {
        final Set<Class<?>> classConfigurations = new HashSet<Class<?>>();
        classConfigurations.add(componentConfiguration.getComponentClass());
        for (final InterceptorDescription interceptor : componentConfiguration.getComponentDescription().getAllInterceptors()) {
            try {
                classConfigurations.add(ClassLoadingUtils.loadClass(interceptor.getInterceptorClassName(), module));
            } catch (ClassNotFoundException e) {
                throw EeLogger.ROOT_LOGGER.cannotLoadInterceptor(e, interceptor.getInterceptorClassName(), componentConfiguration.getComponentClass());
            }
        }
        processClassConfigurations(phaseContext, applicationClasses, moduleConfiguration, deploymentDescriptorBindings, handledClasses, componentConfiguration.getComponentDescription().getNamingMode(), classConfigurations, componentConfiguration.getComponentName(), dependencies);
    }
}
Also used : HashMap(java.util.HashMap) EEModuleConfiguration(org.jboss.as.ee.component.EEModuleConfiguration) ComponentConfiguration(org.jboss.as.ee.component.ComponentConfiguration) InterceptorDescription(org.jboss.as.ee.component.InterceptorDescription) EEModuleDescription(org.jboss.as.ee.component.EEModuleDescription) EEApplicationClasses(org.jboss.as.ee.component.EEApplicationClasses) ServiceName(org.jboss.msc.service.ServiceName) ContextInjectionSource(org.jboss.as.ee.naming.ContextInjectionSource) Module(org.jboss.modules.Module) DeploymentUnit(org.jboss.as.server.deployment.DeploymentUnit) BindingConfiguration(org.jboss.as.ee.component.BindingConfiguration) ContextNames(org.jboss.as.naming.deployment.ContextNames) HashSet(java.util.HashSet)

Example 3 with InterceptorDescription

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

the class ComponentInstallProcessor method deployComponent.

@SuppressWarnings({ "unchecked", "rawtypes" })
protected void deployComponent(final DeploymentPhaseContext phaseContext, final ComponentConfiguration configuration, final List<ServiceName> jndiDependencies, final ServiceName bindingDependencyService) throws DeploymentUnitProcessingException {
    final DeploymentUnit deploymentUnit = phaseContext.getDeploymentUnit();
    final ServiceTarget serviceTarget = phaseContext.getServiceTarget();
    final String applicationName = configuration.getApplicationName();
    final String moduleName = configuration.getModuleName();
    final String componentName = configuration.getComponentName();
    final EEApplicationClasses applicationClasses = deploymentUnit.getAttachment(Attachments.EE_APPLICATION_CLASSES_DESCRIPTION);
    final Module module = deploymentUnit.getAttachment(org.jboss.as.server.deployment.Attachments.MODULE);
    //create additional injectors
    final ServiceName createServiceName = configuration.getComponentDescription().getCreateServiceName();
    final ServiceName startServiceName = configuration.getComponentDescription().getStartServiceName();
    final BasicComponentCreateService createService = configuration.getComponentCreateServiceFactory().constructService(configuration);
    final ServiceBuilder<Component> createBuilder = serviceTarget.addService(createServiceName, createService);
    // inject the DU
    createBuilder.addDependency(deploymentUnit.getServiceName(), DeploymentUnit.class, createService.getDeploymentUnitInjector());
    final ComponentStartService startService = new ComponentStartService();
    final ServiceBuilder<Component> startBuilder = serviceTarget.addService(startServiceName, startService);
    deploymentUnit.addToAttachmentList(org.jboss.as.server.deployment.Attachments.DEPLOYMENT_COMPLETE_SERVICES, startServiceName);
    //WFLY-1402 we don't add the bindings to the jndi dependencies list directly, instead
    //the bindings depend on the this artificial service
    ServiceName jndiDepServiceName = configuration.getComponentDescription().getServiceName().append(JNDI_BINDINGS_SERVICE);
    final ServiceBuilder<Void> jndiDepServiceBuilder = serviceTarget.addService(jndiDepServiceName, Service.NULL);
    jndiDependencies.add(jndiDepServiceName);
    // Add all service dependencies
    for (DependencyConfigurator configurator : configuration.getCreateDependencies()) {
        configurator.configureDependency(createBuilder, createService);
    }
    for (DependencyConfigurator configurator : configuration.getStartDependencies()) {
        configurator.configureDependency(startBuilder, startService);
    }
    // START depends on CREATE
    startBuilder.addDependency(createServiceName, BasicComponent.class, startService.getComponentInjector());
    Services.addServerExecutorDependency(startBuilder, startService.getExecutorInjector(), false);
    //don't start components until all bindings are up
    startBuilder.addDependency(bindingDependencyService);
    final ServiceName contextServiceName;
    //set up the naming context if necessary
    if (configuration.getComponentDescription().getNamingMode() == ComponentNamingMode.CREATE) {
        final NamingStoreService contextService = new NamingStoreService(true);
        serviceTarget.addService(configuration.getComponentDescription().getContextServiceName(), contextService).install();
    }
    final InjectionSource.ResolutionContext resolutionContext = new InjectionSource.ResolutionContext(configuration.getComponentDescription().getNamingMode() == ComponentNamingMode.USE_MODULE, configuration.getComponentName(), configuration.getModuleName(), configuration.getApplicationName());
    // Iterate through each view, creating the services for each
    for (ViewConfiguration viewConfiguration : configuration.getViews()) {
        final ServiceName serviceName = viewConfiguration.getViewServiceName();
        final ViewService viewService = new ViewService(viewConfiguration);
        final ServiceBuilder<ComponentView> componentViewServiceBuilder = serviceTarget.addService(serviceName, viewService);
        componentViewServiceBuilder.addDependency(createServiceName, Component.class, viewService.getComponentInjector());
        for (final DependencyConfigurator<ViewService> depConfig : viewConfiguration.getDependencies()) {
            depConfig.configureDependency(componentViewServiceBuilder, viewService);
        }
        componentViewServiceBuilder.install();
        startBuilder.addDependency(serviceName);
        // The bindings for the view
        for (BindingConfiguration bindingConfiguration : viewConfiguration.getBindingConfigurations()) {
            final String bindingName = bindingConfiguration.getName();
            final ContextNames.BindInfo bindInfo = ContextNames.bindInfoFor(applicationName, moduleName, componentName, bindingName);
            final BinderService service = new BinderService(bindInfo.getBindName(), bindingConfiguration.getSource());
            //these bindings should never be merged, if a view binding is duplicated it is an error
            jndiDepServiceBuilder.addDependency(bindInfo.getBinderServiceName());
            ServiceBuilder<ManagedReferenceFactory> serviceBuilder = serviceTarget.addService(bindInfo.getBinderServiceName(), service);
            bindingConfiguration.getSource().getResourceValue(resolutionContext, serviceBuilder, phaseContext, service.getManagedObjectInjector());
            serviceBuilder.addDependency(bindInfo.getParentContextServiceName(), ServiceBasedNamingStore.class, service.getNamingStoreInjector());
            serviceBuilder.install();
        }
    }
    if (configuration.getComponentDescription().getNamingMode() == ComponentNamingMode.CREATE) {
        // The bindings for the component
        final Set<ServiceName> bound = new HashSet<ServiceName>();
        processBindings(phaseContext, configuration, serviceTarget, resolutionContext, configuration.getComponentDescription().getBindingConfigurations(), jndiDepServiceBuilder, bound);
        //class level bindings should be ignored if the deployment is metadata complete
        if (!MetadataCompleteMarker.isMetadataComplete(phaseContext.getDeploymentUnit())) {
            // The bindings for the component class
            new ClassDescriptionTraversal(configuration.getComponentClass(), applicationClasses) {

                @Override
                protected void handle(final Class<?> clazz, final EEModuleClassDescription classDescription) throws DeploymentUnitProcessingException {
                    if (classDescription != null) {
                        processBindings(phaseContext, configuration, serviceTarget, resolutionContext, classDescription.getBindingConfigurations(), jndiDepServiceBuilder, bound);
                    }
                }
            }.run();
            for (InterceptorDescription interceptor : configuration.getComponentDescription().getAllInterceptors()) {
                final Class<?> interceptorClass;
                try {
                    interceptorClass = module.getClassLoader().loadClass(interceptor.getInterceptorClassName());
                } catch (ClassNotFoundException e) {
                    throw EeLogger.ROOT_LOGGER.cannotLoadInterceptor(e, interceptor.getInterceptorClassName(), configuration.getComponentClass());
                }
                if (interceptorClass != null) {
                    new ClassDescriptionTraversal(interceptorClass, applicationClasses) {

                        @Override
                        protected void handle(final Class<?> clazz, final EEModuleClassDescription classDescription) throws DeploymentUnitProcessingException {
                            if (classDescription != null) {
                                processBindings(phaseContext, configuration, serviceTarget, resolutionContext, classDescription.getBindingConfigurations(), jndiDepServiceBuilder, bound);
                            }
                        }
                    }.run();
                }
            }
        }
    }
    createBuilder.install();
    startBuilder.install();
    jndiDepServiceBuilder.install();
}
Also used : DeploymentUnitProcessingException(org.jboss.as.server.deployment.DeploymentUnitProcessingException) DependencyConfigurator(org.jboss.as.ee.component.DependencyConfigurator) ClassDescriptionTraversal(org.jboss.as.ee.component.ClassDescriptionTraversal) NamingStoreService(org.jboss.as.naming.service.NamingStoreService) BinderService(org.jboss.as.naming.service.BinderService) ViewConfiguration(org.jboss.as.ee.component.ViewConfiguration) InterceptorDescription(org.jboss.as.ee.component.InterceptorDescription) ManagedReferenceFactory(org.jboss.as.naming.ManagedReferenceFactory) BasicComponent(org.jboss.as.ee.component.BasicComponent) Component(org.jboss.as.ee.component.Component) BindingConfiguration(org.jboss.as.ee.component.BindingConfiguration) ComponentStartService(org.jboss.as.ee.component.ComponentStartService) ContextNames(org.jboss.as.naming.deployment.ContextNames) HashSet(java.util.HashSet) ServiceTarget(org.jboss.msc.service.ServiceTarget) ViewService(org.jboss.as.ee.component.ViewService) BasicComponentCreateService(org.jboss.as.ee.component.BasicComponentCreateService) ComponentView(org.jboss.as.ee.component.ComponentView) EEApplicationClasses(org.jboss.as.ee.component.EEApplicationClasses) ServiceName(org.jboss.msc.service.ServiceName) InjectionSource(org.jboss.as.ee.component.InjectionSource) EEModuleClassDescription(org.jboss.as.ee.component.EEModuleClassDescription) Module(org.jboss.modules.Module) DeploymentUnit(org.jboss.as.server.deployment.DeploymentUnit)

Example 4 with InterceptorDescription

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

the class ContainerInterceptorBindingsDDProcessor method deploy.

@Override
public void deploy(final DeploymentPhaseContext phaseContext) throws DeploymentUnitProcessingException {
    final DeploymentUnit deploymentUnit = phaseContext.getDeploymentUnit();
    final EjbJarMetaData metaData = deploymentUnit.getAttachment(EjbDeploymentAttachmentKeys.EJB_JAR_METADATA);
    if (metaData == null || metaData.getAssemblyDescriptor() == null) {
        return;
    }
    final EEModuleDescription eeModuleDescription = deploymentUnit.getAttachment(Attachments.EE_MODULE_DESCRIPTION);
    final Module module = deploymentUnit.getAttachment(org.jboss.as.server.deployment.Attachments.MODULE);
    final DeploymentReflectionIndex index = deploymentUnit.getAttachment(org.jboss.as.server.deployment.Attachments.REFLECTION_INDEX);
    // fetch the container-interceptors
    final List<ContainerInterceptorsMetaData> containerInterceptorConfigurations = metaData.getAssemblyDescriptor().getAny(ContainerInterceptorsMetaData.class);
    if (containerInterceptorConfigurations == null || containerInterceptorConfigurations.isEmpty()) {
        return;
    }
    final ContainerInterceptorsMetaData containerInterceptorsMetaData = containerInterceptorConfigurations.get(0);
    if (containerInterceptorsMetaData == null) {
        return;
    }
    final InterceptorBindingsMetaData containerInterceptorBindings = containerInterceptorsMetaData.getInterceptorBindings();
    // no interceptor-binding == nothing to do
    if (containerInterceptorBindings == null || containerInterceptorBindings.isEmpty()) {
        return;
    }
    // we have now found some container interceptors which are bound to certain EJBs, start the real work!
    final Map<String, List<InterceptorBindingMetaData>> bindingsPerEJB = new HashMap<String, List<InterceptorBindingMetaData>>();
    final List<InterceptorBindingMetaData> bindingsForAllEJBs = new ArrayList<InterceptorBindingMetaData>();
    for (final InterceptorBindingMetaData containerInterceptorBinding : containerInterceptorBindings) {
        if (containerInterceptorBinding.getEjbName().equals("*")) {
            // since all EJBs having the same method is not really practical
            if (containerInterceptorBinding.getMethod() != null) {
                throw EjbLogger.ROOT_LOGGER.defaultInterceptorsNotBindToMethod();
            }
            if (containerInterceptorBinding.getInterceptorOrder() != null) {
                throw EjbLogger.ROOT_LOGGER.defaultInterceptorsNotSpecifyOrder();
            }
            // Make a note that this container interceptor binding is applicable for all EJBs
            bindingsForAllEJBs.add(containerInterceptorBinding);
        } else {
            // fetch existing container interceptor bindings for the EJB, if any.
            List<InterceptorBindingMetaData> bindings = bindingsPerEJB.get(containerInterceptorBinding.getEjbName());
            if (bindings == null) {
                bindings = new ArrayList<InterceptorBindingMetaData>();
                bindingsPerEJB.put(containerInterceptorBinding.getEjbName(), bindings);
            }
            // Make a note that the container interceptor binding is applicable for this specific EJB
            bindings.add(containerInterceptorBinding);
        }
    }
    // At this point we now know which container interceptor bindings have been configured for which EJBs.
    // Next, we create InterceptorDescription(s) out of those.
    final List<InterceptorDescription> interceptorDescriptionsForAllEJBs = new ArrayList<InterceptorDescription>();
    // first process container interceptors applicable for all EJBs
    for (InterceptorBindingMetaData binding : bindingsForAllEJBs) {
        if (binding.getInterceptorClasses() != null) {
            for (final String clazz : binding.getInterceptorClasses()) {
                interceptorDescriptionsForAllEJBs.add(new InterceptorDescription(clazz));
            }
        }
    }
    // Now process container interceptors for each EJB
    for (final ComponentDescription componentDescription : eeModuleDescription.getComponentDescriptions()) {
        if (!(componentDescription instanceof EJBComponentDescription)) {
            continue;
        }
        final EJBComponentDescription ejbComponentDescription = (EJBComponentDescription) componentDescription;
        final Class<?> componentClass;
        try {
            componentClass = module.getClassLoader().loadClass(ejbComponentDescription.getComponentClassName());
        } catch (ClassNotFoundException e) {
            throw EjbLogger.ROOT_LOGGER.failToLoadComponentClass(e, ejbComponentDescription.getComponentClassName());
        }
        final List<InterceptorBindingMetaData> bindingsApplicableForCurrentEJB = bindingsPerEJB.get(ejbComponentDescription.getComponentName());
        final Map<Method, List<InterceptorBindingMetaData>> methodInterceptors = new HashMap<Method, List<InterceptorBindingMetaData>>();
        final List<InterceptorBindingMetaData> classLevelBindings = new ArrayList<InterceptorBindingMetaData>();
        // we only want to exclude default and class level interceptors if every binding
        // has the exclude element.
        boolean classLevelExcludeDefaultInterceptors = false;
        Map<Method, Boolean> methodLevelExcludeDefaultInterceptors = new HashMap<Method, Boolean>();
        Map<Method, Boolean> methodLevelExcludeClassInterceptors = new HashMap<Method, Boolean>();
        // if an absolute order has been defined at any level then absolute ordering takes precedence
        boolean classLevelAbsoluteOrder = false;
        final Map<Method, Boolean> methodLevelAbsoluteOrder = new HashMap<Method, Boolean>();
        if (bindingsApplicableForCurrentEJB != null) {
            for (final InterceptorBindingMetaData binding : bindingsApplicableForCurrentEJB) {
                if (binding.getMethod() == null) {
                    // The container interceptor is expected to be fired for all methods of that EJB
                    classLevelBindings.add(binding);
                    // if even one binding does not say exclude default then we do not exclude
                    if (binding.isExcludeDefaultInterceptors()) {
                        classLevelExcludeDefaultInterceptors = true;
                    }
                    if (binding.isTotalOrdering()) {
                        if (classLevelAbsoluteOrder) {
                            throw EjbLogger.ROOT_LOGGER.twoEjbBindingsSpecifyAbsoluteOrder(componentClass.toString());
                        } else {
                            classLevelAbsoluteOrder = true;
                        }
                    }
                } else {
                    // Method level bindings
                    // First find the right method
                    final NamedMethodMetaData methodData = binding.getMethod();
                    final ClassReflectionIndex classIndex = index.getClassIndex(componentClass);
                    Method resolvedMethod = null;
                    if (methodData.getMethodParams() == null) {
                        final Collection<Method> methods = classIndex.getAllMethods(methodData.getMethodName());
                        if (methods.isEmpty()) {
                            throw EjbLogger.ROOT_LOGGER.failToFindMethodInEjbJarXml(componentClass.getName(), methodData.getMethodName());
                        } else if (methods.size() > 1) {
                            throw EjbLogger.ROOT_LOGGER.multipleMethodReferencedInEjbJarXml(methodData.getMethodName(), componentClass.getName());
                        }
                        resolvedMethod = methods.iterator().next();
                    } else {
                        final Collection<Method> methods = classIndex.getAllMethods(methodData.getMethodName(), methodData.getMethodParams().size());
                        for (final Method method : methods) {
                            boolean match = true;
                            for (int i = 0; i < method.getParameterTypes().length; ++i) {
                                if (!method.getParameterTypes()[i].getName().equals(methodData.getMethodParams().get(i))) {
                                    match = false;
                                    break;
                                }
                            }
                            if (match) {
                                resolvedMethod = method;
                                break;
                            }
                        }
                        if (resolvedMethod == null) {
                            throw EjbLogger.ROOT_LOGGER.failToFindMethodWithParameterTypes(componentClass.getName(), methodData.getMethodName(), methodData.getMethodParams());
                        }
                    }
                    List<InterceptorBindingMetaData> methodSpecificInterceptorBindings = methodInterceptors.get(resolvedMethod);
                    if (methodSpecificInterceptorBindings == null) {
                        methodSpecificInterceptorBindings = new ArrayList<InterceptorBindingMetaData>();
                        methodInterceptors.put(resolvedMethod, methodSpecificInterceptorBindings);
                    }
                    methodSpecificInterceptorBindings.add(binding);
                    if (binding.isExcludeDefaultInterceptors()) {
                        methodLevelExcludeDefaultInterceptors.put(resolvedMethod, true);
                    }
                    if (binding.isExcludeClassInterceptors()) {
                        methodLevelExcludeClassInterceptors.put(resolvedMethod, true);
                    }
                    if (binding.isTotalOrdering()) {
                        if (methodLevelAbsoluteOrder.containsKey(resolvedMethod)) {
                            throw EjbLogger.ROOT_LOGGER.twoEjbBindingsSpecifyAbsoluteOrder(resolvedMethod.toString());
                        } else {
                            methodLevelAbsoluteOrder.put(resolvedMethod, true);
                        }
                    }
                }
            }
        }
        // Now we have all the bindings in a format we can use
        // Build the list of default interceptors
        ejbComponentDescription.setDefaultContainerInterceptors(interceptorDescriptionsForAllEJBs);
        if (classLevelExcludeDefaultInterceptors) {
            ejbComponentDescription.setExcludeDefaultContainerInterceptors(true);
        }
        final List<InterceptorDescription> classLevelInterceptors = new ArrayList<InterceptorDescription>();
        if (classLevelAbsoluteOrder) {
            // We have an absolute ordering for the class level interceptors
            for (final InterceptorBindingMetaData binding : classLevelBindings) {
                // specify an ordering
                if (binding.isTotalOrdering()) {
                    for (final String interceptor : binding.getInterceptorOrder()) {
                        classLevelInterceptors.add(new InterceptorDescription(interceptor));
                    }
                    break;
                }
            }
            // We have merged the default interceptors into the class interceptors
            ejbComponentDescription.setExcludeDefaultContainerInterceptors(true);
        } else {
            for (InterceptorBindingMetaData binding : classLevelBindings) {
                if (binding.getInterceptorClasses() != null) {
                    for (final String interceptor : binding.getInterceptorClasses()) {
                        classLevelInterceptors.add(new InterceptorDescription(interceptor));
                    }
                }
            }
        }
        // We now know about the class level container interceptors for this EJB
        ejbComponentDescription.setClassLevelContainerInterceptors(classLevelInterceptors);
        // Now process method level container interceptors for the EJB
        for (Map.Entry<Method, List<InterceptorBindingMetaData>> entry : methodInterceptors.entrySet()) {
            final Method method = entry.getKey();
            final List<InterceptorBindingMetaData> methodBindings = entry.getValue();
            boolean totalOrder = methodLevelAbsoluteOrder.containsKey(method);
            final MethodIdentifier methodIdentifier = MethodIdentifier.getIdentifierForMethod(method);
            Boolean excludeDefaultInterceptors = methodLevelExcludeDefaultInterceptors.get(method);
            excludeDefaultInterceptors = excludeDefaultInterceptors == null ? false : excludeDefaultInterceptors;
            if (!excludeDefaultInterceptors) {
                excludeDefaultInterceptors = ejbComponentDescription.isExcludeDefaultContainerInterceptors() || ejbComponentDescription.isExcludeDefaultContainerInterceptors(methodIdentifier);
            }
            Boolean excludeClassInterceptors = methodLevelExcludeClassInterceptors.get(method);
            excludeClassInterceptors = excludeClassInterceptors == null ? false : excludeClassInterceptors;
            if (!excludeClassInterceptors) {
                excludeClassInterceptors = ejbComponentDescription.isExcludeClassLevelContainerInterceptors(methodIdentifier);
            }
            final List<InterceptorDescription> methodLevelInterceptors = new ArrayList<InterceptorDescription>();
            if (totalOrder) {
                // If there is a total order we just use it
                for (final InterceptorBindingMetaData binding : methodBindings) {
                    if (binding.isTotalOrdering()) {
                        for (final String interceptor : binding.getInterceptorOrder()) {
                            methodLevelInterceptors.add(new InterceptorDescription(interceptor));
                        }
                    }
                }
            } else {
                // the method specific interceptors
                if (!excludeDefaultInterceptors) {
                    methodLevelInterceptors.addAll(interceptorDescriptionsForAllEJBs);
                }
                if (!excludeClassInterceptors) {
                    for (InterceptorDescription interceptor : classLevelInterceptors) {
                        methodLevelInterceptors.add(interceptor);
                    }
                }
                for (final InterceptorBindingMetaData binding : methodBindings) {
                    if (binding.getInterceptorClasses() != null) {
                        for (final String interceptor : binding.getInterceptorClasses()) {
                            methodLevelInterceptors.add(new InterceptorDescription(interceptor));
                        }
                    }
                }
            }
            // We have already taken component and default interceptors into account
            ejbComponentDescription.excludeClassLevelContainerInterceptors(methodIdentifier);
            ejbComponentDescription.excludeDefaultContainerInterceptors(methodIdentifier);
            ejbComponentDescription.setMethodContainerInterceptors(methodIdentifier, methodLevelInterceptors);
        }
    }
}
Also used : EJBComponentDescription(org.jboss.as.ejb3.component.EJBComponentDescription) ComponentDescription(org.jboss.as.ee.component.ComponentDescription) HashMap(java.util.HashMap) InterceptorBindingMetaData(org.jboss.metadata.ejb.spec.InterceptorBindingMetaData) ArrayList(java.util.ArrayList) EJBComponentDescription(org.jboss.as.ejb3.component.EJBComponentDescription) InterceptorDescription(org.jboss.as.ee.component.InterceptorDescription) EEModuleDescription(org.jboss.as.ee.component.EEModuleDescription) ArrayList(java.util.ArrayList) List(java.util.List) EjbJarMetaData(org.jboss.metadata.ejb.spec.EjbJarMetaData) NamedMethodMetaData(org.jboss.metadata.ejb.spec.NamedMethodMetaData) ClassReflectionIndex(org.jboss.as.server.deployment.reflect.ClassReflectionIndex) InterceptorBindingsMetaData(org.jboss.metadata.ejb.spec.InterceptorBindingsMetaData) Method(java.lang.reflect.Method) MethodIdentifier(org.jboss.invocation.proxy.MethodIdentifier) ContainerInterceptorsMetaData(org.jboss.as.ejb3.interceptor.ContainerInterceptorsMetaData) Module(org.jboss.modules.Module) DeploymentUnit(org.jboss.as.server.deployment.DeploymentUnit) DeploymentReflectionIndex(org.jboss.as.server.deployment.reflect.DeploymentReflectionIndex) HashMap(java.util.HashMap) Map(java.util.Map)

Example 5 with InterceptorDescription

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

the class DeploymentDescriptorInterceptorBindingsProcessor method deploy.

@Override
public void deploy(final DeploymentPhaseContext phaseContext) throws DeploymentUnitProcessingException {
    final DeploymentUnit deploymentUnit = phaseContext.getDeploymentUnit();
    final EjbJarMetaData metaData = deploymentUnit.getAttachment(EjbDeploymentAttachmentKeys.EJB_JAR_METADATA);
    final EEModuleDescription eeModuleDescription = deploymentUnit.getAttachment(Attachments.EE_MODULE_DESCRIPTION);
    final Module module = deploymentUnit.getAttachment(org.jboss.as.server.deployment.Attachments.MODULE);
    final DeploymentReflectionIndex index = deploymentUnit.getAttachment(org.jboss.as.server.deployment.Attachments.REFLECTION_INDEX);
    if (metaData == null) {
        return;
    }
    if (metaData.getAssemblyDescriptor() == null) {
        return;
    }
    if (metaData.getAssemblyDescriptor().getInterceptorBindings() == null) {
        return;
    }
    //default interceptors must be mentioned in the interceptors section
    final Set<String> interceptorClasses = new HashSet<String>();
    if (metaData.getInterceptors() != null) {
        for (final InterceptorMetaData interceptor : metaData.getInterceptors()) {
            interceptorClasses.add(interceptor.getInterceptorClass());
        }
    }
    final Map<String, List<InterceptorBindingMetaData>> bindingsPerComponent = new HashMap<String, List<InterceptorBindingMetaData>>();
    final List<InterceptorBindingMetaData> defaultInterceptorBindings = new ArrayList<InterceptorBindingMetaData>();
    for (final InterceptorBindingMetaData binding : metaData.getAssemblyDescriptor().getInterceptorBindings()) {
        if (binding.getEjbName().equals("*")) {
            if (binding.getMethod() != null) {
                throw EjbLogger.ROOT_LOGGER.defaultInterceptorsNotBindToMethod();
            }
            if (binding.getInterceptorOrder() != null) {
                throw EjbLogger.ROOT_LOGGER.defaultInterceptorsNotSpecifyOrder();
            }
            defaultInterceptorBindings.add(binding);
        } else if (ejbNameRegexService.isEjbNameRegexAllowed()) {
            Pattern pattern = Pattern.compile(binding.getEjbName());
            for (final ComponentDescription componentDescription : eeModuleDescription.getComponentDescriptions()) {
                if (componentDescription instanceof EJBComponentDescription) {
                    String ejbName = ((EJBComponentDescription) componentDescription).getEJBName();
                    if (pattern.matcher(ejbName).matches()) {
                        List<InterceptorBindingMetaData> bindings = bindingsPerComponent.get(ejbName);
                        if (bindings == null) {
                            bindingsPerComponent.put(ejbName, bindings = new ArrayList<InterceptorBindingMetaData>());
                        }
                        bindings.add(binding);
                    }
                }
            }
        } else {
            List<InterceptorBindingMetaData> bindings = bindingsPerComponent.get(binding.getEjbName());
            if (bindings == null) {
                bindingsPerComponent.put(binding.getEjbName(), bindings = new ArrayList<InterceptorBindingMetaData>());
            }
            bindings.add(binding);
        }
    }
    final List<InterceptorDescription> defaultInterceptors = new ArrayList<InterceptorDescription>();
    for (InterceptorBindingMetaData binding : defaultInterceptorBindings) {
        if (binding.getInterceptorClasses() != null) {
            for (final String clazz : binding.getInterceptorClasses()) {
                //we only want default interceptors referenced in the interceptors section
                if (interceptorClasses.contains(clazz)) {
                    defaultInterceptors.add(new InterceptorDescription(clazz));
                } else {
                    ROOT_LOGGER.defaultInterceptorClassNotListed(clazz);
                }
            }
        }
    }
    //we iterate over all components, as we need to process default interceptors
    for (final ComponentDescription componentDescription : eeModuleDescription.getComponentDescriptions()) {
        final Class<?> componentClass;
        try {
            componentClass = module.getClassLoader().loadClass(componentDescription.getComponentClassName());
        } catch (ClassNotFoundException e) {
            throw EjbLogger.ROOT_LOGGER.failToLoadComponentClass(e, componentDescription.getComponentClassName());
        }
        final List<InterceptorBindingMetaData> bindings = bindingsPerComponent.get(componentDescription.getComponentName());
        final Map<Method, List<InterceptorBindingMetaData>> methodInterceptors = new HashMap<Method, List<InterceptorBindingMetaData>>();
        final List<InterceptorBindingMetaData> classLevelBindings = new ArrayList<InterceptorBindingMetaData>();
        //we only want to exclude default and class level interceptors if every binding
        //has the exclude element.
        boolean classLevelExcludeDefaultInterceptors = false;
        Map<Method, Boolean> methodLevelExcludeDefaultInterceptors = new HashMap<Method, Boolean>();
        Map<Method, Boolean> methodLevelExcludeClassInterceptors = new HashMap<Method, Boolean>();
        //if an absolute order has been defined at any level
        //absolute ordering takes precedence
        boolean classLevelAbsoluteOrder = false;
        final Map<Method, Boolean> methodLevelAbsoluteOrder = new HashMap<Method, Boolean>();
        if (bindings != null) {
            for (final InterceptorBindingMetaData binding : bindings) {
                if (binding.getMethod() == null) {
                    classLevelBindings.add(binding);
                    //if even one binding does not say exclude default then we do not exclude
                    if (binding.isExcludeDefaultInterceptors()) {
                        classLevelExcludeDefaultInterceptors = true;
                    }
                    if (binding.isTotalOrdering()) {
                        if (classLevelAbsoluteOrder) {
                            throw EjbLogger.ROOT_LOGGER.twoEjbBindingsSpecifyAbsoluteOrder(componentClass.toString());
                        } else {
                            classLevelAbsoluteOrder = true;
                        }
                    }
                } else {
                    //method level bindings
                    //first find the right method
                    final NamedMethodMetaData methodData = binding.getMethod();
                    final ClassReflectionIndex classIndex = index.getClassIndex(componentClass);
                    Method resolvedMethod = null;
                    if (methodData.getMethodParams() == null) {
                        final Collection<Method> methods = classIndex.getAllMethods(methodData.getMethodName());
                        if (methods.isEmpty()) {
                            throw EjbLogger.ROOT_LOGGER.failToFindMethodInEjbJarXml(componentClass.getName(), methodData.getMethodName());
                        } else if (methods.size() > 1) {
                            throw EjbLogger.ROOT_LOGGER.multipleMethodReferencedInEjbJarXml(methodData.getMethodName(), componentClass.getName());
                        }
                        resolvedMethod = methods.iterator().next();
                    } else {
                        final Collection<Method> methods = classIndex.getAllMethods(methodData.getMethodName(), methodData.getMethodParams().size());
                        for (final Method method : methods) {
                            boolean match = true;
                            for (int i = 0; i < method.getParameterTypes().length; ++i) {
                                if (!method.getParameterTypes()[i].getName().equals(methodData.getMethodParams().get(i))) {
                                    match = false;
                                    break;
                                }
                            }
                            if (match) {
                                resolvedMethod = method;
                                break;
                            }
                        }
                        if (resolvedMethod == null) {
                            throw EjbLogger.ROOT_LOGGER.failToFindMethodWithParameterTypes(componentClass.getName(), methodData.getMethodName(), methodData.getMethodParams());
                        }
                    }
                    List<InterceptorBindingMetaData> list = methodInterceptors.get(resolvedMethod);
                    if (list == null) {
                        methodInterceptors.put(resolvedMethod, list = new ArrayList<InterceptorBindingMetaData>());
                    }
                    list.add(binding);
                    if (binding.isExcludeDefaultInterceptors()) {
                        methodLevelExcludeDefaultInterceptors.put(resolvedMethod, true);
                    }
                    if (binding.isExcludeClassInterceptors()) {
                        methodLevelExcludeClassInterceptors.put(resolvedMethod, true);
                    }
                    if (binding.isTotalOrdering()) {
                        if (methodLevelAbsoluteOrder.containsKey(resolvedMethod)) {
                            throw EjbLogger.ROOT_LOGGER.twoEjbBindingsSpecifyAbsoluteOrder(resolvedMethod.toString());
                        } else {
                            methodLevelAbsoluteOrder.put(resolvedMethod, true);
                        }
                    }
                }
            }
        }
        //now we have all the bindings in a format we can use
        //build the list of default interceptors
        componentDescription.setDefaultInterceptors(defaultInterceptors);
        if (classLevelExcludeDefaultInterceptors) {
            componentDescription.setExcludeDefaultInterceptors(true);
        }
        final List<InterceptorDescription> classLevelInterceptors = new ArrayList<InterceptorDescription>();
        if (classLevelAbsoluteOrder) {
            //we have an absolute ordering for the class level interceptors
            for (final InterceptorBindingMetaData binding : classLevelBindings) {
                if (binding.isTotalOrdering()) {
                    for (final String interceptor : binding.getInterceptorOrder()) {
                        classLevelInterceptors.add(new InterceptorDescription(interceptor));
                    }
                }
            }
            //we have merged the default interceptors into the class interceptors
            componentDescription.setExcludeDefaultInterceptors(true);
        } else {
            //the order we want is default interceptors (this will be empty if they are excluded)
            //the annotation interceptors
            //then dd interceptors
            classLevelInterceptors.addAll(componentDescription.getClassInterceptors());
            for (InterceptorBindingMetaData binding : classLevelBindings) {
                if (binding.getInterceptorClasses() != null) {
                    for (final String interceptor : binding.getInterceptorClasses()) {
                        classLevelInterceptors.add(new InterceptorDescription(interceptor));
                    }
                }
            }
        }
        componentDescription.setClassInterceptors(classLevelInterceptors);
        for (Map.Entry<Method, List<InterceptorBindingMetaData>> entry : methodInterceptors.entrySet()) {
            final Method method = entry.getKey();
            final List<InterceptorBindingMetaData> methodBindings = entry.getValue();
            boolean totalOrder = methodLevelAbsoluteOrder.containsKey(method);
            final MethodIdentifier methodIdentifier = MethodIdentifier.getIdentifierForMethod(method);
            Boolean excludeDefaultInterceptors = methodLevelExcludeDefaultInterceptors.get(method);
            excludeDefaultInterceptors = excludeDefaultInterceptors == null ? false : excludeDefaultInterceptors;
            if (!excludeDefaultInterceptors) {
                excludeDefaultInterceptors = componentDescription.isExcludeDefaultInterceptors() || componentDescription.isExcludeDefaultInterceptors(methodIdentifier);
            }
            Boolean excludeClassInterceptors = methodLevelExcludeClassInterceptors.get(method);
            excludeClassInterceptors = excludeClassInterceptors == null ? false : excludeClassInterceptors;
            if (!excludeClassInterceptors) {
                excludeClassInterceptors = componentDescription.isExcludeClassInterceptors(methodIdentifier);
            }
            final List<InterceptorDescription> methodLevelInterceptors = new ArrayList<InterceptorDescription>();
            if (totalOrder) {
                //if there is a total order we just use it
                for (final InterceptorBindingMetaData binding : methodBindings) {
                    if (binding.isTotalOrdering()) {
                        for (final String interceptor : binding.getInterceptorOrder()) {
                            methodLevelInterceptors.add(new InterceptorDescription(interceptor));
                        }
                    }
                }
            } else {
                //class level interceptors also includes default interceptors
                if (!excludeDefaultInterceptors) {
                    methodLevelInterceptors.addAll(defaultInterceptors);
                }
                if (!excludeClassInterceptors) {
                    for (InterceptorDescription interceptor : classLevelInterceptors) {
                        methodLevelInterceptors.add(interceptor);
                    }
                }
                List<InterceptorDescription> annotationMethodLevel = componentDescription.getMethodInterceptors().get(methodIdentifier);
                if (annotationMethodLevel != null) {
                    methodLevelInterceptors.addAll(annotationMethodLevel);
                }
                //now add all the interceptors from the bindings
                for (InterceptorBindingMetaData binding : methodBindings) {
                    if (binding.getInterceptorClasses() != null) {
                        for (final String interceptor : binding.getInterceptorClasses()) {
                            methodLevelInterceptors.add(new InterceptorDescription(interceptor));
                        }
                    }
                }
            }
            //we have already taken component and default interceptors into account
            componentDescription.excludeClassInterceptors(methodIdentifier);
            componentDescription.excludeDefaultInterceptors(methodIdentifier);
            componentDescription.setMethodInterceptors(methodIdentifier, methodLevelInterceptors);
        }
    }
}
Also used : EJBComponentDescription(org.jboss.as.ejb3.component.EJBComponentDescription) ComponentDescription(org.jboss.as.ee.component.ComponentDescription) HashMap(java.util.HashMap) InterceptorBindingMetaData(org.jboss.metadata.ejb.spec.InterceptorBindingMetaData) ArrayList(java.util.ArrayList) EJBComponentDescription(org.jboss.as.ejb3.component.EJBComponentDescription) InterceptorDescription(org.jboss.as.ee.component.InterceptorDescription) EEModuleDescription(org.jboss.as.ee.component.EEModuleDescription) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet) Pattern(java.util.regex.Pattern) EjbJarMetaData(org.jboss.metadata.ejb.spec.EjbJarMetaData) NamedMethodMetaData(org.jboss.metadata.ejb.spec.NamedMethodMetaData) ClassReflectionIndex(org.jboss.as.server.deployment.reflect.ClassReflectionIndex) Method(java.lang.reflect.Method) MethodIdentifier(org.jboss.invocation.proxy.MethodIdentifier) InterceptorMetaData(org.jboss.metadata.ejb.spec.InterceptorMetaData) Module(org.jboss.modules.Module) DeploymentUnit(org.jboss.as.server.deployment.DeploymentUnit) DeploymentReflectionIndex(org.jboss.as.server.deployment.reflect.DeploymentReflectionIndex) HashMap(java.util.HashMap) Map(java.util.Map)

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