Commit 6d6405c4f0eef15ab2acfd2e320349572f73a374

Authored by Dancovich
1 parent 59e1a071
Exists in master

Refatorando contextos personalizados de escopo para resolver problema de

contextos não serializáveis.
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/bootstrap/CustomContextBootstrap.java
@@ -15,6 +15,7 @@ import br.gov.frameworkdemoiselle.internal.context.TemporarySessionContextImpl; @@ -15,6 +15,7 @@ import br.gov.frameworkdemoiselle.internal.context.TemporarySessionContextImpl;
15 import br.gov.frameworkdemoiselle.internal.context.StaticContextImpl; 15 import br.gov.frameworkdemoiselle.internal.context.StaticContextImpl;
16 import br.gov.frameworkdemoiselle.internal.context.TemporaryConversationContextImpl; 16 import br.gov.frameworkdemoiselle.internal.context.TemporaryConversationContextImpl;
17 import br.gov.frameworkdemoiselle.internal.context.TemporaryViewContextImpl; 17 import br.gov.frameworkdemoiselle.internal.context.TemporaryViewContextImpl;
  18 +import br.gov.frameworkdemoiselle.lifecycle.AfterShutdownProccess;
18 19
19 /** 20 /**
20 * This portable extension registers and starts custom contexts used by 21 * This portable extension registers and starts custom contexts used by
@@ -71,13 +72,17 @@ public class CustomContextBootstrap implements Extension{ @@ -71,13 +72,17 @@ public class CustomContextBootstrap implements Extension{
71 } 72 }
72 } 73 }
73 74
  75 + public void terminateContexts(@Observes AfterShutdownProccess event){
  76 + if (contexts!=null){
  77 + for (CustomContext context : contexts){
  78 + context.deactivate();
  79 + }
  80 +
  81 + contexts.clear();
  82 + }
  83 + }
  84 +
74 public List<CustomContext> getCustomContexts(){ 85 public List<CustomContext> getCustomContexts(){
75 return this.contexts; 86 return this.contexts;
76 } 87 }
77 -  
78 - /*public void storeContexts(@Observes AfterDeploymentValidation event){  
79 - CustomContextProducer producer = Beans.getReference(CustomContextProducer.class);  
80 - producer.addRegisteredContexts(contexts);  
81 - }*/  
82 -  
83 } 88 }
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/context/AbstractCustomContext.java
@@ -37,16 +37,12 @@ @@ -37,16 +37,12 @@
37 package br.gov.frameworkdemoiselle.internal.context; 37 package br.gov.frameworkdemoiselle.internal.context;
38 38
39 import java.lang.annotation.Annotation; 39 import java.lang.annotation.Annotation;
40 -import java.util.Collections;  
41 -import java.util.HashMap;  
42 import java.util.Locale; 40 import java.util.Locale;
43 -import java.util.Map;  
44 41
45 import javax.enterprise.context.ContextNotActiveException; 42 import javax.enterprise.context.ContextNotActiveException;
46 import javax.enterprise.context.spi.Context; 43 import javax.enterprise.context.spi.Context;
47 import javax.enterprise.context.spi.Contextual; 44 import javax.enterprise.context.spi.Contextual;
48 import javax.enterprise.context.spi.CreationalContext; 45 import javax.enterprise.context.spi.CreationalContext;
49 -import javax.enterprise.inject.spi.Bean;  
50 import javax.enterprise.inject.spi.BeanManager; 46 import javax.enterprise.inject.spi.BeanManager;
51 47
52 import org.slf4j.Logger; 48 import org.slf4j.Logger;
@@ -71,7 +67,9 @@ public abstract class AbstractCustomContext implements CustomContext { @@ -71,7 +67,9 @@ public abstract class AbstractCustomContext implements CustomContext {
71 this.active = false; 67 this.active = false;
72 } 68 }
73 69
74 - protected abstract Store getStore(); 70 + protected abstract BeanStore getStore();
  71 +
  72 + protected abstract ContextualStore getContextualStore();
