tomee-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rmannibu...@apache.org
Subject tomee git commit: flag to cache web resources in openejb-http
Date Thu, 05 Feb 2015 18:42:16 GMT
Repository: tomee
Updated Branches:
  refs/heads/develop 74640eb0b -> da12fb12c


flag to cache web resources in openejb-http


Project: http://git-wip-us.apache.org/repos/asf/tomee/repo
Commit: http://git-wip-us.apache.org/repos/asf/tomee/commit/da12fb12
Tree: http://git-wip-us.apache.org/repos/asf/tomee/tree/da12fb12
Diff: http://git-wip-us.apache.org/repos/asf/tomee/diff/da12fb12

Branch: refs/heads/develop
Commit: da12fb12cc79aceaa3056fc77ebcab8ae4237539
Parents: 74640eb
Author: Romain Manni-Bucau <rmannibucau@apache.org>
Authored: Thu Feb 5 19:42:09 2015 +0100
Committer: Romain Manni-Bucau <rmannibucau@apache.org>
Committed: Thu Feb 5 19:42:09 2015 +0100

----------------------------------------------------------------------
 .../server/httpd/HttpListenerRegistry.java      | 70 ++++++++++++--------
 1 file changed, 44 insertions(+), 26 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tomee/blob/da12fb12/server/openejb-http/src/main/java/org/apache/openejb/server/httpd/HttpListenerRegistry.java
----------------------------------------------------------------------
diff --git a/server/openejb-http/src/main/java/org/apache/openejb/server/httpd/HttpListenerRegistry.java
b/server/openejb-http/src/main/java/org/apache/openejb/server/httpd/HttpListenerRegistry.java
index 6d9786e..7cbaa0b 100644
--- a/server/openejb-http/src/main/java/org/apache/openejb/server/httpd/HttpListenerRegistry.java
+++ b/server/openejb-http/src/main/java/org/apache/openejb/server/httpd/HttpListenerRegistry.java
@@ -16,23 +16,7 @@
  */
 package org.apache.openejb.server.httpd;
 
-import org.apache.openejb.AppContext;
-import org.apache.openejb.assembler.classic.WebAppBuilder;
-import org.apache.openejb.cdi.CdiAppContextsService;
-import org.apache.openejb.cdi.Proxys;
-import org.apache.openejb.core.ParentClassLoaderFinder;
-import org.apache.openejb.core.WebContext;
-import org.apache.openejb.loader.IO;
-import org.apache.openejb.loader.SystemInstance;
-import org.apache.openejb.util.AppFinder;
-import org.apache.openejb.web.LightweightWebAppBuilder;
-import org.apache.webbeans.config.WebBeansContext;
-import org.apache.webbeans.spi.ContextsService;
-
-import javax.servlet.ServletContext;
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpSessionEvent;
-import javax.servlet.http.HttpSessionListener;
+import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
@@ -45,6 +29,22 @@ import java.util.LinkedHashMap;
 import java.util.LinkedList;
 import java.util.Map;
 import java.util.StringTokenizer;
+import javax.servlet.ServletContext;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpSessionEvent;
+import javax.servlet.http.HttpSessionListener;
+import org.apache.openejb.AppContext;
+import org.apache.openejb.assembler.classic.WebAppBuilder;
+import org.apache.openejb.cdi.CdiAppContextsService;
+import org.apache.openejb.cdi.Proxys;
+import org.apache.openejb.core.ParentClassLoaderFinder;
+import org.apache.openejb.core.WebContext;
+import org.apache.openejb.loader.IO;
+import org.apache.openejb.loader.SystemInstance;
+import org.apache.openejb.util.AppFinder;
+import org.apache.openejb.web.LightweightWebAppBuilder;
+import org.apache.webbeans.config.WebBeansContext;
+import org.apache.webbeans.spi.ContextsService;
 
 /**
  * @version $Revision$ $Date$
@@ -58,6 +58,8 @@ public class HttpListenerRegistry implements HttpListener {
     private final File[] resourceBases;
     private final Map<String, String> defaultContextTypes = new HashMap<>();
     private final String welcomeFile = SystemInstance.get().getProperty("openejb.http.welcome",
"index.html");
+    private final Map<String, byte[]> cache = new HashMap<>();
+    private final boolean cacheResources = "true".equals(SystemInstance.get().getProperty("openejb.http.resource.cache",
"false"));
 
     public HttpListenerRegistry() {
         HttpServletRequest mock = null;
@@ -199,19 +201,19 @@ public class HttpListenerRegistry implements HttpListener {
                 if (servletPath != null) {
                     URL url = SystemInstance.get().getComponent(ServletContext.class).getResource(servletPath);
                     if (url != null) {
-                        serveResource(response, url);
+                        serveResource(servletPath, response, url);
                     } else {
                         final String pathWithoutSlash = "/".equals(path) ? welcomeFile :
                                 (servletPath.startsWith("/") ? servletPath.substring(1) :
servletPath);
                         url = defaultClassLoader.getResource("META-INF/resources/" + pathWithoutSlash);
                         if (url != null) {
-                            serveResource(response, url);
+                            serveResource(servletPath, response, url);
                         } else if (resourceBases.length > 0) {
                             for (final File f : resourceBases) {
                                 final File file = new File(f, pathWithoutSlash);
                                 if (file.isFile()) {
                                     url = file.toURI().toURL();
-                                    serveResource(response, url);
+                                    serveResource(servletPath, response, url);
                                     break;
                                 }
                             }
@@ -244,12 +246,28 @@ public class HttpListenerRegistry implements HttpListener {
         }
     }
 
-    private void serveResource(HttpResponse response, URL url) throws IOException {
-        final InputStream from = url.openStream();
-        try {
-            IO.copy(from, response.getOutputStream());
-        } finally {
-            IO.close(from);
+    private void serveResource(final String key, final HttpResponse response, final URL url)
throws IOException {
+        if (cacheResources) {
+            byte[] value = cache.get(key);
+            if (value == null) {
+                final InputStream from = url.openStream();
+                try {
+                    ByteArrayOutputStream to = new ByteArrayOutputStream();
+                    IO.copy(from, to);
+                    value = to.toByteArray();
+                    cache.put(key, value);
+                } finally {
+                    IO.close(from);
+                }
+            }
+            response.getOutputStream().write(value);
+        } else {
+            final InputStream from = url.openStream();
+            try {
+                IO.copy(from, response.getOutputStream());
+            } finally {
+                IO.close(from);
+            }
         }
     }
 


Mime
View raw message