Search in sources :

Example 1 with AnnotationManager

use of org.apache.webbeans.annotation.AnnotationManager in project tomee by apache.

the class BeanContext method mergeOWBAndOpenEJBInfo.

public void mergeOWBAndOpenEJBInfo() {
    final CdiEjbBean cdiEjbBean = get(CdiEjbBean.class);
    if (cdiEjbBean == null) {
        return;
    }
    final InjectionTargetImpl<?> injectionTarget = InjectionTargetImpl.class.cast(get(CdiEjbBean.class).getInjectionTarget());
    final InterceptorResolutionService.BeanInterceptorInfo info = injectionTarget.getInterceptorInfo();
    if (info == null) {
        return;
    }
    final Collection<Interceptor<?>> postConstructInterceptors = Collection.class.cast(Reflections.get(injectionTarget, "postConstructInterceptors"));
    final Collection<Interceptor<?>> preDestroyInterceptors = Collection.class.cast(Reflections.get(injectionTarget, "preDestroyInterceptors"));
    if (postConstructInterceptors != null) {
        for (final Interceptor<?> pc : postConstructInterceptors) {
            if (isEjbInterceptor(pc)) {
                continue;
            }
            final InterceptorData interceptorData = createInterceptorData(pc);
            instanceScopedInterceptors.add(interceptorData);
            cdiInterceptors.add(interceptorData);
        }
    }
    if (preDestroyInterceptors != null) {
        for (final Interceptor<?> pd : preDestroyInterceptors) {
            if (isEjbInterceptor(pd)) {
                continue;
            }
            if (postConstructInterceptors.contains(pd)) {
                continue;
            }
            final InterceptorData interceptorData = createInterceptorData(pd);
            instanceScopedInterceptors.add(interceptorData);
            cdiInterceptors.add(interceptorData);
        }
    }
    for (final Map.Entry<Method, InterceptorResolutionService.BusinessMethodInterceptorInfo> entry : info.getBusinessMethodsInfo().entrySet()) {
        final Interceptor<?>[] interceptors = entry.getValue().getCdiInterceptors();
        if (interceptors == null) {
            continue;
        }
        for (final Interceptor<?> i : interceptors) {
            // already at class level, since we merge "hooks" in InterceptorData no need to add it again
            if (postConstructInterceptors.contains(i) || preDestroyInterceptors.contains(i)) {
                continue;
            }
            final InterceptorData data = createInterceptorData(i);
            addCdiMethodInterceptor(entry.getKey(), data);
        }
        entry.getValue().setEjbInterceptors(new ArrayList<>());
        entry.getValue().setCdiInterceptors(new ArrayList<>());
    }
    // handled by OpenEJB now so clean up all duplication from OWB
    if (info.getSelfInterceptorBean() != null) {
        try {
            final Field field = InterceptorResolutionService.BeanInterceptorInfo.class.getDeclaredField("selfInterceptorBean");
            field.setAccessible(true);
            field.set(info, null);
        } catch (final Exception e) {
        // no-op
        }
    }
    Map.class.cast(Reflections.get(injectionTarget, "methodInterceptors")).clear();
    clear(Collection.class.cast(postConstructInterceptors));
    clear(Collection.class.cast(preDestroyInterceptors));
    clear(Collection.class.cast(Reflections.get(injectionTarget, "postConstructMethods")));
    clear(Collection.class.cast(Reflections.get(injectionTarget, "preDestroyMethods")));
    clear(Collection.class.cast(Reflections.get(info, "ejbInterceptors")));
    clear(Collection.class.cast(Reflections.get(info, "cdiInterceptors")));
    // OWB doesn't compute AROUND_INVOKE so let's do it
    final Method timeout = getEjbTimeout();
    if (timeout != null) {
        final AnnotatedType annotatedType = cdiEjbBean.getAnnotatedType();
        final AnnotationManager annotationManager = getWebBeansContext().getAnnotationManager();
        final Collection<Annotation> annotations = new HashSet<>(annotationManager.getInterceptorAnnotations(annotatedType.getAnnotations()));
        final Set<AnnotatedMethod<?>> methods = annotatedType.getMethods();
        for (final AnnotatedMethod<?> m : methods) {
            if (timeout.equals(m.getJavaMember())) {
                annotations.addAll(annotationManager.getInterceptorAnnotations(m.getAnnotations()));
                break;
            }
        }
        if (!annotations.isEmpty()) {
            for (final Interceptor<?> timeoutInterceptor : getWebBeansContext().getBeanManagerImpl().resolveInterceptors(InterceptionType.AROUND_TIMEOUT, AnnotationUtil.asArray(annotations))) {
                if (isEjbInterceptor(timeoutInterceptor)) {
                    continue;
                }
                final InterceptorData data = createInterceptorData(timeoutInterceptor);
                addCdiMethodInterceptor(timeout, data);
            }
        }
    }
}
Also used : AnnotationManager(org.apache.webbeans.annotation.AnnotationManager) AnnotatedMethod(javax.enterprise.inject.spi.AnnotatedMethod) CdiEjbBean(org.apache.openejb.cdi.CdiEjbBean) Field(java.lang.reflect.Field) Interceptor(javax.enterprise.inject.spi.Interceptor) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) InterceptorResolutionService(org.apache.webbeans.intercept.InterceptorResolutionService) Method(java.lang.reflect.Method) AnnotatedMethod(javax.enterprise.inject.spi.AnnotatedMethod) ApplicationException(javax.ejb.ApplicationException) ConstructionException(org.apache.xbean.recipe.ConstructionException) Annotation(java.lang.annotation.Annotation) AnnotatedType(javax.enterprise.inject.spi.AnnotatedType) InterceptorData(org.apache.openejb.core.interceptor.InterceptorData) Collection(java.util.Collection) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

Annotation (java.lang.annotation.Annotation)1 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ApplicationException (javax.ejb.ApplicationException)1 AnnotatedMethod (javax.enterprise.inject.spi.AnnotatedMethod)1 AnnotatedType (javax.enterprise.inject.spi.AnnotatedType)1 Interceptor (javax.enterprise.inject.spi.Interceptor)1 CdiEjbBean (org.apache.openejb.cdi.CdiEjbBean)1 InterceptorData (org.apache.openejb.core.interceptor.InterceptorData)1 AnnotationManager (org.apache.webbeans.annotation.AnnotationManager)1 InterceptorResolutionService (org.apache.webbeans.intercept.InterceptorResolutionService)1 ConstructionException (org.apache.xbean.recipe.ConstructionException)1