75 73
76 protected abstract boolean isStoreInitialized(); 74 protected abstract boolean isStoreInitialized();
77 75
@@ -89,22 +87,22 @@ public abstract class AbstractCustomContext implements CustomContext { @@ -89,22 +87,22 @@ public abstract class AbstractCustomContext implements CustomContext {
89 throw new ContextNotActiveException(); 87 throw new ContextNotActiveException();
90 } 88 }
91 89
92 - Class<?> type = getType(contextual);  
93 - if (getStore().contains(type)) {  
94 - instance = (T) getStore().get(type);  
95 -  
96 - } else if (creationalContext != null) { 90 + String id = getContextualStore().tryRegisterAndGetId(contextual);
  91 + if (getStore().contains(id)) {
  92 + instance = (T) getStore().getInstance(id);
  93 + }
  94 + else if (creationalContext!=null){
97 instance = contextual.create(creationalContext); 95 instance = contextual.create(creationalContext);
98 - getStore().put(type, instance); 96 + getStore().put(id, instance,creationalContext);
99 } 97 }
100 98
101 return instance; 99 return instance;
102 } 100 }
103 101
104 - private <T> Class<?> getType(final Contextual<T> contextual) { 102 + /*private <T> Class<?> getType(final Contextual<T> contextual) {
105 Bean<T> bean = (Bean<T>) contextual; 103 Bean<T> bean = (Bean<T>) contextual;
106 return bean.getBeanClass(); 104 return bean.getBeanClass();
107 - } 105 + }*/
108 106
109 @Override 107 @Override
110 public boolean isActive() { 108 public boolean isActive() {
@@ -136,15 +134,27 @@ public abstract class AbstractCustomContext implements CustomContext { @@ -136,15 +134,27 @@ public abstract class AbstractCustomContext implements CustomContext {
136 return this.active; 134 return this.active;
137 } 135 }
138 136
  137 + @SuppressWarnings({ "rawtypes", "unchecked" })
139 @Override 138 @Override
140 public void deactivate(){ 139 public void deactivate(){
141 if (this.active){ 140 if (this.active){
142 if (isStoreInitialized()){ 141 if (isStoreInitialized()){
  142 + for (String id : getStore()){
  143 + Contextual contextual = getContextualStore().getContextual(id);
  144 + Object instance = getStore().getInstance(id);
  145 + CreationalContext creationalContext = getStore().getCreationalContext(id);
  146 +
  147 + if (contextual!=null && instance!=null){
  148 + contextual.destroy(instance, creationalContext);
  149 + }
  150 + }
  151 +
143 getStore().clear(); 152 getStore().clear();
  153 + getContextualStore().clear();
144 } 154 }
145 - 155 +
146 this.active = false; 156 this.active = false;
147 - 157 +
148 Logger logger = getLogger(); 158 Logger logger = getLogger();
149 ResourceBundle bundle = getBundle(); 159 ResourceBundle bundle = getBundle();
150 logger.debug( bundle.getString("custom-context-was-deactivated" , this.getClass().getCanonicalName() , this.getScope().getSimpleName() ) ); 160 logger.debug( bundle.getString("custom-context-was-deactivated" , this.getClass().getCanonicalName() , this.getScope().getSimpleName() ) );
@@ -156,8 +166,12 @@ public abstract class AbstractCustomContext implements CustomContext { @@ -156,8 +166,12 @@ public abstract class AbstractCustomContext implements CustomContext {
156 return this.scope; 166 return this.scope;
157 } 167 }
158 168
159 - protected static Store createStore() {  
160 - return new Store(); 169 + protected static BeanStore createStore() {
  170 + return new BeanStore();
  171 + }
  172 +
  173 + protected static ContextualStore createContextualStore() {
  174 + return new ContextualStore();
161 } 175 }
162 176
163 private ResourceBundle getBundle(){ 177 private ResourceBundle getBundle(){
@@ -192,39 +206,4 @@ public abstract class AbstractCustomContext implements CustomContext { @@ -192,39 +206,4 @@ public abstract class AbstractCustomContext implements CustomContext {
192 return false; 206 return false;
193 return true; 207 return true;
194 } 208 }
195 -  
196 - static class Store {  
197 -  
198 - private Map<ClassLoader, Map<Class<?>, Object>> cache = Collections  
199 - .synchronizedMap(new HashMap<ClassLoader, Map<Class<?>, Object>>());  
200 -  
201 - private Store() {  
202 - }  
203 -  
204 - private boolean contains(final Class<?> type) {  
205 - return this.getMap().containsKey(type);  
206 - }  
207 -  
208 - private Object get(final Class<?> type) {  
209 - return this.getMap().get(type);  
210 - }  
211 -  
212 - private void put(final Class<?> type, final Object instance) {  
213 - this.getMap().put(type, instance);  
214 - }  
215 -  
216 - public void clear() {  
217 - cache.clear();  
218 - }  
219 -  
220 - private Map<Class<?>, Object> getMap() {  
221 - ClassLoader classLoader = Thread.currentThread().getContextClassLoader();  
222 -  
223 - if (!cache.containsKey(classLoader)) {  
224 - cache.put(classLoader, Collections.synchronizedMap(new HashMap<Class<?>, Object>()));  
225 - }  
226 -  
227 - return cache.get(classLoader);  
228 - }  
229 - }  
230 } 209 }
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/context/AbstractStaticContext.java
@@ -61,7 +61,9 @@ import br.gov.frameworkdemoiselle.configuration.Configuration; @@ -61,7 +61,9 @@ import br.gov.frameworkdemoiselle.configuration.Configuration;
61 @Priority(Priority.MIN_PRIORITY) 61 @Priority(Priority.MIN_PRIORITY)
62 public abstract class AbstractStaticContext extends AbstractCustomContext { 62 public abstract class AbstractStaticContext extends AbstractCustomContext {
63 63
64 - private final static Map<String, Store> staticStore = Collections.synchronizedMap(new HashMap<String, Store>()); 64 + private final static Map<String, BeanStore> staticBeanStore = Collections.synchronizedMap(new HashMap<String, BeanStore>());
  65 +
  66 + private final static Map<String, ContextualStore> staticContextualStore = Collections.synchronizedMap(new HashMap<String, ContextualStore>());
65 67
66 /** 68 /**
67 * Constructs this context to control the provided scope 69 * Constructs this context to control the provided scope
@@ -71,11 +73,22 @@ public abstract class AbstractStaticContext extends AbstractCustomContext { @@ -71,11 +73,22 @@ public abstract class AbstractStaticContext extends AbstractCustomContext {
71 } 73 }
72 74
73 @Override 75 @Override
74 - protected Store getStore() {  
75 - Store store = staticStore.get( this.getClass().getCanonicalName() ); 76 + protected BeanStore getStore() {
  77 + BeanStore store = staticBeanStore.get( this.getClass().getCanonicalName() );
76 if (store==null){ 78 if (store==null){
77 store = createStore(); 79 store = createStore();
78 - staticStore.put(this.getClass().getCanonicalName(), store); 80 + staticBeanStore.put(this.getClass().getCanonicalName(), store);
  81 + }
  82 +
  83 + return store;
  84 + }
  85 +
  86 + @Override
  87 + protected ContextualStore getContextualStore() {
  88 + ContextualStore store = staticContextualStore.get( this.getClass().getCanonicalName() );
  89 + if (store==null){
  90 + store = createContextualStore();
  91 + staticContextualStore.put(this.getClass().getCanonicalName(), store);
79 } 92 }
80 93
81 return store; 94 return store;
@@ -83,6 +96,6 @@ public abstract class AbstractStaticContext extends AbstractCustomContext { @@ -83,6 +96,6 @@ public abstract class AbstractStaticContext extends AbstractCustomContext {
83 96
84 @Override 97 @Override
85 protected boolean isStoreInitialized() { 98 protected boolean isStoreInitialized() {
86 - return staticStore!=null; 99 + return staticBeanStore!=null;
87 } 100 }
88 } 101 }
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/context/AbstractThreadLocalContext.java
@@ -59,7 +59,9 @@ import java.lang.annotation.Annotation; @@ -59,7 +59,9 @@ import java.lang.annotation.Annotation;
59 */ 59 */
60 public abstract class AbstractThreadLocalContext extends AbstractCustomContext { 60 public abstract class AbstractThreadLocalContext extends AbstractCustomContext {
61 61
62 - private final ThreadLocal<Store> threadLocal = new ThreadLocal<Store>(); 62 + private final ThreadLocal<BeanStore> threadLocalBeans = new ThreadLocal<BeanStore>();
  63 +
  64 + private final ThreadLocal<ContextualStore> threadLocalContextual = new ThreadLocal<ContextualStore>();
63 65
64 AbstractThreadLocalContext(final Class<? extends Annotation> scope) { 66 AbstractThreadLocalContext(final Class<? extends Annotation> scope) {
65 super(scope); 67 super(scope);
@@ -67,15 +69,24 @@ public abstract class AbstractThreadLocalContext extends AbstractCustomContext { @@ -67,15 +69,24 @@ public abstract class AbstractThreadLocalContext extends AbstractCustomContext {
67 69
68 @Override 70 @Override
69 protected boolean isStoreInitialized() { 71 protected boolean isStoreInitialized() {
70 - return threadLocal.get()!=null; 72 + return threadLocalBeans.get()!=null;
71 } 73 }
72 74
73 @Override 75 @Override
74 - protected Store getStore() {  
75 - if (this.threadLocal.get() == null) {  
76 - this.threadLocal.set(createStore()); 76 + protected BeanStore getStore() {
  77 + if (this.threadLocalBeans.get() == null) {
  78 + this.threadLocalBeans.set(createStore());
  79 + }
  80 +
  81 + return this.threadLocalBeans.get();
  82 + }
  83 +
  84 + @Override
  85 + protected ContextualStore getContextualStore() {
  86 + if (this.threadLocalContextual.get() == null) {
  87 + this.threadLocalContextual.set(createContextualStore());
77 } 88 }
78 89
79 - return this.threadLocal.get(); 90 + return this.threadLocalContextual.get();
80 } 91 }
81 } 92 }
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/context/BeanStore.java
@@ -8,25 +8,26 @@ import java.util.Map; @@ -8,25 +8,26 @@ import java.util.Map;
8 8
9 import javax.enterprise.context.spi.CreationalContext; 9 import javax.enterprise.context.spi.CreationalContext;
10 10
11 -public class BeanStore<T> implements Iterable<String>,Serializable { 11 +@SuppressWarnings("rawtypes")
  12 +public class BeanStore implements Iterable<String>,Serializable {
12 13
13 private static final long serialVersionUID = 1L; 14 private static final long serialVersionUID = 1L;
14 15
15 - private Map<String, T> instanceCache = Collections.synchronizedMap( new HashMap<String, T>() );  
16 - private Map<String, CreationalContext<T>> creationalCache = Collections.synchronizedMap( new HashMap<String, CreationalContext<T>>() );; 16 + private Map<String, Object> instanceCache = Collections.synchronizedMap( new HashMap<String, Object>() );
  17 + private Map<String, CreationalContext> creationalCache = Collections.synchronizedMap( new HashMap<String, CreationalContext>() );;
17 18
18 - public void put(String id, T instance,CreationalContext<T> creationalContext){ 19 + public <T> void put(String id, T instance,CreationalContext<T> creationalContext){
19 if (!instanceCache.containsKey(id)){ 20 if (!instanceCache.containsKey(id)){
20 instanceCache.put(id, instance); 21 instanceCache.put(id, instance);
21 creationalCache.put(id, creationalContext); 22 creationalCache.put(id, creationalContext);
22 } 23 }
23 } 24 }
24 25
25 - public T getInstance(String id){ 26 + public Object getInstance(String id){
26 return instanceCache.get(id); 27 return instanceCache.get(id);
27 } 28 }
28 29
29 - public CreationalContext<T> getCreationalContext(String id){ 30 + public CreationalContext getCreationalContext(String id){
30 return creationalCache.get(id); 31 return creationalCache.get(id);
31 } 32 }
32 33
impl/core/src/main/java/br/gov/frameworkdemoiselle/internal/context/ContextualStore.java
@@ -8,7 +8,8 @@ import javax.enterprise.context.spi.Contextual; @@ -8,7 +8,8 @@ import javax.enterprise.context.spi.Contextual;
8 import javax.enterprise.inject.spi.PassivationCapable; 8 import javax.enterprise.inject.spi.PassivationCapable;
9 9
10 10
11 -public class ContextualStore<T> implements Serializable { 11 +@SuppressWarnings("rawtypes")
  12 +public class ContextualStore implements Serializable {
12 13
13 private static final long serialVersionUID = 1L; 14 private static final long serialVersionUID = 1L;
14 15
@@ -16,11 +17,18 @@ public class ContextualStore&lt;T&gt; implements Serializable { @@ -16,11 +17,18 @@ public class ContextualStore&lt;T&gt; implements Serializable {
16 17
17 private AtomicInteger idGenerator = new AtomicInteger(); 18 private AtomicInteger idGenerator = new AtomicInteger();
18 19
19 - private HashMap<String, Contextual<T>> idToContextual = new HashMap<String, Contextual<T>>(); 20 + private HashMap<String, Contextual> idToContextual = new HashMap<String, Contextual>();
20 21
21 - private HashMap<Contextual<T>, String> contextualToId = new HashMap<Contextual<T>, String>(); 22 + private HashMap<Contextual, String> contextualToId = new HashMap<Contextual, String>();
22 23
23 - public String tryRegisterAndGetId(Contextual<T> contextual){ 24 + /**
  25 + * The an unique ID for the given contextual. If it's the first time
  26 + * this contextual is accessed, registers the contextual for latter retrieval.
  27 + *
  28 + * @param contextual The contextual to generate an ID
  29 + * @return The unique ID for the contextual
  30 + */
  31 + public String tryRegisterAndGetId(Contextual contextual){
24 String returnedId; 32 String returnedId;
25 33
26 if (contextualToId.containsKey(contextual)){ 34 if (contextualToId.containsKey(contextual)){
@@ -40,7 +48,7 @@ public class ContextualStore&lt;T&gt; implements Serializable { @@ -40,7 +48,7 @@ public class ContextualStore&lt;T&gt; implements Serializable {
40 return returnedId; 48 return returnedId;
41 } 49 }
42 50
43 - public Contextual<T> getContextual(String id){ 51 + public Contextual getContextual(String id){
44 return idToContextual.get(id); 52 return idToContextual.get(id);
45 } 53 }
46 54
impl/extension/jsf/src/main/java/br/gov/frameworkdemoiselle/internal/context/FacesViewContextImpl.java
@@ -70,14 +70,26 @@ public class FacesViewContextImpl extends AbstractCustomContext implements ViewC @@ -70,14 +70,26 @@ public class FacesViewContextImpl extends AbstractCustomContext implements ViewC
70 } 70 }
71 71
72 @Override 72 @Override
73 - protected Store getStore() { 73 + protected BeanStore getStore() {
74 Map<String, Object> viewMap = Faces.getViewMap(); 74 Map<String, Object> viewMap = Faces.getViewMap();
75 - String key = Store.class.getName(); 75 + String key = BeanStore.class.getName();
76 76
77 if (!viewMap.containsKey(key)) { 77 if (!viewMap.containsKey(key)) {
78 viewMap.put(key, createStore()); 78 viewMap.put(key, createStore());
79 } 79 }
80 80
81 - return (Store) viewMap.get(key); 81 + return (BeanStore) viewMap.get(key);
  82 + }
  83 +
  84 + @Override
  85 + protected ContextualStore getContextualStore() {
  86 + Map<String, Object> viewMap = Faces.getViewMap();
  87 + String key = ContextualStore.class.getName();
  88 +
  89 + if (!viewMap.containsKey(key)) {
  90 + viewMap.put(key, createContextualStore());
  91 + }
  92 +
  93 + return (ContextualStore) viewMap.get(key);
82 } 94 }
83 } 95 }