Search in sources :

Example 1 with Interceptor

use of javax.enterprise.inject.spi.Interceptor in project wildfly by wildfly.

the class Jsr299BindingsCreateInterceptor method processInvocation.

@Override
public Object processInvocation(InterceptorContext interceptorContext) throws Exception {
    BeanManagerImpl beanManager = this.beanManager;
    if (beanManager == null) {
        //cache the BM lookup, as it is quite slow
        beanManager = this.beanManager = this.weldContainer.getValue().getBeanManager(beanArchiveId);
    }
    //this is not always called with the deployments TCCL set
    //which causes weld to blow up
    SessionBean<Object> bean = null;
    if (ejbName != null) {
        EjbDescriptor<Object> descriptor = beanManager.getEjbDescriptor(this.ejbName);
        if (descriptor != null) {
            bean = beanManager.getBean(descriptor);
        }
    }
    InterceptorBindings interceptorBindings = this.interceptorBindings.getValue();
    final ComponentInstance componentInstance = interceptorContext.getPrivateData(ComponentInstance.class);
    InterceptorInstances existing = interceptorSupport.getInterceptorInstances(componentInstance);
    if (existing == null) {
        CreationalContext<Object> creationalContext = beanManager.createCreationalContext(bean);
        HashMap<String, SerializableContextualInstance<Interceptor<Object>, Object>> interceptorInstances = new HashMap<String, SerializableContextualInstance<Interceptor<Object>, Object>>();
        if (interceptorBindings != null) {
            for (Interceptor<?> interceptor : interceptorBindings.getAllInterceptors()) {
                addInterceptorInstance((Interceptor<Object>) interceptor, beanManager, interceptorInstances, creationalContext);
            }
        }
        interceptorSupport.setInterceptorInstances(componentInstance, new WeldInterceptorInstances(creationalContext, interceptorInstances));
    }
    return interceptorContext.proceed();
}
Also used : HashMap(java.util.HashMap) InterceptorInstances(org.jboss.as.weld.spi.InterceptorInstances) SerializableContextualInstance(org.jboss.weld.serialization.spi.helpers.SerializableContextualInstance) BeanManagerImpl(org.jboss.weld.manager.BeanManagerImpl) InterceptorBindings(org.jboss.weld.ejb.spi.InterceptorBindings) ComponentInstance(org.jboss.as.ee.component.ComponentInstance) Interceptor(javax.enterprise.inject.spi.Interceptor)

Example 2 with Interceptor

use of javax.enterprise.inject.spi.Interceptor 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<Interceptor<?>>());
        entry.getValue().setCdiInterceptors(new ArrayList<Interceptor<?>>());
    }
    // 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<>();
        annotations.addAll(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

HashMap (java.util.HashMap)2 Interceptor (javax.enterprise.inject.spi.Interceptor)2 Annotation (java.lang.annotation.Annotation)1 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 Collection (java.util.Collection)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 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 ComponentInstance (org.jboss.as.ee.component.ComponentInstance)1