diff --git a/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/context/AbstractCustomContext.java b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/context/AbstractCustomContext.java index 51146fb..d62a174 100644 --- a/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/context/AbstractCustomContext.java +++ b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/context/AbstractCustomContext.java @@ -36,6 +36,7 @@ */ package br.gov.frameworkdemoiselle.internal.context; +import java.io.Serializable; import java.lang.annotation.Annotation; import java.util.Collections; import java.util.HashMap; @@ -156,8 +157,8 @@ public abstract class AbstractCustomContext implements CustomContext { return this.scope; } - protected static Store createStore() { - return new Store(); + protected static Store createStore(Class owningClass) { + return new Store(owningClass); } private ResourceBundle getBundle(){ @@ -193,38 +194,47 @@ public abstract class AbstractCustomContext implements CustomContext { return true; } - static class Store { + static class Store implements Serializable { - private Map, Object>> cache = Collections - .synchronizedMap(new HashMap, Object>>()); + private static final long serialVersionUID = -8237464177510563034L; + + private final String SEPARATOR = "#"; + + private final String ID_PREFIX; + + private Map cache = Collections.synchronizedMap(new HashMap()); - private Store() { + private Store(Class owningClass) { + ID_PREFIX = Thread.currentThread().getContextClassLoader().toString() + + SEPARATOR + owningClass.getCanonicalName(); } private boolean contains(final Class type) { - return this.getMap().containsKey(type); + return cache.containsKey( prefixId(type.getCanonicalName()) ); } private Object get(final Class type) { - return this.getMap().get(type); + return cache.get( prefixId(type.getCanonicalName()) ); } private void put(final Class type, final Object instance) { - this.getMap().put(type, instance); + cache.put( prefixId(type.getCanonicalName()) , instance); } public void clear() { cache.clear(); } + + private String prefixId(String id){ + return ID_PREFIX + SEPARATOR + id; + } - private Map, Object> getMap() { - ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); - + /*private Map getMap() { if (!cache.containsKey(classLoader)) { cache.put(classLoader, Collections.synchronizedMap(new HashMap, Object>())); } return cache.get(classLoader); - } + }*/ } } diff --git a/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/context/AbstractStaticContext.java b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/context/AbstractStaticContext.java index 8db69fe..63a5ac8 100644 --- a/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/context/AbstractStaticContext.java +++ b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/context/AbstractStaticContext.java @@ -37,6 +37,9 @@ package br.gov.frameworkdemoiselle.internal.context; import java.lang.annotation.Annotation; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; import br.gov.frameworkdemoiselle.annotation.Priority; import br.gov.frameworkdemoiselle.annotation.StaticScoped; @@ -58,7 +61,7 @@ import br.gov.frameworkdemoiselle.configuration.Configuration; @Priority(Priority.MIN_PRIORITY) public abstract class AbstractStaticContext extends AbstractCustomContext { - private final static Store store = createStore(); + private static Map cache = Collections.synchronizedMap( new HashMap() ); /** * Constructs this context to control the provided scope @@ -69,11 +72,16 @@ public abstract class AbstractStaticContext extends AbstractCustomContext { @Override protected Store getStore() { + Store store = cache.get( this.getClass().getCanonicalName() ); + if ( store==null ){ + store = createStore(this.getClass()); + cache.put(this.getClass().getCanonicalName(), store); + } return store; } @Override protected boolean isStoreInitialized() { - return store!=null; + return cache.get( this.getClass().getCanonicalName() )!=null; } } diff --git a/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/context/AbstractThreadLocalContext.java b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/context/AbstractThreadLocalContext.java index 67958e9..13208bb 100644 --- a/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/context/AbstractThreadLocalContext.java +++ b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/context/AbstractThreadLocalContext.java @@ -73,7 +73,7 @@ public abstract class AbstractThreadLocalContext extends AbstractCustomContext { @Override protected Store getStore() { if (this.threadLocal.get() == null) { - this.threadLocal.set(createStore()); + this.threadLocal.set(createStore(getClass())); } return this.threadLocal.get(); diff --git a/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/context/CustomContextProducer.java b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/context/CustomContextProducer.java index aae6c7a..659f30f 100644 --- a/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/context/CustomContextProducer.java +++ b/impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/context/CustomContextProducer.java @@ -60,6 +60,7 @@ import javax.enterprise.inject.spi.InjectionPoint; import org.slf4j.Logger; +import br.gov.frameworkdemoiselle.annotation.Priority; import br.gov.frameworkdemoiselle.context.ConversationContext; import br.gov.frameworkdemoiselle.context.CustomContext; import br.gov.frameworkdemoiselle.context.RequestContext; @@ -133,7 +134,7 @@ public class CustomContextProducer { /////////////PRODUCERS/////////////////// @Produces - public RequestContext getRequestContext(InjectionPoint ip){ + protected RequestContext getRequestContext(InjectionPoint ip){ if (ip!=null){ return getContext(ip); } @@ -143,7 +144,7 @@ public class CustomContextProducer { } @Produces - public SessionContext getSessionContext(InjectionPoint ip){ + protected SessionContext getSessionContext(InjectionPoint ip){ if (ip!=null){ return getContext(ip); } @@ -153,7 +154,7 @@ public class CustomContextProducer { } @Produces - public ViewContext getViewContext(InjectionPoint ip){ + protected ViewContext getViewContext(InjectionPoint ip){ if (ip!=null){ return getContext(ip); } @@ -163,7 +164,7 @@ public class CustomContextProducer { } @Produces - public StaticContext getStaticContext(InjectionPoint ip){ + protected StaticContext getStaticContext(InjectionPoint ip){ if (ip!=null){ return getContext(ip); } @@ -173,7 +174,7 @@ public class CustomContextProducer { } @Produces - public ConversationContext getConversationContext(InjectionPoint ip){ + protected ConversationContext getConversationContext(InjectionPoint ip){ if (ip!=null){ return getContext(ip); } @@ -184,8 +185,17 @@ public class CustomContextProducer { /////////////END OF PRODUCERS/////////////////// + /** + * Obtain a custom context for the provided injection point. + * + * @param ip The object containing information about the injection point - most importantly + * the declared type of the injection point, to decide the context to return + * + * @return A context of a type compatible with the type of the injection point, or null if there is + * no such context. + */ @SuppressWarnings("unchecked") - private T getContext(InjectionPoint ip){ + public T getContext(InjectionPoint ip){ T producedContext = null; if (ip!=null){ @@ -200,8 +210,16 @@ public class CustomContextProducer { return producedContext; } + /** + * Obtain a context compatible with the provided type. + * + * @param contextClass The type of the desired context. The returned context will be compatible with this type, if there + * is more than one compatible type, this method will decide witch one to return based on the {@link Priority} annotation. + * + * @return A context of a type compatible with the informed type, or null if there is no such context. + */ @SuppressWarnings("unchecked") - private T getContext(Class contextClass){ + public T getContext(Class contextClass){ CustomContext producedContext = null; ArrayList selectableContexts = new ArrayList(); 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 266ef59..acd7312 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 @@ -41,13 +41,11 @@ import java.util.HashMap; import java.util.Map; import javax.enterprise.context.RequestScoped; -import javax.inject.Inject; import javax.persistence.EntityManager; import javax.persistence.FlushModeType; import org.slf4j.Logger; -import br.gov.frameworkdemoiselle.annotation.Name; import br.gov.frameworkdemoiselle.internal.configuration.EntityManagerConfig; import br.gov.frameworkdemoiselle.internal.configuration.EntityManagerConfig.EntityManagerScope; import br.gov.frameworkdemoiselle.util.Beans; @@ -74,19 +72,6 @@ public abstract class AbstractEntityManagerStore implements EntityManagerStore { private final Map cache = Collections.synchronizedMap(new HashMap()); - @Inject - private EntityManagerFactoryProducer factory; - - @Inject - private Logger logger; - - @Inject - @Name("demoiselle-jpa-bundle") - private ResourceBundle bundle; - - @Inject - private EntityManagerConfig configuration; - public EntityManager getEntityManager(String persistenceUnit) { EntityManager entityManager = null; @@ -114,6 +99,7 @@ public abstract class AbstractEntityManagerStore implements EntityManagerStore { //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){ for (EntityManager entityManager : cache.values()) { entityManager.close(); @@ -131,23 +117,18 @@ public abstract class AbstractEntityManagerStore implements EntityManagerStore { } private EntityManagerFactoryProducer getFactory(){ - if (factory==null){ - factory = Beans.getReference(EntityManagerFactoryProducer.class); - } - return factory; + return Beans.getReference(EntityManagerFactoryProducer.class); } private Logger getLogger(){ - if (logger==null){ - logger = Beans.getReference(Logger.class); - } - return logger; + return Beans.getReference(Logger.class); } private ResourceBundle getBundle(){ - if (bundle==null){ - bundle = Beans.getReference(ResourceBundle.class , new NameQualifier("demoiselle-jpa-bundle")); - } - return bundle; + return Beans.getReference(ResourceBundle.class , new NameQualifier("demoiselle-jpa-bundle")); + } + + private EntityManagerConfig getConfiguration(){ + return Beans.getReference(EntityManagerConfig.class); } } 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 ab8c70c..7670fee 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 @@ -117,8 +117,8 @@ public class EntityManagerProxy implements EntityManager, Serializable { @Override public void persist(Object entity) { joinTransactionIfNecessary(); - checkEntityManagerScopePassivable(entity); getEntityManagerDelegate().persist(entity); + checkEntityManagerScopePassivable(entity); } /* diff --git a/impl/extension/jsf/src/main/java/br/gov/frameworkdemoiselle/internal/context/FacesViewContextImpl.java b/impl/extension/jsf/src/main/java/br/gov/frameworkdemoiselle/internal/context/FacesViewContextImpl.java index 3910557..54b6d1b 100644 --- a/impl/extension/jsf/src/main/java/br/gov/frameworkdemoiselle/internal/context/FacesViewContextImpl.java +++ b/impl/extension/jsf/src/main/java/br/gov/frameworkdemoiselle/internal/context/FacesViewContextImpl.java @@ -75,7 +75,7 @@ public class FacesViewContextImpl extends AbstractCustomContext implements ViewC String key = Store.class.getName(); if (!viewMap.containsKey(key)) { - viewMap.put(key, createStore()); + viewMap.put(key, createStore( getClass() )); } return (Store) viewMap.get(key); -- libgit2 0.21.2