Search in sources :

Example 1 with ProducerMethodBeansBuilder

use of org.apache.webbeans.component.creation.ProducerMethodBeansBuilder in project tomee by apache.

the class CdiPlugin method defineSessionBean.

@Override
public <T> Bean<T> defineSessionBean(final Class<T> clazz, final BeanAttributes<T> attributes, final AnnotatedType<T> annotatedType) {
    final BeanContext bc = findBeanContext(webBeansContext, clazz);
    final Class<?> superClass = bc.getManagedClass().getSuperclass();
    if (annotatedType.isAnnotationPresent(Specializes.class)) {
        if (superClass != Object.class && !isSessionBean(superClass)) {
            throw new DefinitionException("You can only specialize another EJB: " + clazz);
        }
        final BeanContext parentBc = findBeanContext(webBeansContext, superClass);
        final List<Class> businessLocalInterfaces = new ArrayList<>(parentBc.getBusinessLocalInterfaces());
        for (final Class<?> api : bc.getBusinessLocalInterfaces()) {
            businessLocalInterfaces.removeAll(GenericsUtil.getTypeClosure(api));
        }
        if (!businessLocalInterfaces.isEmpty()) {
            throw new DefinitionException("You can only specialize another EJB with at least the same API: " + clazz);
        }
    }
    final CdiEjbBean<T> bean = new OpenEJBBeanBuilder<T>(bc, webBeansContext, annotatedType, attributes).createBean(clazz, !annotatedType.isAnnotationPresent(Vetoed.class));
    bc.set(CdiEjbBean.class, bean);
    bc.set(CurrentCreationalContext.class, new CurrentCreationalContext());
    validateDisposeMethods(bean);
    validateScope(bean);
    final Set<ObserverMethod<?>> observerMethods;
    if (bean.isEnabled()) {
        observerMethods = new ObserverMethodsBuilder<T>(webBeansContext, bean.getAnnotatedType()).defineObserverMethods(bean);
    } else {
        observerMethods = new HashSet<>();
    }
    final WebBeansUtil webBeansUtil = webBeansContext.getWebBeansUtil();
    final Set<ProducerFieldBean<?>> producerFields = new ProducerFieldBeansBuilder(bean.getWebBeansContext(), bean.getAnnotatedType()).defineProducerFields(bean);
    final Set<ProducerMethodBean<?>> producerMethods = new ProducerMethodBeansBuilder(bean.getWebBeansContext(), bean.getAnnotatedType()).defineProducerMethods(bean, producerFields);
    final Map<ProducerMethodBean<?>, AnnotatedMethod<?>> annotatedMethods = new HashMap<>();
    for (final ProducerMethodBean<?> producerMethod : producerMethods) {
        final AnnotatedMethod<?> method = webBeansContext.getAnnotatedElementFactory().newAnnotatedMethod(producerMethod.getCreatorMethod(), annotatedType);
        webBeansUtil.inspectDeploymentErrorStack("There are errors that are added by ProcessProducer event observers for " + "ProducerMethods. Look at logs for further details");
        annotatedMethods.put(producerMethod, method);
    }
    final Map<ProducerFieldBean<?>, AnnotatedField<?>> annotatedFields = new HashMap<>();
    for (final ProducerFieldBean<?> producerField : producerFields) {
        if (!Modifier.isStatic(producerField.getCreatorField().getModifiers())) {
            throw new DefinitionException("In an EJB all producer fields should be static");
        }
        webBeansUtil.inspectDeploymentErrorStack("There are errors that are added by ProcessProducer event observers for" + " ProducerFields. Look at logs for further details");
        annotatedFields.put(producerField, webBeansContext.getAnnotatedElementFactory().newAnnotatedField(producerField.getCreatorField(), webBeansContext.getAnnotatedElementFactory().newAnnotatedType(producerField.getBeanClass())));
    }
    final Map<ObserverMethod<?>, AnnotatedMethod<?>> observerMethodsMap = new HashMap<>();
    for (final ObserverMethod<?> observerMethod : observerMethods) {
        final ObserverMethodImpl<?> impl = (ObserverMethodImpl<?>) observerMethod;
        final AnnotatedMethod<?> method = impl.getObserverMethod();
        observerMethodsMap.put(observerMethod, method);
    }
    validateProduceMethods(bean, producerMethods);
    validateObserverMethods(bean, observerMethodsMap);
    final BeanManagerImpl beanManager = webBeansContext.getBeanManagerImpl();
    //Fires ProcessManagedBean
    final GProcessSessionBean event = new GProcessSessionBean(Bean.class.cast(bean), annotatedType, bc.getEjbName(), bean.getEjbType());
    beanManager.fireEvent(event, true);
    event.setStarted();
    webBeansUtil.inspectDeploymentErrorStack("There are errors that are added by ProcessSessionBean event observers for managed beans. Look at logs for further details");
    //Fires ProcessProducerMethod
    webBeansUtil.fireProcessProducerMethodBeanEvent(annotatedMethods, annotatedType);
    webBeansUtil.inspectDeploymentErrorStack("There are errors that are added by ProcessProducerMethod event observers for producer method beans. Look at logs for further details");
    //Fires ProcessProducerField
    webBeansUtil.fireProcessProducerFieldBeanEvent(annotatedFields);
    webBeansUtil.inspectDeploymentErrorStack("There are errors that are added by ProcessProducerField event observers for producer field beans. Look at logs for further details");
    //Fire ObservableMethods
    webBeansUtil.fireProcessObservableMethodBeanEvent(observerMethodsMap);
    webBeansUtil.inspectDeploymentErrorStack("There are errors that are added by ProcessObserverMethod event observers for observer methods. Look at logs for further details");
    if (!webBeansUtil.isAnnotatedTypeDecoratorOrInterceptor(annotatedType)) {
        for (final ProducerMethodBean<?> producerMethod : producerMethods) {
            beanManager.addBean(producerMethod);
        }
        for (final ProducerFieldBean<?> producerField : producerFields) {
            beanManager.addBean(producerField);
        }
    }
    beanManager.addBean(bean);
    return bean;
}
Also used : AnnotatedMethod(javax.enterprise.inject.spi.AnnotatedMethod) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) WeakHashMap(java.util.WeakHashMap) ArrayList(java.util.ArrayList) ProducerFieldBean(org.apache.webbeans.component.ProducerFieldBean) GProcessSessionBean(org.apache.webbeans.portable.events.generics.GProcessSessionBean) ProducerMethodBean(org.apache.webbeans.component.ProducerMethodBean) Bean(javax.enterprise.inject.spi.Bean) OwbBean(org.apache.webbeans.component.OwbBean) AbstractOwbBean(org.apache.webbeans.component.AbstractOwbBean) ProducerFieldBeansBuilder(org.apache.webbeans.component.creation.ProducerFieldBeansBuilder) GProcessSessionBean(org.apache.webbeans.portable.events.generics.GProcessSessionBean) ObserverMethodsBuilder(org.apache.webbeans.component.creation.ObserverMethodsBuilder) ObserverMethodImpl(org.apache.webbeans.event.ObserverMethodImpl) DefinitionException(javax.enterprise.inject.spi.DefinitionException) ProducerMethodBean(org.apache.webbeans.component.ProducerMethodBean) ObserverMethod(javax.enterprise.inject.spi.ObserverMethod) ProducerFieldBean(org.apache.webbeans.component.ProducerFieldBean) BeanContext(org.apache.openejb.BeanContext) WebBeansUtil(org.apache.webbeans.util.WebBeansUtil) ProducerMethodBeansBuilder(org.apache.webbeans.component.creation.ProducerMethodBeansBuilder) BeanManagerImpl(org.apache.webbeans.container.BeanManagerImpl) AnnotatedField(javax.enterprise.inject.spi.AnnotatedField)

