Commit a7cd5057d19ea854b583c70617f1655076b555c0
1 parent
33801f67
Exists in
master
Utilizando internamente a anotação @Priority para definir a prioridade
das estratégias de transação e controle de acesso
Showing
6 changed files
with
65 additions
and
8 deletions
Show diff stats
impl/core/src/main/java/br/gov/frameworkdemoiselle/annotation/Priority.java
| ... | ... | @@ -37,6 +37,7 @@ |
| 37 | 37 | package br.gov.frameworkdemoiselle.annotation; |
| 38 | 38 | |
| 39 | 39 | import static java.lang.annotation.ElementType.METHOD; |
| 40 | +import static java.lang.annotation.ElementType.TYPE; | |
| 40 | 41 | import static java.lang.annotation.RetentionPolicy.RUNTIME; |
| 41 | 42 | |
| 42 | 43 | import java.lang.annotation.Retention; |
| ... | ... | @@ -45,7 +46,7 @@ import java.lang.annotation.Target; |
| 45 | 46 | /** |
| 46 | 47 | * @author SERPRO |
| 47 | 48 | */ |
| 48 | -@Target(METHOD) | |
| 49 | +@Target({ TYPE, METHOD }) | |
| 49 | 50 | @Retention(RUNTIME) |
| 50 | 51 | public @interface Priority { |
| 51 | 52 | ... | ... |
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/implementation/DefaultTransaction.java
| ... | ... | @@ -36,7 +36,9 @@ |
| 36 | 36 | */ |
| 37 | 37 | package br.gov.frameworkdemoiselle.internal.implementation; |
| 38 | 38 | |
| 39 | +import static br.gov.frameworkdemoiselle.internal.implementation.StrategySelector.CORE_PRIORITY; | |
| 39 | 40 | import br.gov.frameworkdemoiselle.DemoiselleException; |
| 41 | +import br.gov.frameworkdemoiselle.annotation.Priority; | |
| 40 | 42 | import br.gov.frameworkdemoiselle.internal.producer.ResourceBundleProducer; |
| 41 | 43 | import br.gov.frameworkdemoiselle.transaction.Transaction; |
| 42 | 44 | import br.gov.frameworkdemoiselle.transaction.Transactional; |
| ... | ... | @@ -48,6 +50,7 @@ import br.gov.frameworkdemoiselle.util.ResourceBundle; |
| 48 | 50 | * @author SERPRO |
| 49 | 51 | * @see Transaction |
| 50 | 52 | */ |
| 53 | +@Priority(CORE_PRIORITY) | |
| 51 | 54 | public class DefaultTransaction implements Transaction { |
| 52 | 55 | |
| 53 | 56 | private static final long serialVersionUID = 1L; | ... | ... |
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/implementation/SecurityContextImpl.java
| ... | ... | @@ -38,6 +38,8 @@ package br.gov.frameworkdemoiselle.internal.implementation; |
| 38 | 38 | |
| 39 | 39 | import javax.inject.Named; |
| 40 | 40 | |
| 41 | +import br.gov.frameworkdemoiselle.internal.bootstrap.AuthenticatorBootstrap; | |
| 42 | +import br.gov.frameworkdemoiselle.internal.bootstrap.AuthorizerBootstrap; | |
| 41 | 43 | import br.gov.frameworkdemoiselle.internal.configuration.SecurityConfig; |
| 42 | 44 | import br.gov.frameworkdemoiselle.internal.producer.ResourceBundleProducer; |
| 43 | 45 | import br.gov.frameworkdemoiselle.security.AfterLoginSuccessful; |
| ... | ... | @@ -66,8 +68,8 @@ public class SecurityContextImpl implements SecurityContext { |
| 66 | 68 | |
| 67 | 69 | private Authenticator getAuthenticator() { |
| 68 | 70 | if (authenticator == null) { |
| 69 | - authenticator = StrategySelector.getReference("frameworkdemoiselle.security.authenticator.class", | |
| 70 | - Authenticator.class, DefaultAuthenticator.class); | |
| 71 | + AuthenticatorBootstrap bootstrap = Beans.getReference(AuthenticatorBootstrap.class); | |
| 72 | + authenticator = StrategySelector.getPriorityReference(bootstrap.getCache()); | |
| 71 | 73 | } |
| 72 | 74 | |
| 73 | 75 | return authenticator; |
| ... | ... | @@ -75,8 +77,8 @@ public class SecurityContextImpl implements SecurityContext { |
| 75 | 77 | |
| 76 | 78 | private Authorizer getAuthorizer() { |
| 77 | 79 | if (authorizer == null) { |
| 78 | - authorizer = StrategySelector.getReference("frameworkdemoiselle.security.authorizer.class", | |
| 79 | - Authorizer.class, DefaultAuthorizer.class); | |
| 80 | + AuthorizerBootstrap bootstrap = Beans.getReference(AuthorizerBootstrap.class); | |
| 81 | + authorizer = StrategySelector.getPriorityReference(bootstrap.getCache()); | |
| 80 | 82 | } |
| 81 | 83 | |
| 82 | 84 | return authorizer; | ... | ... |
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/implementation/StrategySelector.java
| ... | ... | @@ -36,14 +36,18 @@ |
| 36 | 36 | */ |
| 37 | 37 | package br.gov.frameworkdemoiselle.internal.implementation; |
| 38 | 38 | |
| 39 | +import static br.gov.frameworkdemoiselle.annotation.Priority.MIN_PRIORITY; | |
| 40 | + | |
| 39 | 41 | import java.io.FileNotFoundException; |
| 40 | 42 | import java.io.Serializable; |
| 41 | 43 | import java.net.URL; |
| 44 | +import java.util.List; | |
| 42 | 45 | import java.util.Locale; |
| 43 | 46 | |
| 44 | 47 | import org.apache.commons.configuration.Configuration; |
| 45 | 48 | import org.apache.commons.configuration.PropertiesConfiguration; |
| 46 | 49 | |
| 50 | +import br.gov.frameworkdemoiselle.annotation.Priority; | |
| 47 | 51 | import br.gov.frameworkdemoiselle.configuration.ConfigurationException; |
| 48 | 52 | import br.gov.frameworkdemoiselle.internal.configuration.ConfigurationLoader; |
| 49 | 53 | import br.gov.frameworkdemoiselle.internal.producer.ResourceBundleProducer; |
| ... | ... | @@ -53,11 +57,43 @@ import br.gov.frameworkdemoiselle.util.Strings; |
| 53 | 57 | |
| 54 | 58 | public final class StrategySelector implements Serializable { |
| 55 | 59 | |
| 60 | + public static final int CORE_PRIORITY = MIN_PRIORITY; | |
| 61 | + | |
| 62 | + public static final int EXTENSIONS_PRIORITY = CORE_PRIORITY + 100; | |
| 63 | + | |
| 64 | + public static final int COMPONENTS_PRIORITY = EXTENSIONS_PRIORITY + 100; | |
| 65 | + | |
| 56 | 66 | private static final long serialVersionUID = 1L; |
| 57 | 67 | |
| 58 | 68 | private StrategySelector() { |
| 59 | 69 | } |
| 60 | 70 | |
| 71 | + public static <T> T getPriorityReference(List<Class<T>> options) { | |
| 72 | + Class<T> selected = null; | |
| 73 | + | |
| 74 | + for (Class<T> option : options) { | |
| 75 | + if (selected == null || getPriority(option) < getPriority(selected)) { | |
| 76 | + selected = option; | |
| 77 | + } | |
| 78 | + | |
| 79 | + System.out.println(option.getCanonicalName()); | |
| 80 | + System.out.println(selected.getCanonicalName()); | |
| 81 | + } | |
| 82 | + | |
| 83 | + return Beans.getReference(selected); | |
| 84 | + } | |
| 85 | + | |
| 86 | + private static <T> int getPriority(Class<T> type) { | |
| 87 | + int result = Priority.MAX_PRIORITY; | |
| 88 | + Priority priority = type.getAnnotation(Priority.class); | |
| 89 | + | |
| 90 | + if (priority != null) { | |
| 91 | + result = priority.value(); | |
| 92 | + } | |
| 93 | + | |
| 94 | + return result; | |
| 95 | + } | |
| 96 | + | |
| 61 | 97 | public static <T> T getReference(String configKey, Class<T> type, Class<? extends T> defaultType) { |
| 62 | 98 | Class<T> selectedType = loadSelected(configKey, type, defaultType); |
| 63 | 99 | return Beans.getReference(selectedType); | ... | ... |
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/implementation/TransactionContextImpl.java
| ... | ... | @@ -38,8 +38,10 @@ package br.gov.frameworkdemoiselle.internal.implementation; |
| 38 | 38 | |
| 39 | 39 | import javax.inject.Named; |
| 40 | 40 | |
| 41 | +import br.gov.frameworkdemoiselle.internal.bootstrap.TransactionBootstrap; | |
| 41 | 42 | import br.gov.frameworkdemoiselle.transaction.Transaction; |
| 42 | 43 | import br.gov.frameworkdemoiselle.transaction.TransactionContext; |
| 44 | +import br.gov.frameworkdemoiselle.util.Beans; | |
| 43 | 45 | |
| 44 | 46 | /** |
| 45 | 47 | * This is the default implementation of {@link TransactionContext} interface. |
| ... | ... | @@ -51,10 +53,19 @@ public class TransactionContextImpl implements TransactionContext { |
| 51 | 53 | |
| 52 | 54 | private static final long serialVersionUID = 1L; |
| 53 | 55 | |
| 56 | + private Transaction transaction; | |
| 57 | + | |
| 58 | + private Transaction getTransaction() { | |
| 59 | + if (this.transaction == null) { | |
| 60 | + TransactionBootstrap bootstrap = Beans.getReference(TransactionBootstrap.class); | |
| 61 | + this.transaction = StrategySelector.getPriorityReference(bootstrap.getCache()); | |
| 62 | + } | |
| 63 | + | |
| 64 | + return this.transaction; | |
| 65 | + } | |
| 66 | + | |
| 54 | 67 | @Override |
| 55 | 68 | public Transaction getCurrentTransaction() { |
| 56 | - return StrategySelector.getReference("frameworkdemoiselle.transaction.class", Transaction.class, | |
| 57 | - DefaultTransaction.class); | |
| 69 | + return getTransaction(); | |
| 58 | 70 | } |
| 59 | - | |
| 60 | 71 | } | ... | ... |
impl/extension/jpa/src/main/java/br/gov/frameworkdemoiselle/transaction/JPATransaction.java
| ... | ... | @@ -36,11 +36,14 @@ |
| 36 | 36 | */ |
| 37 | 37 | package br.gov.frameworkdemoiselle.transaction; |
| 38 | 38 | |
| 39 | +import static br.gov.frameworkdemoiselle.internal.implementation.StrategySelector.EXTENSIONS_PRIORITY; | |
| 40 | + | |
| 39 | 41 | import java.util.Collection; |
| 40 | 42 | |
| 41 | 43 | import javax.persistence.EntityManager; |
| 42 | 44 | import javax.persistence.EntityTransaction; |
| 43 | 45 | |
| 46 | +import br.gov.frameworkdemoiselle.annotation.Priority; | |
| 44 | 47 | import br.gov.frameworkdemoiselle.internal.producer.EntityManagerProducer; |
| 45 | 48 | import br.gov.frameworkdemoiselle.util.Beans; |
| 46 | 49 | |
| ... | ... | @@ -50,6 +53,7 @@ import br.gov.frameworkdemoiselle.util.Beans; |
| 50 | 53 | * @author SERPRO |
| 51 | 54 | * @see Transaction |
| 52 | 55 | */ |
| 56 | +@Priority(EXTENSIONS_PRIORITY) | |
| 53 | 57 | public class JPATransaction implements Transaction { |
| 54 | 58 | |
| 55 | 59 | private static final long serialVersionUID = 1L; | ... | ... |