KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > jsf > WebApplicationContextVariableResolver


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * 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, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16
17 package org.springframework.web.jsf;
18
19 import javax.faces.context.FacesContext;
20 import javax.faces.el.EvaluationException;
21 import javax.faces.el.VariableResolver;
22
23 import org.springframework.util.Assert;
24 import org.springframework.web.context.WebApplicationContext;
25
26 /**
27  * Special <code>VariableResolver</code> that exposes the Spring
28  * <code>WebApplicationContext</code> instance under a variable named
29  * "webApplicationContext".
30  *
31  * <p>In contrast to DelegatingVariableResolver, this VariableResolver
32  * does <i>not</i> resolve JSF variable names as Spring bean names. It rather
33  * exposes Spring's root WebApplicationContext <i>itself</i> under a special name.
34  * JSF-managed beans can then use Spring's WebApplicationContext API to retrieve
35  * Spring-managed beans, access resources, etc.
36  *
37  * <p>Configure this resolver in your <code>faces-config.xml</code> file as follows:
38  *
39  * <pre>
40  * &lt;application>
41  * ...
42  * &lt;variable-resolver>org.springframework.web.jsf.WebApplicationContextVariableResolver&lt;/variable-resolver>
43  * &lt;/application></pre>
44  *
45  * @author Colin Sampaleanu
46  * @author Juergen Hoeller
47  * @since 1.2.5
48  * @see DelegatingVariableResolver
49  * @see FacesContextUtils#getWebApplicationContext
50  */

51 public class WebApplicationContextVariableResolver extends VariableResolver {
52
53     /**
54      * Name of the exposed WebApplicationContext variable: "webApplicationContext".
55      */

56     public static final String JavaDoc WEB_APPLICATION_CONTEXT_VARIABLE_NAME = "webApplicationContext";
57
58
59     protected final VariableResolver originalVariableResolver;
60
61
62     /**
63      * Create a new WebApplicationContextVariableResolver, using the given
64      * original VariableResolver.
65      * <p>A JSF implementation will automatically pass its original resolver into the
66      * constructor of a configured resolver, provided that there is a corresponding
67      * constructor argument.
68      * @param originalVariableResolver the original VariableResolver
69      */

70     public WebApplicationContextVariableResolver(VariableResolver originalVariableResolver) {
71         Assert.notNull(originalVariableResolver, "Original JSF VariableResolver must not be null");
72         this.originalVariableResolver = originalVariableResolver;
73     }
74
75     /**
76      * Return the original JSF VariableResolver that this resolver delegates to.
77      * Used to resolve standard JSF-managed beans.
78      */

79     protected final VariableResolver getOriginalVariableResolver() {
80         return originalVariableResolver;
81     }
82
83
84     /**
85      * Check for the special "webApplicationContext" variable first,
86      * then delegate to the original VariableResolver.
87      * <p>If no WebApplicationContext is available, all requests
88      * will be delegated to the original VariableResolver.
89      */

90     public Object JavaDoc resolveVariable(FacesContext context, String JavaDoc name) throws EvaluationException {
91         Object JavaDoc value = null;
92         if (WEB_APPLICATION_CONTEXT_VARIABLE_NAME.equals(name)) {
93             value = getWebApplicationContext(context);
94         }
95         if (value == null) {
96             value = getOriginalVariableResolver().resolveVariable(context, name);
97         }
98         return value;
99     }
100
101     /**
102      * Retrieve the WebApplicationContext reference to expose.
103      * <p>Default implementation delegates to FacesContextUtils,
104      * returning <code>null</code> if no WebApplicationContext found.
105      * @param facesContext the current JSF context
106      * @return the Spring web application context
107      * @see FacesContextUtils#getWebApplicationContext
108      */

109     protected WebApplicationContext getWebApplicationContext(FacesContext facesContext) {
110         return FacesContextUtils.getWebApplicationContext(facesContext);
111     }
112
113 }
114
Popular Tags