Search in sources :

Example 6 with ClassReflectionIndex

use of org.jboss.as.server.deployment.reflect.ClassReflectionIndex in project wildfly by wildfly.

the class InitMethodMergingProcessor method handleDeploymentDescriptor.

@Override
protected void handleDeploymentDescriptor(final DeploymentUnit deploymentUnit, final DeploymentReflectionIndex deploymentReflectionIndex, final Class<?> componentClass, final StatefulComponentDescription description) throws DeploymentUnitProcessingException {
    //first look for old school ejbCreate methods
    //we are only looking on the bean class, not sure if that is correct or not
    Class<?> clazz = componentClass;
    while (clazz != Object.class && clazz != null) {
        final ClassReflectionIndex index = deploymentReflectionIndex.getClassIndex(clazz);
        for (Method method : (Iterable<Method>) index.getMethods()) {
            if (method.getName().startsWith("ejbCreate")) {
                //it will be overridden below
                if (!description.getInitMethods().containsKey(method)) {
                    description.addInitMethod(method, null);
                }
            }
        }
        clazz = clazz.getSuperclass();
    }
    SessionBeanMetaData data = description.getDescriptorData();
    if (data instanceof SessionBean31MetaData) {
        SessionBean31MetaData metaData = (SessionBean31MetaData) data;
        final InitMethodsMetaData inits = metaData.getInitMethods();
        if (inits != null) {
            for (InitMethodMetaData method : inits) {
                Method beanMethod = MethodResolutionUtils.resolveMethod(method.getBeanMethod(), componentClass, deploymentReflectionIndex);
                if (method.getCreateMethod() != null) {
                    description.addInitMethod(beanMethod, method.getCreateMethod().getMethodName());
                } else {
                    description.addInitMethod(beanMethod, null);
                }
            }
        }
    }
}
Also used : InitMethodMetaData(org.jboss.metadata.ejb.spec.InitMethodMetaData) InitMethodsMetaData(org.jboss.metadata.ejb.spec.InitMethodsMetaData) SessionBeanMetaData(org.jboss.metadata.ejb.spec.SessionBeanMetaData) ClassReflectionIndex(org.jboss.as.server.deployment.reflect.ClassReflectionIndex) Method(java.lang.reflect.Method) SessionBean31MetaData(org.jboss.metadata.ejb.spec.SessionBean31MetaData)

Example 7 with ClassReflectionIndex

use of org.jboss.as.server.deployment.reflect.ClassReflectionIndex in project wildfly by wildfly.

the class MethodInjectionTarget method getMethod.

public Method getMethod(final DeploymentReflectionIndex reflectionIndex, final Class<?> clazz) throws DeploymentUnitProcessingException {
    final ClassReflectionIndex classIndex = reflectionIndex.getClassIndex(clazz);
    Collection<Method> methods = null;
    final String paramType = getDeclaredValueClassName();
    final String name = getName();
    final String className = getClassName();
    if (paramType != null) {
        // find the methods with the specific name and the param types
        methods = ClassReflectionIndexUtil.findMethods(reflectionIndex, classIndex, name, paramType);
    }
    // e.g. setMyBoolean(boolean) for a Boolean
    if (methods == null || methods.isEmpty()) {
        // find all the methods with the specific name and which accept just 1 parameter.
        methods = ClassReflectionIndexUtil.findAllMethods(reflectionIndex, classIndex, name, 1);
    }
    Iterator<Method> iterator = methods.iterator();
    if (!iterator.hasNext()) {
        throw EeLogger.ROOT_LOGGER.methodNotFound(name, paramType, className);
    }
    Method method = iterator.next();
    if (iterator.hasNext()) {
        throw EeLogger.ROOT_LOGGER.multipleMethodsFound(name, paramType, className);
    }
    return method;
}
Also used : ClassReflectionIndex(org.jboss.as.server.deployment.reflect.ClassReflectionIndex) Method(java.lang.reflect.Method)

Example 8 with ClassReflectionIndex

use of org.jboss.as.server.deployment.reflect.ClassReflectionIndex 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 9 with ClassReflectionIndex

use of org.jboss.as.server.deployment.reflect.ClassReflectionIndex in project wildfly by wildfly.

the class MethodResolutionUtils method resolveMethods.

