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 {
46 passivatingScope = beanManager.isPassivatingScope(getScope());
47 }
48
49
50
51
52
53
54
55 protected abstract ContextualStorage getContextualStorage(boolean createIfNotExist);
56
57
58
59
60 public boolean isPassivatingScope()
61 {
62 return passivatingScope;
63 }
64
65 @Override
66 public <T> T get(Contextual<T> bean)
67 {
68 checkActive();
69
70 ContextualStorage storage = getContextualStorage(false);
71 if (storage == null)
72 {
73 return null;
74 }
75
76 Map<Object, ContextualInstanceInfo<?>> contextMap = storage.getStorage();
77 ContextualInstanceInfo<?> contextualInstanceInfo = contextMap.get(storage.getBeanKey(bean));
78 if (contextualInstanceInfo == null)
79 {
80 return null;
81 }
82
83 return (T) contextualInstanceInfo.getContextualInstance();
84 }
85
86 @Override
87 public <T> T get(Contextual<T> bean, CreationalContext<T> creationalContext)
88 {
89 checkActive();
90
91 if (passivatingScope)
92 {
93 if (!(bean instanceof PassivationCapable))
94 {
95 throw new IllegalStateException(bean.toString() +
96 " doesn't implement " + PassivationCapable.class.getName());
97 }
98 }
99
100 ContextualStorage storage = getContextualStorage(true);
101
102 Map<Object, ContextualInstanceInfo<?>> contextMap = storage.getStorage();
103 ContextualInstanceInfo<?> contextualInstanceInfo = contextMap.get(storage.getBeanKey(bean));
104
105 if (contextualInstanceInfo != null)
106 {
107 @SuppressWarnings("unchecked")
108 final T instance = (T) contextualInstanceInfo.getContextualInstance();
109
110 if (instance != null)
111 {
112 return instance;
113 }
114 }
115
116 return storage.createContextualInstance(bean, creationalContext);
117 }
118
119
120
121
122
123
124 public boolean destroy(Contextual bean)
125 {
126 ContextualStorage storage = getContextualStorage(false);
127 if (storage == null)
128 {
129 return false;
130 }
131 ContextualInstanceInfo<?> contextualInstanceInfo = storage.getStorage().get(storage.getBeanKey(bean));
132
133 if (contextualInstanceInfo == null)
134 {
135 return false;
136 }
137
138 bean.destroy(contextualInstanceInfo.getContextualInstance(), contextualInstanceInfo.getCreationalContext());
139
140 return true;
141 }
142
143
144
145
146
147 public void destroyAllActive()
148 {
149 ContextualStorage storage = getContextualStorage(false);
150 if (storage == null)
151 {
152 return;
153 }
154
155 destroyAllActive(storage);
156 }
157
158
159
160
161
162
163 public static void destroyAllActive(ContextualStorage storage)
164 {
165 Map<Object, ContextualInstanceInfo<?>> contextMap = storage.getStorage();
166 for (Map.Entry<Object, ContextualInstanceInfo<?>> entry : contextMap.entrySet())
167 {
168 Contextual bean = storage.getBean(entry.getKey());
169
170 ContextualInstanceInfo<?> contextualInstanceInfo = entry.getValue();
171 bean.destroy(contextualInstanceInfo.getContextualInstance(), contextualInstanceInfo.getCreationalContext());
172 }
173 }
174
175
176
177
178
179
180 protected void checkActive()
181 {
182 if (!isActive())
183 {
184 throw new ContextNotActiveException("CDI context with scope annotation @"
185 + getScope().getName() + " is not active with respect to the current thread");
186 }
187 }
188
189 }