KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > el > VariableResolverImpl


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.myfaces.el;
17
18 import java.util.HashMap JavaDoc;
19 import java.util.Map JavaDoc;
20
21 import javax.faces.context.ExternalContext;
22 import javax.faces.context.FacesContext;
23 import javax.faces.el.ReferenceSyntaxException;
24 import javax.faces.el.VariableResolver;
25
26 import org.apache.myfaces.config.ManagedBeanBuilder;
27 import org.apache.myfaces.config.RuntimeConfig;
28 import org.apache.myfaces.config.element.ManagedBean;
29
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32
33
34 /**
35  * @author Manfred Geiler (latest modification by $Author: bdudney $)
36  * @author Anton Koinov
37  * @version $Revision: 1.34 $ $Date: 2004/11/08 03:43:20 $
38  * $Log: VariableResolverImpl.java,v $
39  * Revision 1.34 2004/11/08 03:43:20 bdudney
40  * Added a div element. x:div to use, inserts a div with class or style attributes
41  *
42  * Revision 1.33 2004/10/13 11:51:00 matze
43  * renamed packages to org.apache
44  *
45  * Revision 1.32 2004/07/27 06:28:34 dave0000
46  * fix issue with getType of literal expressions (and other improvements)
47  *
48  * Revision 1.31 2004/07/07 08:34:58 mwessendorf
49  * removed unused import-statements
50  *
51  * Revision 1.30 2004/07/07 00:25:07 o_rossmueller
52  * tidy up config/confignew package (moved confignew classes to package config)
53  *
54  * Revision 1.29 2004/07/01 22:05:12 mwessendorf
55  * ASF switch
56  *
57  * Revision 1.28 2004/06/16 23:02:24 o_rossmueller
58  * merged confignew_branch
59  *
60  * Revision 1.27.2.2 2004/06/16 02:07:23 o_rossmueller
61  * get navigation rules from RuntimeConfig
62  * refactored all remaining usages of MyFacesFactoryFinder to use RuntimeConfig
63  *
64  * Revision 1.27.2.1 2004/06/13 15:59:07 o_rossmueller
65  * started integration of new config mechanism:
66  * - factories
67  * - components
68  * - render kits
69  * - managed beans + managed properties (no list/map initialization)
70  *
71  * Revision 1.27 2004/05/12 07:57:43 manolito
72  * Log in javadoc header
73  *
74  */