public static Collection<Method> resolveMethods(final String methodName, final MethodParametersMetaData parameters, final Class<?> componentClass, final DeploymentReflectionIndex reflectionIndex) throws DeploymentUnitProcessingException {
    Class<?> clazz = componentClass;
    while (clazz != Object.class && clazz != null) {
        final ClassReflectionIndex classIndex = reflectionIndex.getClassIndex(clazz);
        if (parameters == null) {
            final Collection<Method> methods = classIndex.getAllMethods(methodName);
            if (!methods.isEmpty()) {
                return methods;
            }
        } else {
            final Collection<Method> methods = classIndex.getAllMethods(methodName, parameters.size());
            for (final Method method : methods) {
                boolean match = true;
                for (int i = 0; i < method.getParameterTypes().length; ++i) {
                    if (!method.getParameterTypes()[i].getName().equals(parameters.get(i))) {
                        match = false;
                        break;
                    }
                }
                if (match) {
                    return Collections.singleton(method);
                }
            }
        }
        clazz = clazz.getSuperclass();
    }
    throw EjbLogger.ROOT_LOGGER.failToFindMethodInEjbJarXml(componentClass.getName(), methodName);
}
Also used : ClassReflectionIndex(org.jboss.as.server.deployment.reflect.ClassReflectionIndex) Method(java.lang.reflect.Method)

Example 10 with ClassReflectionIndex

use of org.jboss.as.server.deployment.reflect.ClassReflectionIndex in project wildfly by wildfly.

the class EJBValidationConfigurator method configure.

@Override
public void configure(final DeploymentPhaseContext context, final ComponentDescription description, final ComponentConfiguration configuration) throws DeploymentUnitProcessingException {
    final DeploymentReflectionIndex deploymentReflectionIndex = context.getDeploymentUnit().getAttachment(Attachments.REFLECTION_INDEX);
    final ClassReflectionIndex classIndex = deploymentReflectionIndex.getClassIndex(configuration.getComponentClass());
    final Constructor<?> ctor = classIndex.getConstructor(new String[0]);
    boolean noInterface = false;
    for (ViewDescription view : description.getViews()) {
        if (view.getViewClassName().equals(description.getComponentClassName())) {
            noInterface = true;
        }
    }
    if (ctor == null && noInterface) {
        //we only validate this for no interface views
        throw EjbLogger.ROOT_LOGGER.ejbMustHavePublicDefaultConstructor(description.getComponentName(), description.getComponentClassName());
    }
    if (configuration.getComponentClass().getEnclosingClass() != null) {
        throw EjbLogger.ROOT_LOGGER.ejbMustNotBeInnerClass(description.getComponentName(), description.getComponentClassName());
    }
    if (!Modifier.isPublic(configuration.getComponentClass().getModifiers())) {
        throw EjbLogger.ROOT_LOGGER.ejbMustBePublicClass(description.getComponentName(), description.getComponentClassName());
    }
    if (Modifier.isFinal(configuration.getComponentClass().getModifiers())) {
        throw EjbLogger.ROOT_LOGGER.ejbMustNotBeFinalClass(description.getComponentName(), description.getComponentClassName());
    }
}
Also used : ViewDescription(org.jboss.as.ee.component.ViewDescription) ClassReflectionIndex(org.jboss.as.server.deployment.reflect.ClassReflectionIndex) DeploymentReflectionIndex(org.jboss.as.server.deployment.reflect.DeploymentReflectionIndex)

Aggregations

ClassReflectionIndex (org.jboss.as.server.deployment.reflect.ClassReflectionIndex)29 Method (java.lang.reflect.Method)24 DeploymentReflectionIndex (org.jboss.as.server.deployment.reflect.DeploymentReflectionIndex)11 DeploymentUnitProcessingException (org.jboss.as.server.deployment.DeploymentUnitProcessingException)8 DeploymentUnit (org.jboss.as.server.deployment.DeploymentUnit)6 HashMap (java.util.HashMap)5 List (java.util.List)5 ComponentConfiguration (org.jboss.as.ee.component.ComponentConfiguration)5 ViewDescription (org.jboss.as.ee.component.ViewDescription)5 DeploymentPhaseContext (org.jboss.as.server.deployment.DeploymentPhaseContext)5 MethodIdentifier (org.jboss.invocation.proxy.MethodIdentifier)5 Module (org.jboss.modules.Module)5 ArrayList (java.util.ArrayList)4 ComponentDescription (org.jboss.as.ee.component.ComponentDescription)4 ViewConfiguration (org.jboss.as.ee.component.ViewConfiguration)4 ViewConfigurator (org.jboss.as.ee.component.ViewConfigurator)4 WriteReplaceInterface (org.jboss.as.ee.component.serialization.WriteReplaceInterface)4 EJBViewDescription (org.jboss.as.ejb3.component.EJBViewDescription)4 Map (java.util.Map)3 EEModuleDescription (org.jboss.as.ee.component.EEModuleDescription)3