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;
018:
019: import javax.faces.context.FacesContext;
020: import javax.faces.el.EvaluationException;
021: import javax.faces.el.VariableResolver;
022:
023: import org.springframework.util.Assert;
024: import org.springframework.web.context.WebApplicationContext;
025:
026: /**
027: * Special JSF 1.1 <code>VariableResolver</code> that exposes the Spring
028: * <code>WebApplicationContext</code> instance under a variable named
029: * "webApplicationContext".
030: *
031: * <p>In contrast to {@link DelegatingVariableResolver}, this VariableResolver
032: * does <i>not</i> resolve JSF variable names as Spring bean names. It rather
033: * exposes Spring's root WebApplicationContext <i>itself</i> under a special name.
034: * JSF-managed beans can then use Spring's WebApplicationContext API to retrieve
035: * Spring-managed beans, access resources, etc.
036: *
037: * <p>Configure this resolver in your <code>faces-config.xml</code> file as follows:
038: *
039: * <pre>
040: * <application>
041: * ...
042: * <variable-resolver>org.springframework.web.jsf.WebApplicationContextVariableResolver</variable-resolver>
043: * </application></pre>
044: *
045: * @author Colin Sampaleanu
046: * @author Juergen Hoeller
047: * @since 1.2.5
048: * @see DelegatingVariableResolver
049: * @see FacesContextUtils#getWebApplicationContext
050: */
051: public class WebApplicationContextVariableResolver extends
052: VariableResolver {
053:
054: /**
055: * Name of the exposed WebApplicationContext variable: "webApplicationContext".
056: */
057: public static final String WEB_APPLICATION_CONTEXT_VARIABLE_NAME = "webApplicationContext";
058:
059: protected final VariableResolver originalVariableResolver;
060:
061: /**
062: * Create a new WebApplicationContextVariableResolver, using the given
063: * original VariableResolver.
064: * <p>A JSF implementation will automatically pass its original resolver into the
065: * constructor of a configured resolver, provided that there is a corresponding
066: * constructor argument.
067: * @param originalVariableResolver the original VariableResolver
068: */
069: public WebApplicationContextVariableResolver(
070: VariableResolver originalVariableResolver) {
071: Assert.notNull(originalVariableResolver,
072: "Original JSF VariableResolver must not be null");
073: this .originalVariableResolver = originalVariableResolver;
074: }
075:
076: /**
077: * Return the original JSF VariableResolver that this resolver delegates to.
078: * Used to resolve standard JSF-managed beans.
079: */
080: protected final VariableResolver getOriginalVariableResolver() {
081: return this .originalVariableResolver;
082: }
083:
084: /**
085: * Check for the special "webApplicationContext" variable first,
086: * then delegate to the original VariableResolver.
087: * <p>If no WebApplicationContext is available, all requests
088: * will be delegated to the original VariableResolver.
089: */
090: public Object resolveVariable(FacesContext context, String name)
091: throws EvaluationException {
092: Object value = null;
093: if (WEB_APPLICATION_CONTEXT_VARIABLE_NAME.equals(name)) {
094: value = getWebApplicationContext(context);
095: }
096: if (value == null) {
097: value = getOriginalVariableResolver().resolveVariable(
098: context, name);
099: }
100: return value;
101: }
102:
103: /**
104: * Retrieve the WebApplicationContext reference to expose.
105: * <p>The default implementation delegates to FacesContextUtils,
106: * returning <code>null</code> if no WebApplicationContext found.
107: * @param facesContext the current JSF context
108: * @return the Spring web application context
109: * @see FacesContextUtils#getWebApplicationContext
110: */
111: protected WebApplicationContext getWebApplicationContext(
112: FacesContext facesContext) {
113: return FacesContextUtils.getWebApplicationContext(facesContext);
114: }
115:
116: }
|