Commit aae2ffe2f58aeade6445fe2492f9e5b7a1489f2c

Authored by Cleverson Sacramento
1 parent d0256b51
Exists in master

FWK-202: Injeção de java.util.logging.Logger

Task-Url: https://demoiselle.atlassian.net/browse/FWK-202
impl/extension/jpa/src/main/java/br/gov/frameworkdemoiselle/internal/producer/AbstractEntityManagerStore.java
... ... @@ -39,13 +39,12 @@ package br.gov.frameworkdemoiselle.internal.producer;
39 39 import java.util.Collections;
40 40 import java.util.HashMap;
41 41 import java.util.Map;
  42 +import java.util.logging.Logger;
42 43  
43 44 import javax.enterprise.context.RequestScoped;
44 45 import javax.persistence.EntityManager;
45 46 import javax.persistence.FlushModeType;
46 47  
47   -import org.slf4j.Logger;
48   -
49 48 import br.gov.frameworkdemoiselle.internal.configuration.EntityManagerConfig;
50 49 import br.gov.frameworkdemoiselle.internal.configuration.EntityManagerConfig.EntityManagerScope;
51 50 import br.gov.frameworkdemoiselle.util.Beans;
... ... @@ -53,25 +52,25 @@ import br.gov.frameworkdemoiselle.util.NameQualifier;
53 52 import br.gov.frameworkdemoiselle.util.ResourceBundle;
54 53  
55 54 /**
56   - *
57   - * <p>Stores produced entity managers. When the {@link EntityManagerProducer} try to create an entity manager it will
58   - * seach this store for a cached instance, only creating a new instance if this cache doesn't contain a suitable one.</p>
59   - *
60   - * <p>There are several concrete implementations of this class, each one corresponding to a scoped cache (ex: {@link RequestEntityManagerStore}
61   - * stores Entity Managers on the request scope). To select witch implementation is used (and with that, what scope is used to store Entity Managers)
62   - * open the "demoiselle.properties" file and edit the property "frameworkdemoiselle.persistence.entitymanager.scope". The default scope is the
63   - * {@link RequestScoped}.</p>
64   - *
  55 + * <p>
  56 + * Stores produced entity managers. When the {@link EntityManagerProducer} try to create an entity manager it will seach
  57 + * this store for a cached instance, only creating a new instance if this cache doesn't contain a suitable one.
  58 + * </p>
  59 + * <p>
  60 + * There are several concrete implementations of this class, each one corresponding to a scoped cache (ex:
  61 + * {@link RequestEntityManagerStore} stores Entity Managers on the request scope). To select witch implementation is
  62 + * used (and with that, what scope is used to store Entity Managers) open the "demoiselle.properties" file and edit the
  63 + * property "frameworkdemoiselle.persistence.entitymanager.scope". The default scope is the {@link RequestScoped}.
  64 + * </p>
65 65 *
66 66 * @author serpro
67   - *
68 67 */
69 68 public abstract class AbstractEntityManagerStore implements EntityManagerStore {
70   -
  69 +
71 70 private static final long serialVersionUID = 1L;
72 71  
73 72 private final Map<String, EntityManager> cache = Collections.synchronizedMap(new HashMap<String, EntityManager>());
74   -
  73 +
75 74 public EntityManager getEntityManager(String persistenceUnit) {
76 75 EntityManager entityManager = null;
77 76  
... ... @@ -88,7 +87,7 @@ public abstract class AbstractEntityManagerStore implements EntityManagerStore {
88 87  
89 88 return entityManager;
90 89 }
91   -
  90 +
92 91 void init() {
93 92 for (String persistenceUnit : getFactory().getCache().keySet()) {
94 93 getEntityManager(persistenceUnit);
... ... @@ -96,11 +95,11 @@ public abstract class AbstractEntityManagerStore implements EntityManagerStore {
96 95 }
97 96  
98 97 void close() {
99   - //Se o produtor não possui escopo, então o ciclo de vida
100   - //de EntityManager produzidos é responsabilidade do desenvolvedor. Não
101   - //fechamos os EntityManagers aqui.
  98 + // Se o produtor não possui escopo, então o ciclo de vida
  99 + // de EntityManager produzidos é responsabilidade do desenvolvedor. Não
  100 + // fechamos os EntityManagers aqui.
102 101 EntityManagerConfig configuration = getConfiguration();
103   - if (configuration.getEntityManagerScope() != EntityManagerScope.NOSCOPE){
  102 + if (configuration.getEntityManagerScope() != EntityManagerScope.NOSCOPE) {
104 103 for (EntityManager entityManager : cache.values()) {
105 104 entityManager.close();
106 105 }
... ... @@ -109,26 +108,26 @@ public abstract class AbstractEntityManagerStore implements EntityManagerStore {
109 108 }
110 109  
111 110 public Map<String, EntityManager> getCache() {
112   - if (cache==null || cache.isEmpty()){
  111 + if (cache == null || cache.isEmpty()) {
113 112 init();
114 113 }
115   -
  114 +
116 115 return cache;
117 116 }
118   -
119   - private EntityManagerFactoryProducer getFactory(){
  117 +
  118 + private EntityManagerFactoryProducer getFactory() {
120 119 return Beans.getReference(EntityManagerFactoryProducer.class);
121 120 }
122   -
123   - private Logger getLogger(){
124   - return Beans.getReference(Logger.class);
  121 +
  122 + private Logger getLogger() {
  123 + return Beans.getReference(Logger.class, new NameQualifier("br.gov.frameworkdemoiselle.util"));
125 124 }
126   -
127   - private ResourceBundle getBundle(){
128   - return Beans.getReference(ResourceBundle.class , new NameQualifier("demoiselle-jpa-bundle"));
  125 +
  126 + private ResourceBundle getBundle() {
  127 + return Beans.getReference(ResourceBundle.class, new NameQualifier("demoiselle-jpa-bundle"));
129 128 }
130   -
131   - private EntityManagerConfig getConfiguration(){
  129 +
  130 + private EntityManagerConfig getConfiguration() {
132 131 return Beans.getReference(EntityManagerConfig.class);
133 132 }
134 133 }
... ...
impl/extension/jpa/src/main/java/br/gov/frameworkdemoiselle/internal/producer/EntityManagerFactoryProducer.java
... ... @@ -36,11 +36,14 @@
36 36 */
37 37 package br.gov.frameworkdemoiselle.internal.producer;
38 38  
  39 +import static java.util.logging.Level.SEVERE;
  40 +
39 41 import java.io.Serializable;
40 42 import java.util.ArrayList;
41 43 import java.util.Collections;
42 44 import java.util.HashMap;
43 45 import java.util.Map;
  46 +import java.util.logging.Logger;
44 47  
45 48 import javax.annotation.PostConstruct;
46 49 import javax.annotation.PreDestroy;
... ... @@ -54,7 +57,6 @@ import javax.persistence.Persistence;
54 57 import javax.xml.parsers.DocumentBuilder;
55 58 import javax.xml.parsers.DocumentBuilderFactory;
56 59  
57   -import org.slf4j.Logger;
58 60 import org.w3c.dom.Document;
59 61 import org.w3c.dom.Element;
60 62 import org.w3c.dom.Node;
... ... @@ -72,20 +74,20 @@ public class EntityManagerFactoryProducer implements Serializable {
72 74 private static final long serialVersionUID = 1L;
73 75  
74 76 private static final String ENTITY_MANAGER_RESOURCE = "META-INF/persistence.xml";
75   -
  77 +
76 78 @Inject
77 79 protected Logger logger;
78 80  
79 81 @Inject
80 82 @Name("demoiselle-jpa-bundle")
81 83 protected ResourceBundle bundle;
82   -
  84 +
83 85 @Inject
84 86 private Persistences persistenceUnitReader;
85 87  
86 88 private final Map<ClassLoader, Map<String, EntityManagerFactory>> factoryCache = Collections
87 89 .synchronizedMap(new HashMap<ClassLoader, Map<String, EntityManagerFactory>>());
88   -
  90 +
89 91 @Default
90 92 @Produces
91 93 protected EntityManagerFactory createDefault(EntityManagerConfig config) {
... ... @@ -104,7 +106,7 @@ public class EntityManagerFactoryProducer implements Serializable {
104 106 String persistenceUnit = ip.getAnnotated().getAnnotation(Name.class).value();
105 107 return create(persistenceUnit);
106 108 }
107   -
  109 +
108 110 public EntityManagerFactory create(String persistenceUnit) {
109 111 EntityManagerFactory factory;
110 112  
... ... @@ -151,7 +153,7 @@ public class EntityManagerFactoryProducer implements Serializable {
151 153  
152 154 } catch (Exception cause) {
153 155 String message = bundle.getString("can-not-get-persistence-unit-from-persistence");
154   - logger.error(message, cause);
  156 + logger.log(SEVERE, message, cause);
155 157  
156 158 throw new DemoiselleException(message, cause);
157 159 }
... ... @@ -169,7 +171,7 @@ public class EntityManagerFactoryProducer implements Serializable {
169 171 throw new DemoiselleException(cause);
170 172 }
171 173  
172   - logger.debug(bundle.getString("persistence-unit-name-found", persistenceUnit));
  174 + logger.fine(bundle.getString("persistence-unit-name-found", persistenceUnit));
173 175 }
174 176 }
175 177  
... ... @@ -188,7 +190,7 @@ public class EntityManagerFactoryProducer implements Serializable {
188 190 Map<String, EntityManagerFactory> result = factoryCache.get(classLoader);
189 191  
190 192 if (result == null || result.isEmpty()) {
191   - logger.debug(bundle.getString("entity-manager-factory-not-found-in-cache"));
  193 + logger.fine(bundle.getString("entity-manager-factory-not-found-in-cache"));
192 194 for (String persistenceUnit : loadPersistenceUnitFromClassloader(classLoader)) {
193 195 create(persistenceUnit);
194 196 result = factoryCache.get(classLoader);
... ...
impl/extension/jpa/src/main/java/br/gov/frameworkdemoiselle/internal/producer/Persistences.java
... ... @@ -36,16 +36,16 @@
36 36 */
37 37 package br.gov.frameworkdemoiselle.internal.producer;
38 38  
  39 +import static br.gov.frameworkdemoiselle.configuration.Configuration.DEFAULT_RESOURCE;
  40 +
39 41 import java.util.Set;
  42 +import java.util.logging.Logger;
40 43  
41 44 import javax.inject.Inject;
42 45 import javax.inject.Singleton;
43 46  
44   -import org.slf4j.Logger;
45   -
46 47 import br.gov.frameworkdemoiselle.DemoiselleException;
47 48 import br.gov.frameworkdemoiselle.annotation.Name;
48   -import br.gov.frameworkdemoiselle.configuration.Configuration;
49 49 import br.gov.frameworkdemoiselle.internal.configuration.EntityManagerConfig;
50 50 import br.gov.frameworkdemoiselle.util.ResourceBundle;
51 51  
... ... @@ -73,8 +73,7 @@ public class Persistences {
73 73 String persistenceUnit = config.getDefaultPersistenceUnitName();
74 74  
75 75 if (persistenceUnit != null) {
76   - this.logger.debug(bundle.getString("getting-persistence-unit-from-properties",
77   - Configuration.DEFAULT_RESOURCE));
  76 + this.logger.fine(bundle.getString("getting-persistence-unit-from-properties", DEFAULT_RESOURCE));
78 77 }
79 78  
80 79 return persistenceUnit;
... ...
impl/extension/jpa/src/main/java/br/gov/frameworkdemoiselle/internal/proxy/EntityManagerProxy.java
... ... @@ -38,6 +38,7 @@ package br.gov.frameworkdemoiselle.internal.proxy;
38 38  
39 39 import java.io.Serializable;
40 40 import java.util.Map;
  41 +import java.util.logging.Logger;
41 42  
42 43 import javax.persistence.EntityManager;
43 44 import javax.persistence.EntityManagerFactory;
... ... @@ -51,8 +52,6 @@ import javax.persistence.criteria.CriteriaBuilder;
51 52 import javax.persistence.criteria.CriteriaQuery;
52 53 import javax.persistence.metamodel.Metamodel;
53 54  
54   -import org.slf4j.Logger;
55   -
56 55 import br.gov.frameworkdemoiselle.DemoiselleException;
57 56 import br.gov.frameworkdemoiselle.internal.configuration.EntityManagerConfig;
58 57 import br.gov.frameworkdemoiselle.internal.configuration.EntityManagerConfig.EntityManagerScope;
... ... @@ -69,19 +68,18 @@ import br.gov.frameworkdemoiselle.util.ResourceBundle;
69 68 public class EntityManagerProxy implements EntityManager, Serializable {
70 69  
71 70 private static final long serialVersionUID = 1L;
72   -
  71 +
73 72 /*
74 73 * Persistence unit of the delegated EntityManager.
75 74 */
76 75 private String persistenceUnit;
77   -
  76 +
78 77 /*
79 78 * demoiselle-jpa configuration options
80 79 */
81 80 private EntityManagerConfig configuration;
82   -
83   -
84   - private EntityManager delegateCache;
  81 +
  82 + private EntityManager delegateCache;
85 83  
86 84 /**
87 85 * Constructor based on persistence unit name.
... ... @@ -99,14 +97,15 @@ public class EntityManagerProxy implements EntityManager, Serializable {
99 97 * @return Cached EntityManager
100 98 */
101 99 private EntityManager getEntityManagerDelegate() {
102   - //Se o produtor de EntityManager não estiver em um escopo, precisamos guardar em cache o EntityManager produzido,
103   - //do contrário, basta solicitar uma instância do produtor (que estará em um escopo) e obter a instância real
104   - //de EntityManager dele.
105   - if (getConfiguration().getEntityManagerScope()!=EntityManagerScope.NOSCOPE || delegateCache==null){
  100 + // Se o produtor de EntityManager não estiver em um escopo, precisamos guardar em cache o EntityManager
  101 + // produzido,
  102 + // do contrário, basta solicitar uma instância do produtor (que estará em um escopo) e obter a instância real
  103 + // de EntityManager dele.
  104 + if (getConfiguration().getEntityManagerScope() != EntityManagerScope.NOSCOPE || delegateCache == null) {
106 105 EntityManagerProducer emp = Beans.getReference(EntityManagerProducer.class);
107 106 delegateCache = emp.getEntityManager(this.persistenceUnit);
108 107 }
109   -
  108 +
110 109 return delegateCache;
111 110 }
112 111  
... ... @@ -347,7 +346,7 @@ public class EntityManagerProxy implements EntityManager, Serializable {
347 346 */
348 347 @Override
349 348 public Query createQuery(String qlString) {
350   - return new QueryProxy(getEntityManagerDelegate().createQuery(qlString) , this) ;
  349 + return new QueryProxy(getEntityManagerDelegate().createQuery(qlString), this);
351 350 }
352 351  
353 352 /*
... ... @@ -356,7 +355,7 @@ public class EntityManagerProxy implements EntityManager, Serializable {
356 355 */
357 356 @Override
358 357 public <T> TypedQuery<T> createQuery(CriteriaQuery<T> criteriaQuery) {
359   - return new TypedQueryProxy<T>( getEntityManagerDelegate().createQuery(criteriaQuery) , this );
  358 + return new TypedQueryProxy<T>(getEntityManagerDelegate().createQuery(criteriaQuery), this);
360 359 }
361 360  
362 361 /*
... ... @@ -365,7 +364,7 @@ public class EntityManagerProxy implements EntityManager, Serializable {
365 364 */
366 365 @Override
367 366 public <T> TypedQuery<T> createQuery(String qlString, Class<T> resultClass) {
368   - return new TypedQueryProxy<T>(getEntityManagerDelegate().createQuery(qlString, resultClass),this);
  367 + return new TypedQueryProxy<T>(getEntityManagerDelegate().createQuery(qlString, resultClass), this);
369 368 }
370 369  
371 370 /*
... ... @@ -383,7 +382,7 @@ public class EntityManagerProxy implements EntityManager, Serializable {
383 382 */
384 383 @Override
385 384 public <T> TypedQuery<T> createNamedQuery(String name, Class<T> resultClass) {
386   - return new TypedQueryProxy<T>(getEntityManagerDelegate().createNamedQuery(name, resultClass),this);
  385 + return new TypedQueryProxy<T>(getEntityManagerDelegate().createNamedQuery(name, resultClass), this);
387 386 }
388 387  
389 388 /*
... ... @@ -410,7 +409,7 @@ public class EntityManagerProxy implements EntityManager, Serializable {
410 409 */
411 410 @Override
412 411 public Query createNativeQuery(String sqlString, String resultSetMapping) {
413   - return new QueryProxy(getEntityManagerDelegate().createNativeQuery(sqlString, resultSetMapping),this);
  412 + return new QueryProxy(getEntityManagerDelegate().createNativeQuery(sqlString, resultSetMapping), this);
414 413 }
415 414  
416 415 /*
... ... @@ -427,17 +426,16 @@ public class EntityManagerProxy implements EntityManager, Serializable {
427 426 */
428 427 protected final void joinTransactionIfNecessary() {
429 428 try {
430   - /*EntityTransaction transaction = */getEntityManagerDelegate().getTransaction();
  429 + /* EntityTransaction transaction = */getEntityManagerDelegate().getTransaction();
431 430 } catch (IllegalStateException cause) {
432   - //IllegalStateException is launched if we are on a JTA entity manager, so
433   - //we assume we need to join transaction instead of creating one.
434   -
435   - try{
  431 + // IllegalStateException is launched if we are on a JTA entity manager, so
  432 + // we assume we need to join transaction instead of creating one.
  433 +
  434 + try {
436 435 getEntityManagerDelegate().joinTransaction();
437   - }
438   - catch(TransactionRequiredException te){
439   - //It get's launched if there is no JTA transaction opened. It usually means we are
440   - //being launched inside a method not marked with @Transactional so we ignore the exception.
  436 + } catch (TransactionRequiredException te) {
  437 + // It get's launched if there is no JTA transaction opened. It usually means we are
  438 + // being launched inside a method not marked with @Transactional so we ignore the exception.
441 439 }
442 440 }
443 441 }
... ... @@ -540,51 +538,51 @@ public class EntityManagerProxy implements EntityManager, Serializable {
540 538 public String toString() {
541 539 return getEntityManagerDelegate().toString();
542 540 }
543   -
544   - private void checkEntityManagerScopePassivable(Object entity) {
  541 +
  542 + private void checkEntityManagerScopePassivable(Object entity) {
545 543 EntityManagerConfig configuration = getConfiguration();
546   - if (configuration.getEntityManagerScope()==EntityManagerScope.CONVERSATION
547   - || configuration.getEntityManagerScope()==EntityManagerScope.SESSION
548   - || configuration.getEntityManagerScope()==EntityManagerScope.VIEW){
549   -
  544 + if (configuration.getEntityManagerScope() == EntityManagerScope.CONVERSATION
  545 + || configuration.getEntityManagerScope() == EntityManagerScope.SESSION
  546 + || configuration.getEntityManagerScope() == EntityManagerScope.VIEW) {
  547 +
550 548 LockModeType lockMode = null;
551   - if (getEntityManagerDelegate().contains(entity)){
  549 + if (getEntityManagerDelegate().contains(entity)) {
552 550 lockMode = getEntityManagerDelegate().getLockMode(entity);
553 551 }
554 552 checkEntityManagerScopePassivable(lockMode);
555 553 }
556 554 }
557 555  
558   - private void checkEntityManagerScopePassivable(LockModeType lockMode) {
  556 + private void checkEntityManagerScopePassivable(LockModeType lockMode) {
559 557 EntityManagerConfig configuration = getConfiguration();
560   - if (configuration.getEntityManagerScope()==EntityManagerScope.CONVERSATION
561   - || configuration.getEntityManagerScope()==EntityManagerScope.SESSION
562   - || configuration.getEntityManagerScope()==EntityManagerScope.VIEW){
563   -
564   - if (lockMode!=null
565   - && lockMode!=LockModeType.NONE
566   - && lockMode!=LockModeType.OPTIMISTIC_FORCE_INCREMENT){
567   - String message = getBundle().getString("passivable-scope-without-optimistic-lock" , configuration.getEntityManagerScope().toString());
568   - getLogger().error(message);
  558 + if (configuration.getEntityManagerScope() == EntityManagerScope.CONVERSATION
  559 + || configuration.getEntityManagerScope() == EntityManagerScope.SESSION
  560 + || configuration.getEntityManagerScope() == EntityManagerScope.VIEW) {
  561 +
  562 + if (lockMode != null && lockMode != LockModeType.NONE
  563 + && lockMode != LockModeType.OPTIMISTIC_FORCE_INCREMENT) {
  564 + String message = getBundle().getString("passivable-scope-without-optimistic-lock",
  565 + configuration.getEntityManagerScope().toString());
  566 + getLogger().severe(message);
569 567 throw new DemoiselleException(message);
570 568 }
571 569 }
572 570 }
573   -
574   - private EntityManagerConfig getConfiguration(){
575   - if (configuration==null){
  571 +
  572 + private EntityManagerConfig getConfiguration() {
  573 + if (configuration == null) {
576 574 configuration = Beans.getReference(EntityManagerConfig.class);
577 575 }
578   -
  576 +
579 577 return configuration;
580 578 }
581   -
  579 +
582 580 private Logger getLogger() {
583   - return Beans.getReference(Logger.class);
  581 + return Beans.getReference(Logger.class, new NameQualifier("br.gov.frameworkdemoiselle.util"));
584 582 }
585   -
586   - private ResourceBundle getBundle(){
587   - return Beans.getReference(ResourceBundle.class,new NameQualifier("demoiselle-jpa-bundle"));
  583 +
  584 + private ResourceBundle getBundle() {
  585 + return Beans.getReference(ResourceBundle.class, new NameQualifier("demoiselle-jpa-bundle"));
588 586 }
589   -
  587 +
590 588 }
... ...