Commit 43b46f233ab2e1c0ef188dd0de6f037851792823

Authored by Dancovich
1 parent b8efd70f
Exists in master

Revertendo alterações por necessitar de mais testes

impl/extension/jpa/src/main/java/br/gov/frameworkdemoiselle/internal/producer/AbstractEntityManagerStore.java
... ... @@ -53,6 +53,8 @@ import org.slf4j.Logger;
53 53 import br.gov.frameworkdemoiselle.annotation.Name;
54 54 import br.gov.frameworkdemoiselle.internal.configuration.EntityManagerConfig;
55 55 import br.gov.frameworkdemoiselle.internal.configuration.EntityManagerConfig.EntityManagerScope;
  56 +import br.gov.frameworkdemoiselle.util.Beans;
  57 +import br.gov.frameworkdemoiselle.util.NameQualifier;
56 58 import br.gov.frameworkdemoiselle.util.ResourceBundle;
57 59  
58 60 /**
... ... @@ -95,24 +97,35 @@ public abstract class AbstractEntityManagerStore implements Serializable {
95 97 entityManager = cache.get(persistenceUnit);
96 98  
97 99 } else {
98   - entityManager = factory.create(persistenceUnit).createEntityManager();
  100 + entityManager = getFactory().create(persistenceUnit).createEntityManager();
99 101 entityManager.setFlushMode(FlushModeType.AUTO);
100 102  
101 103 cache.put(persistenceUnit, entityManager);
102   - this.logger.info(bundle.getString("entity-manager-was-created", persistenceUnit));
  104 + this.getLogger().info(getBundle().getString("entity-manager-was-created", persistenceUnit));
103 105 }
104 106  
105 107 return entityManager;
106 108 }
107 109  
108   - @PostConstruct
  110 + /**
  111 + * Run this to initialize all persistence units. It's recomended this method
  112 + * be annotated with {@link PostConstruct}, so it runs as soon as an EntityManager gets injected.
  113 + */
  114 + public abstract void initialize();
  115 +
  116 + /**
  117 + * Run this to close all persistence units. It's recomended this method
  118 + * be annotated with {@link PreDestroy}, so it runs as soon as the scope the EntityManager is
  119 + * attached to ends.
  120 + */
  121 + public abstract void terminate();
  122 +
