You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@struts.apache.org by dd...@apache.org on 2006/11/02 20:57:02 UTC

svn commit: r470495 - in /struts/sandbox/trunk/tiles: tiles-api/src/main/java/org/apache/tiles/ tiles-core/src/main/java/org/apache/tiles/context/enhanced/ tiles-core/src/main/java/org/apache/tiles/context/portlet/ tiles-core/src/main/java/org/apache/t...

Author: ddewolf
Date: Thu Nov  2 11:57:00 2006
New Revision: 470495

URL: http://svn.apache.org/viewvc?view=rev&rev=470495
Log:
Adding enhanced application context to provide classpath resource loading

Added:
    struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/context/enhanced/
    struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/context/enhanced/EnhancedTilesApplicationContext.java   (with props)
    struts/sandbox/trunk/tiles/tiles-core/src/test/java/org/apache/tiles/context/
    struts/sandbox/trunk/tiles/tiles-core/src/test/java/org/apache/tiles/context/enhanced/
    struts/sandbox/trunk/tiles/tiles-core/src/test/java/org/apache/tiles/context/enhanced/EnhancedTilesApplicationContextTest.java   (with props)
Modified:
    struts/sandbox/trunk/tiles/tiles-api/src/main/java/org/apache/tiles/TilesApplicationContext.java
    struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/context/portlet/PortletTilesApplicationContext.java
    struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/context/servlet/ServletTilesApplicationContext.java
    struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/impl/BasicTilesContainer.java
    struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/util/TilesUtilImpl.java

Modified: struts/sandbox/trunk/tiles/tiles-api/src/main/java/org/apache/tiles/TilesApplicationContext.java
URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/tiles/tiles-api/src/main/java/org/apache/tiles/TilesApplicationContext.java?view=diff&rev=470495&r1=470494&r2=470495
==============================================================================
--- struts/sandbox/trunk/tiles/tiles-api/src/main/java/org/apache/tiles/TilesApplicationContext.java (original)
+++ struts/sandbox/trunk/tiles/tiles-api/src/main/java/org/apache/tiles/TilesApplicationContext.java Thu Nov  2 11:57:00 2006
@@ -20,6 +20,8 @@
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.util.Map;
+import java.util.Set;
+import java.io.IOException;
 
 /**
  * Defines a set of methods which tiles use to communicate to
@@ -54,7 +56,7 @@
      * @return the first located resource which matches the given path.
      * @throws java.net.MalformedURLException if the path is malformed
      */
-    URL getResource(String path) throws MalformedURLException;
+    URL getResource(String path) throws IOException;
 
     /**
      * Return a URL for the application resource mapped to the specified path.
@@ -63,5 +65,5 @@
      * @return all resources which match the given path.
      * @throws java.net.MalformedURLException if the url is illegal
      */
-    URL[] getResources(String path) throws MalformedURLException;
+    Set<URL> getResources(String path) throws IOException;
 }

