From 3322368106f3e3bca23b5f52e690d80ed052bb40 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Tue, 9 Feb 2010 12:32:32 +0000 Subject: [PATCH] fixed TilesConfigurer's init-param handling; added simple bootstrap test for Tiles (SPR-6606) --- .../SpringTilesApplicationContextFactory.java | 78 +++++++++++++++++++ .../servlet/view/tiles2/TilesConfigurer.java | 46 +++-------- .../view/tiles2/TilesConfigurerTests.java | 59 ++++++++++++++ .../servlet/view/tiles2/tiles-definitions.xml | 10 +++ 4 files changed, 157 insertions(+), 36 deletions(-) create mode 100644 org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/view/tiles2/SpringTilesApplicationContextFactory.java create mode 100644 org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/view/tiles2/TilesConfigurerTests.java create mode 100644 org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/view/tiles2/tiles-definitions.xml diff --git a/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/view/tiles2/SpringTilesApplicationContextFactory.java b/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/view/tiles2/SpringTilesApplicationContextFactory.java new file mode 100644 index 00000000000..3cb5cca3884 --- /dev/null +++ b/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/view/tiles2/SpringTilesApplicationContextFactory.java @@ -0,0 +1,78 @@ +/* + * Copyright 2002-2010 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.servlet.view.tiles2; + +import java.util.Enumeration; +import java.util.LinkedHashMap; +import java.util.Map; +import javax.servlet.ServletContext; + +import org.apache.tiles.Initializable; +import org.apache.tiles.TilesApplicationContext; +import org.apache.tiles.context.AbstractTilesApplicationContextFactory; +import org.apache.tiles.servlet.context.wildcard.WildcardServletTilesApplicationContext; + +/** + * Spring-specific subclass of the standard Tiles AbstractTilesApplicationContextFactory, + * passing given properties through as Tiles init-param map. + * + * @author Juergen Hoeller + * @since 3.0 + * @see TilesConfigurer#setTilesProperties + */ +public class SpringTilesApplicationContextFactory extends AbstractTilesApplicationContextFactory + implements Initializable { + + private Map params; + + public void init(Map params) { + this.params = params; + } + + public TilesApplicationContext createApplicationContext(Object context) { + return new SpringWildcardServletTilesApplicationContext((ServletContext) context, this.params); + } + + + /** + * Custom subclass of the standard Tiles WildcardServletTilesApplicationContext, + * passing given properties through as Tiles init-param map. + */ + private static class SpringWildcardServletTilesApplicationContext extends WildcardServletTilesApplicationContext { + + private final Map mergedInitParams; + + public SpringWildcardServletTilesApplicationContext(ServletContext servletContext, Map params) { + super(servletContext); + this.mergedInitParams = new LinkedHashMap(); + Enumeration initParamNames = servletContext.getInitParameterNames(); + while (initParamNames.hasMoreElements()) { + String initParamName = (String) initParamNames.nextElement(); + this.mergedInitParams.put(initParamName, servletContext.getInitParameter(initParamName)); + } + if (params != null) { + this.mergedInitParams.putAll(params); + } + } + + @Override + public Map getInitParams() { + return this.mergedInitParams; + } + } + +} diff --git a/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/view/tiles2/TilesConfigurer.java b/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/view/tiles2/TilesConfigurer.java index 9b63ac540ee..a406165d455 100644 --- a/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/view/tiles2/TilesConfigurer.java +++ b/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/view/tiles2/TilesConfigurer.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2009 the original author or authors. + * Copyright 2002-2010 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. @@ -16,14 +16,14 @@ package org.springframework.web.servlet.view.tiles2; -import java.util.Enumeration; -import java.util.LinkedHashMap; +import java.util.HashMap; import java.util.Map; import java.util.Properties; import javax.servlet.ServletContext; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.apache.tiles.TilesApplicationContext; import org.apache.tiles.TilesException; import org.apache.tiles.context.AbstractTilesApplicationContextFactory; import org.apache.tiles.definition.DefinitionsFactory; @@ -32,9 +32,7 @@ import org.apache.tiles.evaluator.el.ELAttributeEvaluator; import org.apache.tiles.evaluator.impl.DirectAttributeEvaluator; import org.apache.tiles.factory.TilesContainerFactory; import org.apache.tiles.preparer.BasicPreparerFactory; -import org.apache.tiles.servlet.context.ServletTilesApplicationContext; import org.apache.tiles.servlet.context.ServletUtil; -import org.apache.tiles.servlet.context.wildcard.WildcardServletTilesApplicationContextFactory; import org.apache.tiles.startup.BasicTilesInitializer; import org.apache.tiles.startup.TilesInitializer; @@ -91,14 +89,14 @@ public class TilesConfigurer implements ServletContextAware, InitializingBean, D protected final Log logger = LogFactory.getLog(getClass()); - private final Properties tilesPropertyMap = new Properties(); + private final Map tilesPropertyMap = new HashMap(); private ServletContext servletContext; public TilesConfigurer() { this.tilesPropertyMap.put(AbstractTilesApplicationContextFactory.APPLICATION_CONTEXT_FACTORY_INIT_PARAM, - WildcardServletTilesApplicationContextFactory.class.getName()); + SpringTilesApplicationContextFactory.class.getName()); this.tilesPropertyMap.put(TilesContainerFactory.PREPARER_FACTORY_INIT_PARAM, BasicPreparerFactory.class.getName()); this.tilesPropertyMap.put(TilesContainerFactory.CONTAINER_FACTORY_MUTABLE_INIT_PARAM, @@ -199,13 +197,15 @@ public class TilesConfigurer implements ServletContextAware, InitializingBean, D * @see #createTilesInitializer() */ public void afterPropertiesSet() throws TilesException { - createTilesInitializer().initialize( - new PropertyExposingServletTilesApplicationContext(this.servletContext, this.tilesPropertyMap)); + SpringTilesApplicationContextFactory factory = new SpringTilesApplicationContextFactory(); + factory.init(this.tilesPropertyMap); + TilesApplicationContext preliminaryContext = factory.createApplicationContext(this.servletContext); + createTilesInitializer().initialize(preliminaryContext); } /** * Creates a new instance of {@link org.apache.tiles.startup.BasicTilesInitializer}. - * Override it to use a different initializer. + *

