Commit 22125b4a8862e37b82df4bf307a2ff9dbcb958ac
1 parent
ed9b77f1
Exists in
master
Ajustes no Startup e Shutdown
Showing
4 changed files
with
45 additions
and
54 deletions
Show diff stats
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/ShutdownBootstrap.java
| @@ -39,6 +39,7 @@ package br.gov.frameworkdemoiselle.internal.bootstrap; | @@ -39,6 +39,7 @@ package br.gov.frameworkdemoiselle.internal.bootstrap; | ||
| 39 | import java.lang.annotation.Annotation; | 39 | import java.lang.annotation.Annotation; |
| 40 | import java.util.ArrayList; | 40 | import java.util.ArrayList; |
| 41 | import java.util.Collections; | 41 | import java.util.Collections; |
| 42 | +import java.util.Iterator; | ||
| 42 | import java.util.List; | 43 | import java.util.List; |
| 43 | 44 | ||
| 44 | import javax.enterprise.context.ConversationScoped; | 45 | import javax.enterprise.context.ConversationScoped; |
| @@ -51,6 +52,7 @@ import javax.enterprise.inject.spi.AnnotatedType; | @@ -51,6 +52,7 @@ import javax.enterprise.inject.spi.AnnotatedType; | ||
| 51 | import javax.enterprise.inject.spi.BeanManager; | 52 | import javax.enterprise.inject.spi.BeanManager; |
| 52 | import javax.enterprise.inject.spi.ProcessAnnotatedType; | 53 | import javax.enterprise.inject.spi.ProcessAnnotatedType; |
| 53 | 54 | ||
| 55 | +import br.gov.frameworkdemoiselle.DemoiselleException; | ||
| 54 | import br.gov.frameworkdemoiselle.annotation.Shutdown; | 56 | import br.gov.frameworkdemoiselle.annotation.Shutdown; |
| 55 | import br.gov.frameworkdemoiselle.annotation.ViewScoped; | 57 | import br.gov.frameworkdemoiselle.annotation.ViewScoped; |
| 56 | import br.gov.frameworkdemoiselle.internal.context.CustomContext; | 58 | import br.gov.frameworkdemoiselle.internal.context.CustomContext; |
| @@ -66,11 +68,8 @@ public class ShutdownBootstrap extends AbstractBootstrap { | @@ -66,11 +68,8 @@ public class ShutdownBootstrap extends AbstractBootstrap { | ||
| 66 | 68 | ||
| 67 | private static final List<CustomContext> tempContexts = new ArrayList<CustomContext>(); | 69 | private static final List<CustomContext> tempContexts = new ArrayList<CustomContext>(); |
| 68 | 70 | ||
| 69 | - @SuppressWarnings("rawtypes") | ||
| 70 | - private static final List<ShutdownProcessor> processors = Collections | ||
| 71 | - .synchronizedList(new ArrayList<ShutdownProcessor>()); | ||
| 72 | - | ||
| 73 | - private static AfterBeanDiscovery event; | 71 | + private static final List<ShutdownProcessor<?>> processors = Collections |
| 72 | + .synchronizedList(new ArrayList<ShutdownProcessor<?>>()); | ||
| 74 | 73 | ||
| 75 | /** | 74 | /** |
| 76 | * Observes all methods annotated with @Shutdown and create an instance of ShutdownProcessor for them | 75 | * Observes all methods annotated with @Shutdown and create an instance of ShutdownProcessor for them |
| @@ -90,10 +89,6 @@ public class ShutdownBootstrap extends AbstractBootstrap { | @@ -90,10 +89,6 @@ public class ShutdownBootstrap extends AbstractBootstrap { | ||
| 90 | } | 89 | } |
| 91 | } | 90 | } |
| 92 | 91 | ||
| 93 | - public void saveEvent(@Observes final AfterBeanDiscovery event) { | ||
| 94 | - ShutdownBootstrap.event = event; | ||
| 95 | - } | ||
| 96 | - | ||
| 97 | public static void loadTempContexts(final AfterBeanDiscovery event) { | 92 | public static void loadTempContexts(final AfterBeanDiscovery event) { |
| 98 | // Não registrar o contexto de aplicação pq ele já é registrado pela | 93 | // Não registrar o contexto de aplicação pq ele já é registrado pela |
| 99 | // implementação do CDI | 94 | // implementação do CDI |
| @@ -109,24 +104,31 @@ public class ShutdownBootstrap extends AbstractBootstrap { | @@ -109,24 +104,31 @@ public class ShutdownBootstrap extends AbstractBootstrap { | ||
| 109 | 104 | ||
| 110 | /** | 105 | /** |
| 111 | * Before Shutdown it execute the methods annotateds with @Shutdown considering the priority order; | 106 | * Before Shutdown it execute the methods annotateds with @Shutdown considering the priority order; |
| 112 | - * | ||
| 113 | - * @param event | ||
| 114 | - * @throws Exception | ||
| 115 | */ | 107 | */ |
| 116 | - @SuppressWarnings("unchecked") | ||
| 117 | - public static void shutdown() throws Throwable { | ||
| 118 | - loadTempContexts(ShutdownBootstrap.event); | ||
| 119 | - | 108 | + public synchronized static void shutdown() { |
| 120 | getLogger().debug( | 109 | getLogger().debug( |
| 121 | getBundle("demoiselle-core-bundle").getString("executing-all", annotationClass.getSimpleName())); | 110 | getBundle("demoiselle-core-bundle").getString("executing-all", annotationClass.getSimpleName())); |
| 111 | + | ||
| 122 | Collections.sort(processors); | 112 | Collections.sort(processors); |
| 113 | + Throwable failure = null; | ||
| 114 | + | ||
| 115 | + for (Iterator<ShutdownProcessor<?>> iter = processors.iterator(); iter.hasNext();) { | ||
| 116 | + ShutdownProcessor<?> processor = iter.next(); | ||
| 117 | + | ||
| 118 | + try { | ||
| 119 | + processor.process(); | ||
| 120 | + processors.remove(processor); | ||
| 123 | 121 | ||
| 124 | - for (ShutdownProcessor<?> processor : processors) { | ||
| 125 | - processor.process(); | 122 | + } catch (Throwable cause) { |
| 123 | + failure = cause; | ||
| 124 | + } | ||
| 126 | } | 125 | } |
| 127 | 126 | ||
| 128 | - processors.clear(); | ||
| 129 | unloadTempContexts(); | 127 | unloadTempContexts(); |
| 128 | + | ||
| 129 | + if (failure != null) { | ||
| 130 | + throw new DemoiselleException(failure); | ||
| 131 | + } | ||
| 130 | } | 132 | } |
| 131 | 133 | ||
| 132 | private static void unloadTempContexts() { | 134 | private static void unloadTempContexts() { |
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/StartupBootstrap.java
| @@ -39,6 +39,7 @@ package br.gov.frameworkdemoiselle.internal.bootstrap; | @@ -39,6 +39,7 @@ package br.gov.frameworkdemoiselle.internal.bootstrap; | ||
| 39 | import java.lang.annotation.Annotation; | 39 | import java.lang.annotation.Annotation; |
| 40 | import java.util.ArrayList; | 40 | import java.util.ArrayList; |
| 41 | import java.util.Collections; | 41 | import java.util.Collections; |
| 42 | +import java.util.Iterator; | ||
| 42 | import java.util.List; | 43 | import java.util.List; |
| 43 | 44 | ||
| 44 | import javax.enterprise.context.ConversationScoped; | 45 | import javax.enterprise.context.ConversationScoped; |
| @@ -51,6 +52,7 @@ import javax.enterprise.inject.spi.AnnotatedType; | @@ -51,6 +52,7 @@ import javax.enterprise.inject.spi.AnnotatedType; | ||
| 51 | import javax.enterprise.inject.spi.BeanManager; | 52 | import javax.enterprise.inject.spi.BeanManager; |
| 52 | import javax.enterprise.inject.spi.ProcessAnnotatedType; | 53 | import javax.enterprise.inject.spi.ProcessAnnotatedType; |
| 53 | 54 | ||
| 55 | +import br.gov.frameworkdemoiselle.DemoiselleException; | ||
| 54 | import br.gov.frameworkdemoiselle.annotation.Startup; | 56 | import br.gov.frameworkdemoiselle.annotation.Startup; |
| 55 | import br.gov.frameworkdemoiselle.annotation.ViewScoped; | 57 | import br.gov.frameworkdemoiselle.annotation.ViewScoped; |
| 56 | import br.gov.frameworkdemoiselle.internal.context.ThreadLocalContext; | 58 | import br.gov.frameworkdemoiselle.internal.context.ThreadLocalContext; |
| @@ -65,9 +67,8 @@ public class StartupBootstrap extends AbstractBootstrap { | @@ -65,9 +67,8 @@ public class StartupBootstrap extends AbstractBootstrap { | ||
| 65 | 67 | ||
| 66 | private static final List<ThreadLocalContext> tempContexts = new ArrayList<ThreadLocalContext>(); | 68 | private static final List<ThreadLocalContext> tempContexts = new ArrayList<ThreadLocalContext>(); |
| 67 | 69 | ||
| 68 | - @SuppressWarnings("rawtypes") | ||
| 69 | - private static final List<StartupProcessor> processors = Collections | ||
| 70 | - .synchronizedList(new ArrayList<StartupProcessor>()); | 70 | + private static final List<StartupProcessor<?>> processors = Collections |
| 71 | + .synchronizedList(new ArrayList<StartupProcessor<?>>()); | ||
| 71 | 72 | ||
| 72 | /** | 73 | /** |
| 73 | * Observes all methods annotated with @Startup and create an instance of StartupAction for them | 74 | * Observes all methods annotated with @Startup and create an instance of StartupAction for them |
| @@ -101,23 +102,31 @@ public class StartupBootstrap extends AbstractBootstrap { | @@ -101,23 +102,31 @@ public class StartupBootstrap extends AbstractBootstrap { | ||
| 101 | 102 | ||
| 102 | /** | 103 | /** |
| 103 | * After the deployment validation it execute the methods annotateds with @Startup considering the priority order; | 104 | * After the deployment validation it execute the methods annotateds with @Startup considering the priority order; |
| 104 | - * | ||
| 105 | - * @param event | ||
| 106 | - * @throws Exception | ||
| 107 | - * @throws StartupException | ||
| 108 | */ | 105 | */ |
| 109 | - @SuppressWarnings("unchecked") | ||
| 110 | - public static void startup() throws Throwable { | 106 | + public synchronized static void startup() { |
| 111 | getLogger().debug( | 107 | getLogger().debug( |
| 112 | getBundle("demoiselle-core-bundle").getString("executing-all", annotationClass.getSimpleName())); | 108 | getBundle("demoiselle-core-bundle").getString("executing-all", annotationClass.getSimpleName())); |
| 109 | + | ||
| 113 | Collections.sort(processors); | 110 | Collections.sort(processors); |
| 111 | + Throwable failure = null; | ||
| 112 | + | ||
| 113 | + for (Iterator<StartupProcessor<?>> iter = processors.iterator(); iter.hasNext();) { | ||
| 114 | + StartupProcessor<?> processor = iter.next(); | ||
| 114 | 115 | ||
| 115 | - for (StartupProcessor<?> action : processors) { | ||
| 116 | - action.process(); | 116 | + try { |
| 117 | + processor.process(); | ||
| 118 | + processors.remove(processor); | ||
| 119 | + | ||
| 120 | + } catch (Throwable cause) { | ||
| 121 | + failure = cause; | ||
| 122 | + } | ||
| 117 | } | 123 | } |
| 118 | 124 | ||
| 119 | - processors.clear(); | ||
| 120 | unloadTempContexts(); | 125 | unloadTempContexts(); |
| 126 | + | ||
| 127 | + if (failure != null) { | ||
| 128 | + throw new DemoiselleException(failure); | ||
| 129 | + } | ||
| 121 | } | 130 | } |
| 122 | 131 | ||
| 123 | private static void unloadTempContexts() { | 132 | private static void unloadTempContexts() { |
impl/core/src/test/java/br/gov/frameworkdemoiselle/internal/bootstrap/ShutdownBootstrapTest.java
| @@ -177,31 +177,11 @@ public class ShutdownBootstrapTest { | @@ -177,31 +177,11 @@ public class ShutdownBootstrapTest { | ||
| 177 | EasyMock.expectLastCall().anyTimes(); | 177 | EasyMock.expectLastCall().anyTimes(); |
| 178 | 178 | ||
| 179 | PowerMock.replayAll(); | 179 | PowerMock.replayAll(); |
| 180 | - ShutdownBootstrap.shuttingDown(null); | 180 | + ShutdownBootstrap.shutdown(); |
| 181 | 181 | ||
| 182 | assertTrue(list.isEmpty()); | 182 | assertTrue(list.isEmpty()); |
| 183 | PowerMock.verifyAll(); | 183 | PowerMock.verifyAll(); |
| 184 | } | 184 | } |
| 185 | - | ||
| 186 | - @Test | ||
| 187 | - public void testSaveEvent() throws Throwable { | ||
| 188 | - | ||
| 189 | - ShutdownBootstrap bootstrap = new ShutdownBootstrap(); | ||
| 190 | - | ||
| 191 | - AfterBeanDiscovery event = Whitebox.getInternalState(ShutdownBootstrap.class, AfterBeanDiscovery.class); | ||
| 192 | - | ||
| 193 | - assertNull(event); | ||
| 194 | - | ||
| 195 | - AfterBeanDiscovery newEvent = EasyMock.createMock(AfterBeanDiscovery.class); | ||
| 196 | - | ||
| 197 | - EasyMock.replay(newEvent); | ||
| 198 | - | ||
| 199 | - bootstrap.saveEvent(newEvent); | ||
| 200 | - | ||
| 201 | - event = Whitebox.getInternalState(ShutdownBootstrap.class, AfterBeanDiscovery.class); | ||
| 202 | - | ||
| 203 | - assertNotNull(event); | ||
| 204 | - } | ||
| 205 | } | 185 | } |
| 206 | 186 | ||
| 207 | @SuppressWarnings("rawtypes") | 187 | @SuppressWarnings("rawtypes") |
impl/core/src/test/java/br/gov/frameworkdemoiselle/internal/bootstrap/StartupBootstrapTest.java
| @@ -219,7 +219,7 @@ public class StartupBootstrapTest { | @@ -219,7 +219,7 @@ public class StartupBootstrapTest { | ||
| 219 | expect(processor.process()).andReturn(true).times(1); | 219 | expect(processor.process()).andReturn(true).times(1); |
| 220 | 220 | ||
| 221 | PowerMock.replayAll(); | 221 | PowerMock.replayAll(); |
| 222 | - bootstrap.startup(null); | 222 | + bootstrap.startup(); |
| 223 | 223 | ||
| 224 | assertTrue(list.isEmpty()); | 224 | assertTrue(list.isEmpty()); |
| 225 | PowerMock.verifyAll(); | 225 | PowerMock.verifyAll(); |
| @@ -234,7 +234,7 @@ public class StartupBootstrapTest { | @@ -234,7 +234,7 @@ public class StartupBootstrapTest { | ||
| 234 | Assert.assertFalse(Contexts.getActiveContexts().isEmpty()); | 234 | Assert.assertFalse(Contexts.getActiveContexts().isEmpty()); |
| 235 | 235 | ||
| 236 | try { | 236 | try { |
| 237 | - bootstrap.startup(null); | 237 | + bootstrap.startup(); |
| 238 | Assert.assertTrue(Contexts.getActiveContexts().isEmpty()); | 238 | Assert.assertTrue(Contexts.getActiveContexts().isEmpty()); |
| 239 | } catch (Throwable e) { | 239 | } catch (Throwable e) { |
| 240 | fail(); | 240 | fail(); |