You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tapestry.apache.org by hl...@apache.org on 2008/03/19 23:16:47 UTC

svn commit: r639039 - in /tapestry/tapestry5/trunk: tapestry-core/src/main/java/org/apache/tapestry/internal/renderers/ tapestry-core/src/main/java/org/apache/tapestry/internal/services/ tapestry-core/src/main/java/org/apache/tapestry/internal/util/ ta...

Author: hlship
Date: Wed Mar 19 15:16:34 2008
New Revision: 639039

URL: http://svn.apache.org/viewvc?rev=639039&view=rev
Log:
TAPESTRY-1924: Allow for templates coming from stream

Modified:
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/renderers/LocationRenderer.java
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/services/AssetDispatcher.java
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/services/ComponentMessagesSourceImpl.java
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/services/ComponentTemplateSourceImpl.java
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/services/MessagesSourceImpl.java
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/services/ServicesMessages.java
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/services/TemplateParserImpl.java
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/util/URLChangeTracker.java
    tapestry/tapestry5/trunk/tapestry-core/src/site/apt/upgrade.apt
    tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/internal/util/URLChangeTrackerTest.java
    tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry/ioc/Resource.java
    tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry/ioc/internal/util/AbstractResource.java
    tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry/ioc/internal/util/ClasspathResource.java

Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/renderers/LocationRenderer.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/renderers/LocationRenderer.java?rev=639039&r1=639038&r2=639039&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/renderers/LocationRenderer.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/renderers/LocationRenderer.java Wed Mar 19 15:16:34 2008
@@ -25,7 +25,6 @@
 import org.apache.tapestry.services.ObjectRenderer;
 
 import java.io.*;
-import java.net.URL;
 import java.util.Set;
 
 /**
@@ -56,9 +55,8 @@
 
         if (line <= 0) return;
 
-        URL url = r.toURL();
+        if (!r.exists()) return;
 
-        if (url == null) return;
 
         int start = line - RANGE;
         int end = line + RANGE;
@@ -69,7 +67,7 @@
 
         try
         {
-            InputStream is = new BufferedInputStream(url.openStream());
+            InputStream is = r.openStream();
             InputStreamReader isr = new InputStreamReader(is);
             reader = new LineNumberReader(new BufferedReader(isr));
 

Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/services/AssetDispatcher.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/services/AssetDispatcher.java?rev=639039&r1=639038&r2=639039&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/services/AssetDispatcher.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/services/AssetDispatcher.java Wed Mar 19 15:16:34 2008
@@ -24,7 +24,6 @@
 
 import javax.servlet.http.HttpServletResponse;
 import java.io.IOException;
-import java.net.URL;
 
 /**
  * Recognizes requests where the path begins with "/asset/" and delivers the content therein as a bytestream. Also
@@ -69,9 +68,7 @@
 
         if (resource == null) return true;
 
-        URL url = resource.toURL();
-
-        if (url == null)
+        if (!resource.exists())
         {
             response.sendError(HttpServletResponse.SC_NOT_FOUND, ServicesMessages
                     .assetDoesNotExist(resource));

Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/services/ComponentMessagesSourceImpl.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/services/ComponentMessagesSourceImpl.java?rev=639039&r1=639038&r2=639039&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/services/ComponentMessagesSourceImpl.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/services/ComponentMessagesSourceImpl.java Wed Mar 19 15:16:34 2008
@@ -88,7 +88,7 @@
 
         // If the application catalog exists, set it up as the root, otherwise use null.
 
-        MessagesBundle appCatalogBundle = appCatalogResource.toURL() == null ? null
+        MessagesBundle appCatalogBundle = !appCatalogResource.exists() ? null
                                           : new MessagesBundle()
         {
             public Resource getBaseResource()

Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/services/ComponentTemplateSourceImpl.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/services/ComponentTemplateSourceImpl.java?rev=639039&r1=639038&r2=639039&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/services/ComponentTemplateSourceImpl.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/services/ComponentTemplateSourceImpl.java Wed Mar 19 15:16:34 2008
@@ -25,7 +25,6 @@
 import static org.apache.tapestry.ioc.internal.util.CollectionFactory.newConcurrentMap;
 import org.apache.tapestry.model.ComponentModel;
 
-import java.net.URL;
 import java.util.List;
 import java.util.Locale;
 import java.util.Map;
@@ -131,11 +130,9 @@
         // In a race condition, we may parse the same template more than once. This will likely add
         // the resource to the tracker multiple times. Not likely this will cause a big issue.
 
-        URL resourceURL = r.toURL();
+        if (!r.exists()) return _missingTemplate;
 
-        if (resourceURL == null) return _missingTemplate;
-
-        _tracker.add(resourceURL);
+        _tracker.add(r.toURL());
 
         return _parser.parseTemplate(r);
     }

Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/services/MessagesSourceImpl.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/services/MessagesSourceImpl.java?rev=639039&r1=639038&r2=639039&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/services/MessagesSourceImpl.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/services/MessagesSourceImpl.java Wed Mar 19 15:16:34 2008
@@ -23,9 +23,7 @@
 import org.apache.tapestry.ioc.internal.util.InternalUtils;
 import org.apache.tapestry.ioc.internal.util.LocalizedNameGenerator;
 
-import java.io.BufferedInputStream;
 import java.io.InputStream;
-import java.net.URL;
 import java.util.*;
 
 /**
@@ -189,11 +187,9 @@
      */
     private Map<String, String> readProperties(Resource resource)
     {
-        URL url = resource.toURL();
+        if (!resource.exists()) return _emptyMap;
 
-        if (url == null) return _emptyMap;
-
-        _tracker.add(url);
+        _tracker.add(resource.toURL());
 
         Map<String, String> result = newCaseInsensitiveMap();
 
@@ -202,7 +198,7 @@
 
         try
         {
-            is = new BufferedInputStream(url.openStream());
+            is = resource.openStream();
 
             p.load(is);
 
@@ -212,7 +208,7 @@
         }
         catch (Exception ex)
         {
-            throw new RuntimeException(ServicesMessages.failureReadingMessages(url, ex), ex);
+            throw new RuntimeException(ServicesMessages.failureReadingMessages(resource, ex), ex);
         }
         finally
         {

Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/services/ServicesMessages.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/services/ServicesMessages.java?rev=639039&r1=639038&r2=639039&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/services/ServicesMessages.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/services/ServicesMessages.java Wed Mar 19 15:16:34 2008
@@ -26,7 +26,6 @@
 import org.apache.tapestry.runtime.RenderCommand;
 import org.apache.tapestry.services.TransformMethodSignature;
 
-import java.net.URL;
 import java.util.Collection;
 import java.util.List;
 import java.util.Locale;
@@ -208,7 +207,7 @@
                 .getCompleteId());
     }
 
