You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@click.apache.org by sa...@apache.org on 2009/07/16 20:46:51 UTC

svn commit: r794775 - /incubator/click/trunk/click/framework/src/org/apache/click/service/ClickResourceService.java

Author: sabob
Date: Thu Jul 16 18:46:51 2009
New Revision: 794775

URL: http://svn.apache.org/viewvc?rev=794775&view=rev
Log:
added methods to load resource from either classpath or servlet context. added check to guard against null resource data

Modified:
    incubator/click/trunk/click/framework/src/org/apache/click/service/ClickResourceService.java

Modified: incubator/click/trunk/click/framework/src/org/apache/click/service/ClickResourceService.java
URL: http://svn.apache.org/viewvc/incubator/click/trunk/click/framework/src/org/apache/click/service/ClickResourceService.java?rev=794775&r1=794774&r2=794775&view=diff
==============================================================================
--- incubator/click/trunk/click/framework/src/org/apache/click/service/ClickResourceService.java (original)
+++ incubator/click/trunk/click/framework/src/org/apache/click/service/ClickResourceService.java Thu Jul 16 18:46:51 2009
@@ -317,33 +317,15 @@
             String resourceName = file.substring(pathIndex);
 
             if (resourceName.length() > 0) {
-                byte[] resourceBytes = getResourceByteArray(file);
+                byte[] resourceBytes = getClasspathResourceData(file);
 
-                resourceCache.put("/" + resourceName, resourceBytes);
+                if (resourceBytes != null) {
+                    resourceCache.put("/" + resourceName, resourceBytes);
+                }
             }
         }
     }
 
-    private byte[] getResourceByteArray(String name)
-        throws IOException {
-
-        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
-
-        InputStream inputStream = classLoader.getResourceAsStream(name);
-        if (inputStream == null) {
-            inputStream = getClass().getResourceAsStream(name);
-        }
-
-        try {
-            byte[] resourceData = IOUtils.toByteArray(inputStream);
-
-            return resourceData;
-
-        } finally {
-            ClickUtils.close(inputStream);
-        }
-    }
-
     @SuppressWarnings("unchecked")
     private void loadClickDirResources(ServletContext servletContext)
         throws IOException {
@@ -358,21 +340,66 @@
                 if (!resource.endsWith(".htm") && !resource.endsWith(".jsp")
                     && !resource.endsWith("/")) {
 
-                    InputStream inputStream = null;
-                    try {
-                        inputStream = servletContext.getResourceAsStream(
-                            resource);
-
-                        byte[] resourceData = IOUtils.toByteArray(inputStream);
-
+                    byte[] resourceData = getServletResourceData(servletContext, resource);
+                    if (resourceData != null) {
                         resourceCache.put(resource, resourceData);
-
-                    } finally {
-                        ClickUtils.close(inputStream);
                     }
                 }
             }
         }
     }
 
+    /**
+     * Load the resource for the given resourcePath from the servlet context.
+     *
+     * @param servletContext the application servlet context
+     * @param resourcePath the path of the resource to load
+     * @return the byte array for the given resource path
+     * @throws IOException if the resource could not be loaded
+     */
+    private byte[] getServletResourceData(ServletContext servletContext,
+        String resourcePath) throws IOException {
+        InputStream inputStream = null;
+        try {
+            inputStream = servletContext.getResourceAsStream(resourcePath);
+
+            if (inputStream != null) {
+                return IOUtils.toByteArray(inputStream);
+            } else {
+                return null;
+            }
+
+        } finally {
+            ClickUtils.close(inputStream);
+        }
+    }
+
+    /**
+     * Load the resource for the given resourcePath from the classpath.
+     *
+     * @param resourcePath the path of the resource to load
+     * @return the byte array for the given resource path
+     * @throws IOException if the resource could not be loaded
+     */
+    private byte[] getClasspathResourceData(String resourcePath) throws IOException {
+
+        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
+
+        InputStream inputStream = classLoader.getResourceAsStream(resourcePath);
+        if (inputStream == null) {
+            inputStream = getClass().getResourceAsStream(resourcePath);
+        }
+
+        try {
+
+            if (inputStream != null) {
+                return IOUtils.toByteArray(inputStream);
+            } else {
+                return null;
+            }
+
+        } finally {
+            ClickUtils.close(inputStream);
+        }
+    }
 }