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 ViewTransientScopedContextImpl implements Context
39 {
40
41 private BeanManager beanManager;
42
43 public ViewTransientScopedContextImpl(BeanManager beanManager)
44 {
45 this.beanManager = beanManager;
46 }
47
48 protected ViewTransientScopeBeanHolder getViewTransientScopeBeanHolder()
49 {
50 return getViewTransientScopeBeanHolder(FacesContext.getCurrentInstance());
51 }
52
53 protected ViewTransientScopeBeanHolder getViewTransientScopeBeanHolder(FacesContext facesContext)
54 {
55 return new ViewTransientScopeBeanHolder();
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 getViewTransientScopeBeanHolder(facesContext).getContextualStorage(beanManager, facesContext);
74 }
75 else
76 {
77 return getViewTransientScopeBeanHolder(facesContext).getContextualStorageNoCreate(
78 beanManager, facesContext);
79 }
80 }
81
82 public Class<? extends Annotation> getScope()
83 {
84 return ViewTransientScoped.class;
85 }
86
87 public boolean isActive()
88 {
89 return isActive(FacesContext.getCurrentInstance());
90 }
91
92 public boolean isActive(FacesContext facesContext)
93 {
94 if (facesContext == null)
95 {
96 return false;
97 }
98 else if (facesContext.getViewRoot() == null)
99 {
100 return false;
101 }
102 return true;
103 }
104
105 @Override
106 public <T> T get(Contextual<T> bean)
107 {
108 FacesContext facesContext = FacesContext.getCurrentInstance();
109
110 checkActive(facesContext);
111
112 if (facesContext != null)
113 {
114 ContextualStorage storage = getContextualStorage(false, facesContext);
115 if (storage != null)
116 {
117 Map<Object, ContextualInstanceInfo<?>> contextMap = storage.getStorage();
118 ContextualInstanceInfo<?> contextualInstanceInfo = contextMap.get(storage.getBeanKey(bean));
119
120 if (contextualInstanceInfo != null)
121 {
122 return (T) contextualInstanceInfo.getContextualInstance();
123 }
124 }
125 }
126 else
127 {
128 throw new IllegalStateException("FacesContext cannot be found when resolving bean " +bean.toString());
129 }
130 return null;
131 }
132
133 public <T> T get(Contextual<T> bean, CreationalContext<T> creationalContext)
134 {
135 FacesContext facesContext = FacesContext.getCurrentInstance();
136
137 checkActive(facesContext);
138
139 if (!(bean instanceof PassivationCapable))
140 {
141 throw new IllegalStateException(bean.toString() +
142 " doesn't implement " + PassivationCapable.class.getName());
143 }
144
145 ContextualStorage storage = getContextualStorage(true, facesContext);
146
147 Map<Object, ContextualInstanceInfo<?>> contextMap = storage.getStorage();
148 ContextualInstanceInfo<?> contextualInstanceInfo = contextMap.get(storage.getBeanKey(bean));
149
150 if (contextualInstanceInfo != null)
151 {
152 @SuppressWarnings("unchecked")
153 final T instance = (T) contextualInstanceInfo.getContextualInstance();
154
155 if (instance != null)
156 {
157 return instance;
158 }
159 }
160
161 return storage.createContextualInstance(bean, creationalContext);
162 }
163
164
165
166
167
168
169 public boolean destroy(Contextual bean)
170 {
171 FacesContext facesContext = FacesContext.getCurrentInstance();
172 ContextualStorage storage = getContextualStorage(false, facesContext);
173 if (storage == null)
174 {
175 return false;
176 }
177 ContextualInstanceInfo<?> contextualInstanceInfo = storage.getStorage().get(storage.getBeanKey(bean));
178
179 if (contextualInstanceInfo == null)
180 {
181 return false;
182 }
183
184 bean.destroy(contextualInstanceInfo.getContextualInstance(), contextualInstanceInfo.getCreationalContext());
185 return true;
186 }
187
188
189
190
191
192
193 public static void destroyAllActive(ContextualStorage storage)
194 {
195 Map<Object, ContextualInstanceInfo<?>> contextMap = storage.getStorage();
196 for (Map.Entry<Object, ContextualInstanceInfo<?>> entry : contextMap.entrySet())
197 {
198 if (!ViewTransientScopeBeanHolder.VIEW_TRANSIENT_SCOPE_MAP_INFO.equals(entry.getKey()))
199 {
200 Contextual bean = storage.getBean(entry.getKey());
201
202 ContextualInstanceInfo<?> contextualInstanceInfo = entry.getValue();
203 bean.destroy(contextualInstanceInfo.getContextualInstance(),
204 contextualInstanceInfo.getCreationalContext());
205 }
206 }
207 }
208
209
210
211
212
213
214 protected void checkActive(FacesContext facesContext)
215 {
216 if (!isActive(facesContext))
217 {
218 throw new ContextNotActiveException("CDI context with scope annotation @"
219 + getScope().getName() + " is not active with respect to the current thread");
220 }
221 }
222
223 }