Commit acb23c1755498df7860fdd390223bfe63c6a0982
1 parent
2b4b2250
Exists in
master
Ajustes no produtor de Transaction
Showing
9 changed files
with
55 additions
and
45 deletions
Show diff stats
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/AbstractStrategyBootstrap.java
| ... | ... | @@ -54,7 +54,7 @@ import br.gov.frameworkdemoiselle.util.Strings; |
| 54 | 54 | public abstract class AbstractStrategyBootstrap<T, D extends T> extends AbstractBootstrap { |
| 55 | 55 | |
| 56 | 56 | private Class<T> type; |
| 57 | - | |
| 57 | + | |
| 58 | 58 | private Class<D> defaultClass; |
| 59 | 59 | |
| 60 | 60 | private Class<? extends T> selected; |
| ... | ... | @@ -63,15 +63,15 @@ public abstract class AbstractStrategyBootstrap<T, D extends T> extends Abstract |
| 63 | 63 | if (this.type == null) { |
| 64 | 64 | this.type = Reflections.getGenericTypeArgument(this.getClass(), 0); |
| 65 | 65 | } |
| 66 | - | |
| 66 | + | |
| 67 | 67 | return this.type; |
| 68 | 68 | } |
| 69 | - | |
| 69 | + | |
| 70 | 70 | private Class<D> getDefaultClass() { |
| 71 | 71 | if (this.defaultClass == null) { |
| 72 | 72 | this.defaultClass = Reflections.getGenericTypeArgument(this.getClass(), 1); |
| 73 | 73 | } |
| 74 | - | |
| 74 | + | |
| 75 | 75 | return this.defaultClass; |
| 76 | 76 | } |
| 77 | 77 | |
| ... | ... | @@ -99,7 +99,15 @@ public abstract class AbstractStrategyBootstrap<T, D extends T> extends Abstract |
| 99 | 99 | Configuration config = new PropertiesConfiguration(url); |
| 100 | 100 | canonicalName = config.getString(getConfigKey(), getDefaultClass().getCanonicalName()); |
| 101 | 101 | |
| 102 | - ClassLoader classLoader = ConfigurationLoader.getClassLoaderForResource(canonicalName.replaceAll("\\.", "/") + ".class"); | |
| 102 | + ClassLoader classLoader; | |
| 103 | + | |
| 104 | + try { | |
| 105 | + classLoader = ConfigurationLoader.getClassLoaderForResource(canonicalName.replaceAll("\\.", | |
| 106 | + "/") + ".class"); | |
| 107 | + } catch (FileNotFoundException e) { | |
| 108 | + classLoader = Thread.currentThread().getContextClassLoader(); | |
| 109 | + } | |
| 110 | + | |
| 103 | 111 | result = (Class<T>) Class.forName(canonicalName, false, classLoader); |
| 104 | 112 | result.asSubclass(getType()); |
| 105 | 113 | |
| ... | ... | @@ -113,14 +121,14 @@ public abstract class AbstractStrategyBootstrap<T, D extends T> extends Abstract |
| 113 | 121 | } catch (ClassCastException cause) { |
| 114 | 122 | key = Strings.getString("{0}-class-must-be-of-type", typeName); |
| 115 | 123 | throw new ConfigurationException(getBundle().getString(key, canonicalName, getType())); |
| 116 | - | |
| 124 | + | |
| 117 | 125 | } catch (FileNotFoundException e) { |
| 118 | 126 | throw new ConfigurationException(getBundle().getString("file-not-found", "demoiselle.properties")); |
| 119 | 127 | } |
| 120 | 128 | |
| 121 | 129 | return result; |
| 122 | 130 | } |
| 123 | - | |
| 131 | + | |
| 124 | 132 | public abstract String getConfigKey(); |
| 125 | 133 | |
| 126 | 134 | } | ... | ... |
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/TransactionBootstrap.java
| ... | ... | @@ -36,13 +36,20 @@ |
| 36 | 36 | */ |
| 37 | 37 | package br.gov.frameworkdemoiselle.internal.bootstrap; |
| 38 | 38 | |
| 39 | -import br.gov.frameworkdemoiselle.internal.implementation.DefaultTransaction; | |
| 39 | +import javax.enterprise.event.Observes; | |
| 40 | +import javax.enterprise.inject.spi.Extension; | |
| 41 | +import javax.enterprise.inject.spi.ProcessAnnotatedType; | |
| 42 | + | |
| 40 | 43 | import br.gov.frameworkdemoiselle.transaction.Transaction; |
| 44 | +import br.gov.frameworkdemoiselle.util.Reflections; | |
| 41 | 45 | |
| 42 | -public class TransactionBootstrap extends AbstractStrategyBootstrap<Transaction, DefaultTransaction> { | |
| 46 | +public class TransactionBootstrap implements Extension { | |
| 43 | 47 | |
| 44 | - public String getConfigKey() { | |
| 45 | - return "frameworkdemoiselle.transaction.class"; | |
| 46 | - } | |
| 48 | + public <A> void processAnnotatedType(@Observes final ProcessAnnotatedType<A> event) { | |
| 49 | + Class<A> annotated = event.getAnnotatedType().getJavaClass(); | |
| 47 | 50 | |
| 51 | + if (Reflections.isOfType(annotated, Transaction.class)) { | |
| 52 | + event.veto(); | |
| 53 | + } | |
| 54 | + } | |
| 48 | 55 | } | ... | ... |
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/implementation/DefaultTransaction.java
| ... | ... | @@ -36,9 +36,6 @@ |
| 36 | 36 | */ |
| 37 | 37 | package br.gov.frameworkdemoiselle.internal.implementation; |
| 38 | 38 | |
| 39 | -import javax.enterprise.context.RequestScoped; | |
| 40 | -import javax.enterprise.inject.Any; | |
| 41 | - | |
| 42 | 39 | import br.gov.frameworkdemoiselle.DemoiselleException; |
| 43 | 40 | import br.gov.frameworkdemoiselle.transaction.Transaction; |
| 44 | 41 | import br.gov.frameworkdemoiselle.transaction.Transactional; |
| ... | ... | @@ -49,8 +46,6 @@ import br.gov.frameworkdemoiselle.transaction.Transactional; |
| 49 | 46 | * @author SERPRO |
| 50 | 47 | * @see Transaction |
| 51 | 48 | */ |
| 52 | -@Any | |
| 53 | -@RequestScoped | |
| 54 | 49 | public class DefaultTransaction implements Transaction { |
| 55 | 50 | |
| 56 | 51 | private static final long serialVersionUID = 1L; | ... | ... |
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/producer/AbstractStrategyProducer.java
| ... | ... | @@ -45,6 +45,7 @@ import javax.inject.Inject; |
| 45 | 45 | import org.apache.commons.configuration.Configuration; |
| 46 | 46 | import org.apache.commons.configuration.PropertiesConfiguration; |
| 47 | 47 | |
| 48 | +import br.gov.frameworkdemoiselle.annotation.Name; | |
| 48 | 49 | import br.gov.frameworkdemoiselle.configuration.ConfigurationException; |
| 49 | 50 | import br.gov.frameworkdemoiselle.internal.configuration.ConfigurationLoader; |
| 50 | 51 | import br.gov.frameworkdemoiselle.util.Reflections; |
| ... | ... | @@ -62,6 +63,7 @@ public abstract class AbstractStrategyProducer<T, D extends T> implements Serial |
| 62 | 63 | private Class<? extends T> selected; |
| 63 | 64 | |
| 64 | 65 | @Inject |
| 66 | + @Name("demoiselle-core-bundle") | |
| 65 | 67 | private ResourceBundle bundle; |
| 66 | 68 | |
| 67 | 69 | protected Class<? extends T> getSelected() { |
| ... | ... | @@ -119,8 +121,15 @@ public abstract class AbstractStrategyProducer<T, D extends T> implements Serial |
| 119 | 121 | Configuration config = new PropertiesConfiguration(url); |
| 120 | 122 | canonicalName = config.getString(getConfigKey(), getDefaultClass().getCanonicalName()); |
| 121 | 123 | |
| 122 | - ClassLoader classLoader = ConfigurationLoader.getClassLoaderForResource(canonicalName | |
| 123 | - .replaceAll("\\.", "/") + ".class"); | |
| 124 | + ClassLoader classLoader; | |
| 125 | + | |
| 126 | + try { | |
| 127 | + classLoader = ConfigurationLoader.getClassLoaderForResource(canonicalName.replaceAll("\\.", "/") | |
| 128 | + + ".class"); | |
| 129 | + } catch (FileNotFoundException e) { | |
| 130 | + classLoader = Thread.currentThread().getContextClassLoader(); | |
| 131 | + } | |
| 132 | + | |
| 124 | 133 | result = (Class<T>) Class.forName(canonicalName, false, classLoader); |
| 125 | 134 | result.asSubclass(getType()); |
| 126 | 135 | ... | ... |
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/producer/TransactionProducer.java
| ... | ... | @@ -48,15 +48,11 @@ |
| 48 | 48 | */ |
| 49 | 49 | package br.gov.frameworkdemoiselle.internal.producer; |
| 50 | 50 | |
| 51 | -import java.lang.annotation.Annotation; | |
| 52 | - | |
| 53 | -import javax.enterprise.inject.Any; | |
| 54 | 51 | import javax.enterprise.inject.Default; |
| 55 | 52 | import javax.enterprise.inject.Produces; |
| 56 | 53 | |
| 57 | 54 | import br.gov.frameworkdemoiselle.internal.implementation.DefaultTransaction; |
| 58 | 55 | import br.gov.frameworkdemoiselle.transaction.Transaction; |
| 59 | -import br.gov.frameworkdemoiselle.util.Beans; | |
| 60 | 56 | |
| 61 | 57 | public class TransactionProducer extends AbstractStrategyProducer<Transaction, DefaultTransaction> { |
| 62 | 58 | |
| ... | ... | @@ -64,15 +60,8 @@ public class TransactionProducer extends AbstractStrategyProducer<Transaction, D |
| 64 | 60 | |
| 65 | 61 | @Default |
| 66 | 62 | @Produces |
| 67 | - public Transaction create() { | |
| 68 | - return Beans.getReference(getSelected(), new Any() { | |
| 69 | - | |
| 70 | - @Override | |
| 71 | - public Class<? extends Annotation> annotationType() { | |
| 72 | - return Any.class; | |
| 73 | - | |
| 74 | - } | |
| 75 | - }); | |
| 63 | + public Transaction create() throws InstantiationException, IllegalAccessException { | |
| 64 | + return getSelected().newInstance(); | |
| 76 | 65 | } |
| 77 | 66 | |
| 78 | 67 | @Override | ... | ... |
impl/core/src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension
| 1 | 1 | br.gov.frameworkdemoiselle.internal.bootstrap.CoreBootstrap |
| 2 | -br.gov.frameworkdemoiselle.internal.bootstrap.AuthenticatorBootstrap | |
| 3 | -br.gov.frameworkdemoiselle.internal.bootstrap.AuthorizerBootstrap | |
| 2 | +br.gov.frameworkdemoiselle.internal.bootstrap.TransactionBootstrap | |
| 3 | +#br.gov.frameworkdemoiselle.internal.bootstrap.AuthenticatorBootstrap | |
| 4 | +#br.gov.frameworkdemoiselle.internal.bootstrap.AuthorizerBootstrap | |
| 4 | 5 | br.gov.frameworkdemoiselle.internal.bootstrap.StartupBootstrap |
| 5 | 6 | br.gov.frameworkdemoiselle.internal.bootstrap.ShutdownBootstrap | ... | ... |
impl/extension/jpa/src/main/java/br/gov/frameworkdemoiselle/template/JPACrud.java
| ... | ... | @@ -46,6 +46,7 @@ import javax.persistence.Basic; |
| 46 | 46 | import javax.persistence.Column; |
| 47 | 47 | import javax.persistence.EntityManager; |
| 48 | 48 | import javax.persistence.Enumerated; |
| 49 | +import javax.persistence.PersistenceContext; | |
| 49 | 50 | import javax.persistence.Query; |
| 50 | 51 | import javax.persistence.TransactionRequiredException; |
| 51 | 52 | import javax.persistence.criteria.CriteriaBuilder; |
| ... | ... | @@ -77,6 +78,7 @@ public class JPACrud<T, I> implements Crud<T, I> { |
| 77 | 78 | |
| 78 | 79 | private static final long serialVersionUID = 1L; |
| 79 | 80 | |
| 81 | +// @PersistenceContext | |
| 80 | 82 | private EntityManager entityManager; |
| 81 | 83 | |
| 82 | 84 | @Inject | ... | ... |
impl/extension/jpa/src/main/java/br/gov/frameworkdemoiselle/transaction/JPATransaction.java
| ... | ... | @@ -38,13 +38,11 @@ package br.gov.frameworkdemoiselle.transaction; |
| 38 | 38 | |
| 39 | 39 | import java.util.Collection; |
| 40 | 40 | |
| 41 | -import javax.enterprise.context.RequestScoped; | |
| 42 | -import javax.enterprise.inject.Any; | |
| 43 | -import javax.inject.Inject; | |
| 44 | 41 | import javax.persistence.EntityManager; |
| 45 | 42 | import javax.persistence.EntityTransaction; |
| 46 | 43 | |
| 47 | 44 | import br.gov.frameworkdemoiselle.internal.producer.EntityManagerProducer; |
| 45 | +import br.gov.frameworkdemoiselle.util.Beans; | |
| 48 | 46 | |
| 49 | 47 | /** |
| 50 | 48 | * Represents the strategy destinated to manage JPA transactions. |
| ... | ... | @@ -52,17 +50,22 @@ import br.gov.frameworkdemoiselle.internal.producer.EntityManagerProducer; |
| 52 | 50 | * @author SERPRO |
| 53 | 51 | * @see Transaction |
| 54 | 52 | */ |
| 55 | -@Any | |
| 56 | -@RequestScoped | |
| 57 | 53 | public class JPATransaction implements Transaction { |
| 58 | 54 | |
| 59 | 55 | private static final long serialVersionUID = 1L; |
| 60 | 56 | |
| 61 | - @Inject | |
| 62 | 57 | private EntityManagerProducer producer; |
| 63 | 58 | |
| 59 | + private EntityManagerProducer getProducer() { | |
| 60 | + if (producer == null) { | |
| 61 | + producer = Beans.getReference(EntityManagerProducer.class); | |
| 62 | + } | |
| 63 | + | |
| 64 | + return producer; | |
| 65 | + } | |
| 66 | + | |
| 64 | 67 | public Collection<EntityManager> getDelegate() { |
| 65 | - return producer.getCache().values(); | |
| 68 | + return getProducer().getCache().values(); | |
| 66 | 69 | } |
| 67 | 70 | |
| 68 | 71 | @Override | ... | ... |
impl/extension/jta/src/main/java/br/gov/frameworkdemoiselle/transaction/JTATransaction.java
| ... | ... | @@ -36,16 +36,12 @@ |
| 36 | 36 | */ |
| 37 | 37 | package br.gov.frameworkdemoiselle.transaction; |
| 38 | 38 | |
| 39 | -import javax.enterprise.context.RequestScoped; | |
| 40 | -import javax.enterprise.inject.Any; | |
| 41 | 39 | import javax.transaction.Status; |
| 42 | 40 | import javax.transaction.SystemException; |
| 43 | 41 | import javax.transaction.UserTransaction; |
| 44 | 42 | |
| 45 | 43 | import br.gov.frameworkdemoiselle.util.Beans; |
| 46 | 44 | |
| 47 | -@Any | |
| 48 | -@RequestScoped | |
| 49 | 45 | public class JTATransaction implements Transaction { |
| 50 | 46 | |
| 51 | 47 | private static final long serialVersionUID = 1L; | ... | ... |