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