Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Allow BeanManager.validate() to be called during AfterDeploymentValid…
…ation - CDI-274
  • Loading branch information
jharting committed Dec 19, 2012
1 parent 7390a8e commit 80e5531
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 23 deletions.
6 changes: 5 additions & 1 deletion impl/src/main/java/org/jboss/weld/ContainerState.java
Expand Up @@ -33,11 +33,15 @@ public enum ContainerState {
/**
* The container has started and beans have been deployed
*/
INITIALIZED(true),
DEPLOYED(true),
/**
* The deployment has been validated
*/
VALIDATED(true),
/**
* The container finished initialization and is serving requests
*/
INITIALIZED(true),
/**
* The container has been shutdown
*/
Expand Down
Expand Up @@ -63,61 +63,61 @@ public BeanManagerImpl delegate() {

@Override
public Object getReference(Bean<?> bean, Type beanType, CreationalContext<?> ctx) {
checkApplicationInitializationFinished("getReference()");
checkContainerInitialized("getReference()");
return super.getReference(bean, beanType, ctx);
}

@Override
public Object getInjectableReference(InjectionPoint ij, CreationalContext<?> ctx) {
checkApplicationInitializationFinished("getInjectableReference()");
checkContainerInitialized("getInjectableReference()");
return super.getInjectableReference(ij, ctx);
}

@Override
public Set<Bean<?>> getBeans(Type beanType, Annotation... qualifiers) {
checkApplicationInitializationFinished("getBeans()");
checkContainerInitialized("getBeans()");
return super.getBeans(beanType, qualifiers);
}

@Override
public Set<Bean<?>> getBeans(String name) {
checkApplicationInitializationFinished("getBeans()");
checkContainerInitialized("getBeans()");
return super.getBeans(name);
}

@Override
public Bean<?> getPassivationCapableBean(String id) {
checkApplicationInitializationFinished("getPassivationCapableBean()");
checkContainerInitialized("getPassivationCapableBean()");
return super.getPassivationCapableBean(id);
}

@Override
public <X> Bean<? extends X> resolve(Set<Bean<? extends X>> beans) {
checkApplicationInitializationFinished("resolve()");
checkContainerInitialized("resolve()");
return super.resolve(beans);
}

@Override
public void validate(InjectionPoint injectionPoint) {
checkApplicationInitializationFinished("validate()");
checkContainerInitialized("validate()", ContainerState.VALIDATED, ContainerState.INITIALIZED);
super.validate(injectionPoint);
}

@Override
public <T> Set<ObserverMethod<? super T>> resolveObserverMethods(T event, Annotation... qualifiers) {
checkApplicationInitializationFinished("resolveObserverMethods()");
checkContainerInitialized("resolveObserverMethods()");
return super.resolveObserverMethods(event, qualifiers);
}

@Override
public List<Decorator<?>> resolveDecorators(Set<Type> types, Annotation... qualifiers) {
checkApplicationInitializationFinished("resolveDecorators()");
checkContainerInitialized("resolveDecorators()");
return super.resolveDecorators(types, qualifiers);
}

@Override
public List<Interceptor<?>> resolveInterceptors(InterceptionType type, Annotation... interceptorBindings) {
checkApplicationInitializationFinished("resolveInterceptors()");
checkContainerInitialized("resolveInterceptors()");
return super.resolveInterceptors(type, interceptorBindings);
}

Expand All @@ -126,21 +126,31 @@ protected Object readResolve() {
}

/**
* Verifies that the container has been initialized. If no {@link ContainerState} arguments are provided this method
* verifies that the container is in the INITIALIZED state. If the arguments are provided, this method verifies that the
* container is in one of the states. Otherwise, {@link IllegalStateException} is thrown.
*
* @param methodName
* @throws IllegalStateException If the application initialization is not finished yet
*/
private void checkApplicationInitializationFinished(String methodName) {

if (ContainerState.VALIDATED.equals(container.getState())) {
return;
private void checkContainerInitialized(String methodName, ContainerState... allowedStates) {
if (allowedStates == null || allowedStates.length == 0) {
if (ContainerState.INITIALIZED.equals(container.getState())) {
return;
}
} else {
for (ContainerState state : allowedStates) {
if (container.getState().equals(state)) {
return;
}
}
}
throw new IllegalStateException(METHOD_NOT_AVAILABLE_DURING_INITIALIZATION, methodName);
}

public static BeanManagerImpl unwrap(BeanManager manager) {
if (manager instanceof ForwardingBeanManager) {
manager = Reflections.<ForwardingBeanManager>cast(manager).delegate();
manager = Reflections.<ForwardingBeanManager> cast(manager).delegate();
}
if (manager instanceof BeanManagerImpl) {
return (BeanManagerImpl) manager;
Expand Down
Expand Up @@ -455,7 +455,7 @@ public Bootstrap deployBeans() {
entry.getValue().afterBeanDiscovery(environment);
}
Container.instance().putBeanDeployments(beanDeployments);
Container.instance().setState(ContainerState.INITIALIZED);
Container.instance().setState(ContainerState.DEPLOYED);
}
return this;
}
Expand All @@ -469,6 +469,7 @@ public Bootstrap validateBeans() {
deployment.getServices().get(Validator.class).validateDeployment(beanManager, entry.getValue().getBeanDeployer().getEnvironment());
beanManager.getServices().get(InjectionTargetService.class).validate();
}
Container.instance().setState(ContainerState.VALIDATED);
AfterDeploymentValidationImpl.fire(deploymentManager);
}
return this;
Expand All @@ -478,7 +479,6 @@ public Bootstrap endInitialization() {
// TODO rebuild the manager accessibility graph if the bdas have changed
synchronized (this) {
// Register the managers so external requests can handle them
Container.instance().setState(ContainerState.VALIDATED);
// clear the TypeSafeResolvers, so data that is only used at startup
// is not kept around using up memory
deploymentManager.getBeanResolver().clear();
Expand Down Expand Up @@ -519,6 +519,7 @@ public Bootstrap endInitialization() {
deployment.getBeanManager().getServices().get(EnumService.class).inject();
deployment.getBeanDeployer().cleanup();
}
Container.instance().setState(ContainerState.INITIALIZED);
return this;
}
}
Expand Down
Expand Up @@ -30,7 +30,7 @@ public InjectionTargetService(BeanManagerImpl beanManager) {
}

public void validateProducer(Producer<?> producer) {
if (container.getState().equals(ContainerState.VALIDATED)) {
if (container.getState().equals(ContainerState.VALIDATED) || container.getState().equals(ContainerState.INITIALIZED)) {
// We are past the bootstrap and therefore we can validate the producer immediatelly
validator.validateProducer(producer, beanManager);
} else {
Expand Down
Expand Up @@ -27,6 +27,7 @@

import javax.enterprise.event.Observes;
import javax.enterprise.inject.spi.AfterBeanDiscovery;
import javax.enterprise.inject.spi.AfterDeploymentValidation;
import javax.enterprise.inject.spi.Annotated;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanManager;
Expand All @@ -35,11 +36,13 @@
import javax.enterprise.inject.spi.InjectionPoint;
import javax.enterprise.inject.spi.InterceptionType;
import javax.enterprise.inject.spi.ProcessBean;
import javax.enterprise.inject.spi.ProcessInjectionPoint;
import javax.enterprise.util.AnnotationLiteral;

public class WrongExtension implements Extension {

private Bean<Foo> fooBean;
private InjectionPoint injectionPoint;

public void observeBeforeBeanDiscovery(@Observes BeforeBeanDiscovery event, BeanManager beanManager) {
testUnavailableMethods(beanManager);
Expand All @@ -54,7 +57,7 @@ public void observeAfterBeanDiscovery(@Observes AfterBeanDiscovery event, BeanMa
testUnavailableMethods(beanManager);
}

@SuppressWarnings({ "serial", "unchecked" })
@SuppressWarnings({ "serial" })
private void testUnavailableMethods(BeanManager beanManager) {

if (fooBean != null) {
Expand Down Expand Up @@ -170,4 +173,15 @@ public Annotated getAnnotated() {

}

void observeInjectionPoint(@Observes ProcessInjectionPoint<?, ?> event) {
if (injectionPoint == null) {
// simply store some IP which we'll try to validate later
injectionPoint = event.getInjectionPoint();
}
}

void validate(@Observes AfterDeploymentValidation event, BeanManager manager) {
manager.validate(injectionPoint); // should pass
}

}
Expand Up @@ -37,10 +37,13 @@ public void testStatus() {
Assert.assertEquals(ContainerState.STARTING, Container.instance().getState());
container.getBootstrap().deployBeans();
Assert.assertTrue(Container.available());
Assert.assertEquals(ContainerState.INITIALIZED, Container.instance().getState());
container.getBootstrap().validateBeans().endInitialization();
Assert.assertTrue(Container.available());
Assert.assertEquals(ContainerState.DEPLOYED, Container.instance().getState());
container.getBootstrap().validateBeans();
Assert.assertEquals(ContainerState.VALIDATED, Container.instance().getState());
Assert.assertTrue(Container.available());
container.getBootstrap().endInitialization();
Assert.assertTrue(Container.available());
Assert.assertEquals(ContainerState.INITIALIZED, Container.instance().getState());
container.stopContainer();
Assert.assertFalse(Container.available());
}
Expand Down

0 comments on commit 80e5531

Please sign in to comment.