Skip to content

Commit

Permalink
WELD-1515 Extension field missing dependency does not result in
Browse files Browse the repository at this point in the history
deployment problem anymore
  • Loading branch information
mkouba authored and jharting committed Sep 26, 2013
1 parent 034a786 commit cbc8783
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,13 @@
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;

/**
* @author pmuir
Expand All @@ -50,38 +53,57 @@ public class ExtensionBeanDeployer {
private final BeanDeploymentArchiveMapping bdaMapping;
private final Collection<ContextHolder<? extends Context>> contexts;
private final ContainerLifecycleEvents containerLifecycleEventObservers;
private final MissingDependenciesRegistry missingDependenciesRegistry;

public ExtensionBeanDeployer(BeanManagerImpl manager, Deployment deployment, BeanDeploymentArchiveMapping bdaMapping, Collection<ContextHolder<? extends Context>> contexts) {
public ExtensionBeanDeployer(BeanManagerImpl manager, Deployment deployment, BeanDeploymentArchiveMapping bdaMapping,
Collection<ContextHolder<? extends Context>> contexts) {
this.beanManager = manager;
this.extensions = new HashSet<Metadata<Extension>>();
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() {
ClassTransformer classTransformer = beanManager.getServices().get(ClassTransformer.class);
for (Metadata<Extension> extension : extensions) {
// Locate the BeanDeployment for this extension
BeanDeployment beanDeployment = DeploymentStructures.getOrCreateBeanDeployment(deployment, beanManager, bdaMapping, contexts, extension.getValue().getClass());

EnhancedAnnotatedType<Extension> clazz = cast(classTransformer.getEnhancedAnnotatedType(extension.getValue().getClass(), beanDeployment.getBeanDeploymentArchive().getId()));

ExtensionBean bean = new ExtensionBean(beanDeployment.getBeanManager(), clazz, extension);
Set<ObserverInitializationContext<?, ?>> observerMethodInitializers = new HashSet<ObserverInitializationContext<?, ?>>();
createObserverMethods(bean, beanDeployment.getBeanManager(), clazz, observerMethodInitializers);
beanDeployment.getBeanManager().addBean(bean);
beanDeployment.getBeanDeployer().addExtension(bean);
for (ObserverInitializationContext<?, ?> observerMethodInitializer : observerMethodInitializers) {
observerMethodInitializer.initialize();
beanDeployment.getBeanManager().addObserver(observerMethodInitializer.getObserver());
containerLifecycleEventObservers.processObserverMethod(observerMethodInitializer.getObserver());
BeanDeployment beanDeployment = DeploymentStructures.getOrCreateBeanDeployment(deployment, beanManager, bdaMapping, contexts, extension.getValue()
.getClass());

EnhancedAnnotatedType<Extension> enchancedAnnotatedType = getEnhancedAnnotatedType(classTransformer, extension, beanDeployment);

if (enchancedAnnotatedType != null) {
ExtensionBean bean = new ExtensionBean(beanDeployment.getBeanManager(), enchancedAnnotatedType, extension);
Set<ObserverInitializationContext<?, ?>> observerMethodInitializers = new HashSet<ObserverInitializationContext<?, ?>>();
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());
}
}
}
return this;
}

private EnhancedAnnotatedType<Extension> getEnhancedAnnotatedType(ClassTransformer classTransformer, Metadata<Extension> extension,
BeanDeployment beanDeployment) {
Class<? extends Extension> 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<Metadata<Extension>> extensions) {
for (Metadata<Extension> extension : extensions) {
Expand All @@ -93,13 +115,15 @@ public void addExtension(Metadata<Extension> extension) {
this.extensions.add(extension);
}

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

protected <T, X> void createObserverMethod(RIBean<X> declaringBean, BeanManagerImpl beanManager, EnhancedAnnotatedMethod<T, ? super X> method, Set<ObserverInitializationContext<?, ?>> observerMethodInitializers) {
protected <T, X> void createObserverMethod(RIBean<X> declaringBean, BeanManagerImpl beanManager, EnhancedAnnotatedMethod<T, ? super X> method,
Set<ObserverInitializationContext<?, ?>> observerMethodInitializers) {
ObserverMethodImpl<T, X> observer = ObserverFactory.create(method, declaringBean, beanManager);
ObserverInitializationContext<T, X> observerMethodInitializer = ObserverInitializationContext.of(observer, method);
observerMethodInitializers.add(observerMethodInitializer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ public WeldRuntime startContainer(String contextId, Environment environment, Dep
deploymentServices.add(ReflectionCache.class, registry.get(ReflectionCache.class));
deploymentServices.add(GlobalEnablementBuilder.class, registry.get(GlobalEnablementBuilder.class));
deploymentServices.add(HttpContextActivationFilter.class, registry.get(HttpContextActivationFilter.class));
deploymentServices.add(MissingDependenciesRegistry.class, registry.get(MissingDependenciesRegistry.class));

this.environment = environment;
this.deploymentManager = BeanManagerImpl.newRootManager(contextId, "deployment", deploymentServices);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,4 +190,8 @@ public interface BootstrapLogger extends WeldLogger {
@Message(id = 138, value = "Error while loading class '{0}' : {1}", format = Format.MESSAGE_FORMAT)
void errorWhileLoadingClass(Object param1, Object param2);

@LogMessage(level = Logger.Level.WARN)
@Message(id = 139, value = "Ignoring portable extension class {0} because of underlying class loading error: Type {1} not found. Enable DEBUG logging level to see the full error." , format = Format.MESSAGE_FORMAT)
void ignoringExtensionClassDueToLoadingError(String className, String missingDependency);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2013, 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.tests.extensions.missing.dependencies;

import static org.junit.Assert.assertEquals;

import javax.enterprise.inject.spi.Extension;

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.jboss.weld.test.util.ActionSequence;
import org.jboss.weld.tests.category.Integration;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;

@Category(Integration.class)
@RunWith(Arquillian.class)
public class ExtensionFieldMissingDependencyTest {

@Deployment
public static WebArchive createTestArchive() {
return ShrinkWrap.create(WebArchive.class).addClasses(InvalidExtension.class, ActionSequence.class)
.addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml")
.addAsServiceProvider(Extension.class, InvalidExtension.class);
}

@Test
public void testExtensionDisregarded() {
assertEquals(0, ActionSequence.getSequenceSize());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2013, 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.tests.extensions.missing.dependencies;

public class Foo {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2013, 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.tests.extensions.missing.dependencies;

import javax.enterprise.event.Observes;
import javax.enterprise.inject.spi.AfterDeploymentValidation;
import javax.enterprise.inject.spi.Extension;

import org.jboss.weld.test.util.ActionSequence;

public class InvalidExtension implements Extension {

@SuppressWarnings("unused")
private Foo foo;

public void observeAfterDeploymentValidation(@Observes AfterDeploymentValidation event) {
ActionSequence.addAction(InvalidExtension.class.getName());
}


}

0 comments on commit cbc8783

Please sign in to comment.