1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
package org.apache.myfaces.cdi.util; |
21 | |
|
22 | |
import javax.enterprise.context.ContextNotActiveException; |
23 | |
import javax.enterprise.context.spi.Context; |
24 | |
import javax.enterprise.context.spi.Contextual; |
25 | |
import javax.enterprise.context.spi.CreationalContext; |
26 | |
import javax.enterprise.inject.spi.BeanManager; |
27 | |
import javax.enterprise.inject.spi.PassivationCapable; |
28 | |
import java.util.Map; |
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
public abstract class AbstractContext implements Context |
38 | |
{ |
39 | |
|
40 | |
|
41 | |
|
42 | |
private final boolean passivatingScope; |
43 | |
|
44 | |
protected AbstractContext(BeanManager beanManager) |
45 | 0 | { |
46 | 0 | passivatingScope = beanManager.isPassivatingScope(getScope()); |
47 | 0 | } |
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
protected abstract ContextualStorage getContextualStorage(boolean createIfNotExist); |
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
public boolean isPassivatingScope() |
61 | |
{ |
62 | 0 | return passivatingScope; |
63 | |
} |
64 | |
|
65 | |
@Override |
66 | |
public <T> T get(Contextual<T> bean) |
67 | |
{ |
68 | 0 | checkActive(); |
69 | |
|
70 | 0 | ContextualStorage storage = getContextualStorage(false); |
71 | 0 | if (storage == null) |
72 | |
{ |
73 | 0 | return null; |
74 | |
} |
75 | |
|
76 | 0 | Map<Object, ContextualInstanceInfo<?>> contextMap = storage.getStorage(); |
77 | 0 | ContextualInstanceInfo<?> contextualInstanceInfo = contextMap.get(storage.getBeanKey(bean)); |
78 | 0 | if (contextualInstanceInfo == null) |
79 | |
{ |
80 | 0 | return null; |
81 | |
} |
82 | |
|
83 | 0 | return (T) contextualInstanceInfo.getContextualInstance(); |
84 | |
} |
85 | |
|
86 | |
@Override |
87 | |
public <T> T get(Contextual<T> bean, CreationalContext<T> creationalContext) |
88 | |
{ |
89 | 0 | checkActive(); |
90 | |
|
91 | 0 | if (passivatingScope) |
92 | |
{ |
93 | 0 | if (!(bean instanceof PassivationCapable)) |
94 | |
{ |
95 | 0 | throw new IllegalStateException(bean.toString() + |
96 | |
" doesn't implement " + PassivationCapable.class.getName()); |
97 | |
} |
98 | |
} |
99 | |
|
100 | 0 | ContextualStorage storage = getContextualStorage(true); |
101 | |
|
102 | 0 | Map<Object, ContextualInstanceInfo<?>> contextMap = storage.getStorage(); |
103 | 0 | ContextualInstanceInfo<?> contextualInstanceInfo = contextMap.get(storage.getBeanKey(bean)); |
104 | |
|
105 | 0 | if (contextualInstanceInfo != null) |
106 | |
{ |
107 | |
@SuppressWarnings("unchecked") |
108 | 0 | final T instance = (T) contextualInstanceInfo.getContextualInstance(); |
109 | |
|
110 | 0 | if (instance != null) |
111 | |
{ |
112 | 0 | return instance; |
113 | |
} |
114 | |
} |
115 | |
|
116 | 0 | return storage.createContextualInstance(bean, creationalContext); |
117 | |
} |
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
public boolean destroy(Contextual bean) |
125 | |
{ |
126 | 0 | ContextualStorage storage = getContextualStorage(false); |
127 | 0 | if (storage == null) |
128 | |
{ |
129 | 0 | return false; |
130 | |
} |
131 | 0 | ContextualInstanceInfo<?> contextualInstanceInfo = storage.getStorage().get(storage.getBeanKey(bean)); |
132 | |
|
133 | 0 | if (contextualInstanceInfo == null) |
134 | |
{ |
135 | 0 | return false; |
136 | |
} |
137 | |
|
138 | 0 | bean.destroy(contextualInstanceInfo.getContextualInstance(), contextualInstanceInfo.getCreationalContext()); |
139 | |
|
140 | 0 | return true; |
141 | |
} |
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
public void destroyAllActive() |
148 | |
{ |
149 | 0 | ContextualStorage storage = getContextualStorage(false); |
150 | 0 | if (storage == null) |
151 | |
{ |
152 | 0 | return; |
153 | |
} |
154 | |
|
155 | 0 | destroyAllActive(storage); |
156 | 0 | } |
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
public static void destroyAllActive(ContextualStorage storage) |
164 | |
{ |
165 | 0 | Map<Object, ContextualInstanceInfo<?>> contextMap = storage.getStorage(); |
166 | 0 | for (Map.Entry<Object, ContextualInstanceInfo<?>> entry : contextMap.entrySet()) |
167 | |
{ |
168 | 0 | Contextual bean = storage.getBean(entry.getKey()); |
169 | |
|
170 | 0 | ContextualInstanceInfo<?> contextualInstanceInfo = entry.getValue(); |
171 | 0 | bean.destroy(contextualInstanceInfo.getContextualInstance(), contextualInstanceInfo.getCreationalContext()); |
172 | 0 | } |
173 | 0 | } |
174 | |
|
175 | |
|
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | |
protected void checkActive() |
181 | |
{ |
182 | 0 | if (!isActive()) |
183 | |
{ |
184 | 0 | throw new ContextNotActiveException("CDI context with scope annotation @" |
185 | |
+ getScope().getName() + " is not active with respect to the current thread"); |
186 | |
} |
187 | 0 | } |
188 | |
|
189 | |
} |