1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.myfaces.el.unified.resolver; |
20 | |
|
21 | |
import java.beans.FeatureDescriptor; |
22 | |
import java.util.ArrayList; |
23 | |
import java.util.HashMap; |
24 | |
import java.util.Iterator; |
25 | |
import java.util.List; |
26 | |
import java.util.Map; |
27 | |
import javax.el.ELContext; |
28 | |
import javax.el.ELException; |
29 | |
import javax.el.ELResolver; |
30 | |
import javax.el.PropertyNotFoundException; |
31 | |
import javax.el.PropertyNotWritableException; |
32 | |
import javax.faces.context.ExternalContext; |
33 | |
import javax.faces.context.FacesContext; |
34 | |
import org.apache.commons.logging.Log; |
35 | |
import org.apache.commons.logging.LogFactory; |
36 | |
import org.apache.myfaces.config.ManagedBeanBuilder; |
37 | |
import org.apache.myfaces.config.RuntimeConfig; |
38 | |
import org.apache.myfaces.config.element.ManagedBean; |
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
public class ManagedBeanResolver extends ELResolver { |
46 | 0 | private static final Log log = LogFactory.getLog(ManagedBeanResolver.class); |
47 | |
private static final String BEANS_UNDER_CONSTRUCTION = "org.apache.myfaces.el.unified.resolver.managedbean.beansUnderConstruction"; |
48 | |
|
49 | |
|
50 | 0 | protected static final Map s_standardScopes = new HashMap(16); |
51 | |
static { |
52 | 0 | s_standardScopes.put( |
53 | |
"request", |
54 | |
new Scope() |
55 | 0 | { |
56 | |
public void put(ExternalContext extContext, String name, Object obj) |
57 | |
{ |
58 | 0 | extContext.getRequestMap().put(name, obj); |
59 | 0 | } |
60 | |
}); |
61 | 0 | s_standardScopes.put( |
62 | |
"session", |
63 | |
new Scope() |
64 | 0 | { |
65 | |
public void put(ExternalContext extContext, String name, Object obj) |
66 | |
{ |
67 | 0 | extContext.getSessionMap().put(name, obj); |
68 | 0 | } |
69 | |
}); |
70 | 0 | s_standardScopes.put( |
71 | |
"application", |
72 | |
new Scope() |
73 | 0 | { |
74 | |
public void put(ExternalContext extContext, String name, Object obj) |
75 | |
{ |
76 | 0 | extContext.getApplicationMap().put(name, obj); |
77 | 0 | } |
78 | |
}); |
79 | 0 | s_standardScopes.put( |
80 | |
"none", |
81 | |
new Scope() |
82 | 0 | { |
83 | |
public void put(ExternalContext extContext, String name, Object obj) |
84 | |
{ |
85 | |
|
86 | 0 | } |
87 | |
}); |
88 | 0 | } |
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | 0 | protected final Map _scopes = new HashMap(16); |
104 | |
{ |
105 | 0 | _scopes.putAll(s_standardScopes); |
106 | |
} |
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
private RuntimeConfig runtimeConfig; |
113 | |
|
114 | 0 | private ManagedBeanBuilder beanBuilder = new ManagedBeanBuilder(); |
115 | |
|
116 | |
|
117 | 0 | public ManagedBeanResolver() { |
118 | 0 | } |
119 | |
|
120 | |
public void setValue(final ELContext context, final Object base, final Object property, final Object value) |
121 | |
throws NullPointerException, PropertyNotFoundException, PropertyNotWritableException, ELException { |
122 | |
|
123 | 0 | if ( (base == null) && (property == null)) { |
124 | 0 | throw new PropertyNotFoundException(); |
125 | |
} |
126 | |
|
127 | 0 | } |
128 | |
|
129 | |
public boolean isReadOnly(final ELContext context, final Object base, final Object property) |
130 | |
throws NullPointerException, PropertyNotFoundException, ELException { |
131 | |
|
132 | 0 | if ( (base == null) && (property == null)) { |
133 | 0 | throw new PropertyNotFoundException(); |
134 | |
} |
135 | |
|
136 | 0 | return false; |
137 | |
} |
138 | |
|
139 | |
public Object getValue(final ELContext context, final Object base, final Object property) |
140 | |
throws NullPointerException, PropertyNotFoundException, ELException { |
141 | |
|
142 | 0 | if (base != null) return null; |
143 | |
|
144 | 0 | if (property == null) { |
145 | 0 | throw new PropertyNotFoundException(); |
146 | |
} |
147 | |
|
148 | 0 | final ExternalContext extContext = externalContext(context); |
149 | |
|
150 | 0 | if (extContext == null) return null; |
151 | 0 | if (extContext.getRequestMap().containsKey(property)) return null; |
152 | 0 | if (extContext.getSessionMap().containsKey(property)) return null; |
153 | 0 | if (extContext.getApplicationMap().containsKey(property)) return null; |
154 | |
|
155 | 0 | if ( !(property instanceof String) ) return null; |
156 | |
|
157 | 0 | final String strProperty = (String)property; |
158 | |
|
159 | 0 | final ManagedBean managedBean = runtimeConfig(context).getManagedBean(strProperty); |
160 | 0 | Object beanInstance = null; |
161 | 0 | if (managedBean != null) { |
162 | 0 | FacesContext facesContext = facesContext(context); |
163 | 0 | context.setPropertyResolved(true); |
164 | 0 | beanInstance = createManagedBean(managedBean, facesContext); |
165 | |
} |
166 | |
|
167 | 0 | return beanInstance; |
168 | |
} |
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
|
174 | |
|
175 | |
private Object createManagedBean(final ManagedBean managedBean, |
176 | |
final FacesContext facesContext) |
177 | |
throws ELException { |
178 | |
|
179 | 0 | final ExternalContext extContext = facesContext.getExternalContext(); |
180 | 0 | final Map requestMap = extContext.getRequestMap(); |
181 | |
|
182 | |
|
183 | 0 | List beansUnderConstruction = (List)requestMap.get(BEANS_UNDER_CONSTRUCTION); |
184 | 0 | if (beansUnderConstruction == null) { |
185 | 0 | beansUnderConstruction = new ArrayList(); |
186 | 0 | requestMap.put(BEANS_UNDER_CONSTRUCTION, beansUnderConstruction); |
187 | |
} |
188 | |
|
189 | 0 | final String managedBeanName = managedBean.getManagedBeanName(); |
190 | 0 | if (beansUnderConstruction.contains(managedBeanName)) { |
191 | 0 | throw new ELException( "Detected cyclic reference to managedBean " + managedBeanName); |
192 | |
} |
193 | |
|
194 | 0 | beansUnderConstruction.add(managedBeanName); |
195 | |
|
196 | 0 | Object obj = null; |
197 | |
try { |
198 | 0 | obj = beanBuilder.buildManagedBean(facesContext, managedBean); |
199 | |
} finally { |
200 | 0 | beansUnderConstruction.remove(managedBeanName); |
201 | 0 | } |
202 | |
|
203 | 0 | putInScope(managedBean, extContext, obj); |
204 | |
|
205 | 0 | return obj; |
206 | |
} |
207 | |
|
208 | |
private void putInScope(final ManagedBean managedBean, final ExternalContext extContext, final Object obj) { |
209 | |
|
210 | 0 | final String managedBeanName = managedBean.getManagedBeanName(); |
211 | |
|
212 | 0 | if (obj == null) { |
213 | 0 | if (log.isDebugEnabled()) |
214 | 0 | log.debug("Variable '" + managedBeanName + "' could not be resolved."); |
215 | |
} else { |
216 | |
|
217 | 0 | final String scopeKey = managedBean.getManagedBeanScope(); |
218 | |
|
219 | |
|
220 | 0 | final Scope scope = (Scope) _scopes.get(scopeKey); |
221 | 0 | if (scope == null) { |
222 | 0 | log.error("Managed bean '" + managedBeanName + "' has illegal scope: " + scopeKey); |
223 | |
} else { |
224 | 0 | scope.put(extContext, managedBeanName, obj); |
225 | |
} |
226 | |
} |
227 | |
|
228 | 0 | } |
229 | |
|
230 | |
|
231 | |
private static FacesContext facesContext(final ELContext context) { |
232 | 0 | return (FacesContext)context.getContext(FacesContext.class); |
233 | |
} |
234 | |
|
235 | |
private static ExternalContext externalContext(final ELContext context) { |
236 | 0 | final FacesContext facesContext = facesContext(context); |
237 | |
|
238 | 0 | return facesContext != null ? facesContext.getExternalContext() : null; |
239 | |
} |
240 | |
|
241 | |
public Class<?> getType(final ELContext context, final Object base, final Object property) |
242 | |
throws NullPointerException, PropertyNotFoundException, ELException { |
243 | |
|
244 | 0 | if ( (base == null) && (property == null)) { |
245 | 0 | throw new PropertyNotFoundException(); |
246 | |
} |
247 | |
|
248 | 0 | return null; |
249 | |
} |
250 | |
|
251 | |
public Iterator getFeatureDescriptors(final ELContext context, final Object base) { |
252 | |
|
253 | 0 | if (base != null) return null; |
254 | |
|
255 | 0 | final ArrayList<FeatureDescriptor> descriptors = new ArrayList<FeatureDescriptor>(); |
256 | |
|
257 | 0 | final Map<String, ManagedBean> managedBeans = runtimeConfig(context).getManagedBeans(); |
258 | 0 | for (Map.Entry<String, ManagedBean> managedBean : managedBeans.entrySet()) { |
259 | 0 | descriptors.add(makeDescriptor(managedBean.getKey(), managedBean.getValue())); |
260 | |
} |
261 | |
|
262 | 0 | return descriptors.iterator(); |
263 | |
} |
264 | |
|
265 | |
private static FeatureDescriptor makeDescriptor(final String beanName, final ManagedBean managedBean) { |
266 | 0 | final FeatureDescriptor fd = new FeatureDescriptor(); |
267 | 0 | fd.setValue(ELResolver.RESOLVABLE_AT_DESIGN_TIME, Boolean.TRUE); |
268 | 0 | fd.setValue(ELResolver.TYPE, managedBean.getManagedBeanClass()); |
269 | 0 | fd.setName(beanName); |
270 | 0 | fd.setDisplayName(beanName); |
271 | 0 | fd.setShortDescription(managedBean.getDescription()); |
272 | 0 | fd.setExpert(false); |
273 | 0 | fd.setHidden(false); |
274 | 0 | fd.setPreferred(true); |
275 | 0 | return fd; |
276 | |
} |
277 | |
|
278 | |
protected RuntimeConfig runtimeConfig(final ELContext context) { |
279 | 0 | final FacesContext facesContext = facesContext(context); |
280 | |
|
281 | |
|
282 | 0 | if (this.runtimeConfig == null) { |
283 | 0 | this.runtimeConfig = RuntimeConfig.getCurrentInstance(facesContext.getExternalContext()); |
284 | |
} |
285 | |
|
286 | 0 | return runtimeConfig; |
287 | |
} |
288 | |
|
289 | |
public Class<?> getCommonPropertyType(final ELContext context, final Object base) { |
290 | |
|
291 | 0 | if (base != null) return null; |
292 | |
|
293 | 0 | return Object.class; |
294 | |
} |
295 | |
|
296 | |
interface Scope { |
297 | |
public void put(ExternalContext extContext, String name, Object obj); |
298 | |
} |
299 | |
|
300 | |
} |