001: /*
002: * Copyright 2002-2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.web.jsf.el;
018:
019: import java.beans.FeatureDescriptor;
020: import java.util.Iterator;
021:
022: import javax.el.ELContext;
023: import javax.el.ELException;
024: import javax.el.ELResolver;
025: import javax.faces.context.FacesContext;
026:
027: import org.apache.commons.logging.Log;
028: import org.apache.commons.logging.LogFactory;
029:
030: import org.springframework.beans.BeansException;
031: import org.springframework.web.context.WebApplicationContext;
032: import org.springframework.web.jsf.FacesContextUtils;
033:
034: /**
035: * Special JSF 1.2 <code>ELResolver</code> that exposes the Spring
036: * <code>WebApplicationContext</code> instance under a variable named
037: * "webApplicationContext".
038: *
039: * <p>In contrast to {@link SpringBeanFacesELResolver}, this ELResolver variant
040: * does <i>not</i> resolve JSF variable names as Spring bean names. It rather
041: * exposes Spring's root WebApplicationContext <i>itself</i> under a special name,
042: * and is able to resolve "webApplicationContext.mySpringManagedBusinessObject"
043: * dereferences to Spring-defined beans in that application context.
044: *
045: * <p>Configure this resolver in your <code>faces-config.xml</code> file as follows:
046: *
047: * <pre>
048: * <application>
049: * ...
050: * <el-resolver>org.springframework.web.jsf.el.WebApplicationContextFacesELResolver</el-resolver>
051: * </application></pre>
052: *
053: * @author Juergen Hoeller
054: * @since 2.5
055: * @see SpringBeanFacesELResolver
056: * @see org.springframework.web.jsf.FacesContextUtils#getWebApplicationContext
057: */
058: public class WebApplicationContextFacesELResolver extends ELResolver {
059:
060: /**
061: * Name of the exposed WebApplicationContext variable: "webApplicationContext".
062: */
063: public static final String WEB_APPLICATION_CONTEXT_VARIABLE_NAME = "webApplicationContext";
064:
065: /** Logger available to subclasses */
066: protected final Log logger = LogFactory.getLog(getClass());
067:
068: public Object getValue(ELContext elContext, Object base,
069: Object property) throws ELException {
070: if (base != null) {
071: if (base instanceof WebApplicationContext) {
072: WebApplicationContext wac = (WebApplicationContext) base;
073: String beanName = property.toString();
074: if (logger.isTraceEnabled()) {
075: logger.trace("Attempting to resolve property '"
076: + beanName
077: + "' in root WebApplicationContext");
078: }
079: if (wac.containsBean(beanName)) {
080: if (logger.isDebugEnabled()) {
081: logger.debug("Successfully resolved property '"
082: + beanName
083: + "' in root WebApplicationContext");
084: }
085: elContext.setPropertyResolved(true);
086: try {
087: return wac.getBean(beanName);
088: } catch (BeansException ex) {
089: throw new ELException(ex);
090: }
091: } else {
092: // Mimic standard JSF/JSP behavior when base is a Map by returning null.
093: return null;
094: }
095: }
096: } else {
097: if (WEB_APPLICATION_CONTEXT_VARIABLE_NAME.equals(property)) {
098: elContext.setPropertyResolved(true);
099: return getWebApplicationContext(elContext);
100: }
101: }
102:
103: return null;
104: }
105:
106: public Class<?> getType(ELContext elContext, Object base,
107: Object property) throws ELException {
108: if (base != null) {
109: if (base instanceof WebApplicationContext) {
110: WebApplicationContext wac = (WebApplicationContext) base;
111: String beanName = property.toString();
112: if (logger.isDebugEnabled()) {
113: logger.debug("Attempting to resolve property '"
114: + beanName
115: + "' in root WebApplicationContext");
116: }
117: if (wac.containsBean(beanName)) {
118: if (logger.isDebugEnabled()) {
119: logger.debug("Successfully resolved property '"
120: + beanName
121: + "' in root WebApplicationContext");
122: }
123: elContext.setPropertyResolved(true);
124: try {
125: return wac.getType(beanName);
126: } catch (BeansException ex) {
127: throw new ELException(ex);
128: }
129: } else {
130: // Mimic standard JSF/JSP behavior when base is a Map by returning null.
131: return null;
132: }
133: }
134: } else {
135: if (WEB_APPLICATION_CONTEXT_VARIABLE_NAME.equals(property)) {
136: elContext.setPropertyResolved(true);
137: return WebApplicationContext.class;
138: }
139: }
140:
141: return null;
142: }
143:
144: public void setValue(ELContext elContext, Object base,
145: Object property, Object value) throws ELException {
146: }
147:
148: public boolean isReadOnly(ELContext elContext, Object base,
149: Object property) throws ELException {
150: if (base instanceof WebApplicationContext) {
151: elContext.setPropertyResolved(true);
152: return false;
153: }
154: return false;
155: }
156:
157: public Iterator<FeatureDescriptor> getFeatureDescriptors(
158: ELContext elContext, Object base) {
159: return null;
160: }
161:
162: public Class<?> getCommonPropertyType(ELContext elContext,
163: Object base) {
164: return Object.class;
165: }
166:
167: /**
168: * Retrieve the WebApplicationContext reference to expose.
169: * <p>The default implementation delegates to FacesContextUtils,
170: * returning <code>null</code> if no WebApplicationContext found.
171: * @param elContext the current JSF ELContext
172: * @return the Spring web application context
173: * @see org.springframework.web.jsf.FacesContextUtils#getWebApplicationContext
174: */
175: protected WebApplicationContext getWebApplicationContext(
176: ELContext elContext) {
177: FacesContext facesContext = FacesContext.getCurrentInstance();
178: return FacesContextUtils
179: .getRequiredWebApplicationContext(facesContext);
180: }
181:
182: }
|