Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source
   * Copyright 2008, Red Hat, Inc., and individual contributors
   * by the @authors tag. See the copyright.txt in the distribution for a
   * full listing of individual contributors.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
  * http://www.apache.org/licenses/LICENSE-2.0
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package org.jboss.weld.bootstrap;
 
 import java.util.Set;
 
 

Author(s):
Pete Muir
Ales Justin
Jozef Hartinger
 
 public class AbstractBeanDeployer<E extends BeanDeployerEnvironment> {
 
     private final BeanManagerImpl manager;
     private final ServiceRegistry services;
     private final E environment;
     protected final ClassTransformer classTransformer;
     protected final SlimAnnotatedTypeStore slimAnnotatedTypeStore;
 
     public AbstractBeanDeployer(BeanManagerImpl managerServiceRegistry services, E environment) {
         this. = manager;
         this. = services;
         this. = environment;
         this. = manager.getServices().get(ContainerLifecycleEvents.class);
         this. = services.get(ClassTransformer.class);
         this. = services.get(SlimAnnotatedTypeStore.class);
     }
 
     protected BeanManagerImpl getManager() {
         return ;
     }
 
    // interceptors, decorators and observers go first
        // ensure that all decorators are initialized before initializing
        // the rest of the beans
        for (DecoratorImpl<?> bean : getEnvironment().getDecorators()) {
            bean.initialize(getEnvironment());
            .fireProcessBean(getManager(), bean);
            .addDecorator(bean);
            ..foundDecorator(bean);
        }
        for (InterceptorImpl<?> bean : getEnvironment().getInterceptors()) {
            bean.initialize(getEnvironment());
            .fireProcessBean(getManager(), bean);
            .addInterceptor(bean);
            ..foundInterceptor(bean);
        }
        return this;
    }
    public AbstractBeanDeployer<E> initializeBeans() {
        for (RIBean<?> bean : getEnvironment().getBeans()) {
            bean.initialize(getEnvironment());
        }
        return this;
    }
    public AbstractBeanDeployer<E> fireBeanEvents() {
        for (RIBean<?> bean : getEnvironment().getBeans()) {
            fireBeanEvents(bean);
        }
        return this;
    }
    public void fireBeanEvents(RIBean<?> bean) {
        if (!(bean instanceof NewBean)) {
            if (bean instanceof AbstractProducerBean<?, ?, ?>) {
                .fireProcessProducer(, Reflections.<AbstractProducerBean<?, ?, Member>>cast(bean));
            } else if (bean instanceof AbstractClassBean<?>) {
            }
            .fireProcessBean(getManager(), bean);
        }
    }
    public AbstractBeanDeployer<E> deployBeans() {
        return this;
    }
        for (ObserverInitializationContext<?, ?> observerInitializer : getEnvironment().getObservers()) {
            if (Observers.isObserverMethodEnabled(observerInitializer.getObserver(), )) {
                observerInitializer.initialize();
            }
        }
        return this;
    }
        // TODO -- why do observers have to be the last?
        for (ObserverInitializationContext<?, ?> observerInitializer : getEnvironment().getObservers()) {
            if (Observers.isObserverMethodEnabled(observerInitializer.getObserver(), )) {
                ..foundObserverMethod(observerInitializer.getObserver());
                ProcessObserverMethodImpl.fire(observerInitializer.getObserver());
                .addObserver(observerInitializer.getObserver());
            }
        }
        return this;
    }

    
Creates the sub bean for an class (simple or enterprise) bean

