Commit 5808540381a02319a260d80e1eff2dc0a684fe5a

Authored by Cleverson Sacramento
1 parent d9a64def
Exists in master

Melhoria na implementação do StartupBootstrap e do ShutdownBootstrap

Showing 17 changed files with 806 additions and 759 deletions   Show diff stats
impl/core/src/main/java/br/gov/frameworkdemoiselle/annotation/Priority.java 0 → 100644
@@ -0,0 +1,66 @@ @@ -0,0 +1,66 @@
  1 +/*
  2 + * Demoiselle Framework
  3 + * Copyright (C) 2010 SERPRO
  4 + * ----------------------------------------------------------------------------
  5 + * This file is part of Demoiselle Framework.
  6 + *
  7 + * Demoiselle Framework is free software; you can redistribute it and/or
  8 + * modify it under the terms of the GNU Lesser General Public License version 3
  9 + * as published by the Free Software Foundation.
  10 + *
  11 + * This program is distributed in the hope that it will be useful,
  12 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14 + * GNU General Public License for more details.
  15 + *
  16 + * You should have received a copy of the GNU Lesser General Public License version 3
  17 + * along with this program; if not, see <http://www.gnu.org/licenses/>
  18 + * or write to the Free Software Foundation, Inc., 51 Franklin Street,
  19 + * Fifth Floor, Boston, MA 02110-1301, USA.
  20 + * ----------------------------------------------------------------------------
  21 + * Este arquivo é parte do Framework Demoiselle.
  22 + *
  23 + * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou
  24 + * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação
  25 + * do Software Livre (FSF).
  26 + *
  27 + * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA
  28 + * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou
  29 + * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português
  30 + * para maiores detalhes.
  31 + *
  32 + * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título
  33 + * "LICENCA.txt", junto com esse programa. Se não, acesse <http://www.gnu.org/licenses/>
  34 + * ou escreva para a Fundação do Software Livre (FSF) Inc.,
  35 + * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA.
  36 + */
  37 +package br.gov.frameworkdemoiselle.annotation;
  38 +
  39 +import static java.lang.annotation.ElementType.METHOD;
  40 +import static java.lang.annotation.RetentionPolicy.RUNTIME;
  41 +
  42 +import java.lang.annotation.Retention;
  43 +import java.lang.annotation.Target;
  44 +
  45 +/**
  46 + * @author SERPRO
  47 + */
  48 +@Target(METHOD)
  49 +@Retention(RUNTIME)
  50 +public @interface Priority {
  51 +
  52 + /**
  53 + * Most important priority value.
  54 + */
  55 + public static int MAX_PRIORITY = Integer.MIN_VALUE;
  56 +
  57 + /**
  58 + * Less important priority value.
  59 + */
  60 + public static int MIN_PRIORITY = Integer.MAX_VALUE;
  61 +
  62 + /**
  63 + * An integer value defines the priority order.
  64 + */
  65 + int value();
  66 +}
impl/core/src/main/java/br/gov/frameworkdemoiselle/annotation/Shutdown.java
@@ -52,7 +52,8 @@ import java.lang.annotation.Target; @@ -52,7 +52,8 @@ import java.lang.annotation.Target;
52 * <pre> 52 * <pre>
53 * public class Finalizer { 53 * public class Finalizer {
54 * 54 *
55 - * &#064;Shutdown(priority = 5) 55 + * &#064;Shutdown
  56 + * &#064;Priority(5)
56 * public void finalize() { 57 * public void finalize() {
57 * ... 58 * ...
58 * } 59 * }
@@ -60,6 +61,7 @@ import java.lang.annotation.Target; @@ -60,6 +61,7 @@ import java.lang.annotation.Target;
60 * 61 *
61 * 62 *
62 * 63 *
  64 + *
63 * </pre> 65 * </pre>
64 * 66 *
65 * </blockquote> 67 * </blockquote>
@@ -75,17 +77,29 @@ public @interface Shutdown { @@ -75,17 +77,29 @@ public @interface Shutdown {
75 77
76 /** 78 /**
77 * Most important priority value. 79 * Most important priority value.
  80 + *
  81 + * @deprecated
  82 + * @see Priority
78 */ 83 */
  84 + @Deprecated
79 public static int MAX_PRIORITY = Integer.MIN_VALUE; 85 public static int MAX_PRIORITY = Integer.MIN_VALUE;
80 86
81 /** 87 /**
82 * Less important priority value. 88 * Less important priority value.
  89 + *
  90 + * @deprecated
  91 + * @see Priority
83 */ 92 */
  93 + @Deprecated
84 public static int MIN_PRIORITY = Integer.MAX_VALUE; 94 public static int MIN_PRIORITY = Integer.MAX_VALUE;
85 95
86 /** 96 /**
87 * An integer value defines method execution order (i.e., priority). 97 * An integer value defines method execution order (i.e., priority).
  98 + *
  99 + * @deprecated
  100 + * @see Priority
88 */ 101 */
  102 + @Deprecated
89 int priority() default MIN_PRIORITY; 103 int priority() default MIN_PRIORITY;
90 104
91 } 105 }
impl/core/src/main/java/br/gov/frameworkdemoiselle/annotation/Startup.java
@@ -52,13 +52,16 @@ import java.lang.annotation.Target; @@ -52,13 +52,16 @@ import java.lang.annotation.Target;
52 * <pre> 52 * <pre>
53 * public class Initializer { 53 * public class Initializer {
54 * 54 *
55 - * &#064;Startup(priority = 1) 55 + * &#064;Startup
  56 + * &#064;Priority(1)
56 * public void initialize() { 57 * public void initialize() {
57 * ... 58 * ...
58 * } 59 * }
59 * } 60 * }
60 * 61 *
61 * 62 *
  63 + *
  64 + *
62 * </pre> 65 * </pre>
63 * 66 *
64 * </blockquote> 67 * </blockquote>
@@ -74,17 +77,29 @@ public @interface Startup { @@ -74,17 +77,29 @@ public @interface Startup {
74 77
75 /** 78 /**
76 * Most important priority value. 79 * Most important priority value.
  80 + *
  81 + * @deprecated
  82 + * @see Priority
77 */ 83 */
  84 + @Deprecated
78 public static int MAX_PRIORITY = Integer.MIN_VALUE; 85 public static int MAX_PRIORITY = Integer.MIN_VALUE;
79 86
80 /** 87 /**
81 * Less important priority value. 88 * Less important priority value.
  89 + *
  90 + * @deprecated
  91 + * @see Priority
82 */ 92 */
  93 + @Deprecated
83 public static int MIN_PRIORITY = Integer.MAX_VALUE; 94 public static int MIN_PRIORITY = Integer.MAX_VALUE;
84 95
85 /** 96 /**
86 * An integer value defines method execution order (i.e., priority). 97 * An integer value defines method execution order (i.e., priority).
  98 + *
  99 + * @deprecated
  100 + * @see Priority
87 */ 101 */
  102 + @Deprecated
88 int priority() default MIN_PRIORITY; 103 int priority() default MIN_PRIORITY;
89 104
90 } 105 }
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/ApplicationLifecycleEvent.java 0 → 100644
@@ -0,0 +1,6 @@ @@ -0,0 +1,6 @@
  1 +package br.gov.frameworkdemoiselle.internal.bootstrap;
  2 +
  3 +public interface ApplicationLifecycleEvent {
  4 +
  5 + boolean removeProcessors();
  6 +}
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/AsbratctLifecycleBootstrap.java 0 → 100644
@@ -0,0 +1,158 @@ @@ -0,0 +1,158 @@
  1 +/*
  2 + * Demoiselle Framework
  3 + * Copyright (C) 2010 SERPRO
  4 + * ----------------------------------------------------------------------------
  5 + * This file is part of Demoiselle Framework.
  6 + *
  7 + * Demoiselle Framework is free software; you can redistribute it and/or
  8 + * modify it under the terms of the GNU Lesser General Public License version 3
  9 + * as published by the Free Software Foundation.
  10 + *
  11 + * This program is distributed in the hope that it will be useful,
  12 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14 + * GNU General Public License for more details.
  15 + *
  16 + * You should have received a copy of the GNU Lesser General Public License version 3
  17 + * along with this program; if not, see <http://www.gnu.org/licenses/>
  18 + * or write to the Free Software Foundation, Inc., 51 Franklin Street,
  19 + * Fifth Floor, Boston, MA 02110-1301, USA.
  20 + * ----------------------------------------------------------------------------
  21 + * Este arquivo é parte do Framework Demoiselle.
  22 + *
  23 + * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou
  24 + * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação
  25 + * do Software Livre (FSF).
  26 + *
  27 + * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA
  28 + * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou
  29 + * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português
  30 + * para maiores detalhes.
  31 + *
  32 + * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título
  33 + * "LICENCA.txt", junto com esse programa. Se não, acesse <http://www.gnu.org/licenses/>
  34 + * ou escreva para a Fundação do Software Livre (FSF) Inc.,
  35 + * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA.
  36 + */
  37 +package br.gov.frameworkdemoiselle.internal.bootstrap;
  38 +
  39 +import java.lang.annotation.Annotation;
  40 +import java.util.ArrayList;
  41 +import java.util.Collections;
  42 +import java.util.Iterator;
  43 +import java.util.List;
  44 +
  45 +import javax.enterprise.context.ConversationScoped;
  46 +import javax.enterprise.context.RequestScoped;
  47 +import javax.enterprise.context.SessionScoped;
  48 +import javax.enterprise.event.Observes;
  49 +import javax.enterprise.inject.spi.AfterBeanDiscovery;
  50 +import javax.enterprise.inject.spi.AnnotatedMethod;
  51 +import javax.enterprise.inject.spi.AnnotatedType;
  52 +import javax.enterprise.inject.spi.BeanManager;
  53 +import javax.enterprise.inject.spi.ProcessAnnotatedType;
  54 +
  55 +import br.gov.frameworkdemoiselle.DemoiselleException;
  56 +import br.gov.frameworkdemoiselle.annotation.ViewScoped;
  57 +import br.gov.frameworkdemoiselle.internal.configuration.ConfigurationLoader;
  58 +import br.gov.frameworkdemoiselle.internal.context.CustomContext;
  59 +import br.gov.frameworkdemoiselle.internal.context.ThreadLocalContext;
  60 +import br.gov.frameworkdemoiselle.internal.processor.AnnotatedMethodProcessor;
  61 +import br.gov.frameworkdemoiselle.util.Reflections;
  62 +
  63 +public abstract class AsbratctLifecycleBootstrap<A extends Annotation> extends AbstractBootstrap {
  64 +
  65 + private Class<A> annotationClass;
  66 +
  67 + private final List<AnnotatedMethodProcessor<?>> processors = Collections
  68 + .synchronizedList(new ArrayList<AnnotatedMethodProcessor<?>>());
  69 +
  70 + private final List<CustomContext> tempContexts = new ArrayList<CustomContext>();
  71 +
  72 + private AfterBeanDiscovery afterBeanDiscoveryEvent;
  73 +
  74 + protected abstract <T> AnnotatedMethodProcessor<T> newProcessorInstance(AnnotatedMethod<T> annotatedMethod,
  75 + BeanManager beanManager);
  76 +
  77 + protected Class<A> getAnnotationClass() {
  78 + if (this.annotationClass == null) {
  79 + this.annotationClass = Reflections.getGenericTypeArgument(this.getClass(), 0);
  80 + }
  81 +
  82 + return this.annotationClass;
  83 + }
  84 +
  85 + public <T> void processAnnotatedType(@Observes final ProcessAnnotatedType<T> event, final BeanManager beanManager) {
  86 + final AnnotatedType<T> annotatedType = event.getAnnotatedType();
  87 +
  88 + for (AnnotatedMethod<?> am : annotatedType.getMethods()) {
  89 + if (am.isAnnotationPresent(getAnnotationClass())) {
  90 + @SuppressWarnings("unchecked")
  91 + AnnotatedMethod<T> annotatedMethod = (AnnotatedMethod<T>) am;
  92 + processors.add(newProcessorInstance(annotatedMethod, beanManager));
  93 + }
  94 + }
  95 + }
  96 +
  97 + public void loadTempContexts(@Observes final AfterBeanDiscovery event) {
  98 + // Não registrar o contexto de aplicação pq ele já é registrado pela implementação do CDI
  99 + tempContexts.add(new ThreadLocalContext(ViewScoped.class));
  100 + tempContexts.add(new ThreadLocalContext(SessionScoped.class));
  101 + tempContexts.add(new ThreadLocalContext(ConversationScoped.class));
  102 + tempContexts.add(new ThreadLocalContext(RequestScoped.class));
  103 +
  104 + afterBeanDiscoveryEvent = event;
  105 + }
  106 +
  107 + private static boolean x = true;
  108 +
  109 + protected synchronized void proccessEvent(final ApplicationLifecycleEvent event) {
  110 + getLogger().debug(
  111 + getBundle("demoiselle-core-bundle").getString("executing-all", annotationClass.getSimpleName()));
  112 +
  113 + Collections.sort(processors);
  114 + Throwable failure = null;
  115 +
  116 + if (x) {
  117 + for (CustomContext tempContext : tempContexts) {
  118 + addContext(tempContext, afterBeanDiscoveryEvent);
  119 + }
  120 +
  121 + x = false;
  122 + }
  123 +
  124 + for (Iterator<AnnotatedMethodProcessor<?>> iter = processors.iterator(); iter.hasNext();) {
  125 + AnnotatedMethodProcessor<?> processor = iter.next();
  126 +
  127 + try {
  128 + ClassLoader classLoader = ConfigurationLoader.getClassLoaderForClass(processor.getAnnotatedMethod()
  129 + .getDeclaringType().getJavaClass().getCanonicalName());
  130 +
  131 + if (Thread.currentThread().getContextClassLoader().equals(classLoader)) {
  132 + processor.process();
  133 +
  134 + if (event.removeProcessors()) {
  135 + iter.remove();
  136 + }
  137 + }
  138 +
  139 + } catch (Throwable cause) {
  140 + failure = cause;
  141 + }
  142 + }
  143 +
  144 + if (processors.isEmpty()) {
  145 + unloadTempContexts();
  146 + }
  147 +
  148 + if (failure != null) {
  149 + throw new DemoiselleException(failure);
  150 + }
  151 + }
  152 +
  153 + private void unloadTempContexts() {
  154 + for (CustomContext tempContext : tempContexts) {
  155 + disableContext(tempContext);
  156 + }
  157 + }
  158 +}
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/BeforeApplicationFinalization.java 0 → 100644
@@ -0,0 +1,5 @@ @@ -0,0 +1,5 @@
  1 +package br.gov.frameworkdemoiselle.internal.bootstrap;
  2 +
  3 +public interface BeforeApplicationFinalization extends ApplicationLifecycleEvent {
  4 +
  5 +}
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/BeforeApplicationInitialization.java 0 → 100644
@@ -0,0 +1,5 @@ @@ -0,0 +1,5 @@
  1 +package br.gov.frameworkdemoiselle.internal.bootstrap;
  2 +
  3 +public interface BeforeApplicationInitialization extends ApplicationLifecycleEvent {
  4 +
  5 +}
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/ShutdownBootstrap.java
@@ -36,134 +36,26 @@ @@ -36,134 +36,26 @@
36 */ 36 */
37 package br.gov.frameworkdemoiselle.internal.bootstrap; 37 package br.gov.frameworkdemoiselle.internal.bootstrap;
38 38
39 -import java.lang.annotation.Annotation;  
40 -import java.util.ArrayList;  
41 -import java.util.Collections;  
42 -import java.util.Iterator;  
43 -import java.util.List;  
44 -  
45 -import javax.enterprise.context.ConversationScoped;  
46 -import javax.enterprise.context.RequestScoped;  
47 -import javax.enterprise.context.SessionScoped;  
48 import javax.enterprise.event.Observes; 39 import javax.enterprise.event.Observes;
49 -import javax.enterprise.inject.spi.AfterBeanDiscovery;  
50 import javax.enterprise.inject.spi.AnnotatedMethod; 40 import javax.enterprise.inject.spi.AnnotatedMethod;
51 -import javax.enterprise.inject.spi.AnnotatedType;  
52 import javax.enterprise.inject.spi.BeanManager; 41 import javax.enterprise.inject.spi.BeanManager;
53 -import javax.enterprise.inject.spi.ProcessAnnotatedType;  
54 42
55 -import br.gov.frameworkdemoiselle.DemoiselleException;  
56 import br.gov.frameworkdemoiselle.annotation.Shutdown; 43 import br.gov.frameworkdemoiselle.annotation.Shutdown;
57 -import br.gov.frameworkdemoiselle.annotation.ViewScoped;  
58 -import br.gov.frameworkdemoiselle.internal.configuration.ConfigurationLoader;  
59 -import br.gov.frameworkdemoiselle.internal.context.CustomContext;  
60 -import br.gov.frameworkdemoiselle.internal.context.ThreadLocalContext; 44 +import br.gov.frameworkdemoiselle.internal.processor.AnnotatedMethodProcessor;
61 import br.gov.frameworkdemoiselle.internal.processor.ShutdownProcessor; 45 import br.gov.frameworkdemoiselle.internal.processor.ShutdownProcessor;
62 46
63 /** 47 /**
64 * This class run at application shutdown 48 * This class run at application shutdown
65 */ 49 */
66 -public class ShutdownBootstrap extends AbstractBootstrap {  
67 -  
68 - private static final Class<? extends Annotation> annotationClass = Shutdown.class;  
69 -  
70 - private static final List<CustomContext> tempContexts = new ArrayList<CustomContext>();  
71 -  
72 - private static AfterBeanDiscovery abdEvent;  
73 -  
74 - @SuppressWarnings("rawtypes")  
75 - private static final List<ShutdownProcessor> processors = Collections  
76 - .synchronizedList(new ArrayList<ShutdownProcessor>());  
77 -  
78 - /**  
79 - * Observes all methods annotated with @Shutdown and create an instance of ShutdownProcessor for them  
80 - *  
81 - * @param <T>  
82 - * @param event  
83 - * @param beanManager  
84 - */  
85 - public <T> void processAnnotatedType(@Observes final ProcessAnnotatedType<T> event, final BeanManager beanManager) {  
86 - final AnnotatedType<T> annotatedType = event.getAnnotatedType();  
87 - for (AnnotatedMethod<?> am : annotatedType.getMethods()) {  
88 - if (am.isAnnotationPresent(annotationClass)) {  
89 - @SuppressWarnings("unchecked")  
90 - AnnotatedMethod<T> annotatedMethod = (AnnotatedMethod<T>) am;  
91 - processors.add(new ShutdownProcessor<T>(annotatedMethod, beanManager));  
92 - }  
93 - }  
94 - }  
95 -  
96 - public static void loadTempContexts(@Observes final AfterBeanDiscovery event) {  
97 - // Não registrar o contexto de aplicação pq ele já é registrado pela  
98 - // implementação do CDI  
99 - tempContexts.add(new ThreadLocalContext(ViewScoped.class));  
100 - tempContexts.add(new ThreadLocalContext(SessionScoped.class, false));  
101 - tempContexts.add(new ThreadLocalContext(ConversationScoped.class));  
102 - tempContexts.add(new ThreadLocalContext(RequestScoped.class));  
103 - abdEvent = event;  
104 - }  
105 -  
106 - /**  
107 - * Before Shutdown it execute the methods annotateds with @Shutdown considering the priority order;  
108 - */  
109 - public synchronized static void shutdown() {  
110 - shutdown(true);  
111 - }  
112 -  
113 - private static boolean x = true;  
114 -  
115 - /**  
116 - * Before Shutdown it execute the methods annotateds with @Shutdown considering the priority order;  
117 - */  
118 - @SuppressWarnings({ "unchecked", "rawtypes" })  
119 - public synchronized static void shutdown(boolean remove) {  
120 - getLogger().debug(  
121 - getBundle("demoiselle-core-bundle").getString("executing-all", annotationClass.getSimpleName()));  
122 -  
123 - Collections.sort(processors);  
124 - Throwable failure = null;  
125 -  
126 - if (x) {  
127 - for (CustomContext tempContext : tempContexts) {  
128 - addContext(tempContext, abdEvent);  
129 - }  
130 -  
131 - x = false;  
132 - }  
133 -  
134 - for (Iterator<ShutdownProcessor> iter = processors.iterator(); iter.hasNext();) {  
135 - ShutdownProcessor processor = iter.next();  
136 -  
137 - try {  
138 - ClassLoader classLoader = ConfigurationLoader.getClassLoaderForClass(processor.getAnnotatedMethod()  
139 - .getDeclaringType().getJavaClass().getCanonicalName());  
140 -  
141 - if (Thread.currentThread().getContextClassLoader().equals(classLoader)) {  
142 -  
143 - processor.process();  
144 -  
145 - if (remove) {  
146 - iter.remove();  
147 - }  
148 - }  
149 -  
150 - } catch (Throwable cause) {  
151 - failure = cause;  
152 - }  
153 - }  
154 -  
155 - if (processors.isEmpty()) {  
156 - unloadTempContexts();  
157 - } 50 +public class ShutdownBootstrap extends AsbratctLifecycleBootstrap<Shutdown> {
158 51
159 - if (failure != null) {  
160 - throw new DemoiselleException(failure);  
161 - } 52 + @Override
  53 + protected <T> AnnotatedMethodProcessor<T> newProcessorInstance(AnnotatedMethod<T> annotatedMethod,
  54 + BeanManager beanManager) {
  55 + return new ShutdownProcessor<T>(annotatedMethod, beanManager);
162 } 56 }
163 57
164 - private static void unloadTempContexts() {  
165 - for (CustomContext tempContext : tempContexts) {  
166 - disableContext(tempContext);  
167 - } 58 + public void shutdown(@Observes BeforeApplicationFinalization event) {
  59 + proccessEvent(event);
168 } 60 }
169 } 61 }
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/StartupBootstrap.java
@@ -36,132 +36,26 @@ @@ -36,132 +36,26 @@
36 */ 36 */
37 package br.gov.frameworkdemoiselle.internal.bootstrap; 37 package br.gov.frameworkdemoiselle.internal.bootstrap;
38 38
39 -import java.lang.annotation.Annotation;  
40 -import java.util.ArrayList;  
41 -import java.util.Collections;  
42 -import java.util.Iterator;  
43 -import java.util.List;  
44 -  
45 -import javax.enterprise.context.ConversationScoped;  
46 -import javax.enterprise.context.RequestScoped;  
47 -import javax.enterprise.context.SessionScoped;  
48 import javax.enterprise.event.Observes; 39 import javax.enterprise.event.Observes;
49 -import javax.enterprise.inject.spi.AfterBeanDiscovery;  
50 import javax.enterprise.inject.spi.AnnotatedMethod; 40 import javax.enterprise.inject.spi.AnnotatedMethod;
51 -import javax.enterprise.inject.spi.AnnotatedType;  
52 import javax.enterprise.inject.spi.BeanManager; 41 import javax.enterprise.inject.spi.BeanManager;
53 -import javax.enterprise.inject.spi.ProcessAnnotatedType;  
54 42
55 -import br.gov.frameworkdemoiselle.DemoiselleException;  
56 import br.gov.frameworkdemoiselle.annotation.Startup; 43 import br.gov.frameworkdemoiselle.annotation.Startup;
57 -import br.gov.frameworkdemoiselle.annotation.ViewScoped;  
58 -import br.gov.frameworkdemoiselle.internal.configuration.ConfigurationLoader;  
59 -import br.gov.frameworkdemoiselle.internal.context.CustomContext;  
60 -import br.gov.frameworkdemoiselle.internal.context.ThreadLocalContext; 44 +import br.gov.frameworkdemoiselle.internal.processor.AnnotatedMethodProcessor;
61 import br.gov.frameworkdemoiselle.internal.processor.StartupProcessor; 45 import br.gov.frameworkdemoiselle.internal.processor.StartupProcessor;
62 46
63 /** 47 /**
64 * This class is the bootstrap to execute the processes at load time. 48 * This class is the bootstrap to execute the processes at load time.
65 */ 49 */
66 -public class StartupBootstrap extends AbstractBootstrap {  
67 -  
68 - private static final Class<? extends Annotation> annotationClass = Startup.class;  
69 -  
70 - private static final List<CustomContext> tempContexts = new ArrayList<CustomContext>();  
71 -  
72 - @SuppressWarnings("rawtypes")  
73 - private static final List<StartupProcessor> processors = Collections  
74 - .synchronizedList(new ArrayList<StartupProcessor>());  
75 -  
76 - private static AfterBeanDiscovery abdEvent;  
77 -  
78 - /**  
79 - * Observes all methods annotated with @Startup and create an instance of StartupAction for them  
80 - *  
81 - * @param <T>  
82 - * @param event  
83 - * @param beanManager  
84 - */  
85 - public <T> void processAnnotatedType(@Observes final ProcessAnnotatedType<T> event, final BeanManager beanManager) {  
86 - final AnnotatedType<T> annotatedType = event.getAnnotatedType();  
87 - for (AnnotatedMethod<?> am : annotatedType.getMethods()) {  
88 - if (am.isAnnotationPresent(annotationClass)) {  
89 - @SuppressWarnings("unchecked")  
90 - AnnotatedMethod<T> annotatedMethod = (AnnotatedMethod<T>) am;  
91 - processors.add(new StartupProcessor<T>(annotatedMethod, beanManager));  
92 - }  
93 - }  
94 - }  
95 -  
96 - public void loadTempContexts(@Observes final AfterBeanDiscovery event) {  
97 - // Não registrar o contexto de aplicação pq ele já é registrado pela implementação do CDI  
98 - tempContexts.add(new ThreadLocalContext(ViewScoped.class));  
99 - tempContexts.add(new ThreadLocalContext(SessionScoped.class));  
100 - tempContexts.add(new ThreadLocalContext(ConversationScoped.class));  
101 - tempContexts.add(new ThreadLocalContext(RequestScoped.class));  
102 - abdEvent = event;  
103 - }  
104 -  
105 - /**  
106 - * After the deployment validation it execute the methods annotateds with @Startup considering the priority order;  
107 - */  
108 - public synchronized static void startup() {  
109 - startup(true);  
110 - }  
111 -  
112 - private static boolean x = true;  
113 -  
114 - /**  
115 - * After the deployment validation it execute the methods annotateds with @Startup considering the priority order;  
116 - */  
117 - @SuppressWarnings({ "unchecked", "rawtypes" })  
118 - public synchronized static void startup(boolean remove) {  
119 - getLogger().debug(  
120 - getBundle("demoiselle-core-bundle").getString("executing-all", annotationClass.getSimpleName()));  
121 -  
122 - Collections.sort(processors);  
123 - Throwable failure = null;  
124 -  
125 - if (x) {  
126 - for (CustomContext tempContext : tempContexts) {  
127 - addContext(tempContext, abdEvent);  
128 - }  
129 -  
130 - x = false;  
131 - }  
132 -  
133 - for (Iterator<StartupProcessor> iter = processors.iterator(); iter.hasNext();) {  
134 - StartupProcessor processor = iter.next();  
135 -  
136 - try {  
137 - ClassLoader classLoader = ConfigurationLoader.getClassLoaderForClass(processor.getAnnotatedMethod()  
138 - .getDeclaringType().getJavaClass().getCanonicalName());  
139 -  
140 - if (Thread.currentThread().getContextClassLoader().equals(classLoader)) {  
141 - processor.process();  
142 -  
143 - if (remove) {  
144 - iter.remove();  
145 - }  
146 - }  
147 -  
148 - } catch (Throwable cause) {  
149 - failure = cause;  
150 - }  
151 - }  
152 -  
153 - if (processors.isEmpty()) {  
154 - unloadTempContexts();  
155 - } 50 +public class StartupBootstrap extends AsbratctLifecycleBootstrap<Startup> {
156 51
157 - if (failure != null) {  
158 - throw new DemoiselleException(failure);  
159 - } 52 + @Override
  53 + protected <T> AnnotatedMethodProcessor<T> newProcessorInstance(AnnotatedMethod<T> annotatedMethod,
  54 + BeanManager beanManager) {
  55 + return new StartupProcessor<T>(annotatedMethod, beanManager);
160 } 56 }
161 57
162 - private static void unloadTempContexts() {  
163 - for (CustomContext tempContext : tempContexts) {  
164 - disableContext(tempContext);  
165 - } 58 + public void startup(@Observes BeforeApplicationInitialization event) {
  59 + proccessEvent(event);
166 } 60 }
167 } 61 }
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/processor/AbstractProcessor.java
@@ -51,14 +51,14 @@ import br.gov.frameworkdemoiselle.util.ResourceBundle; @@ -51,14 +51,14 @@ import br.gov.frameworkdemoiselle.util.ResourceBundle;
51 /** 51 /**
52 * It abstract the integration between Processor and the context; 52 * It abstract the integration between Processor and the context;
53 * 53 *
54 - * @param <DC> 54 + * @param <T>
55 * the declaring class 55 * the declaring class
56 */ 56 */
57 -public abstract class AbstractProcessor<DC> implements Processor { 57 +public abstract class AbstractProcessor<T> implements Processor {
58 58
59 private BeanManager beanManager; 59 private BeanManager beanManager;
60 60
61 - private AnnotatedCallable<DC> annotatedCallable; 61 + private AnnotatedCallable<T> annotatedCallable;
62 62
63 private ResourceBundle bundle; 63 private ResourceBundle bundle;
64 64
@@ -68,12 +68,12 @@ public abstract class AbstractProcessor&lt;DC&gt; implements Processor { @@ -68,12 +68,12 @@ public abstract class AbstractProcessor&lt;DC&gt; implements Processor {
68 this.beanManager = beanManager; 68 this.beanManager = beanManager;
69 } 69 }
70 70
71 - public AbstractProcessor(final AnnotatedCallable<DC> annotatedCallable, final BeanManager beanManager) { 71 + public AbstractProcessor(final AnnotatedCallable<T> annotatedCallable, final BeanManager beanManager) {
72 this.annotatedCallable = annotatedCallable; 72 this.annotatedCallable = annotatedCallable;
73 this.beanManager = beanManager; 73 this.beanManager = beanManager;
74 } 74 }
75 75
76 - protected AnnotatedCallable<DC> getAnnotatedCallable() { 76 + protected AnnotatedCallable<T> getAnnotatedCallable() {
77 return this.annotatedCallable; 77 return this.annotatedCallable;
78 } 78 }
79 79
@@ -90,15 +90,15 @@ public abstract class AbstractProcessor&lt;DC&gt; implements Processor { @@ -90,15 +90,15 @@ public abstract class AbstractProcessor&lt;DC&gt; implements Processor {
90 * @return 90 * @return
91 */ 91 */
92 @SuppressWarnings("unchecked") 92 @SuppressWarnings("unchecked")
93 - protected DC getReferencedBean() {  
94 - Class<DC> classType = (Class<DC>) getAnnotatedCallable().getJavaMember().getDeclaringClass(); 93 + protected T getReferencedBean() {
  94 + Class<T> classType = (Class<T>) getAnnotatedCallable().getJavaMember().getDeclaringClass();
95 return getReferencedBean(classType); 95 return getReferencedBean(classType);
96 } 96 }
97 97
98 @SuppressWarnings("unchecked") 98 @SuppressWarnings("unchecked")
99 - protected DC getReferencedBean(final Class<DC> type) {  
100 - Bean<DC> bean = (Bean<DC>) beanManager.getBeans(type).iterator().next();  
101 - return (DC) beanManager.getReference(bean, type, beanManager.createCreationalContext(bean)); 99 + protected T getReferencedBean(final Class<T> type) {
  100 + Bean<T> bean = (Bean<T>) beanManager.getBeans(type).iterator().next();
  101 + return (T) beanManager.getReference(bean, type, beanManager.createCreationalContext(bean));
102 } 102 }
103 103
104 protected ResourceBundle getBundle() { 104 protected ResourceBundle getBundle() {
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/processor/AnnotatedMethodProcessor.java
@@ -47,17 +47,27 @@ import br.gov.frameworkdemoiselle.message.SeverityType; @@ -47,17 +47,27 @@ import br.gov.frameworkdemoiselle.message.SeverityType;
47 /** 47 /**
48 * Represents an annotated method to be processed; 48 * Represents an annotated method to be processed;
49 * 49 *
50 - * @param <DC> 50 + * @param <T>
51 * declaring class owner of the method 51 * declaring class owner of the method
52 */ 52 */
53 -public class AnnotatedMethodProcessor<DC> extends AbstractProcessor<DC> { 53 +public abstract class AnnotatedMethodProcessor<T> extends AbstractProcessor<T> implements
  54 + Comparable<AnnotatedMethodProcessor<T>> {
54 55
55 - public AnnotatedMethodProcessor(final AnnotatedMethod<DC> annotatedMethod, final BeanManager beanManager) { 56 + public AnnotatedMethodProcessor(final AnnotatedMethod<T> annotatedMethod, final BeanManager beanManager) {
56 super(annotatedMethod, beanManager); 57 super(annotatedMethod, beanManager);
57 } 58 }
58 59
59 - public AnnotatedMethod<DC> getAnnotatedMethod() {  
60 - return (AnnotatedMethod<DC>) getAnnotatedCallable(); 60 + public AnnotatedMethod<T> getAnnotatedMethod() {
  61 + return (AnnotatedMethod<T>) getAnnotatedCallable();
  62 + }
  63 +
  64 + abstract protected Integer getPriority(AnnotatedMethod<T> annotatedMethod);
  65 +
  66 + public int compareTo(final AnnotatedMethodProcessor<T> other) {
  67 + Integer orderThis = getPriority(getAnnotatedMethod());
  68 + Integer orderOther = getPriority(other.getAnnotatedMethod());
  69 +
  70 + return orderThis.compareTo(orderOther);
61 } 71 }
62 72
63 public boolean process(Object... args) throws Throwable { 73 public boolean process(Object... args) throws Throwable {
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/processor/ShutdownProcessor.java
@@ -41,23 +41,20 @@ import javax.enterprise.inject.spi.BeanManager; @@ -41,23 +41,20 @@ import javax.enterprise.inject.spi.BeanManager;
41 41
42 import br.gov.frameworkdemoiselle.annotation.Shutdown; 42 import br.gov.frameworkdemoiselle.annotation.Shutdown;
43 43
44 -public class ShutdownProcessor<T> extends AnnotatedMethodProcessor<T> implements Comparable<ShutdownProcessor<T>> { 44 +/**
  45 + * Processor for a {@code Shutdown} annotated method, making it comparable.
  46 + *
  47 + * @param <T>
  48 + */
  49 +public class ShutdownProcessor<T> extends AnnotatedMethodProcessor<T> {
45 50
46 public ShutdownProcessor(AnnotatedMethod<T> annotatedMethod, BeanManager beanManager) { 51 public ShutdownProcessor(AnnotatedMethod<T> annotatedMethod, BeanManager beanManager) {
47 super(annotatedMethod, beanManager); 52 super(annotatedMethod, beanManager);
48 } 53 }
49 54
50 - @Override  
51 - public int compareTo(final ShutdownProcessor<T> other) {  
52 - int result = 0;  
53 - Shutdown annotationThis = getAnnotatedMethod().getAnnotation(Shutdown.class);  
54 - Shutdown annotationOther = other.getAnnotatedMethod().getAnnotation(Shutdown.class);  
55 - if (annotationThis != null && annotationThis != null) {  
56 - Integer orderThis = annotationThis.priority();  
57 - Integer orderOther = annotationOther.priority();  
58 - result = orderThis.compareTo(orderOther);  
59 - }  
60 - return result; 55 + @SuppressWarnings("deprecation")
  56 + protected Integer getPriority(AnnotatedMethod<T> annotatedMethod) {
  57 + Shutdown annotation = annotatedMethod.getAnnotation(Shutdown.class);
  58 + return annotation.priority();
61 } 59 }
62 -  
63 } 60 }
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/processor/StartupProcessor.java
@@ -43,26 +43,18 @@ import br.gov.frameworkdemoiselle.annotation.Startup; @@ -43,26 +43,18 @@ import br.gov.frameworkdemoiselle.annotation.Startup;
43 43
44 /** 44 /**
45 * Processor for a {@code @Startup} annotated method, making it comparable. 45 * Processor for a {@code @Startup} annotated method, making it comparable.
46 - * 46 + *
47 * @param <T> 47 * @param <T>
48 */ 48 */
49 -public class StartupProcessor<T> extends AnnotatedMethodProcessor<T> implements Comparable<StartupProcessor<T>> { 49 +public class StartupProcessor<T> extends AnnotatedMethodProcessor<T> {
50 50
51 public StartupProcessor(final AnnotatedMethod<T> annotatedMethod, final BeanManager beanManager) { 51 public StartupProcessor(final AnnotatedMethod<T> annotatedMethod, final BeanManager beanManager) {
52 super(annotatedMethod, beanManager); 52 super(annotatedMethod, beanManager);
53 } 53 }
54 54
55 - @Override  
56 - public int compareTo(final StartupProcessor<T> other) {  
57 - int result = 0;  
58 - Startup annotationThis = getAnnotatedMethod().getAnnotation(Startup.class);  
59 - Startup annotationOther = other.getAnnotatedMethod().getAnnotation(Startup.class);  
60 - if (annotationThis != null && annotationOther != null) {  
61 - Integer orderThis = annotationThis.priority();  
62 - Integer orderOther = annotationOther.priority();  
63 - result = orderThis.compareTo(orderOther);  
64 - }  
65 - return result; 55 + @SuppressWarnings("deprecation")
  56 + protected Integer getPriority(AnnotatedMethod<T> annotatedMethod) {
  57 + Startup annotation = annotatedMethod.getAnnotation(Startup.class);
  58 + return annotation.priority();
66 } 59 }
67 -  
68 } 60 }
impl/core/src/test/java/br/gov/frameworkdemoiselle/internal/bootstrap/ShutdownBootstrapTest.java
1 -/*  
2 - * Demoiselle Framework  
3 - * Copyright (C) 2010 SERPRO  
4 - * ----------------------------------------------------------------------------  
5 - * This file is part of Demoiselle Framework.  
6 - *  
7 - * Demoiselle Framework is free software; you can redistribute it and/or  
8 - * modify it under the terms of the GNU Lesser General Public License version 3  
9 - * as published by the Free Software Foundation.  
10 - *  
11 - * This program is distributed in the hope that it will be useful,  
12 - * but WITHOUT ANY WARRANTY; without even the implied warranty of  
13 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the  
14 - * GNU General Public License for more details.  
15 - *  
16 - * You should have received a copy of the GNU Lesser General Public License version 3  
17 - * along with this program; if not, see <http://www.gnu.org/licenses/>  
18 - * or write to the Free Software Foundation, Inc., 51 Franklin Street,  
19 - * Fifth Floor, Boston, MA 02110-1301, USA.  
20 - * ----------------------------------------------------------------------------  
21 - * Este arquivo é parte do Framework Demoiselle.  
22 - *  
23 - * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou  
24 - * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação  
25 - * do Software Livre (FSF).  
26 - *  
27 - * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA  
28 - * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou  
29 - * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português  
30 - * para maiores detalhes.  
31 - *  
32 - * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título  
33 - * "LICENCA.txt", junto com esse programa. Se não, acesse <http://www.gnu.org/licenses/>  
34 - * ou escreva para a Fundação do Software Livre (FSF) Inc.,  
35 - * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA.  
36 - */  
37 -package br.gov.frameworkdemoiselle.internal.bootstrap;  
38 -  
39 -import static org.easymock.EasyMock.expect;  
40 -import static org.easymock.EasyMock.replay;  
41 -import static org.easymock.EasyMock.verify;  
42 -import static org.junit.Assert.assertFalse;  
43 -import static org.junit.Assert.assertNotNull;  
44 -import static org.junit.Assert.assertTrue;  
45 -  
46 -import java.lang.reflect.Field;  
47 -import java.util.ArrayList;  
48 -import java.util.HashSet;  
49 -import java.util.List;  
50 -import java.util.Locale;  
51 -import java.util.Set;  
52 -  
53 -import javax.enterprise.inject.spi.AfterBeanDiscovery;  
54 -import javax.enterprise.inject.spi.AnnotatedMethod;  
55 -import javax.enterprise.inject.spi.AnnotatedType;  
56 -import javax.enterprise.inject.spi.BeanManager;  
57 -import javax.enterprise.inject.spi.ProcessAnnotatedType;  
58 -  
59 -import org.easymock.EasyMock;  
60 -import org.junit.Before;  
61 -import org.junit.Ignore;  
62 -import org.junit.Test;  
63 -import org.junit.runner.RunWith;  
64 -import org.powermock.api.easymock.PowerMock;  
65 -import org.powermock.core.classloader.annotations.PrepareForTest;  
66 -import org.powermock.modules.junit4.PowerMockRunner;  
67 -import org.powermock.reflect.Whitebox;  
68 -import org.slf4j.Logger;  
69 -  
70 -import br.gov.frameworkdemoiselle.annotation.Shutdown;  
71 -import br.gov.frameworkdemoiselle.internal.context.Contexts;  
72 -import br.gov.frameworkdemoiselle.internal.context.ThreadLocalContext;  
73 -import br.gov.frameworkdemoiselle.internal.processor.ShutdownProcessor;  
74 -import br.gov.frameworkdemoiselle.internal.producer.LoggerProducer;  
75 -import br.gov.frameworkdemoiselle.internal.producer.ResourceBundleProducer;  
76 -import br.gov.frameworkdemoiselle.util.ResourceBundle;  
77 -  
78 -@Ignore  
79 -@RunWith(PowerMockRunner.class)  
80 -@PrepareForTest({ Contexts.class, LoggerProducer.class, ResourceBundle.class, ResourceBundleProducer.class })  
81 -@SuppressWarnings("rawtypes")  
82 -public class ShutdownBootstrapTest {  
83 -  
84 - private ProcessAnnotatedType event;  
85 -  
86 - private BeanManager beanManager;  
87 -  
88 - private AnnotatedType annotatedType;  
89 -  
90 - @Before  
91 - public void before() {  
92 - event = EasyMock.createMock(ProcessAnnotatedType.class);  
93 - annotatedType = EasyMock.createMock(AnnotatedType.class);  
94 - beanManager = null;  
95 - }  
96 -  
97 - @SuppressWarnings("unchecked")  
98 - private List<ShutdownProcessor> getProcessors(ShutdownBootstrap bootstrap) throws IllegalArgumentException,  
99 - IllegalAccessException {  
100 - Set<Field> fields = Whitebox.getAllStaticFields(ShutdownBootstrap.class);  
101 - List<ShutdownProcessor> list = new ArrayList<ShutdownProcessor>();  
102 - for (Field field : fields) {  
103 - if (field.getName().equals("processors")) {  
104 - list = (List<ShutdownProcessor>) field.get(bootstrap);  
105 - }  
106 - }  
107 - return list;  
108 - }  
109 -  
110 - @SuppressWarnings({ "unchecked" })  
111 - @Test  
112 - public void processAnnotatedType() throws IllegalArgumentException, IllegalAccessException {  
113 - ShutdownBootstrap bootstrap = new ShutdownBootstrap();  
114 - List<ShutdownProcessor> list = getProcessors(bootstrap);  
115 -  
116 - assertTrue(list.isEmpty());  
117 -  
118 - AnnotatedMethod am1 = PowerMock.createMock(AnnotatedMethod.class);  
119 - AnnotatedMethod am2 = PowerMock.createMock(AnnotatedMethod.class);  
120 - AnnotatedMethod am3 = PowerMock.createMock(AnnotatedMethod.class);  
121 -  
122 - Set<AnnotatedMethod> set = new HashSet<AnnotatedMethod>();  
123 - set.add(am1);  
124 - set.add(am2);  
125 - set.add(am3);  
126 -  
127 - expect(am1.isAnnotationPresent(Shutdown.class)).andReturn(true);  
128 - expect(am2.isAnnotationPresent(Shutdown.class)).andReturn(true);  
129 - expect(am3.isAnnotationPresent(Shutdown.class)).andReturn(false);  
130 - expect(event.getAnnotatedType()).andReturn(annotatedType);  
131 - expect(annotatedType.getMethods()).andReturn(set);  
132 -  
133 - replay(event, annotatedType, am1, am2, am3);  
134 - bootstrap.processAnnotatedType(event, beanManager);  
135 - verify(event, annotatedType);  
136 -  
137 - list = getProcessors(bootstrap);  
138 - assertNotNull(list);  
139 - assertFalse(list.isEmpty());  
140 - assertTrue(list.size() == 2);  
141 - }  
142 -  
143 - @SuppressWarnings({ "unchecked", "static-access" })  
144 - @Test  
145 - public void testShuttingDown() throws Throwable {  
146 - ShutdownBootstrap bootstrap = new ShutdownBootstrap();  
147 -  
148 - PowerMock.mockStatic(Contexts.class);  
149 - PowerMock.mockStatic(LoggerProducer.class);  
150 -  
151 - Logger logger = PowerMock.createMock(Logger.class);  
152 - ResourceBundleProducer bundleFactory = PowerMock.createMock(ResourceBundleProducer.class);  
153 - ResourceBundle bundle = PowerMock.createMock(ResourceBundle.class);  
154 -  
155 - expect(LoggerProducer.create(EasyMock.anyObject(Class.class))).andReturn(logger).anyTimes();  
156 - expect(bundleFactory.create(EasyMock.anyObject(String.class), EasyMock.anyObject(Locale.class))).andReturn(  
157 - bundle).anyTimes();  
158 - expect(bundle.getString(EasyMock.anyObject(String.class), EasyMock.anyObject(String.class))).andReturn("")  
159 - .anyTimes();  
160 -  
161 - logger.debug(EasyMock.anyObject(String.class));  
162 - logger.trace(EasyMock.anyObject(String.class));  
163 - EasyMock.expectLastCall().anyTimes();  
164 -  
165 - Whitebox.setInternalState(AbstractBootstrap.class, ResourceBundleProducer.class, bundleFactory);  
166 -  
167 - List<ShutdownProcessor> list = getProcessors(bootstrap);  
168 - list.clear();  
169 -  
170 - MyShuttingDownProcessor<?> processor = PowerMock.createMock(MyShuttingDownProcessor.class);  
171 - list.add(processor);  
172 - expect(processor.process()).andReturn(true).times(1);  
173 -  
174 - Contexts.add(EasyMock.anyObject(ThreadLocalContext.class), EasyMock.anyObject(AfterBeanDiscovery.class));  
175 - EasyMock.expectLastCall().anyTimes();  
176 -  
177 - Contexts.remove(EasyMock.anyObject(ThreadLocalContext.class));  
178 - EasyMock.expectLastCall().anyTimes();  
179 -  
180 - PowerMock.replayAll();  
181 - ShutdownBootstrap.shutdown();  
182 -  
183 - assertTrue(list.isEmpty());  
184 - PowerMock.verifyAll();  
185 - }  
186 -}  
187 -  
188 -@SuppressWarnings("rawtypes")  
189 -class MyShuttingDownProcessor<T> extends ShutdownProcessor<T> {  
190 -  
191 - @SuppressWarnings("unchecked")  
192 - public MyShuttingDownProcessor(AnnotatedMethod annotatedMethod, BeanManager beanManager) {  
193 - super(annotatedMethod, beanManager);  
194 - }  
195 -  
196 - @Override  
197 - public int compareTo(final ShutdownProcessor<T> other) {  
198 - return 1;  
199 - }  
200 -} 1 +///*
  2 +// * Demoiselle Framework
  3 +// * Copyright (C) 2010 SERPRO
  4 +// * ----------------------------------------------------------------------------
  5 +// * This file is part of Demoiselle Framework.
  6 +// *
  7 +// * Demoiselle Framework is free software; you can redistribute it and/or
  8 +// * modify it under the terms of the GNU Lesser General Public License version 3
  9 +// * as published by the Free Software Foundation.
  10 +// *
  11 +// * This program is distributed in the hope that it will be useful,
  12 +// * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13 +// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14 +// * GNU General Public License for more details.
  15 +// *
  16 +// * You should have received a copy of the GNU Lesser General Public License version 3
  17 +// * along with this program; if not, see <http://www.gnu.org/licenses/>
  18 +// * or write to the Free Software Foundation, Inc., 51 Franklin Street,
  19 +// * Fifth Floor, Boston, MA 02110-1301, USA.
  20 +// * ----------------------------------------------------------------------------
  21 +// * Este arquivo é parte do Framework Demoiselle.
  22 +// *
  23 +// * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou
  24 +// * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação
  25 +// * do Software Livre (FSF).
  26 +// *
  27 +// * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA
  28 +// * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou
  29 +// * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português
  30 +// * para maiores detalhes.
  31 +// *
  32 +// * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título
  33 +// * "LICENCA.txt", junto com esse programa. Se não, acesse <http://www.gnu.org/licenses/>
  34 +// * ou escreva para a Fundação do Software Livre (FSF) Inc.,
  35 +// * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA.
  36 +// */
  37 +//package br.gov.frameworkdemoiselle.internal.bootstrap;
  38 +//
  39 +//import static org.easymock.EasyMock.expect;
  40 +//import static org.easymock.EasyMock.replay;
  41 +//import static org.easymock.EasyMock.verify;
  42 +//import static org.junit.Assert.assertFalse;
  43 +//import static org.junit.Assert.assertNotNull;
  44 +//import static org.junit.Assert.assertTrue;
  45 +//
  46 +//import java.lang.reflect.Field;
  47 +//import java.util.ArrayList;
  48 +//import java.util.HashSet;
  49 +//import java.util.List;
  50 +//import java.util.Locale;
  51 +//import java.util.Set;
  52 +//
  53 +//import javax.enterprise.inject.spi.AfterBeanDiscovery;
  54 +//import javax.enterprise.inject.spi.AnnotatedMethod;
  55 +//import javax.enterprise.inject.spi.AnnotatedType;
  56 +//import javax.enterprise.inject.spi.BeanManager;
  57 +//import javax.enterprise.inject.spi.ProcessAnnotatedType;
  58 +//
  59 +//import org.easymock.EasyMock;
  60 +//import org.junit.Before;
  61 +//import org.junit.Ignore;
  62 +//import org.junit.Test;
  63 +//import org.junit.runner.RunWith;
  64 +//import org.powermock.api.easymock.PowerMock;
  65 +//import org.powermock.core.classloader.annotations.PrepareForTest;
  66 +//import org.powermock.modules.junit4.PowerMockRunner;
  67 +//import org.powermock.reflect.Whitebox;
  68 +//import org.slf4j.Logger;
  69 +//
  70 +//import br.gov.frameworkdemoiselle.annotation.Shutdown;
  71 +//import br.gov.frameworkdemoiselle.internal.context.Contexts;
  72 +//import br.gov.frameworkdemoiselle.internal.context.ThreadLocalContext;
  73 +//import br.gov.frameworkdemoiselle.internal.processor.ShutdownProcessor;
  74 +//import br.gov.frameworkdemoiselle.internal.producer.LoggerProducer;
  75 +//import br.gov.frameworkdemoiselle.internal.producer.ResourceBundleProducer;
  76 +//import br.gov.frameworkdemoiselle.util.ResourceBundle;
  77 +//
  78 +//@Ignore
  79 +//@RunWith(PowerMockRunner.class)
  80 +//@PrepareForTest({ Contexts.class, LoggerProducer.class, ResourceBundle.class, ResourceBundleProducer.class })
  81 +//@SuppressWarnings("rawtypes")
  82 +//public class ShutdownBootstrapTest {
  83 +//
  84 +// private ProcessAnnotatedType event;
  85 +//
  86 +// private BeanManager beanManager;
  87 +//
  88 +// private AnnotatedType annotatedType;
  89 +//
  90 +// @Before
  91 +// public void before() {
  92 +// event = EasyMock.createMock(ProcessAnnotatedType.class);
  93 +// annotatedType = EasyMock.createMock(AnnotatedType.class);
  94 +// beanManager = null;
  95 +// }
  96 +//
  97 +// @SuppressWarnings("unchecked")
  98 +// private List<ShutdownProcessor> getProcessors(ShutdownBootstrap bootstrap) throws IllegalArgumentException,
  99 +// IllegalAccessException {
  100 +// Set<Field> fields = Whitebox.getAllStaticFields(ShutdownBootstrap.class);
  101 +// List<ShutdownProcessor> list = new ArrayList<ShutdownProcessor>();
  102 +// for (Field field : fields) {
  103 +// if (field.getName().equals("processors")) {
  104 +// list = (List<ShutdownProcessor>) field.get(bootstrap);
  105 +// }
  106 +// }
  107 +// return list;
  108 +// }
  109 +//
  110 +// @SuppressWarnings({ "unchecked" })
  111 +// @Test
  112 +// public void processAnnotatedType() throws IllegalArgumentException, IllegalAccessException {
  113 +// ShutdownBootstrap bootstrap = new ShutdownBootstrap();
  114 +// List<ShutdownProcessor> list = getProcessors(bootstrap);
  115 +//
  116 +// assertTrue(list.isEmpty());
  117 +//
  118 +// AnnotatedMethod am1 = PowerMock.createMock(AnnotatedMethod.class);
  119 +// AnnotatedMethod am2 = PowerMock.createMock(AnnotatedMethod.class);
  120 +// AnnotatedMethod am3 = PowerMock.createMock(AnnotatedMethod.class);
  121 +//
  122 +// Set<AnnotatedMethod> set = new HashSet<AnnotatedMethod>();
  123 +// set.add(am1);
  124 +// set.add(am2);
  125 +// set.add(am3);
  126 +//
  127 +// expect(am1.isAnnotationPresent(Shutdown.class)).andReturn(true);
  128 +// expect(am2.isAnnotationPresent(Shutdown.class)).andReturn(true);
  129 +// expect(am3.isAnnotationPresent(Shutdown.class)).andReturn(false);
  130 +// expect(event.getAnnotatedType()).andReturn(annotatedType);
  131 +// expect(annotatedType.getMethods()).andReturn(set);
  132 +//
  133 +// replay(event, annotatedType, am1, am2, am3);
  134 +// bootstrap.processAnnotatedType(event, beanManager);
  135 +// verify(event, annotatedType);
  136 +//
  137 +// list = getProcessors(bootstrap);
  138 +// assertNotNull(list);
  139 +// assertFalse(list.isEmpty());
  140 +// assertTrue(list.size() == 2);
  141 +// }
  142 +//
  143 +// @SuppressWarnings({ "unchecked", "static-access" })
  144 +// @Test
  145 +// public void testShuttingDown() throws Throwable {
  146 +// ShutdownBootstrap bootstrap = new ShutdownBootstrap();
  147 +//
  148 +// PowerMock.mockStatic(Contexts.class);
  149 +// PowerMock.mockStatic(LoggerProducer.class);
  150 +//
  151 +// Logger logger = PowerMock.createMock(Logger.class);
  152 +// ResourceBundleProducer bundleFactory = PowerMock.createMock(ResourceBundleProducer.class);
  153 +// ResourceBundle bundle = PowerMock.createMock(ResourceBundle.class);
  154 +//
  155 +// expect(LoggerProducer.create(EasyMock.anyObject(Class.class))).andReturn(logger).anyTimes();
  156 +// expect(bundleFactory.create(EasyMock.anyObject(String.class), EasyMock.anyObject(Locale.class))).andReturn(
  157 +// bundle).anyTimes();
  158 +// expect(bundle.getString(EasyMock.anyObject(String.class), EasyMock.anyObject(String.class))).andReturn("")
  159 +// .anyTimes();
  160 +//
  161 +// logger.debug(EasyMock.anyObject(String.class));
  162 +// logger.trace(EasyMock.anyObject(String.class));
  163 +// EasyMock.expectLastCall().anyTimes();
  164 +//
  165 +// Whitebox.setInternalState(AbstractBootstrap.class, ResourceBundleProducer.class, bundleFactory);
  166 +//
  167 +// List<ShutdownProcessor> list = getProcessors(bootstrap);
  168 +// list.clear();
  169 +//
  170 +// MyShuttingDownProcessor<?> processor = PowerMock.createMock(MyShuttingDownProcessor.class);
  171 +// list.add(processor);
  172 +// expect(processor.process()).andReturn(true).times(1);
  173 +//
  174 +// Contexts.add(EasyMock.anyObject(ThreadLocalContext.class), EasyMock.anyObject(AfterBeanDiscovery.class));
  175 +// EasyMock.expectLastCall().anyTimes();
  176 +//
  177 +// Contexts.remove(EasyMock.anyObject(ThreadLocalContext.class));
  178 +// EasyMock.expectLastCall().anyTimes();
  179 +//
  180 +// PowerMock.replayAll();
  181 +// ShutdownBootstrap.shutdown();
  182 +//
  183 +// assertTrue(list.isEmpty());
  184 +// PowerMock.verifyAll();
  185 +// }
  186 +//}
  187 +//
  188 +//@SuppressWarnings("rawtypes")
  189 +//class MyShuttingDownProcessor<T> extends ShutdownProcessor<T> {
  190 +//
  191 +// @SuppressWarnings("unchecked")
  192 +// public MyShuttingDownProcessor(AnnotatedMethod annotatedMethod, BeanManager beanManager) {
  193 +// super(annotatedMethod, beanManager);
  194 +// }
  195 +//
  196 +// @Override
  197 +// public int compareTo(final ShutdownProcessor<T> other) {
  198 +// return 1;
  199 +// }
  200 +//}
impl/core/src/test/java/br/gov/frameworkdemoiselle/internal/bootstrap/StartupBootstrapTest.java
1 -/*  
2 - * Demoiselle Framework  
3 - * Copyright (C) 2010 SERPRO  
4 - * ----------------------------------------------------------------------------  
5 - * This file is part of Demoiselle Framework.  
6 - *  
7 - * Demoiselle Framework is free software; you can redistribute it and/or  
8 - * modify it under the terms of the GNU Lesser General Public License version 3  
9 - * as published by the Free Software Foundation.  
10 - *  
11 - * This program is distributed in the hope that it will be useful,  
12 - * but WITHOUT ANY WARRANTY; without even the implied warranty of  
13 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the  
14 - * GNU General Public License for more details.  
15 - *  
16 - * You should have received a copy of the GNU Lesser General Public License version 3  
17 - * along with this program; if not, see <http://www.gnu.org/licenses/>  
18 - * or write to the Free Software Foundation, Inc., 51 Franklin Street,  
19 - * Fifth Floor, Boston, MA 02110-1301, USA.  
20 - * ----------------------------------------------------------------------------  
21 - * Este arquivo é parte do Framework Demoiselle.  
22 - *  
23 - * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou  
24 - * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação  
25 - * do Software Livre (FSF).  
26 - *  
27 - * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA  
28 - * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou  
29 - * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português  
30 - * para maiores detalhes.  
31 - *  
32 - * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título  
33 - * "LICENCA.txt", junto com esse programa. Se não, acesse <http://www.gnu.org/licenses/>  
34 - * ou escreva para a Fundação do Software Livre (FSF) Inc.,  
35 - * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA.  
36 - */  
37 -package br.gov.frameworkdemoiselle.internal.bootstrap;  
38 -  
39 -import static junit.framework.Assert.assertEquals;  
40 -import static org.easymock.EasyMock.expect;  
41 -import static org.easymock.EasyMock.replay;  
42 -import static org.easymock.EasyMock.verify;  
43 -import static org.junit.Assert.assertFalse;  
44 -import static org.junit.Assert.assertNotNull;  
45 -import static org.junit.Assert.assertTrue;  
46 -import static org.junit.Assert.fail;  
47 -  
48 -import java.lang.reflect.Field;  
49 -import java.util.HashSet;  
50 -import java.util.List;  
51 -import java.util.Locale;  
52 -import java.util.Set;  
53 -  
54 -import javax.enterprise.context.ConversationScoped;  
55 -import javax.enterprise.context.RequestScoped;  
56 -import javax.enterprise.context.SessionScoped;  
57 -import javax.enterprise.inject.spi.AfterBeanDiscovery;  
58 -import javax.enterprise.inject.spi.AnnotatedMethod;  
59 -import javax.enterprise.inject.spi.AnnotatedType;  
60 -import javax.enterprise.inject.spi.BeanManager;  
61 -import javax.enterprise.inject.spi.ProcessAnnotatedType;  
62 -  
63 -import junit.framework.Assert;  
64 -  
65 -import org.easymock.EasyMock;  
66 -import org.junit.Before;  
67 -import org.junit.Ignore;  
68 -import org.junit.Test;  
69 -import org.junit.runner.RunWith;  
70 -import org.powermock.api.easymock.PowerMock;  
71 -import org.powermock.core.classloader.annotations.PrepareForTest;  
72 -import org.powermock.modules.junit4.PowerMockRunner;  
73 -import org.powermock.reflect.Whitebox;  
74 -import org.slf4j.Logger;  
75 -  
76 -import br.gov.frameworkdemoiselle.annotation.Startup;  
77 -import br.gov.frameworkdemoiselle.annotation.ViewScoped;  
78 -import br.gov.frameworkdemoiselle.internal.context.Contexts;  
79 -import br.gov.frameworkdemoiselle.internal.context.ThreadLocalContext;  
80 -import br.gov.frameworkdemoiselle.internal.processor.StartupProcessor;  
81 -import br.gov.frameworkdemoiselle.internal.producer.LoggerProducer;  
82 -import br.gov.frameworkdemoiselle.internal.producer.ResourceBundleProducer;  
83 -import br.gov.frameworkdemoiselle.util.ResourceBundle;  
84 -  
85 -@Ignore  
86 -@RunWith(PowerMockRunner.class)  
87 -@PrepareForTest({ Contexts.class, LoggerProducer.class, ResourceBundle.class, ResourceBundleProducer.class })  
88 -@SuppressWarnings({ "rawtypes", "unchecked" })  
89 -public class StartupBootstrapTest {  
90 -  
91 - private ProcessAnnotatedType event;  
92 -  
93 - private BeanManager beanManager;  
94 -  
95 - private AnnotatedType annotatedType;  
96 -  
97 - @Before  
98 - public void before() {  
99 - event = EasyMock.createMock(ProcessAnnotatedType.class);  
100 - annotatedType = EasyMock.createMock(AnnotatedType.class);  
101 - beanManager = null;  
102 - }  
103 -  
104 - private List<StartupProcessor> getActions(StartupBootstrap bootstrap) throws IllegalArgumentException,  
105 - IllegalAccessException {  
106 - Set<Field> fields = Whitebox.getAllStaticFields(StartupBootstrap.class);  
107 - List<StartupProcessor> list = null;  
108 - for (Field field : fields) {  
109 - if (field.getName().equals("processors")) {  
110 - list = (List<StartupProcessor>) field.get(bootstrap);  
111 - }  
112 - }  
113 - return list;  
114 - }  
115 -  
116 - @Test  
117 - public void processAnnotatedType() throws IllegalArgumentException, IllegalAccessException {  
118 - StartupBootstrap bootstrap = new StartupBootstrap();  
119 - List<StartupProcessor> list = getActions(bootstrap);  
120 -  
121 - assertNotNull(list);  
122 - assertTrue(list.isEmpty());  
123 -  
124 - AnnotatedMethod am1 = PowerMock.createMock(AnnotatedMethod.class);  
125 - AnnotatedMethod am2 = PowerMock.createMock(AnnotatedMethod.class);  
126 - AnnotatedMethod am3 = PowerMock.createMock(AnnotatedMethod.class);  
127 -  
128 - Set<AnnotatedMethod> set = new HashSet<AnnotatedMethod>();  
129 - set.add(am1);  
130 - set.add(am2);  
131 - set.add(am3);  
132 -  
133 - expect(am1.isAnnotationPresent(Startup.class)).andReturn(true);  
134 - expect(am2.isAnnotationPresent(Startup.class)).andReturn(true);  
135 - expect(am3.isAnnotationPresent(Startup.class)).andReturn(false);  
136 - expect(event.getAnnotatedType()).andReturn(annotatedType);  
137 - expect(annotatedType.getMethods()).andReturn(set);  
138 -  
139 - replay(event, annotatedType, am1, am2, am3);  
140 - bootstrap.processAnnotatedType(event, beanManager);  
141 - verify(event, annotatedType);  
142 -  
143 - list = getActions(bootstrap);  
144 - assertNotNull(list);  
145 - assertFalse(list.isEmpty());  
146 - assertTrue(list.size() == 2);  
147 - }  
148 -  
149 - @SuppressWarnings("static-access")  
150 - @Test  
151 - public void testLoadTempContexts() {  
152 - StartupBootstrap bootstrap = new StartupBootstrap();  
153 -  
154 - Logger logger = PowerMock.createMock(Logger.class);  
155 - ResourceBundleProducer bundleFactory = PowerMock.createMock(ResourceBundleProducer.class);  
156 - ResourceBundle bundle = PowerMock.createMock(ResourceBundle.class);  
157 -  
158 - PowerMock.mockStatic(Contexts.class);  
159 - PowerMock.mockStatic(LoggerProducer.class);  
160 -  
161 - List<ThreadLocalContext> tempContexts = Whitebox.getInternalState(bootstrap, "tempContexts");  
162 -  
163 - assertNotNull(tempContexts);  
164 - assertTrue(tempContexts.isEmpty());  
165 -  
166 - expect(LoggerProducer.create(EasyMock.anyObject(Class.class))).andReturn(logger).anyTimes();  
167 - expect(bundleFactory.create(EasyMock.anyObject(String.class), EasyMock.anyObject(Locale.class))).andReturn(  
168 - bundle).anyTimes();  
169 - expect(bundle.getString(EasyMock.anyObject(String.class), EasyMock.anyObject(String.class))).andReturn("")  
170 - .anyTimes();  
171 -  
172 - logger.trace(EasyMock.anyObject(String.class));  
173 - EasyMock.expectLastCall().anyTimes();  
174 -  
175 - Contexts.add(EasyMock.anyObject(ThreadLocalContext.class), EasyMock.anyObject(AfterBeanDiscovery.class));  
176 - EasyMock.expectLastCall().anyTimes();  
177 -  
178 - Whitebox.setInternalState(AbstractBootstrap.class, ResourceBundleProducer.class, bundleFactory);  
179 -  
180 - PowerMock.replayAll();  
181 - bootstrap.loadTempContexts(null);  
182 - PowerMock.verifyAll();  
183 -  
184 - assertNotNull(tempContexts);  
185 - assertEquals(4, tempContexts.size());  
186 -  
187 - for (ThreadLocalContext tlc : tempContexts) {  
188 - if (!tlc.getScope().equals(SessionScoped.class) && !tlc.getScope().equals(ConversationScoped.class)  
189 - && !tlc.getScope().equals(RequestScoped.class) && !tlc.getScope().equals(ViewScoped.class)) {  
190 - fail();  
191 - }  
192 - }  
193 - }  
194 -  
195 - @SuppressWarnings("static-access")  
196 - @Test  
197 - public void testStartup() throws Throwable {  
198 - StartupBootstrap bootstrap = new StartupBootstrap();  
199 -  
200 - PowerMock.mockStatic(Contexts.class);  
201 - PowerMock.mockStatic(LoggerProducer.class);  
202 -  
203 - Logger logger = PowerMock.createMock(Logger.class);  
204 - ResourceBundleProducer bundleFactory = PowerMock.createMock(ResourceBundleProducer.class);  
205 - ResourceBundle bundle = PowerMock.createMock(ResourceBundle.class);  
206 -  
207 - expect(LoggerProducer.create(EasyMock.anyObject(Class.class))).andReturn(logger).anyTimes();  
208 - expect(bundleFactory.create(EasyMock.anyObject(String.class), EasyMock.anyObject(Locale.class))).andReturn(  
209 - bundle).anyTimes();  
210 - expect(bundle.getString(EasyMock.anyObject(String.class), EasyMock.anyObject(String.class))).andReturn("")  
211 - .anyTimes();  
212 -  
213 - logger.debug(EasyMock.anyObject(String.class));  
214 - EasyMock.expectLastCall().anyTimes();  
215 -  
216 - Whitebox.setInternalState(AbstractBootstrap.class, ResourceBundleProducer.class, bundleFactory);  
217 -  
218 - List<StartupProcessor> list = getActions(bootstrap);  
219 - list.clear();  
220 -  
221 - MyProcessor<?> processor = PowerMock.createMock(MyProcessor.class);  
222 - list.add(processor);  
223 - expect(processor.process()).andReturn(true).times(1);  
224 -  
225 - PowerMock.replayAll();  
226 - bootstrap.startup();  
227 -  
228 - assertTrue(list.isEmpty());  
229 - PowerMock.verifyAll();  
230 - }  
231 -  
232 - @SuppressWarnings("static-access")  
233 - @Test  
234 - public void testLoadTempContextsAndStartup() {  
235 -  
236 - StartupBootstrap bootstrap = new StartupBootstrap();  
237 -  
238 - bootstrap.loadTempContexts(null);  
239 - Assert.assertFalse(Contexts.getActiveContexts().isEmpty());  
240 -  
241 - try {  
242 - bootstrap.startup();  
243 - Assert.assertTrue(Contexts.getActiveContexts().isEmpty());  
244 - } catch (Throwable e) {  
245 - fail();  
246 - }  
247 - }  
248 -}  
249 -  
250 -@SuppressWarnings("rawtypes")  
251 -class MyProcessor<T> extends StartupProcessor<T> {  
252 -  
253 - @SuppressWarnings("unchecked")  
254 - public MyProcessor(AnnotatedMethod annotatedMethod, BeanManager beanManager) {  
255 - super(annotatedMethod, beanManager);  
256 - }  
257 -  
258 - @Override  
259 - public int compareTo(final StartupProcessor<T> other) {  
260 - return 1;  
261 - }  
262 -} 1 +///*
  2 +// * Demoiselle Framework
  3 +// * Copyright (C) 2010 SERPRO
  4 +// * ----------------------------------------------------------------------------
  5 +// * This file is part of Demoiselle Framework.
  6 +// *
  7 +// * Demoiselle Framework is free software; you can redistribute it and/or
  8 +// * modify it under the terms of the GNU Lesser General Public License version 3
  9 +// * as published by the Free Software Foundation.
  10 +// *
  11 +// * This program is distributed in the hope that it will be useful,
  12 +// * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13 +// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14 +// * GNU General Public License for more details.
  15 +// *
  16 +// * You should have received a copy of the GNU Lesser General Public License version 3
  17 +// * along with this program; if not, see <http://www.gnu.org/licenses/>
  18 +// * or write to the Free Software Foundation, Inc., 51 Franklin Street,
  19 +// * Fifth Floor, Boston, MA 02110-1301, USA.
  20 +// * ----------------------------------------------------------------------------
  21 +// * Este arquivo é parte do Framework Demoiselle.
  22 +// *
  23 +// * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou
  24 +// * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação
  25 +// * do Software Livre (FSF).
  26 +// *
  27 +// * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA
  28 +// * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou
  29 +// * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português
  30 +// * para maiores detalhes.
  31 +// *
  32 +// * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título
  33 +// * "LICENCA.txt", junto com esse programa. Se não, acesse <http://www.gnu.org/licenses/>
  34 +// * ou escreva para a Fundação do Software Livre (FSF) Inc.,
  35 +// * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA.
  36 +// */
  37 +//package br.gov.frameworkdemoiselle.internal.bootstrap;
  38 +//
  39 +//import static junit.framework.Assert.assertEquals;
  40 +//import static org.easymock.EasyMock.expect;
  41 +//import static org.easymock.EasyMock.replay;
  42 +//import static org.easymock.EasyMock.verify;
  43 +//import static org.junit.Assert.assertFalse;
  44 +//import static org.junit.Assert.assertNotNull;
  45 +//import static org.junit.Assert.assertTrue;
  46 +//import static org.junit.Assert.fail;
  47 +//
  48 +//import java.lang.reflect.Field;
  49 +//import java.util.HashSet;
  50 +//import java.util.List;
  51 +//import java.util.Locale;
  52 +//import java.util.Set;
  53 +//
  54 +//import javax.enterprise.context.ConversationScoped;
  55 +//import javax.enterprise.context.RequestScoped;
  56 +//import javax.enterprise.context.SessionScoped;
  57 +//import javax.enterprise.inject.spi.AfterBeanDiscovery;
  58 +//import javax.enterprise.inject.spi.AnnotatedMethod;
  59 +//import javax.enterprise.inject.spi.AnnotatedType;
  60 +//import javax.enterprise.inject.spi.BeanManager;
  61 +//import javax.enterprise.inject.spi.ProcessAnnotatedType;
  62 +//
  63 +//import junit.framework.Assert;
  64 +//
  65 +//import org.easymock.EasyMock;
  66 +//import org.junit.Before;
  67 +//import org.junit.Ignore;
  68 +//import org.junit.Test;
  69 +//import org.junit.runner.RunWith;
  70 +//import org.powermock.api.easymock.PowerMock;
  71 +//import org.powermock.core.classloader.annotations.PrepareForTest;
  72 +//import org.powermock.modules.junit4.PowerMockRunner;
  73 +//import org.powermock.reflect.Whitebox;
  74 +//import org.slf4j.Logger;
  75 +//
  76 +//import br.gov.frameworkdemoiselle.annotation.Startup;
  77 +//import br.gov.frameworkdemoiselle.annotation.ViewScoped;
  78 +//import br.gov.frameworkdemoiselle.internal.context.Contexts;
  79 +//import br.gov.frameworkdemoiselle.internal.context.ThreadLocalContext;
  80 +//import br.gov.frameworkdemoiselle.internal.processor.StartupProcessor;
  81 +//import br.gov.frameworkdemoiselle.internal.producer.LoggerProducer;
  82 +//import br.gov.frameworkdemoiselle.internal.producer.ResourceBundleProducer;
  83 +//import br.gov.frameworkdemoiselle.util.ResourceBundle;
  84 +//
  85 +//@Ignore
  86 +//@RunWith(PowerMockRunner.class)
  87 +//@PrepareForTest({ Contexts.class, LoggerProducer.class, ResourceBundle.class, ResourceBundleProducer.class })
  88 +//@SuppressWarnings({ "rawtypes", "unchecked" })
  89 +//public class StartupBootstrapTest {
  90 +//
  91 +// private ProcessAnnotatedType event;
  92 +//
  93 +// private BeanManager beanManager;
  94 +//
  95 +// private AnnotatedType annotatedType;
  96 +//
  97 +// @Before
  98 +// public void before() {
  99 +// event = EasyMock.createMock(ProcessAnnotatedType.class);
  100 +// annotatedType = EasyMock.createMock(AnnotatedType.class);
  101 +// beanManager = null;
  102 +// }
  103 +//
  104 +// private List<StartupProcessor> getActions(StartupBootstrap bootstrap) throws IllegalArgumentException,
  105 +// IllegalAccessException {
  106 +// Set<Field> fields = Whitebox.getAllStaticFields(StartupBootstrap.class);
  107 +// List<StartupProcessor> list = null;
  108 +// for (Field field : fields) {
  109 +// if (field.getName().equals("processors")) {
  110 +// list = (List<StartupProcessor>) field.get(bootstrap);
  111 +// }
  112 +// }
  113 +// return list;
  114 +// }
  115 +//
  116 +// @Test
  117 +// public void processAnnotatedType() throws IllegalArgumentException, IllegalAccessException {
  118 +// StartupBootstrap bootstrap = new StartupBootstrap();
  119 +// List<StartupProcessor> list = getActions(bootstrap);
  120 +//
  121 +// assertNotNull(list);
  122 +// assertTrue(list.isEmpty());
  123 +//
  124 +// AnnotatedMethod am1 = PowerMock.createMock(AnnotatedMethod.class);
  125 +// AnnotatedMethod am2 = PowerMock.createMock(AnnotatedMethod.class);
  126 +// AnnotatedMethod am3 = PowerMock.createMock(AnnotatedMethod.class);
  127 +//
  128 +// Set<AnnotatedMethod> set = new HashSet<AnnotatedMethod>();
  129 +// set.add(am1);
  130 +// set.add(am2);
  131 +// set.add(am3);
  132 +//
  133 +// expect(am1.isAnnotationPresent(Startup.class)).andReturn(true);
  134 +// expect(am2.isAnnotationPresent(Startup.class)).andReturn(true);
  135 +// expect(am3.isAnnotationPresent(Startup.class)).andReturn(false);
  136 +// expect(event.getAnnotatedType()).andReturn(annotatedType);
  137 +// expect(annotatedType.getMethods()).andReturn(set);
  138 +//
  139 +// replay(event, annotatedType, am1, am2, am3);
  140 +// bootstrap.processAnnotatedType(event, beanManager);
  141 +// verify(event, annotatedType);
  142 +//
  143 +// list = getActions(bootstrap);
  144 +// assertNotNull(list);
  145 +// assertFalse(list.isEmpty());
  146 +// assertTrue(list.size() == 2);
  147 +// }
  148 +//
  149 +// @SuppressWarnings("static-access")
  150 +// @Test
  151 +// public void testLoadTempContexts() {
  152 +// StartupBootstrap bootstrap = new StartupBootstrap();
  153 +//
  154 +// Logger logger = PowerMock.createMock(Logger.class);
  155 +// ResourceBundleProducer bundleFactory = PowerMock.createMock(ResourceBundleProducer.class);
  156 +// ResourceBundle bundle = PowerMock.createMock(ResourceBundle.class);
  157 +//
  158 +// PowerMock.mockStatic(Contexts.class);
  159 +// PowerMock.mockStatic(LoggerProducer.class);
  160 +//
  161 +// List<ThreadLocalContext> tempContexts = Whitebox.getInternalState(bootstrap, "tempContexts");
  162 +//
  163 +// assertNotNull(tempContexts);
  164 +// assertTrue(tempContexts.isEmpty());
  165 +//
  166 +// expect(LoggerProducer.create(EasyMock.anyObject(Class.class))).andReturn(logger).anyTimes();
  167 +// expect(bundleFactory.create(EasyMock.anyObject(String.class), EasyMock.anyObject(Locale.class))).andReturn(
  168 +// bundle).anyTimes();
  169 +// expect(bundle.getString(EasyMock.anyObject(String.class), EasyMock.anyObject(String.class))).andReturn("")
  170 +// .anyTimes();
  171 +//
  172 +// logger.trace(EasyMock.anyObject(String.class));
  173 +// EasyMock.expectLastCall().anyTimes();
  174 +//
  175 +// Contexts.add(EasyMock.anyObject(ThreadLocalContext.class), EasyMock.anyObject(AfterBeanDiscovery.class));
  176 +// EasyMock.expectLastCall().anyTimes();
  177 +//
  178 +// Whitebox.setInternalState(AbstractBootstrap.class, ResourceBundleProducer.class, bundleFactory);
  179 +//
  180 +// PowerMock.replayAll();
  181 +// bootstrap.loadTempContexts(null);
  182 +// PowerMock.verifyAll();
  183 +//
  184 +// assertNotNull(tempContexts);
  185 +// assertEquals(4, tempContexts.size());
  186 +//
  187 +// for (ThreadLocalContext tlc : tempContexts) {
  188 +// if (!tlc.getScope().equals(SessionScoped.class) && !tlc.getScope().equals(ConversationScoped.class)
  189 +// && !tlc.getScope().equals(RequestScoped.class) && !tlc.getScope().equals(ViewScoped.class)) {
  190 +// fail();
  191 +// }
  192 +// }
  193 +// }
  194 +//
  195 +// @SuppressWarnings("static-access")
  196 +// @Test
  197 +// public void testStartup() throws Throwable {
  198 +// StartupBootstrap bootstrap = new StartupBootstrap();
  199 +//
  200 +// PowerMock.mockStatic(Contexts.class);
  201 +// PowerMock.mockStatic(LoggerProducer.class);
  202 +//
  203 +// Logger logger = PowerMock.createMock(Logger.class);
  204 +// ResourceBundleProducer bundleFactory = PowerMock.createMock(ResourceBundleProducer.class);
  205 +// ResourceBundle bundle = PowerMock.createMock(ResourceBundle.class);
  206 +//
  207 +// expect(LoggerProducer.create(EasyMock.anyObject(Class.class))).andReturn(logger).anyTimes();
  208 +// expect(bundleFactory.create(EasyMock.anyObject(String.class), EasyMock.anyObject(Locale.class))).andReturn(
  209 +// bundle).anyTimes();
  210 +// expect(bundle.getString(EasyMock.anyObject(String.class), EasyMock.anyObject(String.class))).andReturn("")
  211 +// .anyTimes();
  212 +//
  213 +// logger.debug(EasyMock.anyObject(String.class));
  214 +// EasyMock.expectLastCall().anyTimes();
  215 +//
  216 +// Whitebox.setInternalState(AbstractBootstrap.class, ResourceBundleProducer.class, bundleFactory);
  217 +//
  218 +// List<StartupProcessor> list = getActions(bootstrap);
  219 +// list.clear();
  220 +//
  221 +// MyProcessor<?> processor = PowerMock.createMock(MyProcessor.class);
  222 +// list.add(processor);
  223 +// expect(processor.process()).andReturn(true).times(1);
  224 +//
  225 +// PowerMock.replayAll();
  226 +// bootstrap.startup();
  227 +//
  228 +// assertTrue(list.isEmpty());
  229 +// PowerMock.verifyAll();
  230 +// }
  231 +//
  232 +// @SuppressWarnings("static-access")
  233 +// @Test
  234 +// public void testLoadTempContextsAndStartup() {
  235 +//
  236 +// StartupBootstrap bootstrap = new StartupBootstrap();
  237 +//
  238 +// bootstrap.loadTempContexts(null);
  239 +// Assert.assertFalse(Contexts.getActiveContexts().isEmpty());
  240 +//
  241 +// try {
  242 +// bootstrap.startup();
  243 +// Assert.assertTrue(Contexts.getActiveContexts().isEmpty());
  244 +// } catch (Throwable e) {
  245 +// fail();
  246 +// }
  247 +// }
  248 +//}
  249 +//
  250 +//@SuppressWarnings("rawtypes")
  251 +//class MyProcessor<T> extends StartupProcessor<T> {
  252 +//
  253 +// @SuppressWarnings("unchecked")
  254 +// public MyProcessor(AnnotatedMethod annotatedMethod, BeanManager beanManager) {
  255 +// super(annotatedMethod, beanManager);
  256 +// }
  257 +//
  258 +// @Override
  259 +// public int compareTo(final StartupProcessor<T> other) {
  260 +// return 1;
  261 +// }
  262 +// }
impl/extension/jsf/src/main/java/br/gov/frameworkdemoiselle/util/ServletContextListener.java
@@ -1,19 +0,0 @@ @@ -1,19 +0,0 @@
1 -package br.gov.frameworkdemoiselle.util;  
2 -  
3 -import javax.servlet.ServletContextEvent;  
4 -  
5 -import br.gov.frameworkdemoiselle.internal.bootstrap.ShutdownBootstrap;  
6 -import br.gov.frameworkdemoiselle.internal.bootstrap.StartupBootstrap;  
7 -  
8 -public class ServletContextListener implements javax.servlet.ServletContextListener {  
9 -  
10 - @Override  
11 - public void contextInitialized(ServletContextEvent event) {  
12 - StartupBootstrap.startup();  
13 - }  
14 -  
15 - @Override  
16 - public void contextDestroyed(ServletContextEvent event) {  
17 - ShutdownBootstrap.shutdown();  
18 - }  
19 -}  
impl/extension/servlet/src/main/java/br/gov/frameworkdemoiselle/util/ServletListener.java
@@ -38,18 +38,30 @@ package br.gov.frameworkdemoiselle.util; @@ -38,18 +38,30 @@ package br.gov.frameworkdemoiselle.util;
38 38
39 import javax.servlet.ServletContextEvent; 39 import javax.servlet.ServletContextEvent;
40 40
41 -import br.gov.frameworkdemoiselle.internal.bootstrap.ShutdownBootstrap;  
42 -import br.gov.frameworkdemoiselle.internal.bootstrap.StartupBootstrap; 41 +import br.gov.frameworkdemoiselle.internal.bootstrap.BeforeApplicationFinalization;
  42 +import br.gov.frameworkdemoiselle.internal.bootstrap.BeforeApplicationInitialization;
43 43
44 public class ServletListener implements javax.servlet.ServletContextListener { 44 public class ServletListener implements javax.servlet.ServletContextListener {
45 45
46 @Override 46 @Override
47 public void contextInitialized(ServletContextEvent event) { 47 public void contextInitialized(ServletContextEvent event) {
48 - StartupBootstrap.startup(); 48 + Beans.getBeanManager().fireEvent(new BeforeApplicationInitialization() {
  49 +
  50 + @Override
  51 + public boolean removeProcessors() {
  52 + return false;
  53 + }
  54 + });
49 } 55 }
50 56
51 @Override 57 @Override
52 public void contextDestroyed(ServletContextEvent event) { 58 public void contextDestroyed(ServletContextEvent event) {
53 - ShutdownBootstrap.shutdown(); 59 + Beans.getBeanManager().fireEvent(new BeforeApplicationFinalization() {
  60 +
  61 + @Override
  62 + public boolean removeProcessors() {
  63 + return false;
  64 + }
  65 + });
54 } 66 }
55 } 67 }