Aggregations

ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 WeakHashMap (java.util.WeakHashMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 AnnotatedField (javax.enterprise.inject.spi.AnnotatedField)1 AnnotatedMethod (javax.enterprise.inject.spi.AnnotatedMethod)1 Bean (javax.enterprise.inject.spi.Bean)1 DefinitionException (javax.enterprise.inject.spi.DefinitionException)1 ObserverMethod (javax.enterprise.inject.spi.ObserverMethod)1 BeanContext (org.apache.openejb.BeanContext)1 AbstractOwbBean (org.apache.webbeans.component.AbstractOwbBean)1 OwbBean (org.apache.webbeans.component.OwbBean)1 ProducerFieldBean (org.apache.webbeans.component.ProducerFieldBean)1 ProducerMethodBean (org.apache.webbeans.component.ProducerMethodBean)1 ObserverMethodsBuilder (org.apache.webbeans.component.creation.ObserverMethodsBuilder)1 ProducerFieldBeansBuilder (org.apache.webbeans.component.creation.ProducerFieldBeansBuilder)1 ProducerMethodBeansBuilder (org.apache.webbeans.component.creation.ProducerMethodBeansBuilder)1 BeanManagerImpl (org.apache.webbeans.container.BeanManagerImpl)1 ObserverMethodImpl (org.apache.webbeans.event.ObserverMethodImpl)1 GProcessSessionBean (org.apache.webbeans.portable.events.generics.GProcessSessionBean)1