Upgraded Tiles2 TilesContainer to Tiles 2.2.2 (following the Spring 4.0 baseline)

In sync with our Tiles3 TilesContainer implementation now, as far as possible.

Issue: SPR-11285
This commit is contained in:
Juergen Hoeller
2014-01-06 00:19:36 +01:00
parent 24030a3f61
commit 5d3484c74a
9 changed files with 222 additions and 391 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2013 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.
@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.servlet.view.tiles3;
import java.io.IOException;
@@ -21,14 +22,15 @@ import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Locale;
import javax.servlet.ServletContext;
import org.apache.tiles.request.ApplicationResource;
import org.apache.tiles.request.locale.URLApplicationResource;
import org.apache.tiles.request.servlet.ServletApplicationContext;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.util.ObjectUtils;
import org.springframework.web.context.support.ServletContextResourcePatternResolver;
/**
@@ -39,21 +41,15 @@ import org.springframework.web.context.support.ServletContextResourcePatternReso
*/
public class SpringWildcardServletTilesApplicationContext extends ServletApplicationContext {
/**
* The pattern resolver.
*/
protected ResourcePatternResolver resolver;
private final ResourcePatternResolver resolver;
/**
* Constructor.
*
* @param servletContext The servlet context.
*/
public SpringWildcardServletTilesApplicationContext(ServletContext servletContext) {
super(servletContext);
resolver = new ServletContextResourcePatternResolver(servletContext);
this.resolver = new ServletContextResourcePatternResolver(servletContext);
}
@Override
public ApplicationResource getResource(String localePath) {
ApplicationResource retValue = null;
@@ -78,20 +74,22 @@ public class SpringWildcardServletTilesApplicationContext extends ServletApplica
public Collection<ApplicationResource> getResources(String path) {
Resource[] resources;
try {
resources = resolver.getResources(path);
} catch (IOException e) {
resources = this.resolver.getResources(path);
}
catch (IOException ex) {
return Collections.<ApplicationResource> emptyList();
}
Collection<ApplicationResource> resourceList = new ArrayList<ApplicationResource>();
if (resources != null && resources.length > 0) {
for (int i = 0; i < resources.length; i++) {
if (!ObjectUtils.isEmpty(resources)) {
for (Resource resource : resources) {
URL url;
try {
url = resources[i].getURL();
url = resource.getURL();
resourceList.add(new URLApplicationResource(url.toExternalForm(), url));
} catch (IOException e) {
}
catch (IOException ex) {
// shouldn't happen with the kind of resources we're using
throw new IllegalArgumentException("no URL for " + resources[i].toString(), e);
throw new IllegalArgumentException("No URL for " + resource.toString(), ex);
}
}
}

View File

@@ -18,7 +18,6 @@ package org.springframework.web.servlet.view.tiles3;
import java.util.LinkedList;
import java.util.List;
import javax.el.ArrayELResolver;
import javax.el.BeanELResolver;
import javax.el.CompositeELResolver;
@@ -49,14 +48,15 @@ import org.apache.tiles.extras.complete.CompleteAutoloadTilesContainerFactory;
import org.apache.tiles.extras.complete.CompleteAutoloadTilesInitializer;
import org.apache.tiles.factory.AbstractTilesContainerFactory;
import org.apache.tiles.factory.BasicTilesContainerFactory;
import org.apache.tiles.impl.BasicTilesContainer;
import org.apache.tiles.impl.mgmt.CachingTilesContainer;
import org.apache.tiles.locale.LocaleResolver;
import org.apache.tiles.preparer.factory.PreparerFactory;
import org.apache.tiles.request.ApplicationContext;
import org.apache.tiles.request.ApplicationContextAware;
import org.apache.tiles.request.ApplicationResource;
import org.apache.tiles.startup.DefaultTilesInitializer;
import org.apache.tiles.startup.TilesInitializer;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.PropertyAccessorFactory;
@@ -103,13 +103,15 @@ import org.springframework.web.context.ServletContextAware;
* @author mick semb wever
* @author Rossen Stoyanchev
* @since 3.2
* @see TilesView
* @see TilesViewResolver
*/
public class TilesConfigurer implements ServletContextAware, InitializingBean, DisposableBean {
private static final boolean tilesElPresent = // requires JSP 2.1 as well as Tiles EL module
ClassUtils.isPresent("javax.servlet.jsp.JspApplicationContext", TilesConfigurer.class.getClassLoader()) &&
private static final boolean tilesElPresent =
ClassUtils.isPresent("org.apache.tiles.el.ELAttributeEvaluator", TilesConfigurer.class.getClassLoader());
protected final Log logger = LogFactory.getLog(getClass());
private TilesInitializer tilesInitializer;
@@ -128,8 +130,6 @@ public class TilesConfigurer implements ServletContextAware, InitializingBean, D
private ServletContext servletContext;
public TilesConfigurer() {
}
/**
* Configure Tiles using a custom TilesInitializer, typically specified as an inner bean.
@@ -158,10 +158,11 @@ public class TilesConfigurer implements ServletContextAware, InitializingBean, D
try {
this.tilesInitializer = new SpringCompleteAutoloadTilesInitializer();
}
catch (Exception ex) {
throw new IllegalStateException("tiles-extras 3.x not available", ex);
catch (Throwable ex) {
throw new IllegalStateException("Tiles-Extras 3.0 not available", ex);
}
} else {
}
else {
this.tilesInitializer = null;
}
}
@@ -170,7 +171,7 @@ public class TilesConfigurer implements ServletContextAware, InitializingBean, D
* Set the Tiles definitions, i.e. the list of files containing the definitions.
* Default is "/WEB-INF/tiles.xml".
*/
public void setDefinitions(String[] definitions) {
public void setDefinitions(String... definitions) {
this.definitions = definitions;
}
@@ -318,8 +319,8 @@ public class TilesConfigurer implements ServletContextAware, InitializingBean, D
if (definitionsFactoryClass != null) {
DefinitionsFactory factory = BeanUtils.instantiate(definitionsFactoryClass);
if (factory instanceof org.apache.tiles.request.ApplicationContextAware) {
((org.apache.tiles.request.ApplicationContextAware) factory).setApplicationContext(applicationContext);
if (factory instanceof ApplicationContextAware) {
((ApplicationContextAware) factory).setApplicationContext(applicationContext);
}
BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(factory);
if (bw.isWritableProperty("localeResolver")) {
@@ -352,21 +353,19 @@ public class TilesConfigurer implements ServletContextAware, InitializingBean, D
@Override
protected AttributeEvaluatorFactory createAttributeEvaluatorFactory(ApplicationContext context,
LocaleResolver resolver) {
return new BasicAttributeEvaluatorFactory(createELEvaluator(context));
}
private AttributeEvaluator createELEvaluator(ApplicationContext context) {
if (tilesElPresent) {
AttributeEvaluator evaluator = new TilesElActivator().createEvaluator();
if (evaluator != null) {
return evaluator;
}
AttributeEvaluator evaluator;
if (tilesElPresent && JspFactory.getDefaultFactory() != null) {
evaluator = new TilesElActivator().createEvaluator();
}
return new DirectAttributeEvaluator();
else {
evaluator = new DirectAttributeEvaluator();
}
return new BasicAttributeEvaluatorFactory(evaluator);
}
}
private class SpringCompleteAutoloadTilesInitializer extends CompleteAutoloadTilesInitializer {
private static class SpringCompleteAutoloadTilesInitializer extends CompleteAutoloadTilesInitializer {
@Override
protected AbstractTilesContainerFactory createContainerFactory(ApplicationContext context) {
@@ -374,42 +373,29 @@ public class TilesConfigurer implements ServletContextAware, InitializingBean, D
}
}
private class SpringCompleteAutoloadTilesContainerFactory extends CompleteAutoloadTilesContainerFactory {
private static class SpringCompleteAutoloadTilesContainerFactory extends CompleteAutoloadTilesContainerFactory {
@Override
public TilesContainer createContainer(ApplicationContext applicationContext) {
CachingTilesContainer cachingContainer = (CachingTilesContainer) super.createContainer(applicationContext);
BasicTilesContainer tilesContainer = (BasicTilesContainer) cachingContainer.getWrappedContainer();
BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(tilesContainer.getDefinitionsFactory());
if (bw.isWritableProperty("localeResolver")) {
bw.setPropertyValue("localeResolver", new SpringLocaleResolver());
}
return tilesContainer;
protected LocaleResolver createLocaleResolver(ApplicationContext applicationContext) {
return new SpringLocaleResolver();
}
}
private class TilesElActivator {
public AttributeEvaluator createEvaluator() {
try {
// jsp-api-2.1 doesn't default instantiate a factory for us
JspFactory factory = JspFactory.getDefaultFactory();
if ((factory != null) && (factory.getJspApplicationContext(servletContext).getExpressionFactory() != null)) {
logger.info("Found JSP 2.1 ExpressionFactory");
ELAttributeEvaluator evaluator = new ELAttributeEvaluator();
evaluator.setExpressionFactory(factory.getJspApplicationContext(servletContext).getExpressionFactory());
evaluator.setResolver(new CompositeELResolverImpl());
return evaluator;
}
}
catch (Throwable ex) {
logger.warn("Could not obtain JSP 2.1 ExpressionFactory", ex);
}
return null;
ELAttributeEvaluator evaluator = new ELAttributeEvaluator();
evaluator.setExpressionFactory(
JspFactory.getDefaultFactory().getJspApplicationContext(servletContext).getExpressionFactory());
evaluator.setResolver(new CompositeELResolverImpl());
return evaluator;
}
}
private static class CompositeELResolverImpl extends CompositeELResolver {
public CompositeELResolverImpl() {
@@ -423,4 +409,5 @@ public class TilesConfigurer implements ServletContextAware, InitializingBean, D
add(new BeanELResolver(false));
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2013 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,8 +16,6 @@
package org.springframework.web.servlet.view.tiles3;
import static org.junit.Assert.assertNotNull;
import org.apache.tiles.access.TilesAccess;
import org.apache.tiles.impl.BasicTilesContainer;
import org.apache.tiles.request.ApplicationContext;
@@ -25,11 +23,14 @@ import org.apache.tiles.request.Request;
import org.apache.tiles.request.servlet.ServletRequest;
import org.apache.tiles.request.servlet.ServletUtil;
import org.junit.Test;
import org.springframework.context.annotation.Configuration;
import org.springframework.mock.web.test.MockHttpServletRequest;
import org.springframework.mock.web.test.MockHttpServletResponse;
import org.springframework.mock.web.test.MockServletContext;
import static org.junit.Assert.*;
/**
* Test fixture for {@link TilesConfigurer}.
*
@@ -42,7 +43,7 @@ public class TilesConfigurerTests {
MockServletContext servletContext = new MockServletContext();
TilesConfigurer tc = new TilesConfigurer();
tc.setDefinitions(new String[] { "/org/springframework/web/servlet/view/tiles3/tiles-definitions.xml" });
tc.setDefinitions("/org/springframework/web/servlet/view/tiles3/tiles-definitions.xml");
tc.setCheckRefresh(true);
tc.setServletContext(servletContext);
tc.afterPropertiesSet();
@@ -57,6 +58,7 @@ public class TilesConfigurerTests {
tc.destroy();
}
@Configuration
public static class AppConfig {
}