1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.myfaces.cdi.view; |
20 | |
|
21 | |
import java.util.ArrayList; |
22 | |
import java.util.Collection; |
23 | |
import java.util.HashSet; |
24 | |
import java.util.List; |
25 | |
import java.util.Map; |
26 | |
import java.util.Set; |
27 | |
import javax.enterprise.inject.spi.BeanManager; |
28 | |
import javax.faces.context.FacesContext; |
29 | |
import org.apache.myfaces.cdi.util.CDIUtils; |
30 | |
import org.apache.myfaces.cdi.util.ContextualInstanceInfo; |
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | 0 | public class ViewScopeCDIMap implements Map<String, Object> |
37 | |
{ |
38 | |
private String _viewScopeId; |
39 | |
|
40 | |
private ViewScopeContextualStorage storage; |
41 | |
|
42 | |
public ViewScopeCDIMap(FacesContext facesContext) |
43 | 0 | { |
44 | 0 | BeanManager beanManager = CDIUtils. |
45 | |
getBeanManager(facesContext.getExternalContext()); |
46 | |
|
47 | 0 | ViewScopeBeanHolder bean = CDIUtils.lookup( |
48 | |
beanManager, ViewScopeBeanHolder.class); |
49 | |
|
50 | |
|
51 | 0 | _viewScopeId = bean.generateUniqueViewScopeId(); |
52 | |
|
53 | 0 | storage = bean. |
54 | |
getContextualStorage(beanManager, _viewScopeId); |
55 | 0 | } |
56 | |
|
57 | |
public ViewScopeCDIMap(FacesContext facesContext, String viewScopeId) |
58 | 0 | { |
59 | 0 | BeanManager beanManager = CDIUtils. |
60 | |
getBeanManager(facesContext.getExternalContext()); |
61 | |
|
62 | 0 | ViewScopeBeanHolder bean = CDIUtils.lookup( |
63 | |
beanManager, ViewScopeBeanHolder.class); |
64 | |
|
65 | |
|
66 | 0 | _viewScopeId = viewScopeId; |
67 | |
|
68 | 0 | storage = bean. |
69 | |
getContextualStorage(beanManager, _viewScopeId); |
70 | 0 | } |
71 | |
|
72 | |
private ViewScopeContextualStorage getStorage() |
73 | |
{ |
74 | 0 | if (storage != null && !storage.isActive()) |
75 | |
{ |
76 | 0 | storage = null; |
77 | |
} |
78 | 0 | if (storage == null) |
79 | |
{ |
80 | 0 | FacesContext facesContext = FacesContext.getCurrentInstance(); |
81 | 0 | BeanManager beanManager = CDIUtils. |
82 | |
getBeanManager(facesContext.getExternalContext()); |
83 | |
|
84 | 0 | ViewScopeBeanHolder bean = CDIUtils.lookup( |
85 | |
beanManager, ViewScopeBeanHolder.class); |
86 | |
|
87 | 0 | storage = bean. |
88 | |
getContextualStorage(beanManager, _viewScopeId); |
89 | |
} |
90 | 0 | return storage; |
91 | |
} |
92 | |
|
93 | |
private Map<String, Object> getNameBeanKeyMap() |
94 | |
{ |
95 | 0 | return getStorage().getNameBeanKeyMap(); |
96 | |
} |
97 | |
|
98 | |
private Map<Object, ContextualInstanceInfo<?>> getCreationalContextInstances() |
99 | |
{ |
100 | 0 | return getStorage().getStorage(); |
101 | |
} |
102 | |
|
103 | |
public String getViewScopeId() |
104 | |
{ |
105 | 0 | return _viewScopeId; |
106 | |
} |
107 | |
|
108 | |
public int size() |
109 | |
{ |
110 | 0 | return this.getNameBeanKeyMap().size(); |
111 | |
} |
112 | |
|
113 | |
public boolean isEmpty() |
114 | |
{ |
115 | 0 | return this.getNameBeanKeyMap().isEmpty(); |
116 | |
} |
117 | |
|
118 | |
public boolean containsKey(Object key) |
119 | |
{ |
120 | 0 | return this.getNameBeanKeyMap().containsKey(key); |
121 | |
} |
122 | |
|
123 | |
public boolean containsValue(Object value) |
124 | |
{ |
125 | 0 | if (value != null) |
126 | |
{ |
127 | |
for (Map.Entry<Object, ContextualInstanceInfo<?>> entry : |
128 | 0 | getCreationalContextInstances().entrySet()) |
129 | |
{ |
130 | 0 | if (entry.getValue() != null && |
131 | |
value.equals(entry.getValue().getContextualInstance())) |
132 | |
{ |
133 | 0 | return true; |
134 | |
} |
135 | 0 | } |
136 | |
} |
137 | 0 | return false; |
138 | |
} |
139 | |
|
140 | |
public Object get(Object key) |
141 | |
{ |
142 | 0 | Object beanKey = this.getNameBeanKeyMap().get(key); |
143 | 0 | if (beanKey != null) |
144 | |
{ |
145 | 0 | ContextualInstanceInfo<?> info = this.getCreationalContextInstances().get(beanKey); |
146 | 0 | return info == null ? null : info.getContextualInstance(); |
147 | |
} |
148 | 0 | return null; |
149 | |
} |
150 | |
|
151 | |
public Object put(String key, Object value) |
152 | |
{ |
153 | 0 | Object beanKey = new _ContextualKey(key); |
154 | 0 | this.getNameBeanKeyMap().put(key, beanKey); |
155 | 0 | ContextualInstanceInfo info = new ContextualInstanceInfo(); |
156 | 0 | info.setContextualInstance(value); |
157 | 0 | return this.getCreationalContextInstances().put(beanKey, info); |
158 | |
} |
159 | |
|
160 | |
public Object remove(Object key) |
161 | |
{ |
162 | 0 | Object beanKey = this.getNameBeanKeyMap().remove(key); |
163 | 0 | ContextualInstanceInfo info = this.getCreationalContextInstances().remove(beanKey); |
164 | 0 | return info == null ? null : info.getContextualInstance(); |
165 | |
} |
166 | |
|
167 | |
public void putAll(Map<? extends String, ? extends Object> m) |
168 | |
{ |
169 | 0 | for (Map.Entry<? extends String, ? extends Object> entry : m.entrySet()) |
170 | |
{ |
171 | 0 | put(entry.getKey(), entry.getValue()); |
172 | 0 | } |
173 | 0 | } |
174 | |
|
175 | |
public void clear() |
176 | |
{ |
177 | 0 | boolean destroyed = false; |
178 | |
|
179 | |
|
180 | |
|
181 | 0 | FacesContext facesContext = FacesContext.getCurrentInstance(); |
182 | 0 | if (facesContext != null) |
183 | |
{ |
184 | 0 | BeanManager beanManager = CDIUtils. |
185 | |
getBeanManager(facesContext.getExternalContext()); |
186 | |
|
187 | 0 | if (beanManager != null) |
188 | |
{ |
189 | 0 | ViewScopeBeanHolder bean = CDIUtils.lookup( |
190 | |
beanManager, ViewScopeBeanHolder.class); |
191 | 0 | if (bean != null) |
192 | |
{ |
193 | 0 | ViewScopeContextualStorage st = bean. |
194 | |
getContextualStorage(beanManager, _viewScopeId); |
195 | 0 | if (st != null) |
196 | |
{ |
197 | 0 | ViewScopeContextImpl.destroyAllActive(st); |
198 | 0 | storage = null; |
199 | 0 | destroyed = true; |
200 | |
} |
201 | |
} |
202 | |
} |
203 | |
} |
204 | 0 | if (!destroyed) |
205 | |
{ |
206 | 0 | ViewScopeContextImpl.destroyAllActive(storage); |
207 | |
} |
208 | 0 | } |
209 | |
|
210 | |
public Set<String> keySet() |
211 | |
{ |
212 | 0 | return this.getNameBeanKeyMap().keySet(); |
213 | |
} |
214 | |
|
215 | |
public Collection<Object> values() |
216 | |
{ |
217 | 0 | List<Object> values = new ArrayList<Object>(this.getNameBeanKeyMap().size()); |
218 | |
for (Map.Entry<String, Object> entry : |
219 | 0 | this.getNameBeanKeyMap().entrySet()) |
220 | |
{ |
221 | 0 | if (entry.getValue() != null) |
222 | |
{ |
223 | 0 | ContextualInstanceInfo info = |
224 | |
this.getCreationalContextInstances().get(entry.getValue()); |
225 | 0 | if (info != null) |
226 | |
{ |
227 | 0 | values.add(info.getContextualInstance()); |
228 | |
} |
229 | |
} |
230 | 0 | } |
231 | 0 | return values; |
232 | |
} |
233 | |
|
234 | |
public Set<Entry<String, Object>> entrySet() |
235 | |
{ |
236 | 0 | Set<Entry<String, Object>> values = new HashSet<Entry<String, Object>>(); |
237 | |
for (Map.Entry<String, Object> entry : |
238 | 0 | this.getNameBeanKeyMap().entrySet()) |
239 | |
{ |
240 | 0 | if (entry.getValue() != null) |
241 | |
{ |
242 | 0 | ContextualInstanceInfo info = |
243 | |
this.getCreationalContextInstances().get(entry.getValue()); |
244 | 0 | if (info != null) |
245 | |
{ |
246 | 0 | values.add(new EntryWrapper(entry)); |
247 | |
} |
248 | |
} |
249 | 0 | } |
250 | 0 | return values; |
251 | |
} |
252 | |
|
253 | |
private class EntryWrapper<String, Object> implements Entry<String, Object> |
254 | |
{ |
255 | |
private Map.Entry<String, Object> entry; |
256 | |
|
257 | |
public EntryWrapper(Map.Entry<String, Object> entry) |
258 | 0 | { |
259 | 0 | this.entry = entry; |
260 | 0 | } |
261 | |
|
262 | |
public String getKey() |
263 | |
{ |
264 | 0 | return entry.getKey(); |
265 | |
} |
266 | |
|
267 | |
public Object getValue() |
268 | |
{ |
269 | 0 | ContextualInstanceInfo<?> info = getCreationalContextInstances().get(entry.getValue()); |
270 | 0 | return (Object) (info == null ? null : info.getContextualInstance()); |
271 | |
} |
272 | |
|
273 | |
public Object setValue(Object value) |
274 | |
{ |
275 | 0 | ContextualInstanceInfo info = getCreationalContextInstances().get(entry.getValue()); |
276 | 0 | Object oldValue = null; |
277 | 0 | if (info != null) |
278 | |
{ |
279 | 0 | info.setContextualInstance(value); |
280 | |
} |
281 | |
else |
282 | |
{ |
283 | 0 | info = new ContextualInstanceInfo(); |
284 | 0 | info.setContextualInstance(value); |
285 | 0 | getCreationalContextInstances().put(entry.getValue(), info); |
286 | |
} |
287 | 0 | return oldValue; |
288 | |
} |
289 | |
} |
290 | |
} |