From aae2ffe2f58aeade6445fe2492f9e5b7a1489f2c Mon Sep 17 00:00:00 2001 From: Cleverson Sacramento Date: Thu, 30 Oct 2014 09:19:57 -0200 Subject: [PATCH] FWK-202: Injeção de java.util.logging.Logger --- impl/extension/jpa/src/main/java/br/gov/frameworkdemoiselle/internal/producer/AbstractEntityManagerStore.java | 61 ++++++++++++++++++++++++++++++------------------------------- impl/extension/jpa/src/main/java/br/gov/frameworkdemoiselle/internal/producer/EntityManagerFactoryProducer.java | 18 ++++++++++-------- impl/extension/jpa/src/main/java/br/gov/frameworkdemoiselle/internal/producer/Persistences.java | 9 ++++----- impl/extension/jpa/src/main/java/br/gov/frameworkdemoiselle/internal/proxy/EntityManagerProxy.java | 104 +++++++++++++++++++++++++++++++++++++++++++++++++++----------------------------------------------------- 4 files changed, 95 insertions(+), 97 deletions(-) diff --git a/impl/extension/jpa/src/main/java/br/gov/frameworkdemoiselle/internal/producer/AbstractEntityManagerStore.java b/impl/extension/jpa/src/main/java/br/gov/frameworkdemoiselle/internal/producer/AbstractEntityManagerStore.java index acd7312..876f248 100644 --- a/impl/extension/jpa/src/main/java/br/gov/frameworkdemoiselle/internal/producer/AbstractEntityManagerStore.java +++ b/impl/extension/jpa/src/main/java/br/gov/frameworkdemoiselle/internal/producer/AbstractEntityManagerStore.java @@ -39,13 +39,12 @@ package br.gov.frameworkdemoiselle.internal.producer; import java.util.Collections; import java.util.HashMap; import java.util.Map; +import java.util.logging.Logger; import javax.enterprise.context.RequestScoped; import javax.persistence.EntityManager; import javax.persistence.FlushModeType; -import org.slf4j.Logger; - import br.gov.frameworkdemoiselle.internal.configuration.EntityManagerConfig; import br.gov.frameworkdemoiselle.internal.configuration.EntityManagerConfig.EntityManagerScope; import br.gov.frameworkdemoiselle.util.Beans; @@ -53,25 +52,25 @@ import br.gov.frameworkdemoiselle.util.NameQualifier; import br.gov.frameworkdemoiselle.util.ResourceBundle; /** - * - *

Stores produced entity managers. When the {@link EntityManagerProducer} try to create an entity manager it will - * seach this store for a cached instance, only creating a new instance if this cache doesn't contain a suitable one.

- * - *

There are several concrete implementations of this class, each one corresponding to a scoped cache (ex: {@link RequestEntityManagerStore} - * stores Entity Managers on the request scope). To select witch implementation is used (and with that, what scope is used to store Entity Managers) - * open the "demoiselle.properties" file and edit the property "frameworkdemoiselle.persistence.entitymanager.scope". The default scope is the - * {@link RequestScoped}.

- * + *

+ * Stores produced entity managers. When the {@link EntityManagerProducer} try to create an entity manager it will seach + * this store for a cached instance, only creating a new instance if this cache doesn't contain a suitable one. + *

+ *

+ * There are several concrete implementations of this class, each one corresponding to a scoped cache (ex: + * {@link RequestEntityManagerStore} stores Entity Managers on the request scope). To select witch implementation is + * used (and with that, what scope is used to store Entity Managers) open the "demoiselle.properties" file and edit the + * property "frameworkdemoiselle.persistence.entitymanager.scope". The default scope is the {@link RequestScoped}. + *

* * @author serpro - * */ public abstract class AbstractEntityManagerStore implements EntityManagerStore { - + private static final long serialVersionUID = 1L; private final Map cache = Collections.synchronizedMap(new HashMap()); - + public EntityManager getEntityManager(String persistenceUnit) { EntityManager entityManager = null; @@ -88,7 +87,7 @@ public abstract class AbstractEntityManagerStore implements EntityManagerStore { return entityManager; } - + void init() { for (String persistenceUnit : getFactory().getCache().keySet()) { getEntityManager(persistenceUnit); @@ -96,11 +95,11 @@ public abstract class AbstractEntityManagerStore implements EntityManagerStore { } void close() { - //Se o produtor não possui escopo, então o ciclo de vida - //de EntityManager produzidos é responsabilidade do desenvolvedor. Não - //fechamos os EntityManagers aqui. + // Se o produtor não possui escopo, então o ciclo de vida + // de EntityManager produzidos é responsabilidade do desenvolvedor. Não + // fechamos os EntityManagers aqui. EntityManagerConfig configuration = getConfiguration(); - if (configuration.getEntityManagerScope() != EntityManagerScope.NOSCOPE){ + if (configuration.getEntityManagerScope() != EntityManagerScope.NOSCOPE) { for (EntityManager entityManager : cache.values()) { entityManager.close(); } @@ -109,26 +108,26 @@ public abstract class AbstractEntityManagerStore implements EntityManagerStore { } public Map getCache() { - if (cache==null || cache.isEmpty()){ + if (cache == null || cache.isEmpty()) { init(); } - + return cache; } - - private EntityManagerFactoryProducer getFactory(){ + + private EntityManagerFactoryProducer getFactory() { return Beans.getReference(EntityManagerFactoryProducer.class); } - - private Logger getLogger(){ - return Beans.getReference(Logger.class); + + private Logger getLogger() { + return Beans.getReference(Logger.class, new NameQualifier("br.gov.frameworkdemoiselle.util")); } - - private ResourceBundle getBundle(){ - return Beans.getReference(ResourceBundle.class , new NameQualifier("demoiselle-jpa-bundle")); + + private ResourceBundle getBundle() { + return Beans.getReference(ResourceBundle.class, new NameQualifier("demoiselle-jpa-bundle")); } - - private EntityManagerConfig getConfiguration(){ + + private EntityManagerConfig getConfiguration() { return Beans.getReference(EntityManagerConfig.class); } } diff --git a/impl/extension/jpa/src/main/java/br/gov/frameworkdemoiselle/internal/producer/EntityManagerFactoryProducer.java b/impl/extension/jpa/src/main/java/br/gov/frameworkdemoiselle/internal/producer/EntityManagerFactoryProducer.java index dc2848a..ed205cf 100644 --- a/impl/extension/jpa/src/main/java/br/gov/frameworkdemoiselle/internal/producer/EntityManagerFactoryProducer.java +++ b/impl/extension/jpa/src/main/java/br/gov/frameworkdemoiselle/internal/producer/EntityManagerFactoryProducer.java @@ -36,11 +36,14 @@ */ package br.gov.frameworkdemoiselle.internal.producer; +import static java.util.logging.Level.SEVERE; + import java.io.Serializable; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.Map; +import java.util.logging.Logger; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; @@ -54,7 +57,6 @@ import javax.persistence.Persistence; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; -import org.slf4j.Logger; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; @@ -72,20 +74,20 @@ public class EntityManagerFactoryProducer implements Serializable { private static final long serialVersionUID = 1L; private static final String ENTITY_MANAGER_RESOURCE = "META-INF/persistence.xml"; - + @Inject protected Logger logger; @Inject @Name("demoiselle-jpa-bundle") protected ResourceBundle bundle; - + @Inject private Persistences persistenceUnitReader; private final Map> factoryCache = Collections .synchronizedMap(new HashMap>()); - + @Default @Produces protected EntityManagerFactory createDefault(EntityManagerConfig config) { @@ -104,7 +106,7 @@ public class EntityManagerFactoryProducer implements Serializable { String persistenceUnit = ip.getAnnotated().getAnnotation(Name.class).value(); return create(persistenceUnit); } - + public EntityManagerFactory create(String persistenceUnit) { EntityManagerFactory factory; @@ -151,7 +153,7 @@ public class EntityManagerFactoryProducer implements Serializable { } catch (Exception cause) { String message = bundle.getString("can-not-get-persistence-unit-from-persistence"); - logger.error(message, cause); + logger.log(SEVERE, message, cause); throw new DemoiselleException(message, cause); } @@ -169,7 +171,7 @@ public class EntityManagerFactoryProducer implements Serializable { throw new DemoiselleException(cause); } - logger.debug(bundle.getString("persistence-unit-name-found", persistenceUnit)); + logger.fine(bundle.getString("persistence-unit-name-found", persistenceUnit)); } } @@ -188,7 +190,7 @@ public class EntityManagerFactoryProducer implements Serializable { Map result = factoryCache.get(classLoader); if (result == null || result.isEmpty()) { - logger.debug(bundle.getString("entity-manager-factory-not-found-in-cache")); + logger.fine(bundle.getString("entity-manager-factory-not-found-in-cache")); for (String persistenceUnit : loadPersistenceUnitFromClassloader(classLoader)) { create(persistenceUnit); result = factoryCache.get(classLoader); diff --git a/impl/extension/jpa/src/main/java/br/gov/frameworkdemoiselle/internal/producer/Persistences.java b/impl/extension/jpa/src/main/java/br/gov/frameworkdemoiselle/internal/producer/Persistences.java index 249b67f..b28b065 100644 --- a/impl/extension/jpa/src/main/java/br/gov/frameworkdemoiselle/internal/producer/Persistences.java +++ b/impl/extension/jpa/src/main/java/br/gov/frameworkdemoiselle/internal/producer/Persistences.java @@ -36,16 +36,16 @@ */ package br.gov.frameworkdemoiselle.internal.producer; +import static br.gov.frameworkdemoiselle.configuration.Configuration.DEFAULT_RESOURCE; + import java.util.Set; +import java.util.logging.Logger; import javax.inject.Inject; import javax.inject.Singleton; -import org.slf4j.Logger; - import br.gov.frameworkdemoiselle.DemoiselleException; import br.gov.frameworkdemoiselle.annotation.Name; -import br.gov.frameworkdemoiselle.configuration.Configuration; import br.gov.frameworkdemoiselle.internal.configuration.EntityManagerConfig; import br.gov.frameworkdemoiselle.util.ResourceBundle; @@ -73,8 +73,7 @@ public class Persistences { String persistenceUnit = config.getDefaultPersistenceUnitName(); if (persistenceUnit != null) { - this.logger.debug(bundle.getString("getting-persistence-unit-from-properties", - Configuration.DEFAULT_RESOURCE)); + this.logger.fine(bundle.getString("getting-persistence-unit-from-properties", DEFAULT_RESOURCE)); } return persistenceUnit; diff --git a/impl/extension/jpa/src/main/java/br/gov/frameworkdemoiselle/internal/proxy/EntityManagerProxy.java b/impl/extension/jpa/src/main/java/br/gov/frameworkdemoiselle/internal/proxy/EntityManagerProxy.java index 7670fee..eaf5d81 100644 --- a/impl/extension/jpa/src/main/java/br/gov/frameworkdemoiselle/internal/proxy/EntityManagerProxy.java +++ b/impl/extension/jpa/src/main/java/br/gov/frameworkdemoiselle/internal/proxy/EntityManagerProxy.java @@ -38,6 +38,7 @@ package br.gov.frameworkdemoiselle.internal.proxy; import java.io.Serializable; import java.util.Map; +import java.util.logging.Logger; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; @@ -51,8 +52,6 @@ import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaQuery; import javax.persistence.metamodel.Metamodel; -import org.slf4j.Logger; - import br.gov.frameworkdemoiselle.DemoiselleException; import br.gov.frameworkdemoiselle.internal.configuration.EntityManagerConfig; import br.gov.frameworkdemoiselle.internal.configuration.EntityManagerConfig.EntityManagerScope; @@ -69,19 +68,18 @@ import br.gov.frameworkdemoiselle.util.ResourceBundle; public class EntityManagerProxy implements EntityManager, Serializable { private static final long serialVersionUID = 1L; - + /* * Persistence unit of the delegated EntityManager. */ private String persistenceUnit; - + /* * demoiselle-jpa configuration options */ private EntityManagerConfig configuration; - - - private EntityManager delegateCache; + + private EntityManager delegateCache; /** * Constructor based on persistence unit name. @@ -99,14 +97,15 @@ public class EntityManagerProxy implements EntityManager, Serializable { * @return Cached EntityManager */ private EntityManager getEntityManagerDelegate() { - //Se o produtor de EntityManager não estiver em um escopo, precisamos guardar em cache o EntityManager produzido, - //do contrário, basta solicitar uma instância do produtor (que estará em um escopo) e obter a instância real - //de EntityManager dele. - if (getConfiguration().getEntityManagerScope()!=EntityManagerScope.NOSCOPE || delegateCache==null){ + // Se o produtor de EntityManager não estiver em um escopo, precisamos guardar em cache o EntityManager + // produzido, + // do contrário, basta solicitar uma instância do produtor (que estará em um escopo) e obter a instância real + // de EntityManager dele. + if (getConfiguration().getEntityManagerScope() != EntityManagerScope.NOSCOPE || delegateCache == null) { EntityManagerProducer emp = Beans.getReference(EntityManagerProducer.class); delegateCache = emp.getEntityManager(this.persistenceUnit); } - + return delegateCache; } @@ -347,7 +346,7 @@ public class EntityManagerProxy implements EntityManager, Serializable { */ @Override public Query createQuery(String qlString) { - return new QueryProxy(getEntityManagerDelegate().createQuery(qlString) , this) ; + return new QueryProxy(getEntityManagerDelegate().createQuery(qlString), this); } /* @@ -356,7 +355,7 @@ public class EntityManagerProxy implements EntityManager, Serializable { */ @Override public TypedQuery createQuery(CriteriaQuery criteriaQuery) { - return new TypedQueryProxy( getEntityManagerDelegate().createQuery(criteriaQuery) , this ); + return new TypedQueryProxy(getEntityManagerDelegate().createQuery(criteriaQuery), this); } /* @@ -365,7 +364,7 @@ public class EntityManagerProxy implements EntityManager, Serializable { */ @Override public TypedQuery createQuery(String qlString, Class resultClass) { - return new TypedQueryProxy(getEntityManagerDelegate().createQuery(qlString, resultClass),this); + return new TypedQueryProxy(getEntityManagerDelegate().createQuery(qlString, resultClass), this); } /* @@ -383,7 +382,7 @@ public class EntityManagerProxy implements EntityManager, Serializable { */ @Override public TypedQuery createNamedQuery(String name, Class resultClass) { - return new TypedQueryProxy(getEntityManagerDelegate().createNamedQuery(name, resultClass),this); + return new TypedQueryProxy(getEntityManagerDelegate().createNamedQuery(name, resultClass), this); } /* @@ -410,7 +409,7 @@ public class EntityManagerProxy implements EntityManager, Serializable { */ @Override public Query createNativeQuery(String sqlString, String resultSetMapping) { - return new QueryProxy(getEntityManagerDelegate().createNativeQuery(sqlString, resultSetMapping),this); + return new QueryProxy(getEntityManagerDelegate().createNativeQuery(sqlString, resultSetMapping), this); } /* @@ -427,17 +426,16 @@ public class EntityManagerProxy implements EntityManager, Serializable { */ protected final void joinTransactionIfNecessary() { try { - /*EntityTransaction transaction = */getEntityManagerDelegate().getTransaction(); + /* EntityTransaction transaction = */getEntityManagerDelegate().getTransaction(); } catch (IllegalStateException cause) { - //IllegalStateException is launched if we are on a JTA entity manager, so - //we assume we need to join transaction instead of creating one. - - try{ + // IllegalStateException is launched if we are on a JTA entity manager, so + // we assume we need to join transaction instead of creating one. + + try { getEntityManagerDelegate().joinTransaction(); - } - catch(TransactionRequiredException te){ - //It get's launched if there is no JTA transaction opened. It usually means we are - //being launched inside a method not marked with @Transactional so we ignore the exception. + } catch (TransactionRequiredException te) { + // It get's launched if there is no JTA transaction opened. It usually means we are + // being launched inside a method not marked with @Transactional so we ignore the exception. } } } @@ -540,51 +538,51 @@ public class EntityManagerProxy implements EntityManager, Serializable { public String toString() { return getEntityManagerDelegate().toString(); } - - private void checkEntityManagerScopePassivable(Object entity) { + + private void checkEntityManagerScopePassivable(Object entity) { EntityManagerConfig configuration = getConfiguration(); - if (configuration.getEntityManagerScope()==EntityManagerScope.CONVERSATION - || configuration.getEntityManagerScope()==EntityManagerScope.SESSION - || configuration.getEntityManagerScope()==EntityManagerScope.VIEW){ - + if (configuration.getEntityManagerScope() == EntityManagerScope.CONVERSATION + || configuration.getEntityManagerScope() == EntityManagerScope.SESSION + || configuration.getEntityManagerScope() == EntityManagerScope.VIEW) { + LockModeType lockMode = null; - if (getEntityManagerDelegate().contains(entity)){ + if (getEntityManagerDelegate().contains(entity)) { lockMode = getEntityManagerDelegate().getLockMode(entity); } checkEntityManagerScopePassivable(lockMode); } } - private void checkEntityManagerScopePassivable(LockModeType lockMode) { + private void checkEntityManagerScopePassivable(LockModeType lockMode) { EntityManagerConfig configuration = getConfiguration(); - if (configuration.getEntityManagerScope()==EntityManagerScope.CONVERSATION - || configuration.getEntityManagerScope()==EntityManagerScope.SESSION - || configuration.getEntityManagerScope()==EntityManagerScope.VIEW){ - - if (lockMode!=null - && lockMode!=LockModeType.NONE - && lockMode!=LockModeType.OPTIMISTIC_FORCE_INCREMENT){ - String message = getBundle().getString("passivable-scope-without-optimistic-lock" , configuration.getEntityManagerScope().toString()); - getLogger().error(message); + if (configuration.getEntityManagerScope() == EntityManagerScope.CONVERSATION + || configuration.getEntityManagerScope() == EntityManagerScope.SESSION + || configuration.getEntityManagerScope() == EntityManagerScope.VIEW) { + + if (lockMode != null && lockMode != LockModeType.NONE + && lockMode != LockModeType.OPTIMISTIC_FORCE_INCREMENT) { + String message = getBundle().getString("passivable-scope-without-optimistic-lock", + configuration.getEntityManagerScope().toString()); + getLogger().severe(message); throw new DemoiselleException(message); } } } - - private EntityManagerConfig getConfiguration(){ - if (configuration==null){ + + private EntityManagerConfig getConfiguration() { + if (configuration == null) { configuration = Beans.getReference(EntityManagerConfig.class); } - + return configuration; } - + private Logger getLogger() { - return Beans.getReference(Logger.class); + return Beans.getReference(Logger.class, new NameQualifier("br.gov.frameworkdemoiselle.util")); } - - private ResourceBundle getBundle(){ - return Beans.getReference(ResourceBundle.class,new NameQualifier("demoiselle-jpa-bundle")); + + private ResourceBundle getBundle() { + return Beans.getReference(ResourceBundle.class, new NameQualifier("demoiselle-jpa-bundle")); } - + } -- libgit2 0.21.2