109 123 void init() {
110   - for (String persistenceUnit : factory.getCache().keySet()) {
  124 + for (String persistenceUnit : getFactory().getCache().keySet()) {
111 125 getEntityManager(persistenceUnit);
112 126 }
113 127 }
114 128  
115   - @PreDestroy
116 129 void close() {
117 130 //Se o produtor não possui escopo, então o ciclo de vida
118 131 //de EntityManager produzidos é responsabilidade do desenvolvedor. Não
... ... @@ -128,4 +141,25 @@ public abstract class AbstractEntityManagerStore implements Serializable {
128 141 Map<String, EntityManager> getCache() {
129 142 return cache;
130 143 }
  144 +
  145 + private EntityManagerFactoryProducer getFactory(){
  146 + if (factory==null){
  147 + factory = Beans.getReference(EntityManagerFactoryProducer.class);
  148 + }
  149 + return factory;
  150 + }
  151 +
  152 + protected Logger getLogger(){
  153 + if (logger==null){
  154 + logger = Beans.getReference(Logger.class);
  155 + }
  156 + return logger;
  157 + }
  158 +
  159 + protected ResourceBundle getBundle(){
  160 + if (bundle==null){
  161 + bundle = Beans.getReference(ResourceBundle.class , new NameQualifier("demoiselle-jpa-bundle"));
  162 + }
  163 + return bundle;
  164 + }
131 165 }
... ...
impl/extension/jpa/src/main/java/br/gov/frameworkdemoiselle/internal/producer/ApplicationEntityManagerStore.java
... ... @@ -36,6 +36,8 @@
36 36 */
37 37 package br.gov.frameworkdemoiselle.internal.producer;
38 38  
  39 +import javax.annotation.PostConstruct;
  40 +import javax.annotation.PreDestroy;
39 41 import javax.enterprise.context.ApplicationScoped;
40 42  
41 43 /**
... ... @@ -50,4 +52,16 @@ public class ApplicationEntityManagerStore extends AbstractEntityManagerStore {
50 52  
51 53 private static final long serialVersionUID = 1L;
52 54  
  55 + @Override
  56 + @PostConstruct
  57 + public void initialize() {
  58 + super.init();
  59 + }
  60 +
  61 + @Override
  62 + @PreDestroy
  63 + public void terminate() {
  64 + super.close();
  65 + }
  66 +
53 67 }
... ...
impl/extension/jpa/src/main/java/br/gov/frameworkdemoiselle/internal/producer/ConversationEntityManagerStore.java
... ... @@ -36,6 +36,8 @@
36 36 */
37 37 package br.gov.frameworkdemoiselle.internal.producer;
38 38  
  39 +import javax.annotation.PostConstruct;
  40 +import javax.annotation.PreDestroy;
39 41 import javax.enterprise.context.Conversation;
40 42 import javax.enterprise.context.ConversationScoped;
41 43 import javax.inject.Inject;
... ... @@ -62,4 +64,16 @@ public class ConversationEntityManagerStore extends AbstractEntityManagerStore {
62 64 return conversation;
63 65 }
64 66  
  67 + @Override
  68 + @PostConstruct
  69 + public void initialize() {
  70 + super.init();
  71 + }
  72 +
  73 + @Override
  74 + @PreDestroy
  75 + public void terminate() {
  76 + super.close();
  77 + }
  78 +
65 79 }
... ...
impl/extension/jpa/src/main/java/br/gov/frameworkdemoiselle/internal/producer/DependentEntityManagerStore.java
... ... @@ -36,6 +36,8 @@
36 36 */
37 37 package br.gov.frameworkdemoiselle.internal.producer;
38 38  
  39 +import javax.annotation.PostConstruct;
  40 +import javax.annotation.PreDestroy;
39 41 import javax.enterprise.context.Dependent;
40 42  
41 43 /**
... ... @@ -50,5 +52,17 @@ import javax.enterprise.context.Dependent;
50 52 public class DependentEntityManagerStore extends AbstractEntityManagerStore {
51 53  
52 54 private static final long serialVersionUID = 1L;
  55 +
  56 + @Override
  57 + @PostConstruct
  58 + public void initialize() {
  59 + super.init();
  60 + }
  61 +
  62 + @Override
  63 + @PreDestroy
  64 + public void terminate() {
  65 + super.close();
  66 + }
53 67  
54 68 }
... ...
impl/extension/jpa/src/main/java/br/gov/frameworkdemoiselle/internal/producer/EntityManagerProducer.java
... ... @@ -37,14 +37,20 @@
37 37 package br.gov.frameworkdemoiselle.internal.producer;
38 38  
39 39 import java.io.Serializable;
  40 +import java.util.Collections;
  41 +import java.util.HashMap;
40 42 import java.util.Map;
41 43 import java.util.Set;
42 44  
  45 +import javax.annotation.PostConstruct;
  46 +import javax.annotation.PreDestroy;
  47 +import javax.enterprise.context.RequestScoped;
43 48 import javax.enterprise.inject.Default;
44 49 import javax.enterprise.inject.Produces;
45 50 import javax.enterprise.inject.spi.InjectionPoint;
46 51 import javax.inject.Inject;
47 52 import javax.persistence.EntityManager;
  53 +import javax.persistence.FlushModeType;
48 54  
49 55 import org.slf4j.Logger;
50 56  
... ... @@ -54,6 +60,7 @@ import br.gov.frameworkdemoiselle.configuration.Configuration;
54 60 import br.gov.frameworkdemoiselle.internal.configuration.EntityManagerConfig;
55 61 import br.gov.frameworkdemoiselle.internal.proxy.EntityManagerProxy;
56 62 import br.gov.frameworkdemoiselle.util.Beans;
  63 +import br.gov.frameworkdemoiselle.util.NameQualifier;
57 64 import br.gov.frameworkdemoiselle.util.ResourceBundle;
58 65  
59 66 /**
... ... @@ -62,6 +69,8 @@ import br.gov.frameworkdemoiselle.util.ResourceBundle;
62 69 * persistence.xml, demoiselle.properties or @PersistenceUnit annotation.
63 70 * </p>
64 71 */
  72 +//TODO Concluir implementação de escopo selecionável, testes revelaram problemas na solução atual.
  73 +@RequestScoped
65 74 public class EntityManagerProducer implements Serializable{
66 75  
67 76 private static final long serialVersionUID = 1L;
... ... @@ -76,10 +85,13 @@ public class EntityManagerProducer implements Serializable{
76 85 @Inject
77 86 private EntityManagerFactoryProducer factory;
78 87  
79   - private AbstractEntityManagerStore entityManagerStore;
  88 + /*@Inject
  89 + private Instance<AbstractEntityManagerStore> storeInstance;*/
  90 +
  91 + private final Map<String, EntityManager> cache = Collections.synchronizedMap(new HashMap<String, EntityManager>());
80 92  
81   - @Inject
82   - private EntityManagerConfig configuration;
  93 + /*@Inject
  94 + private EntityManagerConfig configuration;*/
83 95  
84 96 /**
85 97 * <p>
... ... @@ -124,8 +136,45 @@ public class EntityManagerProducer implements Serializable{
124 136 return new EntityManagerProxy(persistenceUnit);
125 137 }
126 138  
127   - public EntityManager getEntityManager(String persistenceUnit) {
  139 + /*public EntityManager getEntityManager(String persistenceUnit) {
128 140 return getStore().getEntityManager(persistenceUnit);
  141 + }*/
  142 +
  143 + @PostConstruct
  144 + void init() {
  145 + for (String persistenceUnit : getFactory().getCache().keySet()) {
  146 + getEntityManager(persistenceUnit);
  147 + }
  148 + }
  149 +
  150 + @PreDestroy
  151 + void close() {
  152 + //Se o produtor não possui escopo, então o ciclo de vida
  153 + //de EntityManager produzidos é responsabilidade do desenvolvedor. Não
  154 + //fechamos os EntityManagers aqui.
  155 + //if (configuration.getEntityManagerScope() != EntityManagerScope.NOSCOPE){
  156 + for (EntityManager entityManager : cache.values()) {
  157 + entityManager.close();
  158 + }
  159 + //}
  160 + cache.clear();
  161 + }
  162 +
  163 + public EntityManager getEntityManager(String persistenceUnit) {
  164 + EntityManager entityManager = null;
  165 +
  166 + if (cache.containsKey(persistenceUnit)) {
  167 + entityManager = cache.get(persistenceUnit);
  168 +
  169 + } else {
  170 + entityManager = getFactory().create(persistenceUnit).createEntityManager();
  171 + entityManager.setFlushMode(FlushModeType.AUTO);
  172 +
  173 + cache.put(persistenceUnit, entityManager);
  174 + this.getLogger().info(getBundle().getString("entity-manager-was-created", persistenceUnit));
  175 + }
  176 +
  177 + return entityManager;
129 178 }
130 179  
131 180 /**
... ... @@ -164,36 +213,47 @@ public class EntityManagerProducer implements Serializable{
164 213 }
165 214  
166 215 public Map<String, EntityManager> getCache() {
167   - return getStore().getCache();
  216 + //return getStore().getCache();
  217 + return this.cache;
168 218 }
169 219  
170   - private AbstractEntityManagerStore getStore(){
171   - if (entityManagerStore==null){
172   - switch(configuration.getEntityManagerScope()){
173   - case APPLICATION:
174   - entityManagerStore = Beans.getReference(ApplicationEntityManagerStore.class);
175   - break;
176   - case CONVERSATION:
177   - entityManagerStore = Beans.getReference(ConversationEntityManagerStore.class);
178   - break;
179   - case NOSCOPE:
180   - entityManagerStore = Beans.getReference(DependentEntityManagerStore.class);
181   - break;
182   - case REQUEST:
183   - entityManagerStore = Beans.getReference(RequestEntityManagerStore.class);
184   - break;
185   - case SESSION:
186   - entityManagerStore = Beans.getReference(SessionEntityManagerStore.class);
187   - break;
188   - case VIEW:
189   - entityManagerStore = Beans.getReference(ViewEntityManagerStore.class);
190   - break;
191   - default:
192   - entityManagerStore = Beans.getReference(RequestEntityManagerStore.class);
193   - break;
194   - }
  220 + /*private AbstractEntityManagerStore getStore(){
  221 + switch(configuration.getEntityManagerScope()){
  222 + case REQUEST:
  223 + return storeInstance.select(RequestEntityManagerStore.class).get();
  224 + case APPLICATION:
  225 + return storeInstance.select(ApplicationEntityManagerStore.class).get();
  226 + case CONVERSATION:
  227 + return storeInstance.select(ConversationEntityManagerStore.class).get();
  228 + case NOSCOPE:
  229 + return storeInstance.select(DependentEntityManagerStore.class).get();
  230 + case SESSION:
  231 + return storeInstance.select(SessionEntityManagerStore.class).get();
  232 + case VIEW:
  233 + return storeInstance.select(ViewEntityManagerStore.class).get();
  234 + default:
  235 + return storeInstance.select(RequestEntityManagerStore.class).get();
  236 + }
  237 + }*/
  238 +
  239 + protected Logger getLogger(){
  240 + if (logger==null){
  241 + logger = Beans.getReference(Logger.class);
  242 + }
  243 + return logger;
  244 + }
  245 +
  246 + protected ResourceBundle getBundle(){
  247 + if (bundle==null){
  248 + bundle = Beans.getReference(ResourceBundle.class , new NameQualifier("demoiselle-jpa-bundle"));
  249 + }
  250 + return bundle;
  251 + }
  252 +
  253 + private EntityManagerFactoryProducer getFactory(){
  254 + if (factory==null){
  255 + factory = Beans.getReference(EntityManagerFactoryProducer.class);
195 256 }
196   -
197   - return entityManagerStore;
  257 + return factory;
198 258 }
199 259 }
... ...
impl/extension/jpa/src/main/java/br/gov/frameworkdemoiselle/internal/producer/RequestEntityManagerStore.java
... ... @@ -36,6 +36,8 @@
36 36 */
37 37 package br.gov.frameworkdemoiselle.internal.producer;
38 38  
  39 +import javax.annotation.PostConstruct;
  40 +import javax.annotation.PreDestroy;
39 41 import javax.enterprise.context.RequestScoped;
40 42  
41 43 /**
... ... @@ -49,5 +51,16 @@ import javax.enterprise.context.RequestScoped;
49 51 public class RequestEntityManagerStore extends AbstractEntityManagerStore {
50 52  
51 53 private static final long serialVersionUID = 1L;
  54 +
  55 + @Override
  56 + @PostConstruct
  57 + public void initialize() {
  58 + super.init();
  59 + }
52 60  
  61 + @Override
  62 + @PreDestroy
  63 + public void terminate() {
  64 + super.close();
  65 + }
53 66 }
... ...
impl/extension/jpa/src/main/java/br/gov/frameworkdemoiselle/internal/producer/SessionEntityManagerStore.java
... ... @@ -36,6 +36,8 @@
36 36 */
37 37 package br.gov.frameworkdemoiselle.internal.producer;
38 38  
  39 +import javax.annotation.PostConstruct;
  40 +import javax.annotation.PreDestroy;
39 41 import javax.enterprise.context.SessionScoped;
40 42  
41 43 /**
... ... @@ -49,5 +51,17 @@ import javax.enterprise.context.SessionScoped;
49 51 public class SessionEntityManagerStore extends AbstractEntityManagerStore {
50 52  
51 53 private static final long serialVersionUID = 1L;
  54 +
  55 + @Override
  56 + @PostConstruct
  57 + public void initialize() {
  58 + super.init();
  59 + }
  60 +
  61 + @Override
  62 + @PreDestroy
  63 + public void terminate() {
  64 + super.close();
  65 + }
52 66  
53 67 }
... ...
impl/extension/jpa/src/main/java/br/gov/frameworkdemoiselle/internal/producer/ViewEntityManagerStore.java
... ... @@ -36,6 +36,9 @@
36 36 */
37 37 package br.gov.frameworkdemoiselle.internal.producer;
38 38  
  39 +import javax.annotation.PostConstruct;
  40 +import javax.annotation.PreDestroy;
  41 +
39 42 import br.gov.frameworkdemoiselle.annotation.ViewScoped;
40 43  
41 44 /**
... ... @@ -49,5 +52,17 @@ import br.gov.frameworkdemoiselle.annotation.ViewScoped;
49 52 public class ViewEntityManagerStore extends AbstractEntityManagerStore {
50 53  
51 54 private static final long serialVersionUID = 1L;
  55 +
  56 + @Override
  57 + @PostConstruct
  58 + public void initialize() {
  59 + super.init();
  60 + }
  61 +
  62 + @Override
  63 + @PreDestroy
  64 + public void terminate() {
  65 + super.close();
  66 + }
52 67  
53 68 }
... ...
impl/extension/jpa/src/main/java/br/gov/frameworkdemoiselle/internal/proxy/EntityManagerProxy.java
... ... @@ -427,7 +427,7 @@ public class EntityManagerProxy implements EntityManager, Serializable {
427 427 */
428 428 protected final void joinTransactionIfNecessary() {
429 429 try {
430   - getEntityManagerDelegate().getTransaction();
  430 + /*EntityTransaction transaction = */getEntityManagerDelegate().getTransaction();
431 431 } catch (IllegalStateException cause) {
432 432 //IllegalStateException is launched if we are on a JTA entity manager, so
433 433 //we assume we need to join transaction instead of creating one.
... ... @@ -586,4 +586,5 @@ public class EntityManagerProxy implements EntityManager, Serializable {
586 586 private ResourceBundle getBundle(){
587 587 return Beans.getReference(ResourceBundle.class,new NameQualifier("demoiselle-jpa-bundle"));
588 588 }
  589 +
589 590 }
... ...
impl/extension/jpa/src/test/java/producer/NoScopedProducerTest.java
... ... @@ -9,6 +9,7 @@ import javax.persistence.EntityManager;
9 9 import org.jboss.arquillian.container.test.api.Deployment;
10 10 import org.jboss.arquillian.junit.Arquillian;
11 11 import org.jboss.shrinkwrap.api.spec.WebArchive;
  12 +import org.junit.Ignore;
12 13 import org.junit.Test;
13 14 import org.junit.runner.RunWith;
14 15  
... ... @@ -17,6 +18,8 @@ import br.gov.frameworkdemoiselle.internal.proxy.EntityManagerProxy;
17 18 import br.gov.frameworkdemoiselle.util.Beans;
18 19 import br.gov.frameworkdemoiselle.util.NameQualifier;
19 20  
  21 +//TODO Implementação de escopo selecionável tem que concluir antes de ativar esse teste
  22 +@Ignore
20 23 @RunWith(Arquillian.class)
21 24 public class NoScopedProducerTest {
22 25  
... ...
impl/extension/jpa/src/test/java/producer/ViewScopedProducerTest.java
... ... @@ -10,6 +10,7 @@ import javax.persistence.EntityManager;
10 10 import org.jboss.arquillian.container.test.api.Deployment;
11 11 import org.jboss.arquillian.junit.Arquillian;
12 12 import org.jboss.shrinkwrap.api.spec.WebArchive;
  13 +import org.junit.Ignore;
13 14 import org.junit.Test;
14 15 import org.junit.runner.RunWith;
15 16  
... ... @@ -19,6 +20,8 @@ import br.gov.frameworkdemoiselle.internal.proxy.EntityManagerProxy;
19 20 import br.gov.frameworkdemoiselle.util.Beans;
20 21 import br.gov.frameworkdemoiselle.util.NameQualifier;
21 22  
  23 +//TODO Implementação de escopo selecionável tem que concluir antes de ativar esse teste
  24 +@Ignore
22 25 @RunWith(Arquillian.class)
23 26 public class ViewScopedProducerTest {
24 27  
... ...
impl/extension/jpa/src/test/java/transaction/interceptor/JPATransactionTest.java
... ... @@ -66,7 +66,6 @@ public class JPATransactionTest {
66 66  
67 67 @Test
68 68 public void commitWithSuccess() {
69   -
70 69 tb.commitWithSuccess();
71 70  
72 71 MyEntity1 entity1 = em1.find(MyEntity1.class, tb.createId("id-1"));
... ... @@ -78,7 +77,6 @@ public class JPATransactionTest {
78 77  
79 78 @Test
80 79 public void rollbackWithSuccess() {
81   -
82 80 try {
83 81 tb.rollbackWithSuccess();
84 82 } catch (Exception e) {
... ...