75 public class VariableResolverImpl
76     extends VariableResolver
77 {
78     //~ Static fields/initializers -----------------------------------------------------------------
79

80     private static final Log log = LogFactory.getLog(VariableResolverImpl.class);
81
82     //~ Instance fields ----------------------------------------------------------------------------
83

84     public static final Map JavaDoc s_standardImplicitObjects = new HashMap JavaDoc(32);
85     static {
86         s_standardImplicitObjects.put(
87             "applicationScope",
88             new ImplicitObject()
89             {
90                 public Object JavaDoc get(FacesContext facesContext)
91                 {
92                     return facesContext.getExternalContext().getApplicationMap();
93                 }
94             });
95         s_standardImplicitObjects.put(
96             "cookie",
97             new ImplicitObject()
98             {
99                 public Object JavaDoc get(FacesContext facesContext)
100                 {
101                     return facesContext.getExternalContext().getRequestCookieMap();
102                 }
103             });
104         s_standardImplicitObjects.put(
105             "facesContext",
106             new ImplicitObject()
107             {
108                 public Object JavaDoc get(FacesContext facesContext)
109                 {
110                     return facesContext;
111                 }
112             });
113         s_standardImplicitObjects.put(
114             "header",
115             new ImplicitObject()
116             {
117                 public Object JavaDoc get(FacesContext facesContext)
118                 {
119                     return facesContext.getExternalContext().getRequestHeaderMap();
120                 }
121             });
122         s_standardImplicitObjects.put(
123             "headerValues",
124             new ImplicitObject()
125             {
126                 public Object JavaDoc get(FacesContext facesContext)
127                 {
128                     return facesContext.getExternalContext().getRequestHeaderValuesMap();
129                 }
130             });
131         s_standardImplicitObjects.put(
132             "initParam",
133             new ImplicitObject()
134             {
135                 public Object JavaDoc get(FacesContext facesContext)
136                 {
137                     return facesContext.getExternalContext().getInitParameterMap();
138                 }
139             });
140         s_standardImplicitObjects.put(
141             "param",
142             new ImplicitObject()
143             {
144                 public Object JavaDoc get(FacesContext facesContext)
145                 {
146                     return facesContext.getExternalContext().getRequestParameterMap();
147                 }
148             });
149         s_standardImplicitObjects.put(
150             "paramValues",
151             new ImplicitObject()
152             {
153                 public Object JavaDoc get(FacesContext facesContext)
154                 {
155                     return facesContext.getExternalContext().getRequestParameterValuesMap();
156                 }
157             });
158         s_standardImplicitObjects.put(
159             "requestScope",
160             new ImplicitObject()
161             {
162                 public Object JavaDoc get(FacesContext facesContext)
163                 {
164                     return facesContext.getExternalContext().getRequestMap();
165                 }
166             });
167         s_standardImplicitObjects.put(
168             "sessionScope",
169             new ImplicitObject()
170             {
171                 public Object JavaDoc get(FacesContext facesContext)
172                 {
173                     return facesContext.getExternalContext().getSessionMap();
174                 }
175             });
176         s_standardImplicitObjects.put(
177             "view",
178             new ImplicitObject()
179             {
180                 public Object JavaDoc get(FacesContext facesContext)
181                 {
182                     return facesContext.getViewRoot();
183                 }
184             });
185     }
186     
187     /**
188      * Stores all implicit objects defined for this instance of <code>VariableResolver</code>
189      * <p>
190      * Can store instances of <code>ImplicitObject</code> which have the ability to
191      * dynamically resolve against FacesContext. Can also store any other object
192      * which itself is the value for the implicit object (this in effect will be
193      * a static object).
194      * </p>
195      * <p>
196      * WARNING: this implementation is not serialized as it is thread safe because
197      * it does not update/add to _implicitObjects after object initialization.
198      * If you need to add your own implicit objects, either extend and add more
199      * in an initialization block, or add proper sychronization
200      * </p>
201      */

202     protected final Map JavaDoc _implicitObjects = new HashMap JavaDoc(32);
203     {
204         _implicitObjects.putAll(s_standardImplicitObjects);
205     }
206
207     protected static final Map JavaDoc s_standardScopes = new HashMap JavaDoc(16);
208     static {
209         s_standardScopes.put(
210             "request",
211             new Scope()
212             {
213                 public void put(ExternalContext extContext, String JavaDoc name, Object JavaDoc obj)
214                 {
215                     extContext.getRequestMap().put(name, obj);
216                 }
217             });
218         s_standardScopes.put(
219             "session",
220             new Scope()
221             {
222                 public void put(ExternalContext extContext, String JavaDoc name, Object JavaDoc obj)
223                 {
224                     extContext.getSessionMap().put(name, obj);
225                 }
226             });
227         s_standardScopes.put(
228             "application",
229             new Scope()
230             {
231                 public void put(ExternalContext extContext, String JavaDoc name, Object JavaDoc obj)
232                 {
233                     extContext.getApplicationMap().put(name, obj);
234                 }
235             });
236         s_standardScopes.put(
237             "none",
238             new Scope()
239             {
240                 public void put(ExternalContext extContext, String JavaDoc name, Object JavaDoc obj)
241                 {
242                     // do nothing
243
}
244             });
245     }
246     
247     /**
248      * Stores all scopes defined for this instance of <code>VariableResolver</code>
249      * <p>
250      * Can store instances of <code>Scope</code> which have the ability to
251      * dynamically resolve against ExternalContext for put operations.
252      * </p>
253      * <p>
254      * WARNING: this implementation is not serialized as it is thread safe because
255      * it does not update/add to _scopes after object initialization.
256      * If you need to add your own scopes, either extend and add more
257      * in an initialization block, or add proper sychronization
258      * </p>
259      */

260     protected final Map JavaDoc _scopes = new HashMap JavaDoc(16);
261     {
262         _scopes.putAll(s_standardScopes);
263     }
264
265     /**
266      * RuntimeConfig is instantiated once per servlet and never changes--we can
267      * safely cache it
268      */

269     private RuntimeConfig _runtimeConfig;
270
271     private ManagedBeanBuilder beanBuilder = new ManagedBeanBuilder();
272
273
274     //~ Methods ---------------------------------------------------------------
275

276     public Object JavaDoc resolveVariable(FacesContext facesContext, String JavaDoc name)
277     {
278         if ((name == null) || (name.length() == 0))
279         {
280             throw new ReferenceSyntaxException("Varible name is null or empty");
281         }
282
283         // Implicit objects
284
Object JavaDoc implicitObject = _implicitObjects.get(name);
285         if (implicitObject != null)
286         {
287             if (implicitObject instanceof ImplicitObject)
288             {
289                 // a complex runtime object
290
return ((ImplicitObject) implicitObject).get(facesContext);
291             }
292             else
293             {
294                 // a simple object
295
return implicitObject;
296             }
297         }
298
299         ExternalContext externalContext = facesContext.getExternalContext();
300
301         // Request context
302
Map JavaDoc requestMap = externalContext.getRequestMap();
303         Object JavaDoc obj = requestMap.get(name);
304         if (obj != null)
305         {
306             return obj;
307         }
308
309         // Session context
310
obj = externalContext.getSessionMap().get(name);
311         if (obj != null)
312         {
313             return obj;
314         }
315
316         // Application context
317
obj = externalContext.getApplicationMap().get(name);
318         if (obj != null)
319         {
320             return obj;
321         }
322
323         // ManagedBean
324
ManagedBean mbc = getRuntimeConfig(facesContext).getManagedBean(name);
325
326         if (mbc != null)
327         {
328             obj = beanBuilder.buildManagedBean(facesContext, mbc);
329
330             // put in scope
331
String JavaDoc scopeKey = mbc.getManagedBeanScope();
332             
333             // find the scope handler object
334
Scope scope = (Scope) _scopes.get(scopeKey);
335             if (scope == null)
336             {
337                 log.error("Managed bean '" + name + "' has illegal scope: "
338                     + scopeKey);
339             }
340             else
341             {
342                 scope.put(externalContext, name, obj);
343             }
344
345             return obj;
346         }
347
348         log.warn("Variable '" + name + "' could not be resolved.");
349         return null;
350     }
351
352     protected RuntimeConfig getRuntimeConfig(FacesContext facesContext)
353     {
354         if (_runtimeConfig == null)
355         {
356             _runtimeConfig = RuntimeConfig.getCurrentInstance(facesContext.getExternalContext());
357         }
358         return _runtimeConfig;
359     }
360 }
361
362
363 interface ImplicitObject
364 {
365     //~ Methods ---------------------------------------------------------------
366

367     public Object JavaDoc get(FacesContext facesContext);
368 }
369
370
371 interface Scope
372 {
373     //~ Methods ---------------------------------------------------------------
374

375     public void put(ExternalContext extContext, String JavaDoc name, Object JavaDoc obj);
376 }
377
Popular Tags