Commit 5d8915eb18885935692b2164796d7c0c60129360
1 parent
a5643bb1
Exists in
master
Bootstrap abstrato para fazer veto de estratégias de transação baseada
na prioridade
Showing
2 changed files
with
150 additions
and
0 deletions
Show diff stats
impl/core/src/main/java/br/gov/frameworkdemoiselle/annotation/Priority.java
0 → 100644
| ... | ... | @@ -0,0 +1,74 @@ |
| 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.FIELD; | |
| 40 | +import static java.lang.annotation.ElementType.METHOD; | |
| 41 | +import static java.lang.annotation.ElementType.PARAMETER; | |
| 42 | +import static java.lang.annotation.ElementType.TYPE; | |
| 43 | +import static java.lang.annotation.RetentionPolicy.RUNTIME; | |
| 44 | + | |
| 45 | +import java.lang.annotation.Inherited; | |
| 46 | +import java.lang.annotation.Retention; | |
| 47 | +import java.lang.annotation.Target; | |
| 48 | + | |
| 49 | +@Inherited | |
| 50 | +@Retention(RUNTIME) | |
| 51 | +@Target({ TYPE, FIELD, METHOD, PARAMETER }) | |
| 52 | +public @interface Priority { | |
| 53 | + | |
| 54 | + /** | |
| 55 | + * Most important priority value. | |
| 56 | + */ | |
| 57 | + public static int MAX_PRIORITY = Integer.MIN_VALUE; | |
| 58 | + | |
| 59 | + /** | |
| 60 | + * Less important priority value. | |
| 61 | + */ | |
| 62 | + public static int MIN_PRIORITY = Integer.MAX_VALUE; | |
| 63 | + | |
| 64 | + /** | |
| 65 | + * Less important priority value. | |
| 66 | + */ | |
| 67 | + public static int MID_PRIORITY = (MIN_PRIORITY - MAX_PRIORITY) / 2; | |
| 68 | + | |
| 69 | + /** | |
| 70 | + * An integer value defines method execution order (i.e., priority). | |
| 71 | + */ | |
| 72 | + int value() default MIN_PRIORITY; | |
| 73 | + | |
| 74 | +} | ... | ... |
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/AbstractTransactionBootstrap.java
0 → 100644
| ... | ... | @@ -0,0 +1,76 @@ |
| 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 javax.enterprise.event.Observes; | |
| 40 | +import javax.enterprise.inject.spi.ProcessAnnotatedType; | |
| 41 | + | |
| 42 | +import br.gov.frameworkdemoiselle.annotation.Priority; | |
| 43 | +import br.gov.frameworkdemoiselle.transaction.Transaction; | |
| 44 | +import br.gov.frameworkdemoiselle.util.Reflections; | |
| 45 | + | |
| 46 | +public abstract class AbstractTransactionBootstrap<T extends Transaction> extends AbstractBootstrap { | |
| 47 | + | |
| 48 | + private Class<T> transactionClass; | |
| 49 | + | |
| 50 | + protected <A> void processAnnotatedType(@Observes final ProcessAnnotatedType<A> event) { | |
| 51 | + Class<?> annotated = event.getAnnotatedType().getJavaClass(); | |
| 52 | + | |
| 53 | + if (Reflections.isOfType(annotated, Transaction.class) && annotated != getTransactionClass() | |
| 54 | + && getPriority(getTransactionClass()) < getPriority(annotated)) { | |
| 55 | + event.veto(); | |
| 56 | + } | |
| 57 | + } | |
| 58 | + | |
| 59 | + private int getPriority(Class<?> type) { | |
| 60 | + int priority = Priority.MAX_PRIORITY; | |
| 61 | + | |
| 62 | + if (type.isAnnotationPresent(Priority.class)) { | |
| 63 | + Priority annotation = type.getAnnotation(Priority.class); | |
| 64 | + priority = annotation.value(); | |
| 65 | + } | |
| 66 | + | |
| 67 | + return priority; | |
| 68 | + } | |
| 69 | + | |
| 70 | + protected Class<T> getTransactionClass() { | |
| 71 | + if (this.transactionClass == null) { | |
| 72 | + this.transactionClass = Reflections.getGenericTypeArgument(this.getClass(), 0); | |
| 73 | + } | |
| 74 | + return this.transactionClass; | |
| 75 | + } | |
| 76 | +} | ... | ... |