Commit 22125b4a8862e37b82df4bf307a2ff9dbcb958ac

Authored by Cleverson Sacramento
1 parent ed9b77f1
Exists in master

Ajustes no Startup e Shutdown

impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/ShutdownBootstrap.java
... ... @@ -39,6 +39,7 @@ package br.gov.frameworkdemoiselle.internal.bootstrap;
39 39 import java.lang.annotation.Annotation;
40 40 import java.util.ArrayList;
41 41 import java.util.Collections;
  42 +import java.util.Iterator;
42 43 import java.util.List;
43 44  
44 45 import javax.enterprise.context.ConversationScoped;
... ... @@ -51,6 +52,7 @@ import javax.enterprise.inject.spi.AnnotatedType;
51 52 import javax.enterprise.inject.spi.BeanManager;
52 53 import javax.enterprise.inject.spi.ProcessAnnotatedType;
53 54  
  55 +import br.gov.frameworkdemoiselle.DemoiselleException;
54 56 import br.gov.frameworkdemoiselle.annotation.Shutdown;
55 57 import br.gov.frameworkdemoiselle.annotation.ViewScoped;
56 58 import br.gov.frameworkdemoiselle.internal.context.CustomContext;
... ... @@ -66,11 +68,8 @@ public class ShutdownBootstrap extends AbstractBootstrap {
66 68  
67 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 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 89 }
91 90 }
92 91  
93   - public void saveEvent(@Observes final AfterBeanDiscovery event) {
94   - ShutdownBootstrap.event = event;
95   - }
96   -
97 92 public static void loadTempContexts(final AfterBeanDiscovery event) {
98 93 // Não registrar o contexto de aplicação pq ele já é registrado pela
99 94 // implementação do CDI
... ... @@ -109,24 +104,31 @@ public class ShutdownBootstrap extends AbstractBootstrap {
109 104  
110 105 /**
111 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 109 getLogger().debug(
121 110 getBundle("demoiselle-core-bundle").getString("executing-all", annotationClass.getSimpleName()));
  111 +
122 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 127 unloadTempContexts();
  128 +
  129 + if (failure != null) {
  130 + throw new DemoiselleException(failure);
  131 + }
130 132 }
131 133  
132 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 39 import java.lang.annotation.Annotation;
40 40 import java.util.ArrayList;
41 41 import java.util.Collections;
  42 +import java.util.Iterator;
42 43 import java.util.List;
43 44  
44 45 import javax.enterprise.context.ConversationScoped;
... ... @@ -51,6 +52,7 @@ import javax.enterprise.inject.spi.AnnotatedType;
51 52 import javax.enterprise.inject.spi.BeanManager;
52 53 import javax.enterprise.inject.spi.ProcessAnnotatedType;
53 54  
  55 +import br.gov.frameworkdemoiselle.DemoiselleException;
54 56 import br.gov.frameworkdemoiselle.annotation.Startup;
55 57 import br.gov.frameworkdemoiselle.annotation.ViewScoped;
56 58 import br.gov.frameworkdemoiselle.internal.context.ThreadLocalContext;
... ... @@ -65,9 +67,8 @@ public class StartupBootstrap extends AbstractBootstrap {
65 67  
66 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 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 102  
102 103 /**
103 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 107 getLogger().debug(
112 108 getBundle("demoiselle-core-bundle").getString("executing-all", annotationClass.getSimpleName()));
  109 +
113 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 125 unloadTempContexts();
  126 +
  127 + if (failure != null) {
  128 + throw new DemoiselleException(failure);
  129 + }
121 130 }
122 131  
123 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 177 EasyMock.expectLastCall().anyTimes();
178 178  
179 179 PowerMock.replayAll();
180   - ShutdownBootstrap.shuttingDown(null);
  180 + ShutdownBootstrap.shutdown();
181 181  
182 182 assertTrue(list.isEmpty());
183 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 187 @SuppressWarnings("rawtypes")
... ...
impl/core/src/test/java/br/gov/frameworkdemoiselle/internal/bootstrap/StartupBootstrapTest.java
... ... @@ -219,7 +219,7 @@ public class StartupBootstrapTest {
219 219 expect(processor.process()).andReturn(true).times(1);
220 220  
221 221 PowerMock.replayAll();
222   - bootstrap.startup(null);
  222 + bootstrap.startup();
223 223  
224 224 assertTrue(list.isEmpty());
225 225 PowerMock.verifyAll();
... ... @@ -234,7 +234,7 @@ public class StartupBootstrapTest {
234 234 Assert.assertFalse(Contexts.getActiveContexts().isEmpty());
235 235  
236 236 try {
237   - bootstrap.startup(null);
  237 + bootstrap.startup();
238 238 Assert.assertTrue(Contexts.getActiveContexts().isEmpty());
239 239 } catch (Throwable e) {
240 240 fail();
... ...