-    static String failureReadingMessages(URL url, Throwable cause)
+    static String failureReadingMessages(Resource url, Throwable cause)
     {
         return MESSAGES.format("failure-reading-messages", url, cause);
     }

Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/services/TemplateParserImpl.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/services/TemplateParserImpl.java?rev=639039&r1=639038&r2=639039&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/services/TemplateParserImpl.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/services/TemplateParserImpl.java Wed Mar 19 15:16:34 2008
@@ -207,15 +207,14 @@
             }
         }
 
-        URL resourceURL = templateResource.toURL();
-
-        if (resourceURL == null) throw new RuntimeException(ServicesMessages.missingTemplateResource(templateResource));
+        if (!templateResource.exists())
+            throw new RuntimeException(ServicesMessages.missingTemplateResource(templateResource));
 
         _templateResource = templateResource;
 
         try
         {
-            InputSource source = new InputSource(resourceURL.openStream());
+            InputSource source = new InputSource(templateResource.openStream());
 
             _reader.parse(source);
 

Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/util/URLChangeTracker.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/util/URLChangeTracker.java?rev=639039&r1=639038&r2=639039&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/util/URLChangeTracker.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/util/URLChangeTracker.java Wed Mar 19 15:16:34 2008
@@ -59,11 +59,14 @@
      * Stores a new URL into the tracker, or returns the previous time stamp for a previously added URL. Filters out all
      * non-file URLs.
      *
-     * @param url of the resource to add
-     * @return the current timestamp for the URL (possibly rounded off for granularity reasons)
+     * @param url of the resource to add, or null if not known
+     * @return the current timestamp for the URL (possibly rounded off for granularity reasons), or 0 if the URL is
+     *         null
      */
     public long add(URL url)
     {
+        if (url == null) return 0;
+
         if (!url.getProtocol().equals("file")) return timestampForNonFileURL(url);
 
         File resourceFile = toFile(url);

Modified: tapestry/tapestry5/trunk/tapestry-core/src/site/apt/upgrade.apt
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/site/apt/upgrade.apt?rev=639039&r1=639038&r2=639039&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/site/apt/upgrade.apt (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/site/apt/upgrade.apt Wed Mar 19 15:16:34 2008
@@ -12,6 +12,14 @@
   You should also check the {{{../release-notes.html}project-wide release notes}} for information
   about bugs fixes and other improvements.
 
+Release 5.0.12
+
+* Resource.openStream()
+
+  The methods <<<exists()>>> and <<<openStream()>>> were added to the
+  {{{../apidocs/org/apache/tapestry/ioc/Resource.html}Resource}} interface. The semantics
+  of some of the other methods were slightly alterred.
+
 Release 5.0.11
 
 * Field.getElementName()

Modified: tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/internal/util/URLChangeTrackerTest.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/internal/util/URLChangeTrackerTest.java?rev=639039&r1=639038&r2=639039&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/internal/util/URLChangeTrackerTest.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/internal/util/URLChangeTrackerTest.java Wed Mar 19 15:16:34 2008
@@ -31,6 +31,14 @@
     }
 
     @Test
+    public void add_null_returns_zero()
+    {
+        URLChangeTracker t = new URLChangeTracker();
+
+        assertEquals(t.add(null), 0l);
+    }
+
+    @Test
     public void contains_changes() throws Exception
     {
         URLChangeTracker t = new URLChangeTracker();

Modified: tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry/ioc/Resource.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry/ioc/Resource.java?rev=639039&r1=639038&r2=639039&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry/ioc/Resource.java (original)
+++ tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry/ioc/Resource.java Wed Mar 19 15:16:34 2008
@@ -1,4 +1,4 @@
-// Copyright 2006 The Apache Software Foundation
+// Copyright 2006, 2008 The Apache Software Foundation
 //
 // Licensed under the Apache License, Version 2.0 (the "License");
 // you may not use this file except in compliance with the License.
@@ -14,19 +14,35 @@
 
 package org.apache.tapestry.ioc;
 
+import java.io.IOException;
+import java.io.InputStream;
 import java.net.URL;
 import java.util.Locale;
 
 /**
- * Represents a resource on the server that may be used for server side processing, or may be
- * exposed to the client side. Generally, this represents an abstraction on top of files on the
- * class path and files stored in the web application context.
+ * Represents a resource on the server that may be used for server side processing, or may be exposed to the client
+ * side. Generally, this represents an abstraction on top of files on the class path and files stored in the web
+ * application context.
  * <p/>
- * Resources are often used as map keys; they should be immutable and should implement hashCode()
- * and equals().
+ * Resources are often used as map keys; they should be immutable and should implement hashCode() and equals().
  */
 public interface Resource
 {
+
+    /**
+     * Returns true if the resource exists; if a stream to the content of the file may be openned.
+     *
+     * @return true if the resource exists, false if it does not
+     */
+    boolean exists();
+
+    /**
+     * Opens a stream to the content of the resource, or returns null if the resource does not exist.
+     *
+     * @return an open, buffered stream to the content, if available
+     */
+    InputStream openStream() throws IOException;
+
     /**
      * Returns the URL for the resource, or null if it does not exist.
      */
@@ -38,16 +54,14 @@
     Resource forLocale(Locale locale);
 
     /**
-     * Returns a Resource based on a relative path, relative to the folder containing the resource.
-     * Understands the "." (current folder) and ".." (parent folder) conventions, and treats
-     * multiple sequential slashes as a single slash.
+     * Returns a Resource based on a relative path, relative to the folder containing the resource. Understands the "."
+     * (current folder) and ".." (parent folder) conventions, and treats multiple sequential slashes as a single slash.
      */
     Resource forFile(String relativePath);
 
     /**
-     * Returns a new Resource with the extension changed (or, if the resource does not have an
-     * extension, the extension is added). The new Resource may not exist (that is, {@link #toURL()}
-     * may return null.
+     * Returns a new Resource with the extension changed (or, if the resource does not have an extension, the extension
+     * is added). The new Resource may not exist (that is, {@link #toURL()} may return null.
      *
      * @param extension to apply to the resource, such as "html" or "properties"
      * @return the new resource
@@ -55,14 +69,13 @@
     Resource withExtension(String extension);
 
     /**
-     * Returns the portion of the path up to the last forward slash; this is the directory or folder
-     * portion of the Resource.
+     * Returns the portion of the path up to the last forward slash; this is the directory or folder portion of the
+     * Resource.
      */
     String getFolder();
 
     /**
-     * Returns the file portion of the Resource path, everything that follows the final forward
-     * slash.
+     * Returns the file portion of the Resource path, everything that follows the final forward slash.
      */
     String getFile();
 

Modified: tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry/ioc/internal/util/AbstractResource.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry/ioc/internal/util/AbstractResource.java?rev=639039&r1=639038&r2=639039&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry/ioc/internal/util/AbstractResource.java (original)
+++ tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry/ioc/internal/util/AbstractResource.java Wed Mar 19 15:16:34 2008
@@ -1,4 +1,4 @@
-// Copyright 2006 The Apache Software Foundation
+// Copyright 2006, 2008 The Apache Software Foundation
 //
 // Licensed under the Apache License, Version 2.0 (the "License");
 // you may not use this file except in compliance with the License.
@@ -18,13 +18,15 @@
 import static org.apache.tapestry.ioc.internal.util.Defense.notBlank;
 import static org.apache.tapestry.ioc.internal.util.Defense.notNull;
 
+import java.io.BufferedInputStream;
+import java.io.IOException;
+import java.io.InputStream;
 import java.net.URL;
 import java.util.Locale;
 
 /**
- * Abstract implementation of {@link Resource}. Subclasses must implement the abstract methods
- * {@link Resource#toURL()} and {@link #newResource(String)} as well as toString(), hashCode() and
- * equals().
+ * Abstract implementation of {@link Resource}. Subclasses must implement the abstract methods {@link Resource#toURL()}
+ * and {@link #newResource(String)} as well as toString(), hashCode() and equals().
  */
 public abstract class AbstractResource implements Resource
 {
@@ -101,9 +103,7 @@
         {
             Resource potential = createResource(path);
 
-            URL url = potential.toURL();
-
-            if (url != null) return potential;
+            if (potential.exists()) return potential;
         }
 
         return null;
@@ -121,14 +121,34 @@
     }
 
     /**
-     * Creates a new resource, unless the path matches the current Resource's path (in which case,
-     * this resource is returned).
+     * Creates a new resource, unless the path matches the current Resource's path (in which case, this resource is
+     * returned).
      */
     private Resource createResource(String path)
     {
         if (_path.equals(path)) return this;
 
         return newResource(path);
+    }
+
+    /**
+     * Simple check for whether {@link #toURL()} returns null or not.
+     */
+    public boolean exists()
+    {
+        return toURL() != null;
+    }
+
+    /**
+     * Obtains the URL for the Resource and opens the stream, wrapped by a BufferedInputStream.
+     */
+    public InputStream openStream() throws IOException
+    {
+        URL url = toURL();
+
+        if (url == null) return null;
+
+        return new BufferedInputStream(url.openStream());
     }
 
     /**

Modified: tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry/ioc/internal/util/ClasspathResource.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry/ioc/internal/util/ClasspathResource.java?rev=639039&r1=639038&r2=639039&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry/ioc/internal/util/ClasspathResource.java (original)
+++ tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry/ioc/internal/util/ClasspathResource.java Wed Mar 19 15:16:34 2008
@@ -1,4 +1,4 @@
-// Copyright 2006, 2007 The Apache Software Foundation
+// Copyright 2006, 2007, 2008 The Apache Software Foundation
 //
 // Licensed under the Apache License, Version 2.0 (the "License");
 // you may not use this file except in compliance with the License.
@@ -20,8 +20,7 @@
 import java.net.URL;
 
 /**
- * Implementation of {@link Resource} for files on the classpath (as defined by a
- * {@link ClassLoader}).
+ * Implementation of {@link Resource} for files on the classpath (as defined by a {@link ClassLoader}).
  */
 public final class ClasspathResource extends AbstractResource
 {
@@ -29,6 +28,8 @@
 
     private URL _url;
 
+    private boolean _urlResolved;
+
     public ClasspathResource(String path)
     {
         this(Thread.currentThread().getContextClassLoader(), path);
@@ -51,7 +52,11 @@
 
     public synchronized URL toURL()
     {
-        if (_url == null) _url = _classLoader.getResource(getPath());
+        if (!_urlResolved)
+        {
+            _url = _classLoader.getResource(getPath());
+            _urlResolved = true;
+        }
 
         return _url;
     }