Commit 8d24c80af7ce32dfcd3810da463a722a5389cacf

Authored by Cleverson Sacramento
1 parent a7cd5057
Exists in master

Melhoria na implementação do processo de Startup e Shutdown com base nos

novos eventos expostos externamente
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/AbstractLifecycleBootstrap.java
... ... @@ -49,7 +49,6 @@ import javax.enterprise.event.Observes;
49 49 import javax.enterprise.inject.spi.AfterBeanDiscovery;
50 50 import javax.enterprise.inject.spi.AnnotatedMethod;
51 51 import javax.enterprise.inject.spi.AnnotatedType;
52   -import javax.enterprise.inject.spi.BeanManager;
53 52 import javax.enterprise.inject.spi.ProcessAnnotatedType;
54 53  
55 54 import br.gov.frameworkdemoiselle.DemoiselleException;
... ... @@ -57,7 +56,7 @@ import br.gov.frameworkdemoiselle.annotation.ViewScoped;
57 56 import br.gov.frameworkdemoiselle.internal.configuration.ConfigurationLoader;
58 57 import br.gov.frameworkdemoiselle.internal.context.CustomContext;
59 58 import br.gov.frameworkdemoiselle.internal.context.ThreadLocalContext;
60   -import br.gov.frameworkdemoiselle.internal.processor.AnnotatedMethodProcessor;
  59 +import br.gov.frameworkdemoiselle.internal.implementation.AnnotatedMethodProcessor;
