Skip to content

Commit 3322368

Browse files
committed
fixed TilesConfigurer's init-param handling; added simple bootstrap test for Tiles (SPR-6606)
1 parent 7aec01b commit 3322368

File tree

4 files changed

+157
-36
lines changed

4 files changed

+157
-36
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright 2002-2010 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.web.servlet.view.tiles2;
18+
19+
import java.util.Enumeration;
20+
import java.util.LinkedHashMap;
21+
import java.util.Map;
22+
import javax.servlet.ServletContext;
23+
24+
import org.apache.tiles.Initializable;
25+
import org.apache.tiles.TilesApplicationContext;
26+
import org.apache.tiles.context.AbstractTilesApplicationContextFactory;
27+
import org.apache.tiles.servlet.context.wildcard.WildcardServletTilesApplicationContext;
28+
29+
/**
30+
* Spring-specific subclass of the standard Tiles AbstractTilesApplicationContextFactory,
31+
* passing given properties through as Tiles init-param map.
32+
*
33+
* @author Juergen Hoeller
34+
* @since 3.0
35+
* @see TilesConfigurer#setTilesProperties
36+
*/
37+
public class SpringTilesApplicationContextFactory extends AbstractTilesApplicationContextFactory
38+
implements Initializable {
39+
40+
private Map<String, String> params;
41+
42+
public void init(Map<String, String> params) {
43+
this.params = params;
44+
}
45+
46+
public TilesApplicationContext createApplicationContext(Object context) {
47+
return new SpringWildcardServletTilesApplicationContext((ServletContext) context, this.params);
48+
}
49+
50+
51+
/**
52+
* Custom subclass of the standard Tiles WildcardServletTilesApplicationContext,
53+
* passing given properties through as Tiles init-param map.
54+
*/
55+
private static class SpringWildcardServletTilesApplicationContext extends WildcardServletTilesApplicationContext {
56+
57+
private final Map<String, String> mergedInitParams;
58+
59+
public SpringWildcardServletTilesApplicationContext(ServletContext servletContext, Map<String, String> params) {
60+
super(servletContext);
61+
this.mergedInitParams = new LinkedHashMap<String, String>();
62+
Enumeration initParamNames = servletContext.getInitParameterNames();
63+
while (initParamNames.hasMoreElements()) {
64+
String initParamName = (String) initParamNames.nextElement();
65+
this.mergedInitParams.put(initParamName, servletContext.getInitParameter(initParamName));
66+
}
67+
if (params != null) {
68+
this.mergedInitParams.putAll(params);
69+
}
70+
}
71+
72+
@Override
73+
public Map<String, String> getInitParams() {
74+
return this.mergedInitParams;
75+
}
76+
}
77+
78+
}

org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/view/tiles2/TilesConfigurer.java

Lines changed: 10 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2009 the original author or authors.
2+
* Copyright 2002-2010 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,14 +16,14 @@
1616

1717
package org.springframework.web.servlet.view.tiles2;
1818

19-
import java.util.Enumeration;
20-
import java.util.LinkedHashMap;
19+
import java.util.HashMap;
2120
import java.util.Map;
2221
import java.util.Properties;
2322
import javax.servlet.ServletContext;
2423

2524
import org.apache.commons.logging.Log;
2625
import org.apache.commons.logging.LogFactory;
26+
import org.apache.tiles.TilesApplicationContext;
2727
import org.apache.tiles.TilesException;
2828
import org.apache.tiles.context.AbstractTilesApplicationContextFactory;
2929
import org.apache.tiles.definition.DefinitionsFactory;
@@ -32,9 +32,7 @@
3232
import org.apache.tiles.evaluator.impl.DirectAttributeEvaluator;
3333
import org.apache.tiles.factory.TilesContainerFactory;
3434
import org.apache.tiles.preparer.BasicPreparerFactory;
35-
import org.apache.tiles.servlet.context.ServletTilesApplicationContext;
3635
import org.apache.tiles.servlet.context.ServletUtil;
37-
import org.apache.tiles.servlet.context.wildcard.WildcardServletTilesApplicationContextFactory;
3836
import org.apache.tiles.startup.BasicTilesInitializer;
3937
import org.apache.tiles.startup.TilesInitializer;
4038

@@ -91,14 +89,14 @@ public class TilesConfigurer implements ServletContextAware, InitializingBean, D
9189

9290
protected final Log logger = LogFactory.getLog(getClass());
9391

94-
private final Properties tilesPropertyMap = new Properties();
92+
private final Map<String, String> tilesPropertyMap = new HashMap<String, String>();
9593

9694
private ServletContext servletContext;
9795

9896

