org.jboss.weld.bootstrap.ExtensionBeanDeployer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of weld-osgi-bundle Show documentation
Show all versions of weld-osgi-bundle Show documentation
Weld runtime packaged as an OSGi bundle
/*
* 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 org.jboss.weld.Container;
import org.jboss.weld.bean.RIBean;
import org.jboss.weld.bean.builtin.ExtensionBean;
import org.jboss.weld.bootstrap.spi.BeanDeploymentArchive;
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.introspector.WeldClass;
import org.jboss.weld.introspector.WeldMethod;
import org.jboss.weld.logging.Category;
import org.jboss.weld.manager.BeanManagerImpl;
import org.jboss.weld.resources.ClassTransformer;
import org.jboss.weld.resources.spi.ResourceLoadingException;
import org.jboss.weld.util.Beans;
import org.jboss.weld.util.DeploymentStructures;
import org.slf4j.cal10n.LocLogger;
import org.slf4j.ext.XLogger;
import org.slf4j.ext.XLogger.Level;
import javax.enterprise.context.spi.Context;
import javax.enterprise.inject.spi.Extension;
import java.util.Collection;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import static org.jboss.weld.logging.LoggerFactory.loggerFactory;
import static org.jboss.weld.logging.messages.BootstrapMessage.IGNORING_EXTENSION_CLASS_DUE_TO_LOADING_ERROR;
import static org.jboss.weld.util.reflection.Reflections.cast;
/**
* @author pmuir
*/
public class ExtensionBeanDeployer {
private transient LocLogger log = loggerFactory().getLogger(Category.CLASS_LOADING);
private transient XLogger xlog = loggerFactory().getXLogger(Category.CLASS_LOADING);
private final BeanManagerImpl beanManager;
private final Set> extensions;
private final Deployment deployment;
private final Map beanDeployments;
private final Collection> contexts;
public ExtensionBeanDeployer(BeanManagerImpl manager, Deployment deployment, Map beanDeployments, Collection> contexts) {
this.beanManager = manager;
this.extensions = new HashSet>();
this.deployment = deployment;
this.beanDeployments = beanDeployments;
this.contexts = contexts;
}
public ExtensionBeanDeployer deployBeans() {
ClassTransformer classTransformer = Container.instance().services().get(ClassTransformer.class);
for (Metadata extension : extensions) {
WeldClass clazz = null;
try {
clazz = cast(classTransformer.loadClass(extension.getValue().getClass()));
} catch (ResourceLoadingException e) {
log.warn(IGNORING_EXTENSION_CLASS_DUE_TO_LOADING_ERROR, extension.getValue().getClass().getName());
xlog.catching(Level.DEBUG, e);
}
if (clazz != null) {
// Locate the BeanDeployment for this extension
BeanDeployment beanDeployment = DeploymentStructures.getOrCreateBeanDeployment(deployment, beanManager, beanDeployments, contexts,
clazz.getJavaClass());
ExtensionBean bean = new ExtensionBean(beanDeployment.getBeanManager(), clazz, extension);
Set> observerMethods = new HashSet>();
createObserverMethods(bean, beanDeployment.getBeanManager(), clazz, observerMethods);
beanDeployment.getBeanManager().addBean(bean);
beanDeployment.getBeanDeployer().addExtension(bean);
for (ObserverMethodImpl observerMethod : observerMethods) {
observerMethod.initialize();
beanDeployment.getBeanManager().addObserver(observerMethod);
}
}
}
return this;
}
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, WeldClass annotatedClass, Set> observerMethods) {
for (WeldMethod method : Beans.getObserverMethods(annotatedClass)) {
createObserverMethod(declaringBean, beanManager, method, observerMethods);
}
}
protected void createObserverMethod(RIBean declaringBean, BeanManagerImpl beanManager, WeldMethod method, Set> observerMethods) {
ObserverMethodImpl observer = ObserverFactory.create(method, declaringBean, beanManager);
observerMethods.add(observer);
}
}