61 60 import br.gov.frameworkdemoiselle.util.Reflections;
62 61  
63 62 public abstract class AbstractLifecycleBootstrap<A extends Annotation> extends AbstractBootstrap {
... ... @@ -74,8 +73,11 @@ public abstract class AbstractLifecycleBootstrap&lt;A extends Annotation&gt; extends A
74 73  
75 74 private boolean registered = false;
76 75  
77   - protected abstract <T> AnnotatedMethodProcessor<T> newProcessorInstance(AnnotatedMethod<T> annotatedMethod,
78   - BeanManager beanManager);
  76 + // protected abstract <T> AnnotatedMethodProcessor<T> newProcessorInstance(AnnotatedMethod<T> annotatedMethod);
  77 +
  78 + protected <T> AnnotatedMethodProcessor<T> newProcessorInstance(AnnotatedMethod<T> annotatedMethod) {
  79 + return new AnnotatedMethodProcessor<T>(annotatedMethod);
  80 + }
79 81  
80 82 protected Class<A> getAnnotationClass() {
81 83 if (this.annotationClass == null) {
... ... @@ -85,14 +87,14 @@ public abstract class AbstractLifecycleBootstrap&lt;A extends Annotation&gt; extends A
85 87 return this.annotationClass;
86 88 }
87 89  
88   - public <T> void processAnnotatedType(@Observes final ProcessAnnotatedType<T> event, final BeanManager beanManager) {
  90 + public <T> void processAnnotatedType(@Observes final ProcessAnnotatedType<T> event) {
89 91 final AnnotatedType<T> annotatedType = event.getAnnotatedType();
90 92  
91 93 for (AnnotatedMethod<?> am : annotatedType.getMethods()) {
92 94 if (am.isAnnotationPresent(getAnnotationClass())) {
93 95 @SuppressWarnings("unchecked")
94 96 AnnotatedMethod<T> annotatedMethod = (AnnotatedMethod<T>) am;
95   - processors.add(newProcessorInstance(annotatedMethod, beanManager));
  97 + processors.add(newProcessorInstance(annotatedMethod));
96 98 }
97 99 }
98 100 }
... ...
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/ShutdownBootstrap.java
... ... @@ -37,25 +37,16 @@
37 37 package br.gov.frameworkdemoiselle.internal.bootstrap;
38 38  
39 39 import javax.enterprise.event.Observes;
40   -import javax.enterprise.inject.spi.AnnotatedMethod;
41   -import javax.enterprise.inject.spi.BeanManager;
42 40  
43   -import br.gov.frameworkdemoiselle.annotation.Shutdown;
44   -import br.gov.frameworkdemoiselle.internal.processor.AnnotatedMethodProcessor;
45   -import br.gov.frameworkdemoiselle.internal.processor.ShutdownProcessor;
  41 +import br.gov.frameworkdemoiselle.lifecycle.AfterShutdownProccess;
  42 +import br.gov.frameworkdemoiselle.lifecycle.Shutdown;
46 43  
47 44 /**
48 45 * This class run at application shutdown
49 46 */
50 47 public class ShutdownBootstrap extends AbstractLifecycleBootstrap<Shutdown> {
51 48  
52   - @Override
53   - protected <T> AnnotatedMethodProcessor<T> newProcessorInstance(AnnotatedMethod<T> annotatedMethod,
54   - BeanManager beanManager) {
55   - return new ShutdownProcessor<T>(annotatedMethod, beanManager);
56   - }
57   -
58   - public void shutdown(@Observes BeforeApplicationFinalization event) {
  49 + public void shutdown(@Observes AfterShutdownProccess event) {
59 50 proccessEvent();
60 51 }
61 52 }
... ...
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/StartupBootstrap.java
... ... @@ -37,25 +37,16 @@
37 37 package br.gov.frameworkdemoiselle.internal.bootstrap;
38 38  
39 39 import javax.enterprise.event.Observes;
40   -import javax.enterprise.inject.spi.AnnotatedMethod;
41   -import javax.enterprise.inject.spi.BeanManager;
42 40  
43   -import br.gov.frameworkdemoiselle.annotation.Startup;
44   -import br.gov.frameworkdemoiselle.internal.processor.AnnotatedMethodProcessor;
45   -import br.gov.frameworkdemoiselle.internal.processor.StartupProcessor;
  41 +import br.gov.frameworkdemoiselle.lifecycle.AfterStartupProccess;
  42 +import br.gov.frameworkdemoiselle.lifecycle.Startup;
46 43  
47 44 /**
48 45 * This class is the bootstrap to execute the processes at load time.
49 46 */
50 47 public class StartupBootstrap extends AbstractLifecycleBootstrap<Startup> {
51 48  
52   - @Override
53   - protected <T> AnnotatedMethodProcessor<T> newProcessorInstance(AnnotatedMethod<T> annotatedMethod,
54   - BeanManager beanManager) {
55   - return new StartupProcessor<T>(annotatedMethod, beanManager);
56   - }
57   -
58   - public void startup(@Observes BeforeApplicationInitialization event) {
  49 + public void startup(@Observes AfterStartupProccess event) {
59 50 proccessEvent();
60 51 }
61 52 }
... ...
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/implementation/AnnotatedMethodProcessor.java 0 → 100644
... ... @@ -0,0 +1,152 @@
  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.implementation;
  38 +
  39 +import java.lang.reflect.InvocationTargetException;
  40 +import java.util.Locale;
  41 +
  42 +import javax.enterprise.inject.spi.AnnotatedMethod;
  43 +
  44 +import org.slf4j.Logger;
  45 +
  46 +import br.gov.frameworkdemoiselle.annotation.Priority;
  47 +import br.gov.frameworkdemoiselle.exception.ApplicationException;
  48 +import br.gov.frameworkdemoiselle.internal.producer.LoggerProducer;
  49 +import br.gov.frameworkdemoiselle.internal.producer.ResourceBundleProducer;
  50 +import br.gov.frameworkdemoiselle.message.SeverityType;
  51 +import br.gov.frameworkdemoiselle.util.Beans;
  52 +import br.gov.frameworkdemoiselle.util.ResourceBundle;
  53 +
  54 +/**
  55 + * Represents an annotated method to be processed;
  56 + *
  57 + * @param <T>
  58 + * declaring class owner of the method
  59 + */
  60 +public class AnnotatedMethodProcessor<T> implements Comparable<AnnotatedMethodProcessor<T>> {
  61 +
  62 + private AnnotatedMethod<T> annotatedMethod;
  63 +
  64 + private ResourceBundle bundle;
  65 +
  66 + public AnnotatedMethodProcessor(final AnnotatedMethod<T> annotatedMethod) {
  67 + this.annotatedMethod = annotatedMethod;
  68 + }
  69 +
  70 + public AnnotatedMethod<T> getAnnotatedMethod() {
  71 + return this.annotatedMethod;
  72 + }
  73 +
  74 + @SuppressWarnings("unchecked")
  75 + protected T getReferencedBean() {
  76 + Class<T> classType = (Class<T>) getAnnotatedMethod().getJavaMember().getDeclaringClass();
  77 +
  78 + return Beans.getReference(classType);
  79 + }
  80 +
  81 + public int compareTo(final AnnotatedMethodProcessor<T> other) {
  82 + Integer orderThis = getPriority(getAnnotatedMethod());
  83 + Integer orderOther = getPriority(other.getAnnotatedMethod());
  84 +
  85 + return orderThis.compareTo(orderOther);
  86 + }
  87 +
  88 + public boolean process(Object... args) throws Throwable {
  89 + getLogger().info(getBundle().getString("processing", getAnnotatedMethod().getJavaMember().toGenericString()));
  90 +
  91 + try {
  92 + getAnnotatedMethod().getJavaMember().invoke(getReferencedBean(), args);
  93 +
  94 + } catch (InvocationTargetException cause) {
  95 + handleException(cause.getCause());
  96 + }
  97 +
  98 + return true;
  99 + }
  100 +
  101 + private void handleException(Throwable cause) throws Throwable {
  102 + ApplicationException ann = cause.getClass().getAnnotation(ApplicationException.class);
  103 +
  104 + if (ann == null || SeverityType.FATAL == ann.severity()) {
  105 + throw cause;
  106 +
  107 + } else {
  108 + switch (ann.severity()) {
  109 + case INFO:
  110 + getLogger().info(cause.getMessage());
  111 + break;
  112 +
  113 + case WARN:
  114 + getLogger().warn(cause.getMessage());
  115 + break;
  116 +
  117 + default:
  118 + getLogger().error(getBundle().getString("processing-fail"), cause);
  119 + break;
  120 + }
  121 + }
  122 + }
  123 +
  124 + private static <T> Integer getPriority(AnnotatedMethod<T> annotatedMethod) {
  125 + Integer priority = Priority.MIN_PRIORITY;
  126 +
  127 + Priority annotation = annotatedMethod.getAnnotation(Priority.class);
  128 + if (annotation != null) {
  129 + priority = annotation.value();
  130 + }
  131 +
  132 + return priority;
  133 + }
  134 +
  135 + // @Override
  136 + // public String toString() {
  137 + // return getBundle().getString("for", getClass().getSimpleName(),
  138 + // getAnnotatedMethod().getJavaMember().toGenericString());
  139 + // }
  140 +
  141 + protected ResourceBundle getBundle() {
  142 + if (this.bundle == null) {
  143 + this.bundle = ResourceBundleProducer.create("demoiselle-core-bundle", Locale.getDefault());
  144 + }
  145 +
  146 + return bundle;
  147 + }
  148 +
  149 + protected Logger getLogger() {
  150 + return LoggerProducer.create(this.getClass());
  151 + }
  152 +}
... ...
impl/core/src/test/java/br/gov/frameworkdemoiselle/internal/bootstrap/MyStartupAnnotatedClass.java
... ... @@ -38,23 +38,27 @@ package br.gov.frameworkdemoiselle.internal.bootstrap;
38 38  
39 39 import java.util.Stack;
40 40  
41   -import br.gov.frameworkdemoiselle.annotation.Startup;
  41 +import br.gov.frameworkdemoiselle.annotation.Priority;
  42 +import br.gov.frameworkdemoiselle.lifecycle.Startup;
42 43  
43 44 public class MyStartupAnnotatedClass {
44 45  
45 46 public static Stack<String> stackOfMethods = new Stack<String>();
46 47  
47   - @Startup(priority=-1)
  48 + @Startup
  49 + @Priority(1)
48 50 public void startMethod1(){
49 51 addMethodExecuted("startMethod1");
50 52 }
51 53  
52   - @Startup(priority=1)
  54 + @Startup
  55 + @Priority(1)
53 56 public void startMethod2(){
54 57 addMethodExecuted("startMethod2");
55 58 }
56 59  
57   - @Startup(priority=0)
  60 + @Startup
  61 + @Priority(0)
58 62 public void startMethod3(){
59 63 addMethodExecuted("startMethod3");
60 64 }
... ...
impl/core/src/uml/lifecycle-shutdown.ucls
1 1 <class-diagram version="1.0.3" icons="true" automaticImage="PNG" always-add-relationships="true" generalizations="true"
2 2 realizations="true" associations="false" dependencies="true" nesting-relationships="true">
3   - <annotation id="1" corner="BOTTOM_RIGHT" language="java" name="br.gov.frameworkdemoiselle.annotation.Shutdown"
  3 + <annotation id="1" corner="BOTTOM_RIGHT" language="java" name="br.gov.frameworkdemoiselle.lifecycle.Shutdown"
4 4 project="demoiselle-core" file="/demoiselle-core/src/main/java/br/gov/frameworkdemoiselle/annotation/Shutdown.java"
5 5 binary="false">
6 6 <position height="-1" width="-1" x="230" y="252"/>
... ...
impl/core/src/uml/lifecycle-startup.ucls
1 1 <class-diagram version="1.0.3" icons="true" automaticImage="PNG" always-add-relationships="true" generalizations="true"
2 2 realizations="true" associations="true" dependencies="true" nesting-relationships="true">
3   - <annotation id="1" corner="BOTTOM_RIGHT" language="java" name="br.gov.frameworkdemoiselle.annotation.Startup"
  3 + <annotation id="1" corner="BOTTOM_RIGHT" language="java" name="br.gov.frameworkdemoiselle.lifecycle.Startup"
4 4 project="demoiselle-core" file="/demoiselle-core/src/main/java/br/gov/frameworkdemoiselle/annotation/Startup.java"
5 5 binary="false">
6 6 <position height="-1" width="-1" x="301" y="232"/>
... ...
impl/extension/servlet/src/main/java/br/gov/frameworkdemoiselle/util/ServletListener.java
... ... @@ -38,20 +38,20 @@ package br.gov.frameworkdemoiselle.util;
38 38  
39 39 import javax.servlet.ServletContextEvent;
40 40  
41   -import br.gov.frameworkdemoiselle.internal.bootstrap.BeforeApplicationFinalization;
42   -import br.gov.frameworkdemoiselle.internal.bootstrap.BeforeApplicationInitialization;
  41 +import br.gov.frameworkdemoiselle.lifecycle.AfterShutdownProccess;
  42 +import br.gov.frameworkdemoiselle.lifecycle.AfterStartupProccess;
43 43  
44 44 public class ServletListener implements javax.servlet.ServletContextListener {
45 45  
46 46 @Override
47 47 public void contextInitialized(ServletContextEvent event) {
48   - Beans.getBeanManager().fireEvent(new BeforeApplicationInitialization() {
  48 + Beans.getBeanManager().fireEvent(new AfterStartupProccess() {
49 49 });
50 50 }
51 51  
52 52 @Override
53 53 public void contextDestroyed(ServletContextEvent event) {
54   - Beans.getBeanManager().fireEvent(new BeforeApplicationFinalization() {
  54 + Beans.getBeanManager().fireEvent(new AfterShutdownProccess() {
55 55 });
56 56 }
57 57 }
... ...