Search in sources :

Example 1 with ResourceContainerContext

use of com.sun.enterprise.deployment.annotation.context.ResourceContainerContext in project Payara by payara.

the class ManagedBeanHandler method processAnnotation.

public HandlerProcessingResult processAnnotation(AnnotationInfo element) throws AnnotationProcessorException {
    AnnotatedElementHandler aeHandler = element.getProcessingContext().getHandler();
    if (aeHandler instanceof ManagedBeanContext) {
        // Ignore @ManagedBean processing during ManagedBean class processing itself
        return getDefaultProcessedResult();
    }
    ManagedBeanDescriptor managedBeanDesc = new ManagedBeanDescriptor();
    ManagedBean resourceAn = (ManagedBean) element.getAnnotation();
    // name() is optional
    String logicalName = resourceAn.value();
    if (!logicalName.equals("")) {
        managedBeanDesc.setName(logicalName);
    }
    Class managedBeanClass = (Class) element.getAnnotatedElement();
    managedBeanDesc.setBeanClassName(managedBeanClass.getName());
    Class[] classInterceptors = null;
    Map<AccessibleObject, Class[]> methodLevelInterceptors = new HashMap<AccessibleObject, Class[]>();
    Map<String, InterceptorDescriptor> interceptorDescs = new HashMap<String, InterceptorDescriptor>();
    // For now, just process the javax.interceptor related annotations directly instead
    // of relying on the annotation framework.   All the existing javax.interceptor
    // handlers are very tightly coupled to ejb so it would be more work to abstract those
    // than to just process the annotations directly.  Also, do javax.interceptor
    // annotation processing reflectively to avoid dependency on javax.interceptor from
    // DOL module.
    // TODO refactor javax.interceptor annotation handlers to support both ejb and non-ejb
    // related interceptors
    Annotation interceptorsAnn = getClassAnnotation(managedBeanClass, "javax.interceptor.Interceptors");
    if (interceptorsAnn != null) {
        try {
            Method m = interceptorsAnn.annotationType().getDeclaredMethod("value");
            classInterceptors = (Class[]) m.invoke(interceptorsAnn);
        } catch (Exception e) {
            AnnotationProcessorException ape = new AnnotationProcessorException(e.getMessage(), element);
            ape.initCause(e);
            throw ape;
        }
    }
    Class nextIntClass = managedBeanClass;
    while (nextIntClass != Object.class) {
        Method managedBeanAroundInvoke = getMethodForMethodAnnotation(nextIntClass, "javax.interceptor.AroundInvoke");
        if ((managedBeanAroundInvoke != null) && !(methodOverridden(managedBeanAroundInvoke, nextIntClass, managedBeanClass))) {
            LifecycleCallbackDescriptor desc = new LifecycleCallbackDescriptor();
            desc.setLifecycleCallbackClass(nextIntClass.getName());
            desc.setLifecycleCallbackMethod(managedBeanAroundInvoke.getName());
            managedBeanDesc.addAroundInvokeDescriptor(desc);
        }
        nextIntClass = nextIntClass.getSuperclass();
    }
    for (Method m : managedBeanClass.getMethods()) {
        processForAnnotations(element, m, methodLevelInterceptors, managedBeanDesc, managedBeanClass);
    }
    for (Constructor c : managedBeanClass.getDeclaredConstructors()) {
        processForAnnotations(element, c, methodLevelInterceptors, managedBeanDesc, managedBeanClass);
    }
    if (aeHandler instanceof ResourceContainerContext) {
        ((ResourceContainerContext) aeHandler).addManagedBean(managedBeanDesc);
        // process managed bean class annotations
        ManagedBeanContext managedBeanContext = new ManagedBeanContext(managedBeanDesc);
        ProcessingContext procContext = element.getProcessingContext();
        procContext.pushHandler(managedBeanContext);
        procContext.getProcessor().process(procContext, new Class[] { managedBeanClass });
        List<InterceptorDescriptor> classInterceptorChain = new LinkedList<InterceptorDescriptor>();
        if (classInterceptors != null) {
            for (Class i : classInterceptors) {
                InterceptorDescriptor nextInterceptor = processInterceptor(i, managedBeanContext, procContext);
                // Add interceptor to class-level chain
                classInterceptorChain.add(nextInterceptor);
                interceptorDescs.put(i.getName(), nextInterceptor);
            }
            managedBeanDesc.setClassInterceptorChain(classInterceptorChain);
        }
        for (Map.Entry<AccessibleObject, Class[]> next : methodLevelInterceptors.entrySet()) {
            AccessibleObject o = next.getKey();
            Class[] interceptors = next.getValue();
            boolean excludeClassInterceptors = (getMethodAnnotation(o, "javax.interceptor.ExcludeClassInterceptors") != null);
            List<InterceptorDescriptor> methodInterceptorChain = excludeClassInterceptors ? new LinkedList<InterceptorDescriptor>() : new LinkedList<InterceptorDescriptor>(classInterceptorChain);
            for (Class nextInterceptor : interceptors) {
                InterceptorDescriptor interceptorDesc = interceptorDescs.get(nextInterceptor.getName());
                if (interceptorDesc == null) {
                    interceptorDesc = processInterceptor(nextInterceptor, managedBeanContext, procContext);
                    interceptorDescs.put(nextInterceptor.getName(), interceptorDesc);
                }
                methodInterceptorChain.add(interceptorDesc);
            }
            MethodDescriptor mDesc = getMethodDescriptor(o, managedBeanClass);
            if (mDesc != null) {
                managedBeanDesc.setMethodLevelInterceptorChain(mDesc, methodInterceptorChain);
            }
        }
    }
    return getDefaultProcessedResult();
}
Also used : HashMap(java.util.HashMap) ManagedBeanContext(com.sun.enterprise.deployment.annotation.context.ManagedBeanContext) AccessibleObject(java.lang.reflect.AccessibleObject) ResourceContainerContext(com.sun.enterprise.deployment.annotation.context.ResourceContainerContext) Constructor(java.lang.reflect.Constructor) Method(java.lang.reflect.Method) Annotation(java.lang.annotation.Annotation) LinkedList(java.util.LinkedList) ManagedBean(javax.annotation.ManagedBean) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with ResourceContainerContext

use of com.sun.enterprise.deployment.annotation.context.ResourceContainerContext in project Payara by payara.

the class PostConstructHandler method processAnnotation.

protected HandlerProcessingResult processAnnotation(AnnotationInfo ainfo, ResourceContainerContext[] rcContexts) throws AnnotationProcessorException {
    Method annMethod = (Method) ainfo.getAnnotatedElement();
    validateAnnotatedLifecycleMethod(annMethod);
    String pcMethodName = annMethod.getName();
    String pcClassName = annMethod.getDeclaringClass().getName();
    for (ResourceContainerContext rcContext : rcContexts) {
        LifecycleCallbackDescriptor postConstructDesc = new LifecycleCallbackDescriptor();
        postConstructDesc.setLifecycleCallbackClass(pcClassName);
        postConstructDesc.setLifecycleCallbackMethod(pcMethodName);
        postConstructDesc.setMetadataSource(MetadataSource.ANNOTATION);
        // override by xml is handled in addPostConstructDescriptor
        rcContext.addPostConstructDescriptor(postConstructDesc);
    }
    return getDefaultProcessedResult();
}
Also used : LifecycleCallbackDescriptor(com.sun.enterprise.deployment.LifecycleCallbackDescriptor) ResourceContainerContext(com.sun.enterprise.deployment.annotation.context.ResourceContainerContext) Method(java.lang.reflect.Method)

Example 3 with ResourceContainerContext

use of com.sun.enterprise.deployment.annotation.context.ResourceContainerContext in project Payara by payara.

the class PreDestroyHandler method processAnnotation.

protected HandlerProcessingResult processAnnotation(AnnotationInfo ainfo, ResourceContainerContext[] rcContexts) throws AnnotationProcessorException {
    Method annMethod = (Method) ainfo.getAnnotatedElement();
    validateAnnotatedLifecycleMethod(annMethod);
    String pdMethodName = annMethod.getName();
    String pdClassName = annMethod.getDeclaringClass().getName();
    for (ResourceContainerContext rcContext : rcContexts) {
        LifecycleCallbackDescriptor preDestroyDesc = new LifecycleCallbackDescriptor();
        preDestroyDesc.setLifecycleCallbackClass(pdClassName);
        preDestroyDesc.setLifecycleCallbackMethod(pdMethodName);
        preDestroyDesc.setMetadataSource(MetadataSource.ANNOTATION);
        // override by xml is handled in addPreDestroyDescriptor
        rcContext.addPreDestroyDescriptor(preDestroyDesc);
    }
    return getDefaultProcessedResult();
}
Also used : LifecycleCallbackDescriptor(com.sun.enterprise.deployment.LifecycleCallbackDescriptor) ResourceContainerContext(com.sun.enterprise.deployment.annotation.context.ResourceContainerContext) Method(java.lang.reflect.Method)

Example 4 with ResourceContainerContext

use of com.sun.enterprise.deployment.annotation.context.ResourceContainerContext in project Payara by payara.

