cedric 2002/11/05 06:15:53 Modified: src/share/org/apache/struts/tiles DefinitionsFactoryConfig.java DefinitionsUtil.java TilesPlugin.java Log: Let TilesPlugin be struts 1.1 module aware: Change the initialization method, Add a configurable TilesUtil class Change other class accordingly Revision Changes Path 1.3 +55 -5 jakarta-struts/src/share/org/apache/struts/tiles/DefinitionsFactoryConfig.java Index: DefinitionsFactoryConfig.java =================================================================== RCS file: /home/cvs/jakarta-struts/src/share/org/apache/struts/tiles/DefinitionsFactoryConfig.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- DefinitionsFactoryConfig.java 24 Jul 2002 09:37:32 -0000 1.2 +++ DefinitionsFactoryConfig.java 5 Nov 2002 14:15:53 -0000 1.3 @@ -91,12 +91,14 @@ /** * Debug level value. 0=no debug info >0 = debug info. + * @deprecated Use commons-logging mechanism. */ protected int debugLevel = 0; /** * Debug level value used when parsing configuration file. * . 0=no debug info >0 = debug info. + * @deprecated Use commons-logging mechanism. */ protected int parserDebugLevel = 0; @@ -108,6 +110,19 @@ /** Definition configuration file specofied by user */ protected String definitionConfigFiles; + /** Does the factory is module aware ? */ + protected boolean moduleAware = true; + + /** + * The name associated to this factory. + * <br> + * With Struts > 1.1b3, this name is the module name to which this factory + * belong. It is set by the system. + * <br> + * In other versions, this properties is not used. + */ + protected String factoryName; + /** Alternate name for parser debug details properties in configuration file */ public static final String PARSER_DETAILS_PARAMETER_NAME = "definitions-parser-details"; /** Alternate name for parser validate properties in configuration file */ @@ -144,6 +159,26 @@ } + /** + * Get the module aware flag. + * true: user want a single factory instance + * false: user want multiple factory instance (one per module with Struts) + */ + public boolean isModuleAware() + { + return moduleAware; + } + /** + * Set the module aware flag. + * true: user want a single factory instance + * false: user want multiple factory instance (one per module with Struts) + * @param singleFactoryInstance + */ + public void setModuleAware(boolean moduleAware) + { + this.moduleAware = moduleAware; + } + /** * Access method for the factoryClassname property. * @@ -310,7 +345,7 @@ * access to the property accessor method * @exception InvocationTargetException if the property accessor method * throws an exception - * @see org.apache.commons.beanutils.BeanUtil + * @see org.apache.commons.beanutils.BeanUtils */ public void populate( Map properties) throws java.lang.IllegalAccessException,java.lang.reflect.InvocationTargetException @@ -348,5 +383,20 @@ } // end loop if( toAdd.size() > 0 ) properties.putAll( toAdd ); + } + + /** + * Get the factory name. + */ + public String getFactoryName() + { + return factoryName; + } + /** + * Set the factory name. + */ + public void setFactoryName(String factoryName) + { + this.factoryName = factoryName; } } 1.6 +12 -59 jakarta-struts/src/share/org/apache/struts/tiles/DefinitionsUtil.java Index: DefinitionsUtil.java =================================================================== RCS file: /home/cvs/jakarta-struts/src/share/org/apache/struts/tiles/DefinitionsUtil.java,v retrieving revision 1.5 retrieving revision 1.6 diff -u -r1.5 -r1.6 --- DefinitionsUtil.java 18 Oct 2002 15:27:42 -0000 1.5 +++ DefinitionsUtil.java 5 Nov 2002 14:15:53 -0000 1.6 @@ -82,8 +82,9 @@ * Utilities class for definitions factory. * Also define userDebugLevel property (to be moved from this class ?). * (to do). + * @deprecated Use {@link TilesUtil#createDefinitionsFactory(ServletContext, DefinitionsFactoryConfig)} */ -public class DefinitionsUtil implements ComponentConstants +public class DefinitionsUtil extends TilesUtil implements ComponentConstants { /** Commons Logging instance. */ protected static Log log = LogFactory.getLog(DefinitionsUtil.class); @@ -111,6 +112,7 @@ /** * Set user debug level. This property control level of errors output. + * @deprecated Use commons-logging package instead. */ public static void setUserDebugLevel( int level ) { @@ -246,36 +248,10 @@ return createDefinitionsFactory( servletContext, servletConfig); } - /** - * 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 static DefinitionsFactory createDefinitionsFactory(ServletContext servletContext, DefinitionsFactoryConfig factoryConfig) - throws DefinitionsFactoryException - { - // Set user debug level - setUserDebugLevel( factoryConfig.getDebugLevel() ); - // Create configurable factory - DefinitionsFactory factory = createDefinitionFactoryInstance(factoryConfig.getFactoryClassname()); - factory.init( factoryConfig, servletContext ); - // Make factory accessible from jsp tags - DefinitionsUtil.makeDefinitionsFactoryAccessible(factory, servletContext ); - return factory; - } - - /** * Create Definition factory from provided classname. * Factory class must extends TilesDefinitionsFactory. + * @deprecated No direct replacement. Use createDefinitionFactory. * @param classname Class name of the factory to create. * @return newly created factory. * @throws DefinitionsFactoryException If an error occur while initializing factory @@ -328,35 +304,10 @@ } /** - * Get a definition by its name. - * First, retrieve definition factory, and then get requested definition. - * Throw appropriate exception if definition or definition factory is not found. - * @param definitionName Name of requested definition. - * @param request Current servelet request - * @param servletContext current servlet context - * @throws FactoryNotFoundException Can't find definition factory. - * @throws DefinitionsFactoryException General error in factory while getting definition. - * @throws NoSuchDefinitionException No definition found for specified name - */ - static public ComponentDefinition getDefinition(String definitionName, ServletRequest request, ServletContext servletContext) - throws FactoryNotFoundException, DefinitionsFactoryException - { - try - { - return getDefinitionsFactory(servletContext).getDefinition(definitionName, - (HttpServletRequest)request, - servletContext); - } - catch( NullPointerException ex ) - { // Factory not found in context - throw new FactoryNotFoundException( "Can't get definitions factory from context." ); - } - } - - /** * Get a component / template definition by its name. * First, retrieve instance factory, and then get requested instance. * Throw appropriate exception if definition is not found. + * @deprecated Use {@link TilesUtil#getDefinition(String, ServletRequest, ServletContext)} * @param definitionName Name of requested definition. * @param pageContext Current pageContext * @throws FactoryNotFoundException Can't find definition factory. @@ -374,16 +325,18 @@ /** * Get definition factory from appropriate servlet context. * @return Definitions factory or null if not found. + * @deprecated Use {@link TilesUtil#getDefinitionsFactory(ServletRequest, ServletContext)} * @since 20020708 */ static public DefinitionsFactory getDefinitionsFactory(ServletContext servletContext) { - return (DefinitionsFactory)servletContext.getAttribute(DEFINITIONS_FACTORY); + return getDefaultDefinitionsFactory(servletContext); } /** * Make definition factory accessible to Tags. * Factory is stored in servlet context. + * @deprecated Use {@link TilesUtil#createDefinitionsFactory(ServletContext, DefinitionsFactoryConfig)} * @param factory Factory to make accessible * @param servletContext Current servlet context * @since 20020708 @@ -428,7 +381,7 @@ * access to the property accessor method * @exception java.lang.reflect.InvocationTargetException if the property accessor method * throws an exception - * @see org.apache.commons.beanutils.BeanUtil + * @see org.apache.commons.beanutils.BeanUtils * @since tiles 20020708 */ static public void populateDefinitionsFactoryConfig( DefinitionsFactoryConfig factoryConfig, ServletConfig servletConfig) 1.7 +84 -48 jakarta-struts/src/share/org/apache/struts/tiles/TilesPlugin.java Index: TilesPlugin.java =================================================================== RCS file: /home/cvs/jakarta-struts/src/share/org/apache/struts/tiles/TilesPlugin.java,v retrieving revision 1.6 retrieving revision 1.7 diff -u -r1.6 -r1.7 --- TilesPlugin.java 20 Oct 2002 19:24:32 -0000 1.6 +++ TilesPlugin.java 5 Nov 2002 14:15:53 -0000 1.7 @@ -85,17 +85,18 @@ * This plugin is to be used with Struts 1.1 in association with * TilesRequestProcessor. * <br> - * Plugin should be declared in each "struts-config.xml" file. This plugin - * ensures one common definition factory for all modules. The definition factory + * This plugin create one definition factory for each struts module. The definition factory * configuration is read first from 'web.xml' (backward compatibility), then it is * overloaded by values found in plugin property values. * <br> * The plugin changes Struts configuration by specifying a TilesRequestProcessor as - * request processor. If the user has also specified another request processor, this - * should subclass TilesRequestProcessor. + * request processor. If the user has also specified another request processor, + * this later should subclass TilesRequestProcessor. * <br> - * In case of several modules, the definition factory - * configuration is read by the first tiles plugin to be initialized. The order is + * This plugin can also be used to create one single factory for all module. + * This behavior is enable by specifying <code>moduleAware=false</code> in each + * plugin properties. In this case, the definition factory + * configuration file is read by the first tiles plugin to be initialized. The order is * determined by the order of modules declaration in web.xml. The first module * is always the default one if it exists. * The plugin should be declared in each struts-config.xml file in order to @@ -105,11 +106,34 @@ */ public class TilesPlugin implements PlugIn { - /** Commons Logging instance. */ - protected static Log log = LogFactory.getLog(TilesPlugin.class); - + /** Commons Logging instance. */ + protected static Log log = LogFactory.getLog(TilesPlugin.class); + + /** Does the factory is module aware ? */ + protected boolean moduleAware = false; + /** Associated definition factory */ - protected DefinitionsFactory definitionFactory; + protected DefinitionsFactory definitionFactory; + + /** + * Get the module aware flag. + * true: user want a single factory instance + * false: user want multiple factory instance (one per module with Struts) + */ + public boolean isModuleAware() + { + return moduleAware; + } + /** + * Set the module aware flag. + * true: user want a single factory instance + * false: user want multiple factory instance (one per module with Struts) + * @param singleFactoryInstance + */ + public void setModuleAware(boolean moduleAware) + { + this.moduleAware = moduleAware; + } /** * <p>Receive notification that the specified module is being @@ -131,21 +155,32 @@ // Set RequestProcessor class initRequestProcessorClass( config ); - // Check if factory already exist in context. - definitionFactory = DefinitionsUtil.getDefinitionsFactory( servlet.getServletContext() ); - if( definitionFactory != null ) - { - if(log.isInfoEnabled()) + // Check if user want one single factory instance for all module, + // or one factory for each module. Set the appropriate TilesUtil + // object accordingly + if( isModuleAware() ) + { + // Use appropriate TilesUtil implementation + TilesUtil.setTilesUtil( new StrutsModulesTilesUtilImpl() ); + // Set the module name in the config. This name will be used to compute + // the name under which the factory is stored. + factoryConfig.setFactoryName(config.getPrefix()); + } + else + { // We want one single shared instance. Check if it already exists in servlet context. + definitionFactory = TilesUtil.getDefaultDefinitionsFactory( servlet.getServletContext() ); + if( definitionFactory != null ) { - log.info("Factory already exists. No new creation." ); - } - return; + if(log.isInfoEnabled()) + log.info("Factory already exists for module '"+ config.getPrefix()+ "'. No new creation." ); + return; + } // end if } // end if // Create configurable factory try { - definitionFactory = DefinitionsUtil.createDefinitionsFactory(servlet.getServletContext(), factoryConfig ); + definitionFactory = TilesUtil.createDefinitionsFactory(servlet.getServletContext(), factoryConfig ); } catch( DefinitionsFactoryException ex ) { @@ -153,10 +188,8 @@ } if(log.isInfoEnabled()) { - log.info( "Tiles definition factory loaded for processor '" + config.getPrefix() + "'." ); + log.info( "Tiles definition factory loaded for module '" + config.getPrefix() + "'." ); } - - //servlet.log( "Tiles definition factory loaded for processor '" + config.getPrefix() + "'." ); } /** @@ -236,10 +269,7 @@ if( plugIns[index] != this ) { String msg = "Can't initialize tiles definition factory : plugin configuration object not found."; - if(log.isFatalEnabled()) - { - log.fatal(msg); - } + log.fatal(msg); throw new ServletException( msg ); } // end if // Get plugin @@ -263,33 +293,39 @@ String tilesProcessorClassname = TilesRequestProcessor.class.getName(); ControllerConfig ctrlConfig = config.getControllerConfig(); String configProcessorClassname = ctrlConfig.getProcessorClass(); - // Check if it is default request processor. If true, replace by Tiles' one. - if( configProcessorClassname.equals(RequestProcessor.class.getName()) - || configProcessorClassname.endsWith(tilesProcessorClassname) ) - { - ctrlConfig.setProcessorClass(tilesProcessorClassname); - return; - } - // Check if specified request processor is compatible with Tiles. + // Check if specified classname exist + Class configProcessorClass; try { - Class tilesProcessorClass = TilesRequestProcessor.class; - Class configProcessorClass = Class.forName( configProcessorClassname ); - if( !tilesProcessorClass.isAssignableFrom( configProcessorClass)) - { // Not compatible - String msg = "TilesPlugin : Specified RequestProcessor not compatible with TilesRequestProcessor"; - if(log.isFatalEnabled()) - { - log.fatal(msg); - } - throw new ServletException( msg ); - } // end if + configProcessorClass = Class.forName( configProcessorClassname ); } catch( java.lang.ClassNotFoundException ex ) { + log.fatal( "Can't set TilesRequestProcessor: bad class name '" + configProcessorClassname + "'."); throw new ServletException( ex ); } + + // Check if it is the default request processor or Tiles one. + // If true, replace by Tiles' one. + if( configProcessorClassname.equals(RequestProcessor.class.getName()) + || configProcessorClassname.endsWith(tilesProcessorClassname) ) + { + ctrlConfig.setProcessorClass(tilesProcessorClassname); + return; + } + + // Check if specified request processor is compatible with Tiles. + Class tilesProcessorClass = TilesRequestProcessor.class; + if( !tilesProcessorClass.isAssignableFrom( configProcessorClass)) + { // Not compatible + String msg = "TilesPlugin : Specified RequestProcessor not compatible with TilesRequestProcessor"; + if(log.isFatalEnabled()) + { + log.fatal(msg); + } + throw new ServletException( msg ); + } // end if } }
-- To unsubscribe, e-mail: <mailto:struts-dev-unsubscribe@;jakarta.apache.org> For additional commands, e-mail: <mailto:struts-dev-help@;jakarta.apache.org>