Parameters:
bean The class bean
    protected <T> void createObserversProducersDisposers(AbstractClassBean<T> bean) {
        if (bean instanceof ManagedBean<?> || bean instanceof SessionBean<?>) {
            // disposal methods have to go first as we want them to be ready for resolution when initializing producer method/fields
            createDisposalMethods(beanbean.getEnhancedAnnotated());
            createProducerMethods(beanbean.getEnhancedAnnotated());
            createProducerFields(beanbean.getEnhancedAnnotated());
            if (.isBeanEnabled(bean)) {
                createObserverMethods(beanbean.getEnhancedAnnotated());
            }
        }
    }
    protected <X> DisposalMethod<X, ?> resolveDisposalMethod(BeanAttributes<?> attributesAbstractClassBean<X> declaringBean) {
        Set<DisposalMethod<X, ?>> disposalBeans = .<X>resolveDisposalBeans(attributes.getTypes(), attributes.getQualifiers(), declaringBean);
        if (disposalBeans.size() == 1) {
            return disposalBeans.iterator().next();
        } else if (disposalBeans.size() > 1) {
            throw ..multipleDisposalMethods(this, WeldCollections.toMultiRowString(disposalBeans));
        }
        return null;
    }
    protected <X> void createProducerMethods(AbstractClassBean<X> declaringBeanEnhancedAnnotatedType<X> type) {
        for (EnhancedAnnotatedMethod<?, ? super X> method : BeanMethods.filterOutBridgeMethods(type.getDeclaredEnhancedMethods(Produces.class))) {
            // create method for now
            // specialization and PBA processing is handled later
            createProducerMethod(declaringBeanmethod);
        }
    }
    protected <X> void createDisposalMethods(AbstractClassBean<X> declaringBeanEnhancedAnnotatedType<X> annotatedClass) {
        for (EnhancedAnnotatedMethod<?, ? super X> method : BeanMethods.filterOutBridgeMethods(annotatedClass
                .getDeclaredEnhancedMethodsWithAnnotatedParameters(Disposes.class))) {
            DisposalMethod<? super X, ?> disposalBean = DisposalMethod.of(methoddeclaringBean);
            getEnvironment().addDisposesMethod(disposalBean);
        }
    }
    protected <X, T> void createProducerMethod(AbstractClassBean<X> declaringBeanEnhancedAnnotatedMethod<T, ? super X> annotatedMethod) {
        BeanAttributes<T> attributes = BeanAttributesFactory.forBean(annotatedMethodgetManager());
        DisposalMethod<X, ?> disposalMethod = resolveDisposalMethod(attributesdeclaringBean);
        ProducerMethod<? super X, T> bean = ProducerMethod.of(attributesannotatedMethoddeclaringBeandisposalMethod);
        .preloadProcessProducer(bean.getBeanClass(), annotatedMethod.getBaseType());
        getEnvironment().addProducerMethod(bean);
    }
    protected <X, T> void createProducerField(AbstractClassBean<X> declaringBeanEnhancedAnnotatedField<T, ? super X> field) {
        BeanAttributes<T> attributes = BeanAttributesFactory.forBean(fieldgetManager());
        DisposalMethod<X, ?> disposalMethod = resolveDisposalMethod(attributesdeclaringBean);
        ProducerField<X, T> bean;
        if (isEEResourceProducerField(field)) {
            if (field.isStatic()) {
                bean = StaticEEResourceProducerField.of(attributesfielddeclaringBeandisposalMethod);
            } else {
                bean = EEResourceProducerField.of(attributesfielddeclaringBeandisposalMethod);
            }
        } else {
            bean = ProducerField.of(attributesfielddeclaringBeandisposalMethod);
        }
        getEnvironment().addProducerField(bean);
    }
    protected <X> void createProducerFields(AbstractClassBean<X> declaringBeanEnhancedAnnotatedType<X> annotatedClass) {
        for (EnhancedAnnotatedField<?, ? super X> field : annotatedClass.getDeclaredEnhancedFields(Produces.class)) {
            createProducerField(declaringBeanfield);
        }
    }
    protected <X> void createObserverMethods(AbstractClassBean<X> declaringBeanEnhancedAnnotatedType<? super X> annotatedClass) {
        for (EnhancedAnnotatedMethod<?, ? super X> method : BeanMethods.getObserverMethods(annotatedClass)) {
            createObserverMethod(declaringBeanmethod);
        }
    }
    protected <T, X> void createObserverMethod(AbstractClassBean<X> declaringBeanEnhancedAnnotatedMethod<T, ? super X> method) {
        ObserverMethodImpl<T, X> observer = ObserverFactory.create(methoddeclaringBean);
        ObserverInitializationContext<T, ? super X> observerInitializer = ObserverInitializationContext.of(observermethod);
        getEnvironment().addObserverMethod(observerInitializer);
    }
    protected <T> ManagedBean<T> createManagedBean(EnhancedAnnotatedType<T> weldClass) {
        BeanAttributes<T> attributes = BeanAttributesFactory.forBean(weldClassgetManager());
        ManagedBean<T> bean = ManagedBean.of(attributesweldClass);
        getEnvironment().addManagedBean(bean);
        return bean;
    }
    protected <T> void createNewManagedBean(EnhancedAnnotatedType<T> annotatedClass) {
        // TODO resolve existing beans first
        .put(annotatedClass.slim());
        getEnvironment().addManagedBean(NewManagedBean.of(BeanAttributesFactory.forNewManagedBean(annotatedClass), annotatedClass));
    }
    protected <T> void createDecorator(EnhancedAnnotatedType<T> weldClass) {
        BeanAttributes<T> attributes = BeanAttributesFactory.forBean(weldClassgetManager());
        DecoratorImpl<T> bean = DecoratorImpl.of(attributesweldClass);
        getEnvironment().addDecorator(bean);
    }
    protected <T> void createInterceptor(EnhancedAnnotatedType<T> weldClass) {
        BeanAttributes<T> attributes = BeanAttributesFactory.forBean(weldClassgetManager());
        InterceptorImpl<T> bean = InterceptorImpl.of(attributesweldClass);
        getEnvironment().addInterceptor(bean);
    }
    protected <T> SessionBean<T> createSessionBean(InternalEjbDescriptor<T> descriptor) {
        .put(type.slim());
        return createSessionBean(descriptortype);
    }
    protected <T> SessionBean<T> createSessionBean(InternalEjbDescriptor<T> descriptorEnhancedAnnotatedType<T> weldClass) {
        // TODO Don't create enterprise bean if it has no local interfaces!
        BeanAttributes<T> attributes = BeanAttributesFactory.forSessionBean(weldClassdescriptorgetManager());
        SessionBean<T> bean = SessionBean.of(attributesdescriptorweldClass);
        getEnvironment().addSessionBean(bean);
        return bean;
    }
    protected <T> void createNewSessionBean(InternalEjbDescriptor<T> ejbDescriptorBeanAttributes<?> originalAttributesEnhancedAnnotatedType<?> type) {
        .put(type.slim());
        BeanAttributes<T> attributes = Reflections.cast(BeanAttributesFactory.forNewSessionBean(originalAttributestype.getJavaClass()));
        getEnvironment().addSessionBean(NewSessionBean.of(attributesejbDescriptor));
    }
    protected boolean isEEResourceProducerField(EnhancedAnnotatedField<?, ?> field) {
        EJBApiAbstraction ejbApiAbstraction = .getServices().get(EJBApiAbstraction.class);
        PersistenceApiAbstraction persistenceApiAbstraction = .getServices().get(PersistenceApiAbstraction.class);
        WSApiAbstraction wsApiAbstraction = .getServices().get(WSApiAbstraction.class);
        return field.isAnnotationPresent(ejbApiAbstraction.EJB_ANNOTATION_CLASS) || field.isAnnotationPresent(ejbApiAbstraction.RESOURCE_ANNOTATION_CLASS) || field.isAnnotationPresent(persistenceApiAbstraction.PERSISTENCE_UNIT_ANNOTATION_CLASS) || field.isAnnotationPresent(persistenceApiAbstraction.PERSISTENCE_CONTEXT_ANNOTATION_CLASS) || field.isAnnotationPresent(wsApiAbstraction.WEB_SERVICE_REF_ANNOTATION_CLASS);
    }
    public E getEnvironment() {
        return ;
    }
    public void addBuiltInBean(AbstractBuiltInBean<?> bean) {
        getEnvironment().addBuiltInBean(bean);
    }
    public void addExtension(ExtensionBean bean) {
        getEnvironment().addExtension(bean);
    }
    protected <T, S> boolean fireProcessBeanAttributes(AbstractBean<T, S> bean) {
            return false;
        }
        if (event == null) {
            return false;
        }
        if (event.isVeto()) {
            return true;
        }
        if (event.isDirty()) {
            bean.setAttributes(ExternalBeanAttributesFactory.<T>of(event.getBeanAttributesInternal(), ));
            bean.checkSpecialization();
        }
        return false;
    }
New to GrepCode? Check out our FAQ X