diff --git a/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/AbstractLifecycleBootstrap.java b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/AbstractLifecycleBootstrap.java index c23778f..aa07123 100644 --- a/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/AbstractLifecycleBootstrap.java +++ b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/AbstractLifecycleBootstrap.java @@ -49,7 +49,6 @@ import javax.enterprise.event.Observes; import javax.enterprise.inject.spi.AfterBeanDiscovery; import javax.enterprise.inject.spi.AnnotatedMethod; import javax.enterprise.inject.spi.AnnotatedType; -import javax.enterprise.inject.spi.BeanManager; import javax.enterprise.inject.spi.ProcessAnnotatedType; import br.gov.frameworkdemoiselle.DemoiselleException; @@ -57,7 +56,7 @@ import br.gov.frameworkdemoiselle.annotation.ViewScoped; import br.gov.frameworkdemoiselle.internal.configuration.ConfigurationLoader; import br.gov.frameworkdemoiselle.internal.context.CustomContext; import br.gov.frameworkdemoiselle.internal.context.ThreadLocalContext; -import br.gov.frameworkdemoiselle.internal.processor.AnnotatedMethodProcessor; +import br.gov.frameworkdemoiselle.internal.implementation.AnnotatedMethodProcessor; import br.gov.frameworkdemoiselle.util.Reflections; public abstract class AbstractLifecycleBootstrap extends AbstractBootstrap { @@ -74,8 +73,11 @@ public abstract class AbstractLifecycleBootstrap extends A private boolean registered = false; - protected abstract AnnotatedMethodProcessor newProcessorInstance(AnnotatedMethod annotatedMethod, - BeanManager beanManager); + // protected abstract AnnotatedMethodProcessor newProcessorInstance(AnnotatedMethod annotatedMethod); + + protected AnnotatedMethodProcessor newProcessorInstance(AnnotatedMethod annotatedMethod) { + return new AnnotatedMethodProcessor(annotatedMethod); + } protected Class getAnnotationClass() { if (this.annotationClass == null) { @@ -85,14 +87,14 @@ public abstract class AbstractLifecycleBootstrap extends A return this.annotationClass; } - public void processAnnotatedType(@Observes final ProcessAnnotatedType event, final BeanManager beanManager) { + public void processAnnotatedType(@Observes final ProcessAnnotatedType event) { final AnnotatedType annotatedType = event.getAnnotatedType(); for (AnnotatedMethod am : annotatedType.getMethods()) { if (am.isAnnotationPresent(getAnnotationClass())) { @SuppressWarnings("unchecked") AnnotatedMethod annotatedMethod = (AnnotatedMethod) am; - processors.add(newProcessorInstance(annotatedMethod, beanManager)); + processors.add(newProcessorInstance(annotatedMethod)); } } } diff --git a/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/ShutdownBootstrap.java b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/ShutdownBootstrap.java index 89641da..fbc39cd 100644 --- a/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/ShutdownBootstrap.java +++ b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/ShutdownBootstrap.java @@ -37,25 +37,16 @@ package br.gov.frameworkdemoiselle.internal.bootstrap; import javax.enterprise.event.Observes; -import javax.enterprise.inject.spi.AnnotatedMethod; -import javax.enterprise.inject.spi.BeanManager; -import br.gov.frameworkdemoiselle.annotation.Shutdown; -import br.gov.frameworkdemoiselle.internal.processor.AnnotatedMethodProcessor; -import br.gov.frameworkdemoiselle.internal.processor.ShutdownProcessor; +import br.gov.frameworkdemoiselle.lifecycle.AfterShutdownProccess; +import br.gov.frameworkdemoiselle.lifecycle.Shutdown; /** * This class run at application shutdown */ public class ShutdownBootstrap extends AbstractLifecycleBootstrap { - @Override - protected AnnotatedMethodProcessor newProcessorInstance(AnnotatedMethod annotatedMethod, - BeanManager beanManager) { - return new ShutdownProcessor(annotatedMethod, beanManager); - } - - public void shutdown(@Observes BeforeApplicationFinalization event) { + public void shutdown(@Observes AfterShutdownProccess event) { proccessEvent(); } } diff --git a/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/StartupBootstrap.java b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/StartupBootstrap.java index f2278ba..c658ede 100644 --- a/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/StartupBootstrap.java +++ b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/StartupBootstrap.java @@ -37,25 +37,16 @@ package br.gov.frameworkdemoiselle.internal.bootstrap; import javax.enterprise.event.Observes; -import javax.enterprise.inject.spi.AnnotatedMethod; -import javax.enterprise.inject.spi.BeanManager; -import br.gov.frameworkdemoiselle.annotation.Startup; -import br.gov.frameworkdemoiselle.internal.processor.AnnotatedMethodProcessor; -import br.gov.frameworkdemoiselle.internal.processor.StartupProcessor; +import br.gov.frameworkdemoiselle.lifecycle.AfterStartupProccess; +import br.gov.frameworkdemoiselle.lifecycle.Startup; /** * This class is the bootstrap to execute the processes at load time. */ public class StartupBootstrap extends AbstractLifecycleBootstrap { - @Override - protected AnnotatedMethodProcessor newProcessorInstance(AnnotatedMethod annotatedMethod, - BeanManager beanManager) { - return new StartupProcessor(annotatedMethod, beanManager); - } - - public void startup(@Observes BeforeApplicationInitialization event) { + public void startup(@Observes AfterStartupProccess event) { proccessEvent(); } } diff --git a/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/implementation/AnnotatedMethodProcessor.java b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/implementation/AnnotatedMethodProcessor.java new file mode 100644 index 0000000..5f99e08 --- /dev/null +++ b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/implementation/AnnotatedMethodProcessor.java @@ -0,0 +1,152 @@ +/* + * Demoiselle Framework + * Copyright (C) 2010 SERPRO + * ---------------------------------------------------------------------------- + * This file is part of Demoiselle Framework. + * + * Demoiselle Framework is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License version 3 + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License version 3 + * along with this program; if not, see + * or write to the Free Software Foundation, Inc., 51 Franklin Street, + * Fifth Floor, Boston, MA 02110-1301, USA. + * ---------------------------------------------------------------------------- + * Este arquivo é parte do Framework Demoiselle. + * + * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou + * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação + * do Software Livre (FSF). + * + * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA + * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou + * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português + * para maiores detalhes. + * + * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título + * "LICENCA.txt", junto com esse programa. Se não, acesse + * ou escreva para a Fundação do Software Livre (FSF) Inc., + * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. + */ +package br.gov.frameworkdemoiselle.internal.implementation; + +import java.lang.reflect.InvocationTargetException; +import java.util.Locale; + +import javax.enterprise.inject.spi.AnnotatedMethod; + +import org.slf4j.Logger; + +import br.gov.frameworkdemoiselle.annotation.Priority; +import br.gov.frameworkdemoiselle.exception.ApplicationException; +import br.gov.frameworkdemoiselle.internal.producer.LoggerProducer; +import br.gov.frameworkdemoiselle.internal.producer.ResourceBundleProducer; +import br.gov.frameworkdemoiselle.message.SeverityType; +import br.gov.frameworkdemoiselle.util.Beans; +import br.gov.frameworkdemoiselle.util.ResourceBundle; + +/** + * Represents an annotated method to be processed; + * + * @param + * declaring class owner of the method + */ +public class AnnotatedMethodProcessor implements Comparable> { + + private AnnotatedMethod annotatedMethod; + + private ResourceBundle bundle; + + public AnnotatedMethodProcessor(final AnnotatedMethod annotatedMethod) { + this.annotatedMethod = annotatedMethod; + } + + public AnnotatedMethod getAnnotatedMethod() { + return this.annotatedMethod; + } + + @SuppressWarnings("unchecked") + protected T getReferencedBean() { + Class classType = (Class) getAnnotatedMethod().getJavaMember().getDeclaringClass(); + + return Beans.getReference(classType); + } + + public int compareTo(final AnnotatedMethodProcessor other) { + Integer orderThis = getPriority(getAnnotatedMethod()); + Integer orderOther = getPriority(other.getAnnotatedMethod()); + + return orderThis.compareTo(orderOther); + } + + public boolean process(Object... args) throws Throwable { + getLogger().info(getBundle().getString("processing", getAnnotatedMethod().getJavaMember().toGenericString())); + + try { + getAnnotatedMethod().getJavaMember().invoke(getReferencedBean(), args); + + } catch (InvocationTargetException cause) { + handleException(cause.getCause()); + } + + return true; + } + + private void handleException(Throwable cause) throws Throwable { + ApplicationException ann = cause.getClass().getAnnotation(ApplicationException.class); + + if (ann == null || SeverityType.FATAL == ann.severity()) { + throw cause; + + } else { + switch (ann.severity()) { + case INFO: + getLogger().info(cause.getMessage()); + break; + + case WARN: + getLogger().warn(cause.getMessage()); + break; + + default: + getLogger().error(getBundle().getString("processing-fail"), cause); + break; + } + } + } + + private static Integer getPriority(AnnotatedMethod annotatedMethod) { + Integer priority = Priority.MIN_PRIORITY; + + Priority annotation = annotatedMethod.getAnnotation(Priority.class); + if (annotation != null) { + priority = annotation.value(); + } + + return priority; + } + + // @Override + // public String toString() { + // return getBundle().getString("for", getClass().getSimpleName(), + // getAnnotatedMethod().getJavaMember().toGenericString()); + // } + + protected ResourceBundle getBundle() { + if (this.bundle == null) { + this.bundle = ResourceBundleProducer.create("demoiselle-core-bundle", Locale.getDefault()); + } + + return bundle; + } + + protected Logger getLogger() { + return LoggerProducer.create(this.getClass()); + } +} diff --git a/impl/core/src/test/java/br/gov/frameworkdemoiselle/internal/bootstrap/MyStartupAnnotatedClass.java b/impl/core/src/test/java/br/gov/frameworkdemoiselle/internal/bootstrap/MyStartupAnnotatedClass.java index 210a1f0..582b5a5 100644 --- a/impl/core/src/test/java/br/gov/frameworkdemoiselle/internal/bootstrap/MyStartupAnnotatedClass.java +++ b/impl/core/src/test/java/br/gov/frameworkdemoiselle/internal/bootstrap/MyStartupAnnotatedClass.java @@ -38,23 +38,27 @@ package br.gov.frameworkdemoiselle.internal.bootstrap; import java.util.Stack; -import br.gov.frameworkdemoiselle.annotation.Startup; +import br.gov.frameworkdemoiselle.annotation.Priority; +import br.gov.frameworkdemoiselle.lifecycle.Startup; public class MyStartupAnnotatedClass { public static Stack stackOfMethods = new Stack(); - @Startup(priority=-1) + @Startup + @Priority(1) public void startMethod1(){ addMethodExecuted("startMethod1"); } - @Startup(priority=1) + @Startup + @Priority(1) public void startMethod2(){ addMethodExecuted("startMethod2"); } - @Startup(priority=0) + @Startup + @Priority(0) public void startMethod3(){ addMethodExecuted("startMethod3"); } diff --git a/impl/core/src/uml/lifecycle-shutdown.ucls b/impl/core/src/uml/lifecycle-shutdown.ucls index d8d9429..1dd2e42 100644 --- a/impl/core/src/uml/lifecycle-shutdown.ucls +++ b/impl/core/src/uml/lifecycle-shutdown.ucls @@ -1,6 +1,6 @@ - diff --git a/impl/core/src/uml/lifecycle-startup.ucls b/impl/core/src/uml/lifecycle-startup.ucls index 3790d92..dd91db3 100644 --- a/impl/core/src/uml/lifecycle-startup.ucls +++ b/impl/core/src/uml/lifecycle-startup.ucls @@ -1,6 +1,6 @@ - diff --git a/impl/extension/servlet/src/main/java/br/gov/frameworkdemoiselle/util/ServletListener.java b/impl/extension/servlet/src/main/java/br/gov/frameworkdemoiselle/util/ServletListener.java index 1788e40..911cf99 100644 --- a/impl/extension/servlet/src/main/java/br/gov/frameworkdemoiselle/util/ServletListener.java +++ b/impl/extension/servlet/src/main/java/br/gov/frameworkdemoiselle/util/ServletListener.java @@ -38,20 +38,20 @@ package br.gov.frameworkdemoiselle.util; import javax.servlet.ServletContextEvent; -import br.gov.frameworkdemoiselle.internal.bootstrap.BeforeApplicationFinalization; -import br.gov.frameworkdemoiselle.internal.bootstrap.BeforeApplicationInitialization; +import br.gov.frameworkdemoiselle.lifecycle.AfterShutdownProccess; +import br.gov.frameworkdemoiselle.lifecycle.AfterStartupProccess; public class ServletListener implements javax.servlet.ServletContextListener { @Override public void contextInitialized(ServletContextEvent event) { - Beans.getBeanManager().fireEvent(new BeforeApplicationInitialization() { + Beans.getBeanManager().fireEvent(new AfterStartupProccess() { }); } @Override public void contextDestroyed(ServletContextEvent event) { - Beans.getBeanManager().fireEvent(new BeforeApplicationFinalization() { + Beans.getBeanManager().fireEvent(new AfterShutdownProccess() { }); } } -- libgit2 0.21.2