9997
public TilesConfigurer() {
10098
this.tilesPropertyMap.put(AbstractTilesApplicationContextFactory.APPLICATION_CONTEXT_FACTORY_INIT_PARAM,
101-
WildcardServletTilesApplicationContextFactory.class.getName());
99+
SpringTilesApplicationContextFactory.class.getName());
102100
this.tilesPropertyMap.put(TilesContainerFactory.PREPARER_FACTORY_INIT_PARAM,
103101
BasicPreparerFactory.class.getName());
104102
this.tilesPropertyMap.put(TilesContainerFactory.CONTAINER_FACTORY_MUTABLE_INIT_PARAM,
@@ -199,13 +197,15 @@ public void setServletContext(ServletContext servletContext) {
199197
* @see #createTilesInitializer()
200198
*/
201199
public void afterPropertiesSet() throws TilesException {
202-
createTilesInitializer().initialize(
203-
new PropertyExposingServletTilesApplicationContext(this.servletContext, this.tilesPropertyMap));
200+
SpringTilesApplicationContextFactory factory = new SpringTilesApplicationContextFactory();
201+
factory.init(this.tilesPropertyMap);
202+
TilesApplicationContext preliminaryContext = factory.createApplicationContext(this.servletContext);
203+
createTilesInitializer().initialize(preliminaryContext);
204204
}
205205

206206
/**
207207
* Creates a new instance of {@link org.apache.tiles.startup.BasicTilesInitializer}.
208-
* Override it to use a different initializer.
208+
* <p>Override it to use a different initializer.
209209
* @see org.apache.tiles.web.startup.TilesListener#createTilesInitializer()
210210
*/
211211
protected TilesInitializer createTilesInitializer() {
@@ -220,30 +220,4 @@ public void destroy() throws TilesException {
220220
ServletUtil.setContainer(this.servletContext, null);
221221
}
222222

223-
224-
private static class PropertyExposingServletTilesApplicationContext extends ServletTilesApplicationContext {
225-
226-
private final Map<String, String> mergedInitParams;
227-
228-
public PropertyExposingServletTilesApplicationContext(ServletContext servletContext, Properties properties) {
229-
super(servletContext);
230-
this.mergedInitParams = new LinkedHashMap<String, String>();
231-
Enumeration initParamNames = servletContext.getInitParameterNames();
232-
while (initParamNames.hasMoreElements()) {
233-
String initParamName = (String) initParamNames.nextElement();
234-
this.mergedInitParams.put(initParamName, servletContext.getInitParameter(initParamName));
235-
}
236-
Enumeration propertyNames = properties.propertyNames();
237-
while (propertyNames.hasMoreElements()) {
238-
String propertyName = (String) propertyNames.nextElement();
239-
this.mergedInitParams.put(propertyName, properties.getProperty(propertyName));
240-
}
241-
}
242-
243-
@Override
244-
public Map<String, String> getInitParams() {
245-
return this.mergedInitParams;
246-
}
247-
}
248-
249223
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright 2002-2010 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.web.servlet.view.tiles2;
18+
19+
import java.util.Properties;
20+
21+
import org.apache.tiles.TilesApplicationContext;
22+
import org.apache.tiles.context.TilesRequestContext;
23+
import org.apache.tiles.evaluator.impl.DirectAttributeEvaluator;
24+
import org.apache.tiles.factory.TilesContainerFactory;
25+
import org.apache.tiles.impl.BasicTilesContainer;
26+
import org.apache.tiles.servlet.context.ServletTilesApplicationContext;
27+
import org.apache.tiles.servlet.context.ServletTilesRequestContext;
28+
import org.apache.tiles.servlet.context.ServletUtil;
29+
import static org.junit.Assert.*;
30+
import org.junit.Test;
31+
32+
import org.springframework.mock.web.MockHttpServletRequest;
33+
import org.springframework.mock.web.MockHttpServletResponse;
34+
import org.springframework.mock.web.MockServletContext;
35+
36+
/**
37+
* @author Juergen Hoeller
38+
* */
39+
public class TilesConfigurerTests {
40+
41+
@Test
42+
public void simpleBootstrap() {
43+
MockServletContext sc = new MockServletContext();
44+
TilesConfigurer tc = new TilesConfigurer();
45+
tc.setDefinitions(new String[] {"/org/springframework/web/servlet/view/tiles2/tiles-definitions.xml"});
46+
Properties props = new Properties();
47+
props.setProperty(TilesContainerFactory.ATTRIBUTE_EVALUATOR_INIT_PARAM, DirectAttributeEvaluator.class.getName());
48+
tc.setTilesProperties(props);
49+
tc.setServletContext(sc);
50+
tc.afterPropertiesSet();
51+
52+
BasicTilesContainer container = (BasicTilesContainer) ServletUtil.getContainer(sc);
53+
TilesApplicationContext appContext = new ServletTilesApplicationContext(sc);
54+
TilesRequestContext requestContext = new ServletTilesRequestContext(appContext,
55+
new MockHttpServletRequest(), new MockHttpServletResponse());
56+
assertNotNull(container.getDefinitionsFactory().getDefinition("test", requestContext));
57+
}
58+
59+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<!DOCTYPE tiles-definitions PUBLIC
3+
"-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"
4+
"http://tiles.apache.org/dtds/tiles-config_2_0.dtd">
5+
6+
<tiles-definitions>
7+
8+
<definition name="test" template="/WEB-INF/tiles/test.jsp"/>
9+
10+
</tiles-definitions>

0 commit comments

Comments
 (0)