Added: struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/context/enhanced/EnhancedTilesApplicationContext.java
URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/context/enhanced/EnhancedTilesApplicationContext.java?view=auto&rev=470495
==============================================================================
--- struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/context/enhanced/EnhancedTilesApplicationContext.java (added)
+++ struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/context/enhanced/EnhancedTilesApplicationContext.java Thu Nov  2 11:57:00 2006
@@ -0,0 +1,104 @@
+/*
+ * 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.context.enhanced;
+
+import org.apache.tiles.TilesApplicationContext;
+import org.apache.commons.logging.LogFactory;
+import org.apache.commons.logging.Log;
+
+import java.util.*;
+import java.net.URL;
+import java.io.IOException;
+
+/**
+ * ApplicationContext decorator used to provide
+ * enhancements to the standard context.
+ *
+ * Specifically, it provides:
+ * <ul>
+ *   <li>Ability to load resources from the classpath</li>
+ *   <li>Ability to retrieve multiple resources of the same name</li>
+ * </ul>
+ *
+ * Future features will include:
+ * <ul>
+ *   <li>Ability to utilize wildcards in resource pathcs</li>
+ * </ul>
+ *
+ * @since Tiles 2.0
+ * @version $Rev$
+ * 
+ */
+public class EnhancedTilesApplicationContext implements TilesApplicationContext {
+
+    private static final Log LOG =
+        LogFactory.getLog(EnhancedTilesApplicationContext.class);
+
+    private TilesApplicationContext rootContext;
+
+    public EnhancedTilesApplicationContext(TilesApplicationContext rootContext) {
+        this.rootContext = rootContext;
+    }
+
+    public Map<String, Object> getApplicationScope() {
+        return rootContext.getApplicationScope();
+    }
+
+    public Map<String, String> getInitParams() {
+        return rootContext.getInitParams();
+    }
+
+    public URL getResource(String path) throws IOException {
+        URL rootUrl = rootContext.getResource(path);
+        if(rootUrl == null) {
+            Set<URL> resources = getResources(path);
+            if(resources.size() > 0) {
+                rootUrl = resources.toArray(new URL[resources.size()])[0];
+            }
+        }
+        return rootUrl;
+    }
+
+    public Set<URL> getResources(String path) throws IOException {
+        Set<URL> resources = new HashSet<URL>();
+        resources.addAll(rootContext.getResources(path));
+        resources.addAll(searchResources(getClass().getClassLoader(), path));
+
+        ClassLoader contextLoader = Thread.currentThread().getContextClassLoader();
+        if(contextLoader != null) {
+            resources.addAll(searchResources(contextLoader, path));
+        }
+
+        return resources;
+    }
+
+    protected Set<URL> searchResources(ClassLoader loader, String path) {
+        Set<URL> resources = new HashSet<URL>();
+        try {
+            Enumeration<URL> e = loader.getResources(path);
+            while(e.hasMoreElements()) {
+                resources.add(e.nextElement());
+            }
+        } catch (IOException e) {
+            LOG.warn("Unable to retrieved resources from classloader: "+loader, e);
+        }
+        return resources;
+    }
+}

Propchange: struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/context/enhanced/EnhancedTilesApplicationContext.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/context/enhanced/EnhancedTilesApplicationContext.java
------------------------------------------------------------------------------
    svn:keywords = Id Author Date Rev

