1 /*
2 * Copyright 2002-2008 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 1.1 <code>VariableResolver</code> that first delegates to the
32 * original resolver of the underlying JSF implementation (for resolving
33 * managed-bean objects as defined in <code>faces-config.xml</code>
34 * as well as well-known implicit EL attributes), then to the Spring
35 * root <code>WebApplicationContext</code> (for resolving Spring beans).
36 *
37 * <p>Configure this resolver in your <code>faces-config.xml</code> file as follows:
38 *
39 * <pre>
40 * <application>
41 * ...
42 * <variable-resolver>org.springframework.web.jsf.DelegatingVariableResolver</variable-resolver>
43 * </application></pre>
44 *
45 * All your JSF expressions can then implicitly refer to the names of
46 * Spring-managed service layer beans, for example in property values of
47 * JSF-managed beans:
48 *
49 * <pre>
50 * <managed-bean>
51 * <managed-bean-name>myJsfManagedBean</managed-bean-name>
52 * <managed-bean-class>example.MyJsfManagedBean</managed-bean-class>
53 * <managed-bean-scope>session</managed-bean-scope>
54 * <managed-property>
55 * <property-name>mySpringManagedBusinessObject</property-name>
56 * <value>#{mySpringManagedBusinessObject}</value>
57 * </managed-property>
58 * </managed-bean></pre>
59 *
60 * with "mySpringManagedBusinessObject" defined as Spring bean in
61 * applicationContext.xml:
62 *
63 * <pre>
64 * <bean id="mySpringManagedBusinessObject" class="example.MySpringManagedBusinessObject">
65 * ...
66 * </bean></pre>
67 *
68 * @author Juergen Hoeller
69 * @since 1.1
70 * @see WebApplicationContextVariableResolver
71 * @see FacesContextUtils#getRequiredWebApplicationContext
72 */
73 public class DelegatingVariableResolver extends VariableResolver {
74
75 /** Logger available to subclasses */
76 protected final Log logger = LogFactory.getLog(getClass());
77
78 protected final VariableResolver originalVariableResolver;
79
80
81 /**
82 * Create a new DelegatingVariableResolver, using the given original VariableResolver.
83 * <p>A JSF implementation will automatically pass its original resolver into the
84 * constructor of a configured resolver, provided that there is a corresponding
85 * constructor argument.
86 * @param originalVariableResolver the original VariableResolver
87 */
88 public DelegatingVariableResolver(VariableResolver originalVariableResolver) {
89 Assert.notNull(originalVariableResolver, "Original JSF VariableResolver must not be null");
90 this.originalVariableResolver = originalVariableResolver;
91 }
92
93 /**
94 * Return the original JSF VariableResolver that this resolver delegates to.
95 * Used to resolve standard JSF-managed beans.
96 */
97 protected final VariableResolver getOriginalVariableResolver() {
98 return this.originalVariableResolver;
99 }
100
101
102 /**
103 * Delegate to the original VariableResolver first, then try to
104 * resolve the variable as Spring bean in the root WebApplicationContext.
105 */
106 public Object resolveVariable(FacesContext facesContext, String name) throws EvaluationException {
107 Object value = resolveOriginal(facesContext, name);
108 if (value != null) {
109 return value;
110 }
111 Object bean = resolveSpringBean(facesContext, name);
112 if (bean != null) {
113 return bean;
114 }
115 return null;
116 }
117
118 /**
119 * Resolve the attribute via the original JSF VariableResolver.
120 */
121 protected Object resolveOriginal(FacesContext facesContext, String name) {
122 Object value = getOriginalVariableResolver().resolveVariable(facesContext, name);
123 if (value != null && logger.isTraceEnabled()) {
124 logger.trace("Successfully resolved variable '" + name + "' via original VariableResolver");
125 }
126 return value;
127 }
128
129 /**
130 * Resolve the attribute as a Spring bean in the ApplicationContext.
131 */
132 protected Object resolveSpringBean(FacesContext facesContext, String name) {
133 BeanFactory bf = getBeanFactory(facesContext);
134 if (bf.containsBean(name)) {
135 if (logger.isTraceEnabled()) {
136 logger.trace("Successfully resolved variable '" + name + "' in Spring BeanFactory");
137 }
138 return bf.getBean(name);
139 }
140 else {
141 return null;
142 }
143 }
144
145 /**
146 * Retrieve the Spring BeanFactory to delegate bean name resolution to.
147 * <p>The default implementation delegates to <code>getWebApplicationContext</code>.
148 * Can be overridden to provide an arbitrary BeanFactory reference to resolve
149 * against; usually, this will be a full Spring ApplicationContext.
150 * @param facesContext the current JSF context
151 * @return the Spring BeanFactory (never <code>null</code>)
152 * @see #getWebApplicationContext
153 */
154 protected BeanFactory getBeanFactory(FacesContext facesContext) {
155 return getWebApplicationContext(facesContext);
156 }
157
158 /**
159 * Retrieve the web application context to delegate bean name resolution to.
160 * <p>The default implementation delegates to FacesContextUtils.
161 * @param facesContext the current JSF context
162 * @return the Spring web application context (never <code>null</code>)
163 * @see FacesContextUtils#getRequiredWebApplicationContext
164 */
165 protected WebApplicationContext getWebApplicationContext(FacesContext facesContext) {
166 return FacesContextUtils.getRequiredWebApplicationContext(facesContext);
167 }
168
169 }