Override it to use a different initializer. * @see org.apache.tiles.web.startup.TilesListener#createTilesInitializer() */ protected TilesInitializer createTilesInitializer() { @@ -220,30 +220,4 @@ public class TilesConfigurer implements ServletContextAware, InitializingBean, D ServletUtil.setContainer(this.servletContext, null); } - - private static class PropertyExposingServletTilesApplicationContext extends ServletTilesApplicationContext { - - private final Map mergedInitParams; - - public PropertyExposingServletTilesApplicationContext(ServletContext servletContext, Properties properties) { - super(servletContext); - this.mergedInitParams = new LinkedHashMap(); - Enumeration initParamNames = servletContext.getInitParameterNames(); - while (initParamNames.hasMoreElements()) { - String initParamName = (String) initParamNames.nextElement(); - this.mergedInitParams.put(initParamName, servletContext.getInitParameter(initParamName)); - } - Enumeration propertyNames = properties.propertyNames(); - while (propertyNames.hasMoreElements()) { - String propertyName = (String) propertyNames.nextElement(); - this.mergedInitParams.put(propertyName, properties.getProperty(propertyName)); - } - } - - @Override - public Map getInitParams() { - return this.mergedInitParams; - } - } - } diff --git a/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/view/tiles2/TilesConfigurerTests.java b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/view/tiles2/TilesConfigurerTests.java new file mode 100644 index 00000000000..0388fef56a5 --- /dev/null +++ b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/view/tiles2/TilesConfigurerTests.java @@ -0,0 +1,59 @@ +/* + * Copyright 2002-2010 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.servlet.view.tiles2; + +import java.util.Properties; + +import org.apache.tiles.TilesApplicationContext; +import org.apache.tiles.context.TilesRequestContext; +import org.apache.tiles.evaluator.impl.DirectAttributeEvaluator; +import org.apache.tiles.factory.TilesContainerFactory; +import org.apache.tiles.impl.BasicTilesContainer; +import org.apache.tiles.servlet.context.ServletTilesApplicationContext; +import org.apache.tiles.servlet.context.ServletTilesRequestContext; +import org.apache.tiles.servlet.context.ServletUtil; +import static org.junit.Assert.*; +import org.junit.Test; + +import org.springframework.mock.web.MockHttpServletRequest; +import org.springframework.mock.web.MockHttpServletResponse; +import org.springframework.mock.web.MockServletContext; + +/** + * @author Juergen Hoeller + * */ +public class TilesConfigurerTests { + + @Test + public void simpleBootstrap() { + MockServletContext sc = new MockServletContext(); + TilesConfigurer tc = new TilesConfigurer(); + tc.setDefinitions(new String[] {"/org/springframework/web/servlet/view/tiles2/tiles-definitions.xml"}); + Properties props = new Properties(); + props.setProperty(TilesContainerFactory.ATTRIBUTE_EVALUATOR_INIT_PARAM, DirectAttributeEvaluator.class.getName()); + tc.setTilesProperties(props); + tc.setServletContext(sc); + tc.afterPropertiesSet(); + + BasicTilesContainer container = (BasicTilesContainer) ServletUtil.getContainer(sc); + TilesApplicationContext appContext = new ServletTilesApplicationContext(sc); + TilesRequestContext requestContext = new ServletTilesRequestContext(appContext, + new MockHttpServletRequest(), new MockHttpServletResponse()); + assertNotNull(container.getDefinitionsFactory().getDefinition("test", requestContext)); + } + +} diff --git a/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/view/tiles2/tiles-definitions.xml b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/view/tiles2/tiles-definitions.xml new file mode 100644 index 00000000000..0b18f927bef --- /dev/null +++ b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/view/tiles2/tiles-definitions.xml @@ -0,0 +1,10 @@ + + + + + + + +