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