cedric 2002/12/27 02:41:24 Modified: src/share/org/apache/struts/tiles TilesUtil.java Added: src/share/org/apache/struts/tiles TilesUtilStrutsModulesImpl.java TilesUtilStrutsImpl.java TilesUtilImpl.java Log: Change the TilesUtil implementations hierarchie: TilesUtil is the main entry TilesUtilImpl is the root and default implementation TilesUtilStrutsImpl is the implementation for struts TilesUtilStrutsModuleImpl is the implementation for struts with multi module. Normal user doesn't have to take care about the implementation to use. It is selected by the TilesPlugin Revision Changes Path 1.4 +40 -33 jakarta-struts/src/share/org/apache/struts/tiles/TilesUtil.java Index: TilesUtil.java =================================================================== RCS file: /home/cvs/jakarta-struts/src/share/org/apache/struts/tiles/TilesUtil.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- TilesUtil.java 17 Dec 2002 00:57:36 -0000 1.3 +++ TilesUtil.java 27 Dec 2002 10:41:23 -0000 1.4 @@ -74,6 +74,15 @@ /** * Class containing utilities for Tiles. + * Methods of this class are static, and so are accessible from anywhere. + * The underlying implementation can be changed with + * {@link void setTilesUtil(TilesUtilImpl tilesUtil)}. + * <br> + * Real implementation classes should derive from the {@link TilesUtilImpl } class. + * <br> + * Some methods are specified to throw the <code>UnsupportedOperationException</code> + * if the underlying implementation doesn't support the operation. + * * */ public class TilesUtil @@ -82,30 +91,13 @@ protected static Log log = LogFactory.getLog(TilesUtil.class); /** The implementation of tilesUtilImpl */ - protected static TilesUtilInterface tilesUtilImpl = new DefaultTilesUtilImpl(); - - protected static String test=null; - - /** - * Static constructor for tests - */ - public TilesUtil() - { - if(test==null) - { - test="initialized"; - log.warn( "TilesUtil is called for the first time" ); - } - else - { - log.error( "TilesUtil is already initialized" ); - } // end if - } + protected static TilesUtilImpl tilesUtilImpl = new TilesUtilImpl(); /** * Get the real implementation. + * @return The underlying implementation object. */ - static public TilesUtilInterface getTilesUtil() + static public TilesUtilImpl getTilesUtil() { return tilesUtilImpl; } @@ -115,13 +107,25 @@ * This method should be called only once. * Successive calls have no effect. */ - static public void setTilesUtil(TilesUtilInterface tilesUtil) + static public void setTilesUtil(TilesUtilImpl tilesUtil) { if( implAlreadySet) return; tilesUtilImpl = tilesUtil; implAlreadySet = true; } + + /** + * Getter to know if the underlying implementation is already set to another + * value than the default value. + * @return true if setTilesUtil() has already been called. + */ + static boolean isTilesUtilImplSet() + { + return implAlreadySet; + } + + /** Flag to know if internal implementation have been set by the setter method */ private static boolean implAlreadySet=false; @@ -158,15 +162,6 @@ } /** - * @param servletContext Current servlet context - * @return Definitions factory or null if not found. - */ - static public DefinitionsFactory getDefaultDefinitionsFactory(ServletContext servletContext) - { - return tilesUtilImpl.getDefaultDefinitionsFactory(servletContext); - } - - /** * Get definition factory from appropriate servlet context. * @return Definitions factory or null if not found. */ @@ -232,4 +227,16 @@ { return tilesUtilImpl.applicationClass(className); } + + + /** + * Reset internal state. + * This method is used by test suites to reset the class to its original state. + */ + protected static void testReset() + { + implAlreadySet = false; + tilesUtilImpl = new TilesUtilImpl(); + } + } 1.1 jakarta-struts/src/share/org/apache/struts/tiles/TilesUtilStrutsModulesImpl.java Index: TilesUtilStrutsModulesImpl.java =================================================================== /* * $Header: /home/cvs/jakarta-struts/src/share/org/apache/struts/tiles/TilesUtilStrutsModulesImpl.java,v 1.1 2002/12/27 10:41:23 cedric Exp $ * $Revision: 1.1 $ * $Date: 2002/12/27 10:41:23 $ * * ==================================================================== * * 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 [EMAIL PROTECTED] * * 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.tiles; import java.io.IOException; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.struts.Globals; import org.apache.struts.config.ModuleConfig; import org.apache.struts.util.RequestUtils; /** * Implementation of TilesUtil for Struts multi modules. * Methods in this implementation are aware of the Struts module context. * <br> * <ul> * <li>The method getFactory(...) return the factory for the current struts * module.</li> * <li>Methods doForward() and doInclude() use their counterparts in the * current RequestProcessor (todo).</li> * <li>The method createFactory(...) creates a factory for the current module and * stores it under appropriate property name.</li> * </ul> */ public class TilesUtilStrutsModulesImpl extends TilesUtilStrutsImpl { /** * Do a forward using request dispatcher. * * This method is used by the Tiles package anytime a forward is required. * @param uri Uri or Definition name to forward * @param request Current page request * @param response Current page response * @param servletContext Current servlet context */ public void doForward( String uri, HttpServletRequest request, HttpServletResponse response, ServletContext servletContext) throws IOException, ServletException { request.getRequestDispatcher(uri).include(request, response); } /** * Do an include using request dispatcher. * * This method is used by the Tiles package anytime an include is required. * @param uri Uri or Definition name to forward * @param request Current page request * @param response Current page response * @param servletContext Current servlet context */ public void doInclude(String uri, HttpServletRequest request, HttpServletResponse response, ServletContext servletContext) throws IOException, ServletException { // modify uri request.getRequestDispatcher(uri).forward(request, response); } /** * Get definition factory from appropriate servlet context. * @request Current request * @servletContext Current servlet context * @return Definitions factory or null if not found. */ public DefinitionsFactory getDefinitionsFactory( ServletRequest request, ServletContext servletContext) { return getDefinitionsFactory( servletContext, getModuleConfig((HttpServletRequest) request, servletContext)); } /** * Get definition factory for the module attached to specified moduleConfig. * @servletContext Current servlet context * @moduleConfig Module config of the module for which the factory is requested. * @return Definitions factory or null if not found. */ public DefinitionsFactory getDefinitionsFactory(ServletContext servletContext, ModuleConfig moduleConfig) { return (DefinitionsFactory) servletContext.getAttribute( DEFINITIONS_FACTORY + moduleConfig.getPrefix()); } /** * Create Definition factory from specified configuration object. * Create a ConfigurableDefinitionsFactory and initialize it with the configuration * object. This later can contains the factory classname to use. * Factory is made accessible from tags. * <p> * Fallback of several factory creation methods. * * @param servletContext Servlet Context passed to newly created factory. * @param factoryConfig Configuration object passed to factory. * @return newly created factory of type ConfigurableDefinitionsFactory. * @throws DefinitionsFactoryException If an error occur while initializing factory */ /* public DefinitionsFactory createDefinitionsFactory( ServletContext servletContext, DefinitionsFactoryConfig factoryConfig) throws DefinitionsFactoryException { // Create configurable factory DefinitionsFactory factory = createDefinitionFactoryInstance(factoryConfig.getFactoryClassname()); factory.init(factoryConfig, servletContext); // Make factory accessible from jsp tags (push it in appropriate context) makeDefinitionsFactoryAccessible(factory, servletContext); return factory; } */ /** * Make definition factory accessible to Tags. * Factory is stored in servlet context. * @param factory Factory to make accessible * @param servletContext Current servlet context */ protected void makeDefinitionsFactoryAccessible( DefinitionsFactory factory, ServletContext servletContext) { String prefix = factory.getConfig().getFactoryName(); servletContext.setAttribute(DEFINITIONS_FACTORY + prefix, factory); } /** * Get Tiles request processor associated to the current module. * @request Current request * @servletContext Current servlet context* * @return The TilesRequestProcessor for current request */ protected TilesRequestProcessor getRequestProcessor( HttpServletRequest request, ServletContext servletContext) { ModuleConfig moduleConfig = getModuleConfig(request, servletContext); return (TilesRequestProcessor) servletContext.getAttribute( Globals.REQUEST_PROCESSOR_KEY + moduleConfig.getPrefix()); } /** * Get the current ModuleConfig. * <br> * Lookup in the request, and do selectModule if not found. The side effect * is that the ModuleConfig object is set in the request if it was not present. * @request Current request * @servletContext Current servlet context* * @return The ModuleConfig for current request */ protected ModuleConfig getModuleConfig( HttpServletRequest request, ServletContext servletContext) { ModuleConfig moduleConfig = RequestUtils.getModuleConfig(request, servletContext); if (moduleConfig == null) { // ModuleConfig not found in current request. Select it. RequestUtils.selectModule(request, servletContext); moduleConfig = RequestUtils.getModuleConfig(request, servletContext); } return moduleConfig; } } 1.1 jakarta-struts/src/share/org/apache/struts/tiles/TilesUtilStrutsImpl.java Index: TilesUtilStrutsImpl.java =================================================================== /* * $Header: /home/cvs/jakarta-struts/src/share/org/apache/struts/tiles/TilesUtilStrutsImpl.java,v 1.1 2002/12/27 10:41:23 cedric Exp $ * $Revision: 1.1 $ * $Date: 2002/12/27 10:41:23 $ * * ==================================================================== * * 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 [EMAIL PROTECTED] * * 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.tiles; import java.io.IOException; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.struts.tiles.definition.ComponentDefinitionsFactoryWrapper; import org.apache.struts.util.RequestUtils; import org.apache.struts.config.ModuleConfig; /** * TilesUtil implementation for Struts 1.1 with one single factory. * This class contains default implementation of utilities. This implementation * is intended to be used with Struts 1.1. * This class is used as the base class for all struts 1.1 implementation of TilesUtil. */ public class TilesUtilStrutsImpl extends TilesUtilImpl { /** * Return the <code>Class</code> object for the specified fully qualified * class name, from the struts class loader. * * @param className Fully qualified class name to be loaded * @return Class object * @exception ClassNotFoundException if the class cannot be found */ public Class applicationClass(String className) throws ClassNotFoundException { return RequestUtils.applicationClass(className); } /** * Get definition factory for the module attached to specified moduleConfig. * @servletContext Current servlet context * @moduleConfig Module config of the module for which the factory is requested. * @return Definitions factory or null if not found. */ public DefinitionsFactory getDefinitionsFactory(ServletContext servletContext, ModuleConfig moduleConfig) { return (DefinitionsFactory)servletContext.getAttribute(DEFINITIONS_FACTORY); } } 1.1 jakarta-struts/src/share/org/apache/struts/tiles/TilesUtilImpl.java Index: TilesUtilImpl.java =================================================================== /* * $Header: /home/cvs/jakarta-struts/src/share/org/apache/struts/tiles/TilesUtilImpl.java,v 1.1 2002/12/27 10:41:23 cedric Exp $ * $Revision: 1.1 $ * $Date: 2002/12/27 10:41:23 $ * * ==================================================================== * * 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 [EMAIL PROTECTED] * * 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.tiles; import java.io.IOException; import java.io.Serializable; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.struts.tiles.definition.ComponentDefinitionsFactoryWrapper; /** * Default implementation of TilesUtil. * This class conatains default implementation of utilities. This implementation * is intended to be used without Struts */ public class TilesUtilImpl implements Serializable { /** Commons Logging instance.*/ protected Log log = LogFactory.getLog(TilesUtil.class); /** Constant name used to store factory in servlet context */ public static final String DEFINITIONS_FACTORY = "org.apache.struts.tiles.DEFINITIONS_FACTORY"; /** * Do a forward using request dispatcher. * * This method is used by the Tiles package anytime a forward is required. * @param uri Uri or Definition name to forward * @param request Current page request * @param servletContext Current servlet context */ public void doForward(String uri, HttpServletRequest request, HttpServletResponse response, ServletContext servletContext) throws IOException, ServletException { request.getRequestDispatcher( uri ).forward(request, response); } /** * Do an include using request dispatcher. * * This method is used by the Tiles package anytime an include is required. * @param uri Uri or Definition name to forward * @param request Current page request * @param response Current page response * @param servletContext Current servlet context */ public void doInclude(String uri, HttpServletRequest request, HttpServletResponse response, ServletContext servletContext) throws IOException, ServletException { request.getRequestDispatcher( uri ).include(request, response); } /** * Get definition factory from appropriate servlet context. * @return Definitions factory or null if not found. */ public DefinitionsFactory getDefinitionsFactory(ServletRequest request, ServletContext servletContext) { return (DefinitionsFactory)servletContext.getAttribute(DEFINITIONS_FACTORY); } /** * Create Definition factory from specified configuration object. * Create an instance of the factory , with the class specified in the config * object. Then, initialize this factory, and finally store the factory in * appropriate context by the way of * {@link #makeDefinitionsFactoryAccessible(DefinitionsFactory, ServletContext)}. * Factory creation is done by the way of {@link #createDefinitionFactoryInstance(String)} * <p> * * @param servletContext Servlet Context passed to newly created factory. * @param factoryConfig Configuration object passed to factory. * @return newly created factory of type specified in the config object. * @throws DefinitionsFactoryException If an error occur while initializing factory */ public DefinitionsFactory createDefinitionsFactory(ServletContext servletContext, DefinitionsFactoryConfig factoryConfig) throws DefinitionsFactoryException { // Create configurable factory DefinitionsFactory factory = createDefinitionFactoryInstance(factoryConfig.getFactoryClassname()); factory.init( factoryConfig, servletContext ); // Make factory accessible from jsp tags (push it in appropriate context) makeDefinitionsFactoryAccessible(factory, servletContext ); return factory; } /** * Create Definition factory of specified classname. * Factory class must extends the {@link DefinitionsFactory} class. * The factory is wrapped appropriately with {@link ComponentDefinitionsFactoryWrapper} * if it is instance of the deprecated ComponentDefinitionsFactory class. * @param classname Class name of the factory to create. * @return newly created factory. * @throws DefinitionsFactoryException If an error occur while initializing factory */ protected DefinitionsFactory createDefinitionFactoryInstance(String classname) throws DefinitionsFactoryException { try { Class factoryClass = applicationClass(classname); Object factory = factoryClass.newInstance(); // Backward compatibility : if factory classes implements old interface, // provide appropriate wrapper if( factory instanceof ComponentDefinitionsFactory ) { factory = new ComponentDefinitionsFactoryWrapper( (ComponentDefinitionsFactory)factory ); } // end if return (DefinitionsFactory)factory; } catch( ClassCastException ex ) { // Bad classname throw new DefinitionsFactoryException( "Error - createDefinitionsFactory : Factory class '" + classname +" must implements 'TilesDefinitionsFactory'.", ex ); } catch( ClassNotFoundException ex ) { // Bad classname throw new DefinitionsFactoryException( "Error - createDefinitionsFactory : Bad class name '" + classname +"'.", ex ); } catch( InstantiationException ex ) { // Bad constructor or error throw new DefinitionsFactoryException( ex ); } catch( IllegalAccessException ex ) { // throw new DefinitionsFactoryException( ex ); } } /** * Make definition factory accessible to Tags. * Factory is stored in servlet context. * @param factory Factory to make accessible * @param servletContext Current servlet context */ protected void makeDefinitionsFactoryAccessible(DefinitionsFactory factory, ServletContext servletContext) { servletContext.setAttribute(DEFINITIONS_FACTORY, factory); } /** * Return the <code>Class</code> object for the specified fully qualified * class name, from the underlying class loader. * * @param className Fully qualified class name to be loaded * @return Class object * @exception ClassNotFoundException if the class cannot be found */ public Class applicationClass(String className) throws ClassNotFoundException { return Class.forName(className); } }
-- To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>