fixed TilesConfigurer's init-param handling; added simple bootstrap test for Tiles (SPR-6606)

This commit is contained in:
Juergen Hoeller 2010-02-09 12:32:32 +00:00
parent 7aec01bbac
commit 3322368106
4 changed files with 157 additions and 36 deletions

View File

@ -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<String, String> params;
public void init(Map<String, String> 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<String, String> mergedInitParams;
public SpringWildcardServletTilesApplicationContext(ServletContext servletContext, Map<String, String> params) {
super(servletContext);
this.mergedInitParams = new LinkedHashMap<String, String>();
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<String, String> getInitParams() {
return this.mergedInitParams;
}
}
}

View File

@ -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<String, String> tilesPropertyMap = new HashMap<String, String>();
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.
* <p>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<String, String> mergedInitParams;
public PropertyExposingServletTilesApplicationContext(ServletContext servletContext, Properties properties) {
super(servletContext);
this.mergedInitParams = new LinkedHashMap<String, String>();
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<String, String> getInitParams() {
return this.mergedInitParams;
}
}
}

View File

@ -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));
}
}

View File

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE tiles-definitions PUBLIC
"-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"
"http://tiles.apache.org/dtds/tiles-config_2_0.dtd">
<tiles-definitions>
<definition name="test" template="/WEB-INF/tiles/test.jsp"/>
</tiles-definitions>