diff --git a/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/ShutdownBootstrap.java b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/ShutdownBootstrap.java index 4713d92..34b08d4 100644 --- a/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/ShutdownBootstrap.java +++ b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/ShutdownBootstrap.java @@ -39,6 +39,7 @@ package br.gov.frameworkdemoiselle.internal.bootstrap; import java.lang.annotation.Annotation; import java.util.ArrayList; import java.util.Collections; +import java.util.Iterator; import java.util.List; import javax.enterprise.context.ConversationScoped; @@ -51,6 +52,7 @@ import javax.enterprise.inject.spi.AnnotatedType; import javax.enterprise.inject.spi.BeanManager; import javax.enterprise.inject.spi.ProcessAnnotatedType; +import br.gov.frameworkdemoiselle.DemoiselleException; import br.gov.frameworkdemoiselle.annotation.Shutdown; import br.gov.frameworkdemoiselle.annotation.ViewScoped; import br.gov.frameworkdemoiselle.internal.context.CustomContext; @@ -66,11 +68,8 @@ public class ShutdownBootstrap extends AbstractBootstrap { private static final List tempContexts = new ArrayList(); - @SuppressWarnings("rawtypes") - private static final List processors = Collections - .synchronizedList(new ArrayList()); - - private static AfterBeanDiscovery event; + private static final List> processors = Collections + .synchronizedList(new ArrayList>()); /** * Observes all methods annotated with @Shutdown and create an instance of ShutdownProcessor for them @@ -90,10 +89,6 @@ public class ShutdownBootstrap extends AbstractBootstrap { } } - public void saveEvent(@Observes final AfterBeanDiscovery event) { - ShutdownBootstrap.event = event; - } - public static void loadTempContexts(final AfterBeanDiscovery event) { // Não registrar o contexto de aplicação pq ele já é registrado pela // implementação do CDI @@ -109,24 +104,31 @@ public class ShutdownBootstrap extends AbstractBootstrap { /** * Before Shutdown it execute the methods annotateds with @Shutdown considering the priority order; - * - * @param event - * @throws Exception */ - @SuppressWarnings("unchecked") - public static void shutdown() throws Throwable { - loadTempContexts(ShutdownBootstrap.event); - + public synchronized static void shutdown() { getLogger().debug( getBundle("demoiselle-core-bundle").getString("executing-all", annotationClass.getSimpleName())); + Collections.sort(processors); + Throwable failure = null; + + for (Iterator> iter = processors.iterator(); iter.hasNext();) { + ShutdownProcessor processor = iter.next(); + + try { + processor.process(); + processors.remove(processor); - for (ShutdownProcessor processor : processors) { - processor.process(); + } catch (Throwable cause) { + failure = cause; + } } - processors.clear(); unloadTempContexts(); + + if (failure != null) { + throw new DemoiselleException(failure); + } } private static void unloadTempContexts() { diff --git a/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/StartupBootstrap.java b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/StartupBootstrap.java index b319408..1e34b0d 100644 --- a/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/StartupBootstrap.java +++ b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/StartupBootstrap.java @@ -39,6 +39,7 @@ package br.gov.frameworkdemoiselle.internal.bootstrap; import java.lang.annotation.Annotation; import java.util.ArrayList; import java.util.Collections; +import java.util.Iterator; import java.util.List; import javax.enterprise.context.ConversationScoped; @@ -51,6 +52,7 @@ import javax.enterprise.inject.spi.AnnotatedType; import javax.enterprise.inject.spi.BeanManager; import javax.enterprise.inject.spi.ProcessAnnotatedType; +import br.gov.frameworkdemoiselle.DemoiselleException; import br.gov.frameworkdemoiselle.annotation.Startup; import br.gov.frameworkdemoiselle.annotation.ViewScoped; import br.gov.frameworkdemoiselle.internal.context.ThreadLocalContext; @@ -65,9 +67,8 @@ public class StartupBootstrap extends AbstractBootstrap { private static final List tempContexts = new ArrayList(); - @SuppressWarnings("rawtypes") - private static final List processors = Collections - .synchronizedList(new ArrayList()); + private static final List> processors = Collections + .synchronizedList(new ArrayList>()); /** * Observes all methods annotated with @Startup and create an instance of StartupAction for them @@ -101,23 +102,31 @@ public class StartupBootstrap extends AbstractBootstrap { /** * After the deployment validation it execute the methods annotateds with @Startup considering the priority order; - * - * @param event - * @throws Exception - * @throws StartupException */ - @SuppressWarnings("unchecked") - public static void startup() throws Throwable { + public synchronized static void startup() { getLogger().debug( getBundle("demoiselle-core-bundle").getString("executing-all", annotationClass.getSimpleName())); + Collections.sort(processors); + Throwable failure = null; + + for (Iterator> iter = processors.iterator(); iter.hasNext();) { + StartupProcessor processor = iter.next(); - for (StartupProcessor action : processors) { - action.process(); + try { + processor.process(); + processors.remove(processor); + + } catch (Throwable cause) { + failure = cause; + } } - processors.clear(); unloadTempContexts(); + + if (failure != null) { + throw new DemoiselleException(failure); + } } private static void unloadTempContexts() { diff --git a/impl/core/src/test/java/br/gov/frameworkdemoiselle/internal/bootstrap/ShutdownBootstrapTest.java b/impl/core/src/test/java/br/gov/frameworkdemoiselle/internal/bootstrap/ShutdownBootstrapTest.java index b3cd74f..dfd362f 100644 --- a/impl/core/src/test/java/br/gov/frameworkdemoiselle/internal/bootstrap/ShutdownBootstrapTest.java +++ b/impl/core/src/test/java/br/gov/frameworkdemoiselle/internal/bootstrap/ShutdownBootstrapTest.java @@ -177,31 +177,11 @@ public class ShutdownBootstrapTest { EasyMock.expectLastCall().anyTimes(); PowerMock.replayAll(); - ShutdownBootstrap.shuttingDown(null); + ShutdownBootstrap.shutdown(); assertTrue(list.isEmpty()); PowerMock.verifyAll(); } - - @Test - public void testSaveEvent() throws Throwable { - - ShutdownBootstrap bootstrap = new ShutdownBootstrap(); - - AfterBeanDiscovery event = Whitebox.getInternalState(ShutdownBootstrap.class, AfterBeanDiscovery.class); - - assertNull(event); - - AfterBeanDiscovery newEvent = EasyMock.createMock(AfterBeanDiscovery.class); - - EasyMock.replay(newEvent); - - bootstrap.saveEvent(newEvent); - - event = Whitebox.getInternalState(ShutdownBootstrap.class, AfterBeanDiscovery.class); - - assertNotNull(event); - } } @SuppressWarnings("rawtypes") diff --git a/impl/core/src/test/java/br/gov/frameworkdemoiselle/internal/bootstrap/StartupBootstrapTest.java b/impl/core/src/test/java/br/gov/frameworkdemoiselle/internal/bootstrap/StartupBootstrapTest.java index 9b459fa..6db3445 100644 --- a/impl/core/src/test/java/br/gov/frameworkdemoiselle/internal/bootstrap/StartupBootstrapTest.java +++ b/impl/core/src/test/java/br/gov/frameworkdemoiselle/internal/bootstrap/StartupBootstrapTest.java @@ -219,7 +219,7 @@ public class StartupBootstrapTest { expect(processor.process()).andReturn(true).times(1); PowerMock.replayAll(); - bootstrap.startup(null); + bootstrap.startup(); assertTrue(list.isEmpty()); PowerMock.verifyAll(); @@ -234,7 +234,7 @@ public class StartupBootstrapTest { Assert.assertFalse(Contexts.getActiveContexts().isEmpty()); try { - bootstrap.startup(null); + bootstrap.startup(); Assert.assertTrue(Contexts.getActiveContexts().isEmpty()); } catch (Throwable e) { fail(); -- libgit2 0.21.2