Modified: struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/context/portlet/PortletTilesApplicationContext.java
URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/context/portlet/PortletTilesApplicationContext.java?view=diff&rev=470495&r1=470494&r2=470495
==============================================================================
--- struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/context/portlet/PortletTilesApplicationContext.java (original)
+++ struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/context/portlet/PortletTilesApplicationContext.java Thu Nov  2 11:57:00 2006
@@ -27,6 +27,8 @@
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.util.Map;
+import java.util.Set;
+import java.util.HashSet;
 
 /**
  * Portlet-based TilesApplicationContext implementation.
@@ -122,8 +124,10 @@
         return context.getResource(path);
     }
 
-    public URL[] getResources(String path) throws MalformedURLException {
-        return new URL[]{context.getResource(path)};
+    public Set<URL> getResources(String path) throws MalformedURLException {
+        HashSet<URL> set = new HashSet<URL>();
+        set.add(getResource(path));
+        return set;
     }
 
     public TilesRequestContext createRequestContext(Object request, Object response) {

Modified: struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/context/servlet/ServletTilesApplicationContext.java
URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/context/servlet/ServletTilesApplicationContext.java?view=diff&rev=470495&r1=470494&r2=470495
==============================================================================
--- struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/context/servlet/ServletTilesApplicationContext.java (original)
+++ struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/context/servlet/ServletTilesApplicationContext.java Thu Nov  2 11:57:00 2006
@@ -27,6 +27,8 @@
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.util.Map;
+import java.util.Set;
+import java.util.HashSet;
 
 /**
  * Servlet-bsed implementation of the TilesApplicationContext interface.
@@ -83,8 +85,10 @@
         return servletContext.getResource(path);
     }
 
-    public URL[] getResources(String path) throws MalformedURLException {
-        return new URL[]{servletContext.getResource(path)};
+    public Set<URL> getResources(String path) throws MalformedURLException {
+        HashSet<URL> urls = new HashSet<URL>();
+        urls.add(getResource(path));
+        return urls;
     }
 
     public ServletContext getServletContext() {

Modified: struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/impl/BasicTilesContainer.java
URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/impl/BasicTilesContainer.java?view=diff&rev=470495&r1=470494&r2=470495
==============================================================================
--- struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/impl/BasicTilesContainer.java (original)
+++ struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/impl/BasicTilesContainer.java Thu Nov  2 11:57:00 2006
@@ -36,6 +36,7 @@
 import java.util.List;
 import java.util.Map;
 import java.util.StringTokenizer;
+import java.io.IOException;
 
 /**
  * Basic implementation of the tiles container interface.
@@ -97,7 +98,7 @@
                     definitionsFactory.addSource(resourceUrl);
                 }
             }
-        } catch (MalformedURLException e) {
+        } catch (IOException e) {
             throw new DefinitionsFactoryException("Unable to parse definitions from "
                 + resourceString, e);
         }

Modified: struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/util/TilesUtilImpl.java
URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/util/TilesUtilImpl.java?view=diff&rev=470495&r1=470494&r2=470495
==============================================================================
--- struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/util/TilesUtilImpl.java (original)
+++ struts/sandbox/trunk/tiles/tiles-core/src/main/java/org/apache/tiles/util/TilesUtilImpl.java Thu Nov  2 11:57:00 2006
@@ -205,7 +205,7 @@
                 String filename = (String) filenames.get(i);
                 factory.addSource(applicationContext.getResource(filename));
             }
-        } catch (MalformedURLException e) {
+        } catch (IOException e) {
             throw new DefinitionsFactoryException("Problem with filename URL: ", e);
         }
 

Added: struts/sandbox/trunk/tiles/tiles-core/src/test/java/org/apache/tiles/context/enhanced/EnhancedTilesApplicationContextTest.java
URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/tiles/tiles-core/src/test/java/org/apache/tiles/context/enhanced/EnhancedTilesApplicationContextTest.java?view=auto&rev=470495
==============================================================================
--- struts/sandbox/trunk/tiles/tiles-core/src/test/java/org/apache/tiles/context/enhanced/EnhancedTilesApplicationContextTest.java (added)
+++ struts/sandbox/trunk/tiles/tiles-core/src/test/java/org/apache/tiles/context/enhanced/EnhancedTilesApplicationContextTest.java Thu Nov  2 11:57:00 2006
@@ -0,0 +1,80 @@
+/*
+ * 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.context.enhanced;
+
+import junit.framework.TestCase;
+import org.apache.tiles.TilesApplicationContext;
+import org.easymock.EasyMock;
+
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.Enumeration;
+import java.util.Vector;
+import java.util.HashSet;
+
+
+public class EnhancedTilesApplicationContextTest extends TestCase {
+
+    private TilesApplicationContext root;
+    private EnhancedTilesApplicationContext context;
+
+    public void setUp() {
+        root = EasyMock.createMock(TilesApplicationContext.class);
+        context = new EnhancedTilesApplicationContext(root);
+    }
+
+    public void testGetResources() throws IOException {
+        ClassLoader original = Thread.currentThread().getContextClassLoader();
+        try {
+            String url = "test.properties";
+            HashSet<URL> set = new HashSet<URL>();
+            URL u = new URL("file://tiles/test.properties");
+            set.add(u);
+            EasyMock.expect(root.getResources(url)).andReturn(set);
+            EasyMock.replay(root);
+            Thread.currentThread().setContextClassLoader(new MockClassLoader());
+
+            assertEquals(4, context.getResources("test.properties").size());
+            EasyMock.verify(root);
+        }
+        finally {
+            Thread.currentThread().setContextClassLoader(original);
+        }
+    }
+
+    public class MockClassLoader extends ClassLoader {
+
+        private Vector<URL> resources;
+
+        public MockClassLoader() throws MalformedURLException {
+            resources = new Vector<URL>();
+            resources.add(new URL("file://tiles/test/test.properties"));
+            resources.add(new URL("file://tiles/two/test.properties"));
+            resources.add(new URL("file://tiles/three/test.properties"));
+        }
+
+        public Enumeration<URL> findResources(String path) {
+            return resources.elements();
+        }
+    }
+
+
+}

Propchange: struts/sandbox/trunk/tiles/tiles-core/src/test/java/org/apache/tiles/context/enhanced/EnhancedTilesApplicationContextTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: struts/sandbox/trunk/tiles/tiles-core/src/test/java/org/apache/tiles/context/enhanced/EnhancedTilesApplicationContextTest.java
------------------------------------------------------------------------------
    svn:keywords = Id Author Date Rev