Skip to content

Commit 57b3ee3

Browse files
committed
Tiles 2 support back for Spring 4.3 (to be eventually removed for 5.0 now)
Issue: SPR-13229
1 parent dd930b3 commit 57b3ee3

File tree

18 files changed

+1099
-4
lines changed

18 files changed

+1099
-4
lines changed

build.gradle

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ configure(allprojects) { project ->
7171
ext.snakeyamlVersion = "1.16"
7272
ext.snifferVersion = "1.14"
7373
ext.testngVersion = "6.9.10"
74+
ext.tiles2Version = "2.2.2"
7475
ext.tiles3Version = "3.0.5"
7576
ext.tomcatVersion = "8.0.30"
7677
ext.tyrusVersion = "1.3.5" // constrained by WebLogic 12.1.3 support
@@ -945,6 +946,38 @@ project("spring-webmvc") {
945946
}
946947
}
947948

949+
project("spring-webmvc-tiles2") {
950+
description = "Spring Framework Tiles2 Integration"
951+
merge.into = project(":spring-webmvc")
952+
953+
dependencies {
954+
provided(project(":spring-context"))
955+
provided(project(":spring-web"))
956+
provided("javax.servlet:javax.servlet-api:3.0.1")
957+
optional("javax.servlet.jsp:javax.servlet.jsp-api:2.2.1")
958+
optional("javax.servlet.jsp.jstl:javax.servlet.jsp.jstl-api:1.2.1")
959+
optional("org.apache.tiles:tiles-api:${tiles2Version}")
960+
optional("org.apache.tiles:tiles-core:${tiles2Version}") {
961+
exclude group: "org.slf4j", module: "jcl-over-slf4j"
962+
}
963+
optional("org.apache.tiles:tiles-servlet:${tiles2Version}") {
964+
exclude group: "org.slf4j", module: "jcl-over-slf4j"
965+
}
966+
optional("org.apache.tiles:tiles-jsp:${tiles2Version}") {
967+
exclude group: "org.slf4j", module: "jcl-over-slf4j"
968+
}
969+
optional("org.apache.tiles:tiles-el:${tiles2Version}") {
970+
exclude group: "org.slf4j", module: "jcl-over-slf4j"
971+
}
972+
optional("org.apache.tiles:tiles-extras:${tiles2Version}") {
973+
exclude group: "org.slf4j", module: "jcl-over-slf4j"
974+
exclude group: "org.apache.velocity", module: "velocity-tools"
975+
exclude group: "org.springframework", module: "spring-web"
976+
}
977+
testCompile("org.slf4j:slf4j-jcl:${slf4jVersion}")
978+
}
979+
}
980+
948981
project("spring-webmvc-portlet") {
949982
description = "Spring Web Portlet"
950983

settings.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ include "spring-tx"
2222
include "spring-web"
2323
include "spring-webmvc"
2424
include "spring-webmvc-portlet"
25+
include "spring-webmvc-tiles2"
2526
include "spring-websocket"
2627
include "spring-framework-bom"
2728

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright 2002-2012 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 org.apache.tiles.TilesException;
20+
import org.apache.tiles.context.TilesRequestContext;
21+
import org.apache.tiles.preparer.PreparerFactory;
22+
import org.apache.tiles.preparer.ViewPreparer;
23+
24+
import org.springframework.web.context.WebApplicationContext;
25+
import org.springframework.web.servlet.DispatcherServlet;
26+
27+
/**
28+
* Abstract implementation of the Tiles {@link org.apache.tiles.preparer.PreparerFactory}
29+
* interface, obtaining the current Spring WebApplicationContext and delegating to
30+
* {@link #getPreparer(String, org.springframework.web.context.WebApplicationContext)}.
31+
*
32+
* <p><b>NOTE: Tiles 2 support is deprecated in favor of Tiles 3 and will be removed
33+
* as of Spring Framework 5.0.</b>.
34+
*
35+
* @author Juergen Hoeller
36+
* @since 2.5
37+
* @see #getPreparer(String, org.springframework.web.context.WebApplicationContext)
38+
* @see SimpleSpringPreparerFactory
39+
* @see SpringBeanPreparerFactory
40+
* @deprecated as of Spring 4.2, in favor of Tiles 3
41+
*/
42+
@Deprecated
43+
public abstract class AbstractSpringPreparerFactory implements PreparerFactory {
44+
45+
@Override
46+
public ViewPreparer getPreparer(String name, TilesRequestContext context) throws TilesException {
47+
WebApplicationContext webApplicationContext = (WebApplicationContext) context.getRequestScope().get(
48+
DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE);
49+
if (webApplicationContext == null) {
50+
webApplicationContext = (WebApplicationContext) context.getApplicationContext().getApplicationScope().get(
51+
WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
52+
if (webApplicationContext == null) {
53+
throw new IllegalStateException("No WebApplicationContext found: no ContextLoaderListener registered?");
54+
}
55+
}
56+
return getPreparer(name, webApplicationContext);
57+
}
58+
59+
/**
60+
* Obtain a preparer instance for the given preparer name,
61+
* based on the given Spring WebApplicationContext.
62+
* @param name the name of the preparer
63+
* @param context the current Spring WebApplicationContext
64+
* @return the preparer instance
65+
* @throws TilesException in case of failure
66+
*/
67+
protected abstract ViewPreparer getPreparer(String name, WebApplicationContext context) throws TilesException;
68+
69+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright 2002-2012 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.Map;
20+
import java.util.concurrent.ConcurrentHashMap;
21+
22+
import org.apache.tiles.TilesException;
23+
import org.apache.tiles.preparer.NoSuchPreparerException;
24+
import org.apache.tiles.preparer.PreparerException;
25+
import org.apache.tiles.preparer.ViewPreparer;
26+
27+
import org.springframework.web.context.WebApplicationContext;
28+
29+
/**
30+
* Tiles {@link org.apache.tiles.preparer.PreparerFactory} implementation
31+
* that expects preparer class names and builds preparer instances for those,
32+
* creating them through the Spring ApplicationContext in order to apply
33+
* Spring container callbacks and configured Spring BeanPostProcessors.
34+
*
35+
* <p><b>NOTE: Tiles 2 support is deprecated in favor of Tiles 3 and will be removed
36+
* as of Spring Framework 5.0.</b>.
37+
*
38+
* @author Juergen Hoeller
39+
* @since 2.5
40+
* @see SpringBeanPreparerFactory
41+
* @deprecated as of Spring 4.2, in favor of Tiles 3
42+
*/
43+
@Deprecated
44+
public class SimpleSpringPreparerFactory extends AbstractSpringPreparerFactory {
45+
46+
/** Cache of shared ViewPreparer instances: bean name -> bean instance */
47+
private final Map<String, ViewPreparer> sharedPreparers = new ConcurrentHashMap<String, ViewPreparer>(16);
48+
49+
50+
@Override
51+
protected ViewPreparer getPreparer(String name, WebApplicationContext context) throws TilesException {
52+
// Quick check on the concurrent map first, with minimal locking.
53+
ViewPreparer preparer = this.sharedPreparers.get(name);
54+
if (preparer == null) {
55+
synchronized (this.sharedPreparers) {
56+
preparer = this.sharedPreparers.get(name);
57+
if (preparer == null) {
58+
try {
59+
Class<?> beanClass = context.getClassLoader().loadClass(name);
60+
if (!ViewPreparer.class.isAssignableFrom(beanClass)) {
61+
throw new PreparerException(
62+
"Invalid preparer class [" + name + "]: does not implement ViewPreparer interface");
63+
}
64+
preparer = (ViewPreparer) context.getAutowireCapableBeanFactory().createBean(beanClass);
65+
this.sharedPreparers.put(name, preparer);
66+
}
67+
catch (ClassNotFoundException ex) {
68+
throw new NoSuchPreparerException("Preparer class [" + name + "] not found", ex);
69+
}
70+
}
71+
}
72+
}
73+
return preparer;
74+
}
75+
76+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2002-2009 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 org.apache.tiles.TilesException;
20+
import org.apache.tiles.preparer.ViewPreparer;
21+
22+
import org.springframework.web.context.WebApplicationContext;
23+
24+
/**
25+
* Tiles {@link org.apache.tiles.preparer.PreparerFactory} implementation
26+
* that expects preparer bean names and obtains preparer beans from the
27+
* Spring ApplicationContext. The full bean creation process will be in
28+
* the control of the Spring application context in this case, allowing
29+
* for the use of scoped beans etc.
30+
*
31+
* <p><b>NOTE: Tiles 2 support is deprecated in favor of Tiles 3 and will be removed
32+
* as of Spring Framework 5.0.</b>.
33+
*
34+
* @author Juergen Hoeller
35+
* @since 2.5
36+
* @see SimpleSpringPreparerFactory
37+
* @deprecated as of Spring 4.2, in favor of Tiles 3
38+
*/
39+
@Deprecated
40+
public class SpringBeanPreparerFactory extends AbstractSpringPreparerFactory {
41+
42+
@Override
43+
protected ViewPreparer getPreparer(String name, WebApplicationContext context) throws TilesException {
44+
return context.getBean(name, ViewPreparer.class);
45+
}
46+
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright 2002-2009 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.Locale;
20+
import javax.servlet.http.HttpServletRequest;
21+
import javax.servlet.jsp.PageContext;
22+
23+
import org.apache.tiles.context.TilesRequestContext;
24+
import org.apache.tiles.jsp.context.JspTilesRequestContext;
25+
import org.apache.tiles.locale.impl.DefaultLocaleResolver;
26+
import org.apache.tiles.servlet.context.ServletTilesRequestContext;
27+
28+
import org.springframework.web.servlet.support.RequestContextUtils;
29+
30+
/**
31+
* Tiles LocaleResolver adapter that delegates to a Spring
32+
* {@link org.springframework.web.servlet.LocaleResolver},
33+
* exposing the DispatcherServlet-managed locale.
34+
*
35+
* <p>This adapter gets automatically registered by {@link TilesConfigurer}.
36+
* If you are using standard Tiles bootstrap, specify the name of this class
37+
* as value for the init-param "org.apache.tiles.locale.LocaleResolver".
38+
*
39+
* <p><b>NOTE: Tiles 2 support is deprecated in favor of Tiles 3 and will be removed
40+
* as of Spring Framework 5.0.</b>.
41+
*
42+
* @author Juergen Hoeller
43+
* @since 2.5
44+
* @see org.apache.tiles.definition.UrlDefinitionsFactory#LOCALE_RESOLVER_IMPL_PROPERTY
45+
* @deprecated as of Spring 4.2, in favor of Tiles 3
46+
*/
47+
@Deprecated
48+
public class SpringLocaleResolver extends DefaultLocaleResolver {
49+
50+
@Override
51+
public Locale resolveLocale(TilesRequestContext context) {
52+
if (context instanceof JspTilesRequestContext) {
53+
PageContext pc = ((JspTilesRequestContext) context).getPageContext();
54+
return RequestContextUtils.getLocale((HttpServletRequest) pc.getRequest());
55+
}
56+
else if (context instanceof ServletTilesRequestContext) {
57+
HttpServletRequest request = ((ServletTilesRequestContext) context).getRequest();
58+
if (request != null) {
59+
return RequestContextUtils.getLocale(request);
60+
}
61+
}
62+
return super.resolveLocale(context);
63+
}
64+
65+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright 2002-2014 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.io.IOException;
20+
import java.net.URL;
21+
import java.util.LinkedHashSet;
22+
import java.util.Set;
23+
import javax.servlet.ServletContext;
24+
25+
import org.apache.tiles.servlet.context.ServletTilesApplicationContext;
26+
27+
import org.springframework.core.io.Resource;
28+
import org.springframework.core.io.support.ResourcePatternResolver;
29+
import org.springframework.util.CollectionUtils;
30+
import org.springframework.util.ObjectUtils;
31+
import org.springframework.web.context.support.ServletContextResourcePatternResolver;
32+
33+
/**
34+
* Spring-specific subclass of the Tiles ServletTilesApplicationContext.
35+
*
36+
* <p><b>NOTE: Tiles 2 support is deprecated in favor of Tiles 3 and will be removed
37+
* as of Spring Framework 5.0.</b>.
38+
*
39+
* @author Juergen Hoeller
40+
* @since 4.0.1
41+
* @deprecated as of Spring 4.2, in favor of Tiles 3
42+
*/
43+
@Deprecated
44+
public class SpringWildcardServletTilesApplicationContext extends ServletTilesApplicationContext {
45+
46+
private final ResourcePatternResolver resolver;
47+
48+
49+
public SpringWildcardServletTilesApplicationContext(ServletContext servletContext) {
50+
super(servletContext);
51+
this.resolver = new ServletContextResourcePatternResolver(servletContext);
52+
}
53+
54+
55+
@Override
56+
public URL getResource(String path) throws IOException {
57+
Set<URL> urlSet = getResources(path);
58+
if (!CollectionUtils.isEmpty(urlSet)) {
59+
return urlSet.iterator().next();
60+
}
61+
return null;
62+
}
63+
64+
@Override
65+
public Set<URL> getResources(String path) throws IOException {
66+
Set<URL> urlSet = null;
67+
Resource[] resources = this.resolver.getResources(path);
68+
if (!ObjectUtils.isEmpty(resources)) {
69+
urlSet = new LinkedHashSet<URL>(resources.length);
70+
for (Resource resource : resources) {
71+
urlSet.add(resource.getURL());
72+
}
73+
}
74+
return urlSet;
75+
}
76+
77+
}

0 commit comments

Comments
 (0)