From 80e5531e1b123694ac38fc081ee371b4b66d9c89 Mon Sep 17 00:00:00 2001 From: Jozef Hartinger Date: Wed, 19 Dec 2012 13:03:28 +0100 Subject: [PATCH] Allow BeanManager.validate() to be called during AfterDeploymentValidation - CDI-274 --- .../java/org/jboss/weld/ContainerState.java | 6 ++- .../weld/bean/builtin/BeanManagerProxy.java | 40 ++++++++++++------- .../jboss/weld/bootstrap/WeldBootstrap.java | 5 ++- .../producer/InjectionTargetService.java | 2 +- .../unavailable/methods/WrongExtension.java | 16 +++++++- .../unit/bootstrap/ContainerStatusTest.java | 9 +++-- 6 files changed, 55 insertions(+), 23 deletions(-) diff --git a/impl/src/main/java/org/jboss/weld/ContainerState.java b/impl/src/main/java/org/jboss/weld/ContainerState.java index 802fe089007..e6e3edf5552 100644 --- a/impl/src/main/java/org/jboss/weld/ContainerState.java +++ b/impl/src/main/java/org/jboss/weld/ContainerState.java @@ -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 */ diff --git a/impl/src/main/java/org/jboss/weld/bean/builtin/BeanManagerProxy.java b/impl/src/main/java/org/jboss/weld/bean/builtin/BeanManagerProxy.java index 2c342d8adcc..15db1a07a2a 100644 --- a/impl/src/main/java/org/jboss/weld/bean/builtin/BeanManagerProxy.java +++ b/impl/src/main/java/org/jboss/weld/bean/builtin/BeanManagerProxy.java @@ -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> getBeans(Type beanType, Annotation... qualifiers) { - checkApplicationInitializationFinished("getBeans()"); + checkContainerInitialized("getBeans()"); return super.getBeans(beanType, qualifiers); } @Override public Set> 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 Bean resolve(Set> 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 Set> resolveObserverMethods(T event, Annotation... qualifiers) { - checkApplicationInitializationFinished("resolveObserverMethods()"); + checkContainerInitialized("resolveObserverMethods()"); return super.resolveObserverMethods(event, qualifiers); } @Override public List> resolveDecorators(Set types, Annotation... qualifiers) { - checkApplicationInitializationFinished("resolveDecorators()"); + checkContainerInitialized("resolveDecorators()"); return super.resolveDecorators(types, qualifiers); } @Override public List> resolveInterceptors(InterceptionType type, Annotation... interceptorBindings) { - checkApplicationInitializationFinished("resolveInterceptors()"); + checkContainerInitialized("resolveInterceptors()"); return super.resolveInterceptors(type, interceptorBindings); } @@ -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.cast(manager).delegate(); + manager = Reflections. cast(manager).delegate(); } if (manager instanceof BeanManagerImpl) { return (BeanManagerImpl) manager; diff --git a/impl/src/main/java/org/jboss/weld/bootstrap/WeldBootstrap.java b/impl/src/main/java/org/jboss/weld/bootstrap/WeldBootstrap.java index 15c23ed13ad..3d09a595ab6 100644 --- a/impl/src/main/java/org/jboss/weld/bootstrap/WeldBootstrap.java +++ b/impl/src/main/java/org/jboss/weld/bootstrap/WeldBootstrap.java @@ -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; } @@ -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; @@ -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(); @@ -519,6 +519,7 @@ public Bootstrap endInitialization() { deployment.getBeanManager().getServices().get(EnumService.class).inject(); deployment.getBeanDeployer().cleanup(); } + Container.instance().setState(ContainerState.INITIALIZED); return this; } } diff --git a/impl/src/main/java/org/jboss/weld/injection/producer/InjectionTargetService.java b/impl/src/main/java/org/jboss/weld/injection/producer/InjectionTargetService.java index b82b3a6a840..fb5f391581d 100644 --- a/impl/src/main/java/org/jboss/weld/injection/producer/InjectionTargetService.java +++ b/impl/src/main/java/org/jboss/weld/injection/producer/InjectionTargetService.java @@ -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 { diff --git a/tests-arquillian/src/test/java/org/jboss/weld/tests/beanManager/bootstrap/unavailable/methods/WrongExtension.java b/tests-arquillian/src/test/java/org/jboss/weld/tests/beanManager/bootstrap/unavailable/methods/WrongExtension.java index 7c2be7e00ec..d7ffcf9c951 100644 --- a/tests-arquillian/src/test/java/org/jboss/weld/tests/beanManager/bootstrap/unavailable/methods/WrongExtension.java +++ b/tests-arquillian/src/test/java/org/jboss/weld/tests/beanManager/bootstrap/unavailable/methods/WrongExtension.java @@ -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; @@ -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 fooBean; + private InjectionPoint injectionPoint; public void observeBeforeBeanDiscovery(@Observes BeforeBeanDiscovery event, BeanManager beanManager) { testUnavailableMethods(beanManager); @@ -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) { @@ -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 + } + } diff --git a/tests/src/test/java/org/jboss/weld/tests/unit/bootstrap/ContainerStatusTest.java b/tests/src/test/java/org/jboss/weld/tests/unit/bootstrap/ContainerStatusTest.java index 547a2e54ca4..9cc98aa949f 100644 --- a/tests/src/test/java/org/jboss/weld/tests/unit/bootstrap/ContainerStatusTest.java +++ b/tests/src/test/java/org/jboss/weld/tests/unit/bootstrap/ContainerStatusTest.java @@ -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()); }