/*
* $Header: /home/cvs/jakarta-struts/src/share/org/apache/struts/taglib/tiles/util/TagUtils.java,v 1.1 2002/06/25 03:17:19 craigmcc Exp $
* $Revision: 1.1 $
* $Date: 2002/06/25 03:17:19 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Struts", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.struts.taglib.tiles.util;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.struts.taglib.tiles.ComponentConstants;
import org.apache.struts.tiles.ComponentContext;
import org.apache.struts.tiles.ComponentDefinition;
import org.apache.struts.tiles.DefinitionsUtil;
import org.apache.struts.tiles.NoSuchDefinitionException;
import org.apache.struts.tiles.FactoryNotFoundException;
import org.apache.struts.tiles.DefinitionsFactoryException;
import org.apache.struts.action.Action;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
import javax.servlet.ServletException;
import javax.servlet.jsp.PageContext;
import java.lang.reflect.InvocationTargetException;
/**
* Collection of utilities.
* This class also serve as an interface between Components and Struts. If
* you want to rip away Struts, simply reimplement some methods in this class.
* You can copy them from Struts.
*/
public class TagUtils {
/** Debug flag */
static public final boolean debug = true;
/**
* Get scope value from string value
* @param scopeName scope as a String
* @param default returned value, if not found.
* @return scope as an int, or defaultValue if scope is null.
* @throws JspException Scope name is not recognize as a valid scope.
*/
static public int getScope( String scopeName, int defaultValue ) throws JspException
{
if(scopeName==null)
return defaultValue;
else if( scopeName.equals("request") )
{
return PageContext.REQUEST_SCOPE;
}
else if( scopeName.equals("page") )
{
return PageContext.PAGE_SCOPE;
}
else if( scopeName.equals("session") )
{
return PageContext.SESSION_SCOPE;
}
else if( scopeName.equals("application") )
{
return PageContext.APPLICATION_SCOPE;
}
else if( scopeName.equals("component") )
{
return ComponentConstants.COMPONENT_SCOPE;
}
else if( scopeName.equals("template") )
{
return ComponentConstants.COMPONENT_SCOPE;
}
else if( scopeName.equals("tile") )
{
return ComponentConstants.COMPONENT_SCOPE;
}
else
{
throw new JspException( "Error - scope translation tag : unrecognized scope '"
+ scopeName
+ "'" );
}
}
/**
* Return the value of the specified property of the specified bean,
* no matter which property reference format is used, with no
* type conversions.
*
* @param bean Bean whose property is to be extracted
* @param name Possibly indexed and/or nested name of the property
* to be extracted
*
* @exception IllegalAccessException if the caller does not have
* access to the property accessor method
* @exception InvocationTargetException if the property accessor method
* throws an exception
* @exception NoSuchMethodException if an accessor method for this
* propety cannot be found
*/
public static Object getProperty(Object bean, String name)
throws IllegalAccessException, InvocationTargetException,
NoSuchMethodException
{
return (PropertyUtils.getProperty(bean, name));
}
/**
* Retrieve bean from page context, using specified scope.
* If scope is not setted, use findAttribute.
*
* @param bean Name Name of bean to retrieve
* @param scope Name Scope or null. If null, bean is searched using
* findAttribute.
* @param pageContext Current pageContext.
*
* @return requested bean or null if not found.
* @throws JspException If scopeName is not recognize.
*/
public static Object retrieveBean( String beanName, String scopeName,
PageContext pageContext )
throws JspException
{
if( scopeName == null )
{
return findAttribute( beanName, pageContext );
} // end if
// Default value doesn't matter because we have already check it
int scope = getScope( scopeName, PageContext.PAGE_SCOPE );
//return pageContext.getAttribute( beanName, scope );
return getAttribute( beanName, scope, pageContext);
}
/**
* Search attribute in different contexts.
* First, check in component context, then use pageContext.findAttribute().
* @param beanName Name of bean to retrieve
* @param pageContext Current pageContext.
* @return requested bean or null if not found.
*/
public static Object findAttribute( String beanName,
PageContext pageContext )
{
Object attribute;
ComponentContext compContext = ComponentContext.getContext(pageContext.getRequest());
if( compContext != null )
{
attribute = compContext.findAttribute( beanName, pageContext );
if( attribute != null )
return attribute;
} // end if
// Search in pageContext scopes
return pageContext.findAttribute( beanName );
}
/**
* Get object from requested context. Return null if not found.
* Context can be "component" or normal jsp contexts.
* @param beanName Name of bean to retrieve
* @param scope Scope from which bean must be retrieved.
* @param pageContext Current pageContext.
* @return requested bean or null if not found.
*/
public static Object getAttribute( String beanName, int scope,
PageContext pageContext )
{
if( scope == ComponentConstants.COMPONENT_SCOPE )
{
ComponentContext compContext = ComponentContext.getContext(pageContext.getRequest());
return compContext.getAttribute( beanName );
}
return pageContext.getAttribute( beanName, scope );
}
/**
* Locate and return the specified property of the specified bean, from
* an optionally specified scope, in the specified page context.
*
* @param pageContext Page context to be searched
* @param beanName Name of the bean to be retrieved
* @param beanProperty Name of the property to be retrieved, or
* <code>null</code> to retrieve the bean itself
* @param beanScope Scope to be searched (page, request, session, application)
* or <code>null</code> to use <code>findAttribute()</code> instead
*
* @exception JspException if an invalid scope name
* is requested
* @exception JspException if the specified bean is not found
* @exception JspException if accessing this property causes an
* IllegalAccessException, IllegalArgumentException,
* InvocationTargetException, or NoSuchMethodException
*/
static public Object getRealValueFromBean( String beanName, String beanProperty, String beanScope, PageContext pageContext)
throws JspException
{
try
{
Object realValue;
Object bean = retrieveBean( beanName, beanScope, pageContext );
if( bean != null && beanProperty != null )
realValue = getProperty( bean, beanProperty );
else
realValue = bean; // value can be null
return realValue;
}
catch( NoSuchMethodException ex )
{
throw new JspException( "Error - component.PutAttributeTag : Error while retrieving value from bean '"
+ beanName + "' with property '"
+ beanProperty + "' in scope '"
+ beanScope + "'. (exception : "
+ ex.getMessage() );
}
catch( InvocationTargetException ex )
{
throw new JspException( "Error - component.PutAttributeTag : Error while retrieving value from bean '"
+ beanName + "' with property '"
+ beanProperty + "' in scope '"
+ beanScope + "'. (exception : "
+ ex.getMessage() );
}
catch( IllegalAccessException ex )
{
throw new JspException( "Error - component.PutAttributeTag : Error while retrieving value from bean '"
+ beanName + "' with property '"
+ beanProperty + "' in scope '"
+ beanScope + "'. (exception : "
+ ex.getMessage() );
}
}
/**
* Store bean in requested context.
* If scope is null, save in REQUEST_SCOPE context.
*
* @param pageContext current pageContext
* @param name Name of the bean
* @param scope Scope under which bean is saved (page, request, session, application)
* or <code>null</code> to store in <code>request()</code> instead
* @param value Bean value to store
*
* @exception JspException if an invalid scope name
* is requested
*/
static public void setAttribute( PageContext pageContext, String name, Object value, String scope)
throws JspException
{
if (scope == null)
pageContext.setAttribute(name, value, PageContext.REQUEST_SCOPE);
else if (scope.equalsIgnoreCase("page"))
pageContext.setAttribute(name, value, PageContext.PAGE_SCOPE);
else if (scope.equalsIgnoreCase("request"))
pageContext.setAttribute(name, value, PageContext.REQUEST_SCOPE);
else if (scope.equalsIgnoreCase("session"))
pageContext.setAttribute(name, value, PageContext.SESSION_SCOPE);
else if (scope.equalsIgnoreCase("application"))
pageContext.setAttribute(name, value, PageContext.APPLICATION_SCOPE);
else {
throw new JspException( "Error - bad scope name '" + scope + "'");
}
}
/**
* Store bean in REQUEST_SCOPE context.
*
* @param pageContext current pageContext
* @param name Name of the bean
* @param value Bean value to store
*
* @exception JspException if an invalid scope name
* is requested
*/
static public void setAttribute( PageContext pageContext, String name, Object beanValue)
throws JspException
{
pageContext.setAttribute(name, beanValue, PageContext.REQUEST_SCOPE);
}
/**
* Save the specified exception as a request attribute for later use.
*
* @param pageContext The PageContext for the current page
* @param exception The exception to be saved
*/
public static void saveException(PageContext pageContext, Throwable exception)
{
pageContext.setAttribute(Action.EXCEPTION_KEY, exception, PageContext.REQUEST_SCOPE);
}
/**
* Get component definition by its name.
* @param name Definition name
* @param pageContext The PageContext for the current page
* @throws JspException -
*/
public static ComponentDefinition getComponentDefinition(String name, PageContext pageContext)
throws JspException
{
try
{
return DefinitionsUtil.getDefinition(name, pageContext);
}
catch( NoSuchDefinitionException ex )
{
throw new JspException ( "Error : Can't get component definition for '"
+ name
+ "'. Check if this name exist in component definitions." );
}
catch( FactoryNotFoundException ex )
{ // factory not found.
throw new JspException ( ex.getMessage() );
} // end catch
catch( DefinitionsFactoryException ex )
{
if(debug)
ex.printStackTrace( );
// Save exception to be able to show it later
saveException( pageContext, ex);
throw new JspException ( ex.getMessage() );
} // end catch
}
}