the class AbstractResourceHandler method processAnnotation.

/**
 * Process a particular annotation which type is the same as the
 * one returned by @see getAnnotationType(). All information
 * pertinent to the annotation and its context is encapsulated
 * in the passed AnnotationInfo instance.
 *
 * @param ainfo the annotation information
 * @return
 * @throws AnnotationProcessorException
 */
@Override
public HandlerProcessingResult processAnnotation(AnnotationInfo ainfo) throws AnnotationProcessorException {
    AnnotatedElementHandler aeHandler = ainfo.getProcessingContext().getHandler();
    if (aeHandler instanceof EjbBundleContext) {
        EjbBundleContext ejbBundleContext = (EjbBundleContext) aeHandler;
        aeHandler = ejbBundleContext.createContextForEjb();
        if (aeHandler == null) {
            aeHandler = ejbBundleContext.createContextForEjbInterceptor();
        }
        // If it's still null and we're in an ejb-jar, use the EjbBundleContext.
        // This way we process dependencies on any classes (other than ejbs ,
        // interceptors , and their super-classes) that have annotations in case
        // we need the info for managed classes we wouldn't normally know about
        // (e.g. 299 classes).   In a .war, those are already processed during the
        // .war annotation scanning.
        EjbBundleDescriptor bundleDesc = ejbBundleContext.getDescriptor();
        RootDeploymentDescriptor enclosingBundle = bundleDesc.getModuleDescriptor().getDescriptor();
        boolean ejbJar = enclosingBundle instanceof EjbBundleDescriptor;
        if ((aeHandler == null) && ejbJar) {
            aeHandler = ejbBundleContext;
        }
    }
    if (aeHandler == null) {
        // not an ejb, interceptor in ejbBundle
        return getInvalidAnnotatedElementHandlerResult(ainfo.getProcessingContext().getHandler(), ainfo);
    }
    ResourceContainerContext[] rcContexts = null;
    if (aeHandler instanceof EjbsContext) {
        EjbsContext ejbsContext = (EjbsContext) aeHandler;
        rcContexts = (ResourceContainerContext[]) ejbsContext.getEjbContexts();
    } else if (aeHandler instanceof WebComponentsContext) {
        WebComponentsContext webCompsContext = (WebComponentsContext) aeHandler;
        rcContexts = (ResourceContainerContext[]) webCompsContext.getWebComponentContexts();
    } else if (aeHandler instanceof ResourceContainerContext) {
        rcContexts = new ResourceContainerContext[] { (ResourceContainerContext) aeHandler };
    } else {
        return getInvalidAnnotatedElementHandlerResult(aeHandler, ainfo);
    }
    return processAnnotation(ainfo, rcContexts);
}
Also used : EjbBundleContext(com.sun.enterprise.deployment.annotation.context.EjbBundleContext) ResourceContainerContext(com.sun.enterprise.deployment.annotation.context.ResourceContainerContext) WebComponentsContext(com.sun.enterprise.deployment.annotation.context.WebComponentsContext) EjbBundleDescriptor(com.sun.enterprise.deployment.EjbBundleDescriptor) RootDeploymentDescriptor(org.glassfish.deployment.common.RootDeploymentDescriptor) AnnotatedElementHandler(org.glassfish.apf.AnnotatedElementHandler) EjbsContext(com.sun.enterprise.deployment.annotation.context.EjbsContext)

Aggregations

ResourceContainerContext (com.sun.enterprise.deployment.annotation.context.ResourceContainerContext)4 Method (java.lang.reflect.Method)3 LifecycleCallbackDescriptor (com.sun.enterprise.deployment.LifecycleCallbackDescriptor)2 EjbBundleDescriptor (com.sun.enterprise.deployment.EjbBundleDescriptor)1 EjbBundleContext (com.sun.enterprise.deployment.annotation.context.EjbBundleContext)1 EjbsContext (com.sun.enterprise.deployment.annotation.context.EjbsContext)1 ManagedBeanContext (com.sun.enterprise.deployment.annotation.context.ManagedBeanContext)1 WebComponentsContext (com.sun.enterprise.deployment.annotation.context.WebComponentsContext)1 Annotation (java.lang.annotation.Annotation)1 AccessibleObject (java.lang.reflect.AccessibleObject)1 Constructor (java.lang.reflect.Constructor)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 Map (java.util.Map)1 ManagedBean (javax.annotation.ManagedBean)1 AnnotatedElementHandler (org.glassfish.apf.AnnotatedElementHandler)1 RootDeploymentDescriptor (org.glassfish.deployment.common.RootDeploymentDescriptor)1