Author: apetrelli
Date: Sat Jul 12 06:01:03 2008
New Revision: 676174
URL: http://svn.apache.org/viewvc?rev=676174&view=rev
Log:
TILES-84
Using Spring to resolve wildcard-specified resources and paths.
Added:
tiles/framework/trunk/tiles-servlet/src/main/java/org/apache/tiles/servlet/context/wildcard/
tiles/framework/trunk/tiles-servlet/src/main/java/org/apache/tiles/servlet/context/wildcard/WildcardServletTilesApplicationContext.java
(with props)
tiles/framework/trunk/tiles-servlet/src/main/java/org/apache/tiles/servlet/context/wildcard/WildcardServletTilesContextFactory.java
(with props)
tiles/framework/trunk/tiles-servlet/src/test/java/org/apache/tiles/servlet/context/wildcard/
tiles/framework/trunk/tiles-servlet/src/test/java/org/apache/tiles/servlet/context/wildcard/WildcardServletTilesApplicationContextTest.java
(with props)
Modified:
tiles/framework/trunk/tiles-servlet/pom.xml
tiles/framework/trunk/tiles-servlet/src/main/java/org/apache/tiles/servlet/context/ServletTilesApplicationContext.java
Modified: tiles/framework/trunk/tiles-servlet/pom.xml
URL:
http://svn.apache.org/viewvc/tiles/framework/trunk/tiles-servlet/pom.xml?rev=676174&r1=676173&r2=676174&view=diff
==============================================================================
--- tiles/framework/trunk/tiles-servlet/pom.xml (original)
+++ tiles/framework/trunk/tiles-servlet/pom.xml Sat Jul 12 06:01:03 2008
@@ -133,6 +133,13 @@
</dependency>
<dependency>
+ <groupId>org.springframework</groupId>
+ <artifactId>spring-web</artifactId>
+ <version>2.5.5</version>
+ <optional>true</optional>
+ </dependency>
+
+ <dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
Modified:
tiles/framework/trunk/tiles-servlet/src/main/java/org/apache/tiles/servlet/context/ServletTilesApplicationContext.java
URL:
http://svn.apache.org/viewvc/tiles/framework/trunk/tiles-servlet/src/main/java/org/apache/tiles/servlet/context/ServletTilesApplicationContext.java?rev=676174&r1=676173&r2=676174&view=diff
==============================================================================
---
tiles/framework/trunk/tiles-servlet/src/main/java/org/apache/tiles/servlet/context/ServletTilesApplicationContext.java
(original)
+++
tiles/framework/trunk/tiles-servlet/src/main/java/org/apache/tiles/servlet/context/ServletTilesApplicationContext.java
Sat Jul 12 06:01:03 2008
@@ -26,7 +26,8 @@
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
-import java.net.MalformedURLException;
+
+import java.io.IOException;
import java.net.URL;
import java.util.Map;
import java.util.Set;
@@ -95,12 +96,12 @@
}
/** [EMAIL PROTECTED] */
- public URL getResource(String path) throws MalformedURLException {
+ public URL getResource(String path) throws IOException {
return servletContext.getResource(path);
}
/** [EMAIL PROTECTED] */
- public Set<URL> getResources(String path) throws MalformedURLException {
+ public Set<URL> getResources(String path) throws IOException {
HashSet<URL> urls = new HashSet<URL>();
urls.add(getResource(path));
return urls;
Added:
tiles/framework/trunk/tiles-servlet/src/main/java/org/apache/tiles/servlet/context/wildcard/WildcardServletTilesApplicationContext.java
URL:
http://svn.apache.org/viewvc/tiles/framework/trunk/tiles-servlet/src/main/java/org/apache/tiles/servlet/context/wildcard/WildcardServletTilesApplicationContext.java?rev=676174&view=auto
==============================================================================
---
tiles/framework/trunk/tiles-servlet/src/main/java/org/apache/tiles/servlet/context/wildcard/WildcardServletTilesApplicationContext.java
(added)
+++
tiles/framework/trunk/tiles-servlet/src/main/java/org/apache/tiles/servlet/context/wildcard/WildcardServletTilesApplicationContext.java
Sat Jul 12 06:01:03 2008
@@ -0,0 +1,95 @@
+/*
+ * $Id$
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.apache.tiles.servlet.context.wildcard;
+
+import java.io.IOException;
+import java.net.URL;
+import java.util.HashSet;
+import java.util.Set;
+
+import javax.servlet.ServletContext;
+
+import org.apache.tiles.servlet.context.ServletTilesApplicationContext;
+import org.springframework.core.io.Resource;
+import org.springframework.core.io.support.ResourcePatternResolver;
+import
org.springframework.web.context.support.ServletContextResourcePatternResolver;
+
+/**
+ * Servlet-based implementation of the TilesApplicationContext interface that
+ * can resolve resources even using wildcards.
+ *
+ * @version $Rev$ $Date$
+ * @since 2.1.0
+ */
+public class WildcardServletTilesApplicationContext extends
+ ServletTilesApplicationContext {
+
+ /**
+ * The pattern resolver.
+ *
+ * @since 2.1.0
+ */
+ protected ResourcePatternResolver resolver;
+
+ /**
+ * Constructor.
+ *
+ * @param servletContext The servlet context.
+ * @since 2.1.0
+ */
+ public WildcardServletTilesApplicationContext(ServletContext
servletContext) {
+ super(servletContext);
+ }
+
+ /** [EMAIL PROTECTED] */
+ @Override
+ public void initialize(ServletContext context) {
+ super.initialize(context);
+
+ resolver = new ServletContextResourcePatternResolver(context);
+ }
+
+ /** [EMAIL PROTECTED] */
+ @Override
+ public URL getResource(String path) throws IOException {
+ URL retValue = null;
+ Set<URL> urlSet = getResources(path);
+ if (urlSet != null && !urlSet.isEmpty()) {
+ retValue = urlSet.iterator().next();
+ }
+ return retValue;
+ }
+
+ /** [EMAIL PROTECTED] */
+ @Override
+ public Set<URL> getResources(String path) throws IOException {
+ Set<URL> urlSet = null;
+ Resource[] resources = resolver.getResources(path);
+ if (resources != null && resources.length > 0) {
+ urlSet = new HashSet<URL>();
+ for (int i = 0; i < resources.length; i++) {
+ urlSet.add(resources[i].getURL());
+ }
+ }
+ return urlSet;
+ }
+}
Propchange:
tiles/framework/trunk/tiles-servlet/src/main/java/org/apache/tiles/servlet/context/wildcard/WildcardServletTilesApplicationContext.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
tiles/framework/trunk/tiles-servlet/src/main/java/org/apache/tiles/servlet/context/wildcard/WildcardServletTilesApplicationContext.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Added:
tiles/framework/trunk/tiles-servlet/src/main/java/org/apache/tiles/servlet/context/wildcard/WildcardServletTilesContextFactory.java
URL:
http://svn.apache.org/viewvc/tiles/framework/trunk/tiles-servlet/src/main/java/org/apache/tiles/servlet/context/wildcard/WildcardServletTilesContextFactory.java?rev=676174&view=auto
==============================================================================
---
tiles/framework/trunk/tiles-servlet/src/main/java/org/apache/tiles/servlet/context/wildcard/WildcardServletTilesContextFactory.java
(added)
+++
tiles/framework/trunk/tiles-servlet/src/main/java/org/apache/tiles/servlet/context/wildcard/WildcardServletTilesContextFactory.java
Sat Jul 12 06:01:03 2008
@@ -0,0 +1,48 @@
+/*
+ * $Id$
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.apache.tiles.servlet.context.wildcard;
+
+import javax.servlet.ServletContext;
+
+import org.apache.tiles.TilesApplicationContext;
+import org.apache.tiles.servlet.context.ServletTilesContextFactory;
+
+/**
+ * Acts like [EMAIL PROTECTED] ServletTilesContextFactory}, except in the
+ * [EMAIL PROTECTED] #createApplicationContext(Object)} method that creates an
instance of
+ * [EMAIL PROTECTED] WildcardServletTilesApplicationContext}.
+ *
+ * @version $Rev$ $Date$
+ */
+public class WildcardServletTilesContextFactory extends
+ ServletTilesContextFactory {
+
+ /** [EMAIL PROTECTED] */
+ @Override
+ public TilesApplicationContext createApplicationContext(Object context) {
+ if (context instanceof ServletContext) {
+ ServletContext servletContext = (ServletContext) context;
+ return new WildcardServletTilesApplicationContext(servletContext);
+ }
+ return null;
+ }
+}
Propchange:
tiles/framework/trunk/tiles-servlet/src/main/java/org/apache/tiles/servlet/context/wildcard/WildcardServletTilesContextFactory.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
tiles/framework/trunk/tiles-servlet/src/main/java/org/apache/tiles/servlet/context/wildcard/WildcardServletTilesContextFactory.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Added:
tiles/framework/trunk/tiles-servlet/src/test/java/org/apache/tiles/servlet/context/wildcard/WildcardServletTilesApplicationContextTest.java
URL:
http://svn.apache.org/viewvc/tiles/framework/trunk/tiles-servlet/src/test/java/org/apache/tiles/servlet/context/wildcard/WildcardServletTilesApplicationContextTest.java?rev=676174&view=auto
==============================================================================
---
tiles/framework/trunk/tiles-servlet/src/test/java/org/apache/tiles/servlet/context/wildcard/WildcardServletTilesApplicationContextTest.java
(added)
+++
tiles/framework/trunk/tiles-servlet/src/test/java/org/apache/tiles/servlet/context/wildcard/WildcardServletTilesApplicationContextTest.java
Sat Jul 12 06:01:03 2008
@@ -0,0 +1,153 @@
+/*
+ * $Id$
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.apache.tiles.servlet.context.wildcard;
+
+import junit.framework.TestCase;
+import org.easymock.EasyMock;
+
+import java.io.File;
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.Enumeration;
+import java.util.Set;
+import java.util.Vector;
+import java.util.HashSet;
+
+import javax.servlet.ServletContext;
+
+
+/**
+ * Tests [EMAIL PROTECTED] WildcardServletTilesApplicationContext}.
+ *
+ * @version $Rev$ $Date$
+ */
+public class WildcardServletTilesApplicationContextTest extends TestCase {
+
+ /**
+ * Number of properties container inside the test.properties file.
+ */
+ private static final int TEST_PROPERTIES_SIZE = 3;
+
+ /**
+ * The root Tiles application context.
+ */
+ private ServletContext servletContext;
+
+ /**
+ * The enhanced Tiles application context.
+ */
+ private WildcardServletTilesApplicationContext context;
+
+ /**
+ * The original class loader.
+ */
+ private ClassLoader original;
+
+ /** [EMAIL PROTECTED] */
+ @Override
+ public void setUp() {
+ servletContext = EasyMock.createMock(ServletContext.class);
+ original = Thread.currentThread().getContextClassLoader();
+ try {
+ Thread.currentThread().setContextClassLoader(new
MockClassLoader());
+ } catch (MalformedURLException e) {
+ throw new RuntimeException("Error when using the mock
classloader");
+ }
+ context = new WildcardServletTilesApplicationContext(servletContext);
+ }
+
+ /** [EMAIL PROTECTED] */
+ @Override
+ protected void tearDown() throws Exception {
+ Thread.currentThread().setContextClassLoader(original);
+ }
+
+ /**
+ * Tests resource getting.
+ *
+ * @throws IOException If something goes wrong.
+ */
+ public void testGetResources() throws IOException {
+ String url = "test.properties";
+ HashSet<URL> set = new HashSet<URL>();
+ URL u = new URL("file://tiles/test.properties");
+ set.add(u);
+ EasyMock.expect(servletContext.getResource("/" + url)).andReturn(u)
+ .anyTimes();
+ File dir = new File(".");
+ EasyMock.expect(servletContext.getResource("/WEB-INF/")).andReturn(
+ dir.toURI().toURL());
+ URL pomUrl = new URL("file://tiles/pom.xml");
+ EasyMock.expect(servletContext.getResource("/WEB-INF/pom.xml"))
+ .andReturn(pomUrl);
+ Set<String> elementSet = new HashSet<String>();
+ elementSet.add("/WEB-INF/pom.xml");
+
EasyMock.expect(servletContext.getResourcePaths("/WEB-INF/")).andReturn(elementSet);
+ EasyMock.replay(servletContext);
+
+ assertEquals(u, context.getResource("/" + url));
+ assertEquals(pomUrl, context.getResource("/WEB-INF/*.xml"));
+ assertEquals(TEST_PROPERTIES_SIZE, context.getResources(
+ "classpath*:/test.properties").size());
+
+ assertEquals(TEST_PROPERTIES_SIZE, context.getResources(
+
"classpath*:/org/apache/tiles/servlet/context/*Test.class").size());
+ EasyMock.verify(servletContext);
+ }
+
+ /**
+ * An mock class loader.
+ */
+ public class MockClassLoader extends ClassLoader {
+
+ /**
+ * A vector of resources.
+ */
+ private Vector<URL> testPropertiesResources;
+
+ /**
+ * Constructor.
+ *
+ * @throws MalformedURLException If the URL is not valid (that should
+ * not happen).
+ */
+ public MockClassLoader() throws MalformedURLException {
+ testPropertiesResources = new Vector<URL>();
+ testPropertiesResources.add(new
URL("file://tiles/test/test.properties"));
+ testPropertiesResources.add(new
URL("file://tiles/two/test.properties"));
+ testPropertiesResources.add(new
URL("file://tiles/three/test.properties"));
+ }
+
+ /** [EMAIL PROTECTED] */
+ @Override
+ public Enumeration<URL> findResources(String path) throws IOException {
+ Enumeration<URL> retValue = null;
+ if ("test.properties".equals(path)) {
+ retValue = testPropertiesResources.elements();
+ } else {
+ retValue = super.findResources(path);
+ }
+
+ return retValue;
+ }
+ }
+}
Propchange:
tiles/framework/trunk/tiles-servlet/src/test/java/org/apache/tiles/servlet/context/wildcard/WildcardServletTilesApplicationContextTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
tiles/framework/trunk/tiles-servlet/src/test/java/org/apache/tiles/servlet/context/wildcard/WildcardServletTilesApplicationContextTest.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL