alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Spring Framework example source code file (DelegatingVariableResolver.java)

This example Spring Framework source code file (DelegatingVariableResolver.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Spring Framework tags/keywords

applicationcontext, attempting, beanfactory, beanfactory, delegatingvariableresolver, object, object, spring, string, string, successfully, variableresolver, variableresolver, webapplicationcontext

The Spring Framework DelegatingVariableResolver.java source code

/*
 * Copyright 2002-2007 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.web.jsf;

import javax.faces.context.FacesContext;
import javax.faces.el.EvaluationException;
import javax.faces.el.VariableResolver;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.util.Assert;
import org.springframework.web.context.WebApplicationContext;

/**
 * JSF 1.1 <code>VariableResolver that first delegates to the Spring
 * root <code>WebApplicationContext, then to the original resolver
 * of the underlying JSF implementation.
 *
 * <p>Configure this resolver in your faces-config.xml file as follows:
 *
 * <pre>
 * <application>
 *   ...
 *   <variable-resolver>org.springframework.web.jsf.DelegatingVariableResolver</variable-resolver>
 * </application></pre>
 *
 * All your JSF expressions can then implicitly refer to the names of
 * Spring-managed service layer beans, for example in property values of
 * JSF-managed beans:
 *
 * <pre>
 * <managed-bean>
 *   <managed-bean-name>myJsfManagedBean</managed-bean-name>
 *   <managed-bean-class>example.MyJsfManagedBean</managed-bean-class>
 *   <managed-bean-scope>session</managed-bean-scope>
 *   <managed-property>
 *     <property-name>mySpringManagedBusinessObject</property-name>
 *     <value>#{mySpringManagedBusinessObject}</value>
 *   </managed-property>
 * </managed-bean></pre>
 *
 * with "mySpringManagedBusinessObject" defined as Spring bean in
 * applicationContext.xml:
 *
 * <pre>
 * <bean id="mySpringManagedBusinessObject" class="example.MySpringManagedBusinessObject">
 *   ...
 * </bean></pre>
 *
 * @author Juergen Hoeller
 * @since 1.1
 * @see WebApplicationContextVariableResolver
 * @see FacesContextUtils#getRequiredWebApplicationContext
 */
public class DelegatingVariableResolver extends VariableResolver {

	/** Logger available to subclasses */
	protected final Log logger = LogFactory.getLog(getClass());

	protected final VariableResolver originalVariableResolver;


	/**
	 * Create a new DelegatingVariableResolver, using the given original VariableResolver.
	 * <p>A JSF implementation will automatically pass its original resolver into the
	 * constructor of a configured resolver, provided that there is a corresponding
	 * constructor argument.
	 * @param originalVariableResolver the original VariableResolver
	 */
	public DelegatingVariableResolver(VariableResolver originalVariableResolver) {
		Assert.notNull(originalVariableResolver, "Original JSF VariableResolver must not be null");
		this.originalVariableResolver = originalVariableResolver;
	}

	/**
	 * Return the original JSF VariableResolver that this resolver delegates to.
	 * Used to resolve standard JSF-managed beans.
	 */
	protected final VariableResolver getOriginalVariableResolver() {
		return this.originalVariableResolver;
	}


	/**
	 * Delegate to the original VariableResolver first, then try to
	 * resolve the variable as Spring bean in the root WebApplicationContext.
	 */
	public Object resolveVariable(FacesContext facesContext, String name) throws EvaluationException {
		Object value = resolveOriginal(facesContext, name);
		if (value != null) {
			return value;
		}
		Object bean = resolveSpringBean(facesContext, name);
		if (bean != null) {
			return bean;
		}
		return null;
	}

	/**
	 * Resolve the attribute via the original JSF VariableResolver.
	 */
	protected Object resolveOriginal(FacesContext facesContext, String name) {
		if (logger.isTraceEnabled()) {
			logger.trace("Attempting to resolve variable '" + name + "' via original VariableResolver");
		}
		Object value = getOriginalVariableResolver().resolveVariable(facesContext, name);
		if (value != null && logger.isDebugEnabled()) {
			logger.debug("Successfully resolved variable '" + name + "' via original VariableResolver");
		}
		return value;
	}

	/**
	 * Resolve the attribute as a Spring bean in the ApplicationContext.
	 */
	protected Object resolveSpringBean(FacesContext facesContext, String name) {
		if (logger.isTraceEnabled()) {
			logger.trace("Attempting to resolve variable '" + name + "' in Spring ApplicationContext");
		}
		BeanFactory bf = getBeanFactory(facesContext);
		if (bf.containsBean(name)) {
			if (logger.isDebugEnabled()) {
				logger.debug("Successfully resolved variable '" + name + "' in Spring ApplicationContext");
			}
			return bf.getBean(name);
		}
		else {
			return null;
		}
	}

	/**
	 * Retrieve the Spring BeanFactory to delegate bean name resolution to.
	 * <p>The default implementation delegates to getWebApplicationContext.
	 * Can be overridden to provide an arbitrary BeanFactory reference to resolve
	 * against; usually, this will be a full Spring ApplicationContext.
	 * @param facesContext the current JSF context
	 * @return the Spring BeanFactory (never <code>null)
	 * @see #getWebApplicationContext
	 */
	protected BeanFactory getBeanFactory(FacesContext facesContext) {
		return getWebApplicationContext(facesContext);
	}

	/**
	 * Retrieve the web application context to delegate bean name resolution to.
	 * <p>The default implementation delegates to FacesContextUtils.
	 * @param facesContext the current JSF context
	 * @return the Spring web application context (never <code>null)
	 * @see FacesContextUtils#getRequiredWebApplicationContext
	 */
	protected WebApplicationContext getWebApplicationContext(FacesContext facesContext) {
		return FacesContextUtils.getRequiredWebApplicationContext(facesContext);
	}

}

Other Spring Framework examples (source code examples)

Here is a short list of links related to this Spring Framework DelegatingVariableResolver.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.