Author: ddewolf Date: Tue Oct 31 11:52:16 2006 New Revision: 469624 URL: http://svn.apache.org/viewvc?view=rev&rev=469624 Log: Adding container initialization of tiles-defs
Added: struts/sandbox/trunk/tiles/tiles-core/src/test/java/org/apache/tiles/factory/test-defs.xml (with props) Modified: struts/sandbox/trunk/tiles/tiles-api/src/main/java/org/apache/tiles/TilesApplicationContext.java struts/sandbox/trunk/tiles/tiles-container-test/src/main/webapp/WEB-INF/web.xml struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/impl/BasicTilesContainer.java struts/sandbox/trunk/tiles/tiles-core/src/test/java/org/apache/tiles/factory/TilesContainerFactoryTest.java Modified: struts/sandbox/trunk/tiles/tiles-api/src/main/java/org/apache/tiles/TilesApplicationContext.java URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/tiles/tiles-api/src/main/java/org/apache/tiles/TilesApplicationContext.java?view=diff&rev=469624&r1=469623&r2=469624 ============================================================================== --- struts/sandbox/trunk/tiles/tiles-api/src/main/java/org/apache/tiles/TilesApplicationContext.java (original) +++ struts/sandbox/trunk/tiles/tiles-api/src/main/java/org/apache/tiles/TilesApplicationContext.java Tue Oct 31 11:52:16 2006 @@ -35,22 +35,34 @@ /** * Returns a mutable Map that maps application scope attribute names to * their values. + * + * @return Map of key value pairs. */ - public Map getApplicationScope(); + Map<String, Object> getApplicationScope(); /** * Return an immutable Map that maps context application initialization * parameters to their values. + * + * @return initialization parameters */ - public Map getInitParams(); + Map<String, String> getInitParams(); /** * Return a URL for the application resource mapped to the specified path. + * + * @param path to the desired resource. + * @return the first located resource which matches the given path. + * @throws java.net.MalformedURLException if the path is malformed */ - public URL getResource(String path) throws MalformedURLException; + URL getResource(String path) throws MalformedURLException; /** * Return a URL for the application resource mapped to the specified path. + * + * @param path to the desired resource. + * @return all resources which match the given path. + * @throws java.net.MalformedURLException if the url is illegal */ - public URL[] getResources(String path) throws MalformedURLException; + URL[] getResources(String path) throws MalformedURLException; } Modified: struts/sandbox/trunk/tiles/tiles-container-test/src/main/webapp/WEB-INF/web.xml URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/tiles/tiles-container-test/src/main/webapp/WEB-INF/web.xml?view=diff&rev=469624&r1=469623&r2=469624 ============================================================================== --- struts/sandbox/trunk/tiles/tiles-container-test/src/main/webapp/WEB-INF/web.xml (original) +++ struts/sandbox/trunk/tiles/tiles-container-test/src/main/webapp/WEB-INF/web.xml Tue Oct 31 11:52:16 2006 @@ -1,40 +1,38 @@ <?xml version="1.0" encoding="ISO-8859-1"?> <web-app xmlns="http://java.sun.com/xml/ns/j2ee" - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" - version="2.4"> - - <display-name>Tiles 2 Test Application</display-name> - - <!-- Standard Action Servlet Configuration --> - <servlet> - <servlet-name>tiles</servlet-name> - <servlet-class>org.apache.tiles.servlet.TilesServlet</servlet-class> - <init-param> - <param-name>definitions-config</param-name> - <param-value>/WEB-INF/tiles-defs.xml</param-value> - </init-param> - <load-on-startup>2</load-on-startup> - </servlet> - - <!-- Standard Action Servlet Configuration --> - <servlet> - <servlet-name>layoutServlet</servlet-name> - <servlet-class>org.apache.tiles.test.servlet.IncludingServlet</servlet-class> - <init-param> - <param-name>include</param-name> - <param-value>/layout.jsp</param-value> - </init-param> - </servlet> - - <welcome-file-list> - <welcome-file>index.jsp</welcome-file> - </welcome-file-list> - - <servlet-mapping> - <servlet-name>layoutServlet</servlet-name> - <url-pattern>/servlets/layoutServlet</url-pattern> - </servlet-mapping> + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" + version="2.4"> + + <display-name>Tiles 2 Test Application</display-name> + + <context-param> + <param-name>org.apache.tiles.CONTEXT_FACTORY</param-name> + <param-value>org.apache.tiles.context.BasicTilesContextFactory</param-value> + </context-param> + + <listener> + <listener-class>org.apache.tiles.listener.TilesContainerListener</listener-class> + </listener> + + <!-- Standard Action Servlet Configuration --> + <servlet> + <servlet-name>layoutServlet</servlet-name> + <servlet-class>org.apache.tiles.test.servlet.IncludingServlet</servlet-class> + <init-param> + <param-name>include</param-name> + <param-value>/layout.jsp</param-value> + </init-param> + </servlet> + + <welcome-file-list> + <welcome-file>index.jsp</welcome-file> + </welcome-file-list> + + <servlet-mapping> + <servlet-name>layoutServlet</servlet-name> + <url-pattern>/servlets/layoutServlet</url-pattern> + </servlet-mapping> </web-app> Modified: struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/impl/BasicTilesContainer.java URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/impl/BasicTilesContainer.java?view=diff&rev=469624&r1=469623&r2=469624 ============================================================================== --- struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/impl/BasicTilesContainer.java (original) +++ struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/impl/BasicTilesContainer.java Tue Oct 31 11:52:16 2006 @@ -19,11 +19,14 @@ import org.apache.tiles.*; import org.apache.tiles.context.TilesContextFactory; -import org.apache.tiles.definition.UrlDefinitionsFactory; import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.Log; import java.util.Map; +import java.util.List; +import java.util.StringTokenizer; +import java.util.ArrayList; +import java.net.MalformedURLException; /** * Basic implementation of the tiles container interface. @@ -31,13 +34,25 @@ * injecting customized services, not necessarily by * override the container * - * @since 2.0 * @version $Rev$ - * + * @since 2.0 */ public class BasicTilesContainer implements TilesContainer { /** + * Constant representing the configuration parameter + * used to define the tiles definition resources. + */ + public static final String DEFINITIONS_CONFIG = "tiles-definitions-config"; + + /** + * Compatibility constant. + * + * @deprecated use [EMAIL PROTECTED] #DEFINITIONS_CONFIG} to avoid namespace collisions. + */ + private static final String LEGACY_DEFINITIONS_CONFIG = "definitions-config"; + + /** * Log instance for all BasicTilesContainer * instances. */ @@ -51,7 +66,7 @@ /** * Initialize the Container with the given configuration. * - * @param context + * @param context application context for this container * @throws TilesException */ public void init(TilesApplicationContext context) throws TilesException { @@ -59,6 +74,19 @@ this.context = context; contextFactory.init(context.getInitParams()); definitionsFactory.init(context.getInitParams()); + + //Everything is now initialized. We will populate + // our definitions + String resourceString = getResourceString(); + List<String> resources = getResourceNames(resourceString); + try { + for(String resource : resources) { + definitionsFactory.addSource(context.getResource(resource)); + } + } catch (MalformedURLException e) { + throw new DefinitionsFactoryException("Unable to parse definitions from " + +resourceString, e); + } } /** @@ -66,16 +94,18 @@ * initialized. Utility method used for methods which * can not be invoked after the container has been * started. - * @throws IllegalStateException if the container has already been initialized. + * + * @throws IllegalStateException if the container has already been initialized. */ private void checkInit() { - if(context != null) { + if (context != null) { throw new IllegalStateException("Container allready initialized"); } } /** * Standard Getter + * * @return the application context for this container. */ public TilesApplicationContext getApplicationContext() { @@ -85,6 +115,7 @@ /** * Standard Getter + * * @return the definitions factory used by this container. */ public DefinitionsFactory getDefinitionsFactory() { @@ -93,6 +124,7 @@ /** * Standard Setter + * * @param definitionsFactory the definitions factory for this instance. */ public void setDefinitionsFactory(DefinitionsFactory definitionsFactory) { @@ -124,7 +156,7 @@ definitionsFactory.getDefinition(definitionName, request); if (definition == null) { - if(LOG.isWarnEnabled()) { + if (LOG.isWarnEnabled()) { String message = "Unable to find the definition '" + definitionName + "'"; LOG.warn(message); } @@ -165,6 +197,42 @@ context.addMissing(definition.getAttributes()); } return context; + } + + /** + * Derive the resource string from the initialization parameters. + * If no parameter [EMAIL PROTECTED] #DEFINITIONS_CONFIG} is available, attempts + * to retrieve [EMAIL PROTECTED] #LEGACY_DEFINITIONS_CONFIG}. If niether are + * available, returns "/WEB-INF/tiles.xml". + * + * @return resource string to be parsed. + */ + protected String getResourceString() { + Map<String, String> parms = context.getInitParams(); + String resourceStr = parms.get(DEFINITIONS_CONFIG); + if (resourceStr == null) { + resourceStr = parms.get(LEGACY_DEFINITIONS_CONFIG); + } + if (resourceStr == null) { + resourceStr = "/WEB-INF/tiles.xml"; + } + return resourceStr; + } + + /** + * Parse the resourceString into a list of resource paths + * which can be loaded by the application context. + * + * @param resourceString comma seperated resources + * @return parsed resources + */ + protected List<String> getResourceNames(String resourceString) { + StringTokenizer tokenizer = new StringTokenizer(resourceString, ","); + List<String> filenames = new ArrayList<String>(tokenizer.countTokens()); + while (tokenizer.hasMoreTokens()) { + filenames.add(tokenizer.nextToken().trim()); + } + return filenames; } } Modified: struts/sandbox/trunk/tiles/tiles-core/src/test/java/org/apache/tiles/factory/TilesContainerFactoryTest.java URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/tiles/tiles-core/src/test/java/org/apache/tiles/factory/TilesContainerFactoryTest.java?view=diff&rev=469624&r1=469623&r2=469624 ============================================================================== --- struts/sandbox/trunk/tiles/tiles-core/src/test/java/org/apache/tiles/factory/TilesContainerFactoryTest.java (original) +++ struts/sandbox/trunk/tiles/tiles-core/src/test/java/org/apache/tiles/factory/TilesContainerFactoryTest.java Tue Oct 31 11:52:16 2006 @@ -30,6 +30,8 @@ import java.util.Map; import java.util.Vector; +import java.net.URL; +import java.net.MalformedURLException; public class TilesContainerFactoryTest extends TestCase { @@ -65,11 +67,13 @@ } } - public void testCreateContainer() throws TilesException { + public void testCreateContainer() throws TilesException, MalformedURLException { + URL url = getClass().getResource("test-defs.xml"); EasyMock.expect(context.getInitParameter(TilesContainerFactory.CONTAINER_FACTORY_INIT_PARAM)).andReturn(null); EasyMock.expect(context.getInitParameter(TilesContainerFactory.CONTEXT_FACTORY_INIT_PARAM)).andReturn(null); EasyMock.expect(context.getInitParameter(TilesContainerFactory.DEFINITIONS_FACTORY_INIT_PARAM)).andReturn(null); EasyMock.expect(context.getInitParameter(EasyMock.isA(String.class))).andReturn(null).anyTimes(); + EasyMock.expect(context.getResource("/WEB-INF/tiles.xml")).andReturn(url); EasyMock.replay(context); TilesContainerFactory factory = TilesContainerFactory.getFactory(context); Added: struts/sandbox/trunk/tiles/tiles-core/src/test/java/org/apache/tiles/factory/test-defs.xml URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/tiles/tiles-core/src/test/java/org/apache/tiles/factory/test-defs.xml?view=auto&rev=469624 ============================================================================== --- struts/sandbox/trunk/tiles/tiles-core/src/test/java/org/apache/tiles/factory/test-defs.xml (added) +++ struts/sandbox/trunk/tiles/tiles-core/src/test/java/org/apache/tiles/factory/test-defs.xml Tue Oct 31 11:52:16 2006 @@ -0,0 +1,17 @@ +<?xml version="1.0" encoding="ISO-8859-1" ?> + + <!DOCTYPE tiles-definitions PUBLIC + "-//Apache Software Foundation//DTD Tiles Configuration 1.1//EN" + "http://struts.apache.org/dtds/tiles-config_1_1.dtd"> + +<tiles-definitions> + <definition name="test.def1" path="/test.jsp"> + <put name="country" value="default"/> + <put name="title" value="Tiles Library Documentation" /> + <put name="header" value="/common/header.jsp" /> + <put name="menu" value="doc.menu.main" /> + <put name="footer" value="/common/footer.jsp" /> + <put name="body" value="doc.portal.body" /> + </definition> + +</tiles-definitions> Propchange: struts/sandbox/trunk/tiles/tiles-core/src/test/java/org/apache/tiles/factory/test-defs.xml ------------------------------------------------------------------------------ svn:eol-style = native Propchange: struts/sandbox/trunk/tiles/tiles-core/src/test/java/org/apache/tiles/factory/test-defs.xml ------------------------------------------------------------------------------ svn:keywords = Id Author Date