KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2002-2005 the original author or authors.
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
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.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25
26 import org.springframework.beans.factory.BeanFactory;
27 import org.springframework.util.Assert;
28 import org.springframework.web.context.WebApplicationContext;
29
30 /**
31  * JSF VariableResolver that first delegates to the original resolver of the
32  * underlying JSF implementation, then to the Spring root WebApplicationContext.
33  *
34  * <p>Configure this resolver in your <code>faces-config.xml</code> file as follows:
35  *
36  * <pre>
37  * &lt;application>
38  * ...
39  * &lt;variable-resolver>org.springframework.web.jsf.DelegatingVariableResolver&lt;/variable-resolver>
40  * &lt;/application></pre>
41  *
42  * All your JSF expressions can then implicitly refer to the names of
43  * Spring-managed service layer beans, for example in property values of
44  * JSF-managed beans:
45  *
46  * <pre>
47  * &lt;managed-bean>
48  * &lt;managed-bean-name>myJsfManagedBean&lt;/managed-bean-name>
49  * &lt;managed-bean-class>example.MyJsfManagedBean&lt;/managed-bean-class>
50  * &lt;managed-bean-scope>session&lt;/managed-bean-scope>
51  * &lt;managed-property>
52  * &lt;property-name>mySpringManagedBusinessObject&lt;/property-name>
53  * &lt;value>#{mySpringManagedBusinessObject}&lt;/value>
54  * &lt;/managed-property>
55  * &lt;/managed-bean></pre>
56  *
57  * with "mySpringManagedBusinessObject" defined as Spring bean in
58  * applicationContext.xml:
59  *
60  * <pre>
61  * &lt;bean id="mySpringManagedBusinessObject" class="example.MySpringManagedBusinessObject">
62  * ...
63  * &lt;/bean></pre>
64  *
65  * @author Juergen Hoeller
66  * @since 1.1
67  * @see WebApplicationContextVariableResolver
68  * @see FacesContextUtils#getRequiredWebApplicationContext
69  */

70 public class DelegatingVariableResolver extends VariableResolver {
71
72     /** Logger available to subclasses */
73     protected final Log logger = LogFactory.getLog(getClass());
74
75     protected final VariableResolver originalVariableResolver;
76
77
78     /**
79      * Create a new DelegatingVariableResolver, using the given original VariableResolver.
80      * <p>A JSF implementation will automatically pass its original resolver into the
81      * constructor of a configured resolver, provided that there is a corresponding
82      * constructor argument.
83      * @param originalVariableResolver the original VariableResolver
84      */

85     public DelegatingVariableResolver(VariableResolver originalVariableResolver) {
86         Assert.notNull(originalVariableResolver, "Original JSF VariableResolver must not be null");
87         this.originalVariableResolver = originalVariableResolver;
88     }
89
90     /**
91      * Return the original JSF VariableResolver that this resolver delegates to.
92      * Used to resolve standard JSF-managed beans.
93      */

94     protected final VariableResolver getOriginalVariableResolver() {
95         return originalVariableResolver;
96     }
97
98
99     /**
100      * Delegate to the original VariableResolver first, then try to
101      * resolve the variable as Spring bean in the root WebApplicationContext.
102      */

103     public Object JavaDoc resolveVariable(FacesContext facesContext, String JavaDoc name) throws EvaluationException {
104         // Ask original JSF variable resolver.
105
if (logger.isDebugEnabled()) {
106             logger.debug("Attempting to resolve variable '" + name + "' in via original VariableResolver");
107         }
108         Object JavaDoc originalResult = getOriginalVariableResolver().resolveVariable(facesContext, name);
109         if (originalResult != null) {
110             return originalResult;
111         }
112
113         // Ask Spring root application context.
114
if (logger.isDebugEnabled()) {
115             logger.debug("Attempting to resolve variable '" + name + "' in root WebApplicationContext");
116         }
117         BeanFactory bf = getBeanFactory(facesContext);
118         if (bf.containsBean(name)) {
119             if (logger.isDebugEnabled()) {
120                 logger.debug("Successfully resolved variable '" + name + "' in root WebApplicationContext");
121             }
122             return bf.getBean(name);
123         }
124
125         if (logger.isDebugEnabled()) {
126             logger.debug("Could not resolve variable '" + name + "'");
127         }
128         return null;
129     }
130
131     /**
132      * Retrieve the Spring BeanFactory to delegate bean name resolution to.
133      * <p>Default implementation delegates to <code>getWebApplicationContext</code>.
134      * Can be overridden to provide an arbitrary BeanFactory reference to resolve
135      * against; usually, this will be a full Spring ApplicationContext.
136      * @param facesContext the current JSF context
137      * @return the Spring BeanFactory (never <code>null</code>)
138      * @see #getWebApplicationContext
139      */

140     protected BeanFactory getBeanFactory(FacesContext facesContext) {
141         return getWebApplicationContext(facesContext);
142     }
143
144     /**
145      * Retrieve the web application context to delegate bean name resolution to.
146      * <p>Default implementation delegates to FacesContextUtils.
147      * @param facesContext the current JSF context
148      * @return the Spring web application context (never <code>null</code>)
149      * @see FacesContextUtils#getRequiredWebApplicationContext
150      */

151     protected WebApplicationContext getWebApplicationContext(FacesContext facesContext) {
152         return FacesContextUtils.getRequiredWebApplicationContext(facesContext);
153     }
154
155 }
156
Popular Tags