All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jboss.weld.bootstrap.ExtensionBeanDeployer Maven / Gradle / Ivy

There is a newer version: 3.0.0.Alpha1
Show newest version
/*
 * 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 static org.jboss.weld.util.reflection.Reflections.cast;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import javax.enterprise.context.spi.Context;
import javax.enterprise.inject.spi.Extension;

import org.jboss.weld.annotated.enhanced.EnhancedAnnotatedMethod;
import org.jboss.weld.annotated.enhanced.EnhancedAnnotatedType;
import org.jboss.weld.bean.RIBean;
import org.jboss.weld.bean.builtin.ExtensionBean;
import org.jboss.weld.bootstrap.events.ContainerLifecycleEvents;
import org.jboss.weld.bootstrap.spi.Deployment;
import org.jboss.weld.bootstrap.spi.Metadata;
import org.jboss.weld.event.ObserverFactory;
import org.jboss.weld.event.ObserverMethodImpl;
import org.jboss.weld.logging.BootstrapLogger;
import org.jboss.weld.manager.BeanManagerImpl;
import org.jboss.weld.resources.ClassTransformer;
import org.jboss.weld.resources.spi.ResourceLoadingException;
import org.jboss.weld.util.BeanMethods;
import org.jboss.weld.util.DeploymentStructures;
import org.jboss.weld.util.reflection.Formats;
import org.jboss.weld.logging.EventLogger;
import org.jboss.weld.util.Observers;

/**
 * @author pmuir
 */
public class ExtensionBeanDeployer {

    private final BeanManagerImpl beanManager;
    private final Set> extensions;
    private final Deployment deployment;
    private final BeanDeploymentArchiveMapping bdaMapping;
    private final Collection> contexts;
    private final ContainerLifecycleEvents containerLifecycleEventObservers;
    private final MissingDependenciesRegistry missingDependenciesRegistry;

    public ExtensionBeanDeployer(BeanManagerImpl manager, Deployment deployment, BeanDeploymentArchiveMapping bdaMapping,
            Collection> contexts) {
        this.beanManager = manager;
        this.extensions = new HashSet>();
        this.deployment = deployment;
        this.bdaMapping = bdaMapping;
        this.contexts = contexts;
        this.containerLifecycleEventObservers = beanManager.getServices().get(ContainerLifecycleEvents.class);
        this.missingDependenciesRegistry = beanManager.getServices().get(MissingDependenciesRegistry.class);
    }

    public ExtensionBeanDeployer deployBeans() {
        final ClassTransformer classTransformer = beanManager.getServices().get(ClassTransformer.class);
        for (Metadata extension : extensions) {
            deployBean(extension, classTransformer);
        }
        return this;
    }

    private  void deployBean(Metadata extension, ClassTransformer classTransformer) {
     // Locate the BeanDeployment for this extension
        BeanDeployment beanDeployment = DeploymentStructures.getOrCreateBeanDeployment(deployment, beanManager, bdaMapping, contexts, extension.getValue()
                .getClass());

        EnhancedAnnotatedType enchancedAnnotatedType = getEnhancedAnnotatedType(classTransformer, extension, beanDeployment);

        if (enchancedAnnotatedType != null) {
            ExtensionBean bean = new ExtensionBean(beanDeployment.getBeanManager(), enchancedAnnotatedType, extension);
            Set> observerMethodInitializers = new HashSet>();
            createObserverMethods(bean, beanDeployment.getBeanManager(), enchancedAnnotatedType, observerMethodInitializers);
            beanDeployment.getBeanManager().addBean(bean);
            beanDeployment.getBeanDeployer().addExtension(bean);
            for (ObserverInitializationContext observerMethodInitializer : observerMethodInitializers) {
                observerMethodInitializer.initialize();
                beanDeployment.getBeanManager().addObserver(observerMethodInitializer.getObserver());
                containerLifecycleEventObservers.processObserverMethod(observerMethodInitializer.getObserver());
            }
            BootstrapLogger.LOG.extensionBeanDeployed(bean);
        }
    }

    private  EnhancedAnnotatedType getEnhancedAnnotatedType(ClassTransformer classTransformer, Metadata extension,
            BeanDeployment beanDeployment) {
        Class clazz = extension.getValue().getClass();
        try {
            return cast(classTransformer.getEnhancedAnnotatedType(clazz, beanDeployment.getBeanDeploymentArchive().getId()));
        } catch (ResourceLoadingException e) {
            String missingDependency = Formats.getNameOfMissingClassLoaderDependency(e);
            BootstrapLogger.LOG.ignoringExtensionClassDueToLoadingError(clazz.getName(), missingDependency);
            BootstrapLogger.LOG.catchingDebug(e);
            missingDependenciesRegistry.registerClassWithMissingDependency(clazz.getName(), missingDependency);
            return null;
        }
    }

    public void addExtensions(Iterable> extensions) {
        for (Metadata extension : extensions) {
            addExtension(extension);
        }
    }

    public void addExtension(Metadata extension) {
        this.extensions.add(extension);
    }

    protected  void createObserverMethods(RIBean declaringBean, BeanManagerImpl beanManager, EnhancedAnnotatedType annotatedClass,
            Set> observerMethodInitializers) {
        for (EnhancedAnnotatedMethod method : BeanMethods.getObserverMethods(annotatedClass)) {
            createObserverMethod(declaringBean, beanManager, method, observerMethodInitializers);
        }
    }

    protected  void createObserverMethod(RIBean declaringBean, BeanManagerImpl beanManager, EnhancedAnnotatedMethod method,
            Set> observerMethodInitializers) {
        ObserverMethodImpl observer = ObserverFactory.create(method, declaringBean, beanManager);
        ObserverInitializationContext observerMethodInitializer = ObserverInitializationContext.of(observer, method);
        if (Observers.isContainerLifecycleObserverMethod(observer) && method.isStatic()) {
            throw EventLogger.LOG.staticContainerLifecycleEventObserver(observer, Formats.formatAsStackTraceElement(method.getJavaMember()));
        }
        observerMethodInitializers.add(observerMethodInitializer);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy