From c0e6e66b49831a26307b73b678cf5c385b69a912 Mon Sep 17 00:00:00 2001 From: Greg Wilkins Date: Mon, 26 Oct 2015 09:10:55 +1100 Subject: [PATCH 1/2] Added CachingWebAppClassLoader --- .../webapp/CachingWebAppClassLoader.java | 121 ++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 jetty-webapp/src/main/java/org/eclipse/jetty/webapp/CachingWebAppClassLoader.java diff --git a/jetty-webapp/src/main/java/org/eclipse/jetty/webapp/CachingWebAppClassLoader.java b/jetty-webapp/src/main/java/org/eclipse/jetty/webapp/CachingWebAppClassLoader.java new file mode 100644 index 00000000000..b099a348100 --- /dev/null +++ b/jetty-webapp/src/main/java/org/eclipse/jetty/webapp/CachingWebAppClassLoader.java @@ -0,0 +1,121 @@ +// +// ======================================================================== +// Copyright (c) 1995-2015 Mort Bay Consulting Pty. Ltd. +// ------------------------------------------------------------------------ +// All rights reserved. This program and the accompanying materials +// are made available under the terms of the Eclipse Public License v1.0 +// and Apache License v2.0 which accompanies this distribution. +// +// The Eclipse Public License is available at +// http://www.eclipse.org/legal/epl-v10.html +// +// The Apache License v2.0 is available at +// http://www.opensource.org/licenses/apache2.0.php +// +// You may elect to redistribute this code under either of these licenses. +// ======================================================================== +// + +package org.eclipse.jetty.webapp; + +import java.io.IOException; +import java.net.URL; +import java.util.concurrent.ConcurrentHashMap; + +import org.eclipse.jetty.util.ConcurrentHashSet; +import org.eclipse.jetty.util.annotation.ManagedObject; +import org.eclipse.jetty.util.annotation.ManagedOperation; + + +/** + * A WebAppClassLoader that caches {@link #getResource(String)} results. + * Specifically this ClassLoader caches not found classes and resources, + * which can greatly increase performance for applications that search + * for resources. + */ +@ManagedObject +public class CachingWebAppClassLoader extends WebAppClassLoader +{ + private final ConcurrentHashSet _notFound = new ConcurrentHashSet<>(); + private final ConcurrentHashMap _cache = new ConcurrentHashMap<>(); + + public CachingWebAppClassLoader(ClassLoader parent, Context context) throws IOException + { + super(parent,context); + } + + public CachingWebAppClassLoader(Context context) throws IOException + { + super(context); + } + + @Override + public URL getResource(String name) + { + if (_notFound.contains(name)) + return null; + + URL url = _cache.get(name); + + if (name==null) + { + url = super.getResource(name); + + if (url==null) + { + _notFound.add(name); + } + else + { + _cache.putIfAbsent(name,url); + } + } + + return url; + } + + @Override + protected Class loadClass(String name, boolean resolve) throws ClassNotFoundException + { + if (_notFound.contains(name)) + throw new ClassNotFoundException(name+": in notfound cache"); + try + { + return super.loadClass(name,resolve); + } + catch (ClassNotFoundException nfe) + { + _notFound.add(name); + throw nfe; + } + } + + @Override + protected Class findClass(String name) throws ClassNotFoundException + { + if (_notFound.contains(name)) + throw new ClassNotFoundException(name+": in notfound cache"); + try + { + return super.findClass(name); + } + catch (ClassNotFoundException nfe) + { + _notFound.add(name); + throw nfe; + } + } + + @ManagedOperation + public void clearCache() + { + _cache.clear(); + _notFound.clear(); + } + + @Override + public String toString() + { + return "Caching["+super.toString()+"]"; + } +} From 9a66395f0c27a23e6c3e9b0d211e6329f184197a Mon Sep 17 00:00:00 2001 From: Greg Wilkins Date: Mon, 26 Oct 2015 09:45:28 +1100 Subject: [PATCH 2/2] Fixed demobase example xml --- .../src/main/config/etc/jetty-rewrite.xml | 48 +++++++++---------- .../main/config/demo-base/webapps/test.xml | 9 +++- 2 files changed, 31 insertions(+), 26 deletions(-) diff --git a/jetty-rewrite/src/main/config/etc/jetty-rewrite.xml b/jetty-rewrite/src/main/config/etc/jetty-rewrite.xml index 63af3c21d71..65a5dea44a2 100644 --- a/jetty-rewrite/src/main/config/etc/jetty-rewrite.xml +++ b/jetty-rewrite/src/main/config/etc/jetty-rewrite.xml @@ -19,31 +19,29 @@ - - - - - REQUEST - ASYNC - - + + + + REQUEST + ASYNC + + + + + + + - - - - - - diff --git a/tests/test-webapps/test-jetty-webapp/src/main/config/demo-base/webapps/test.xml b/tests/test-webapps/test-jetty-webapp/src/main/config/demo-base/webapps/test.xml index 0037cb49061..cae730e89d2 100644 --- a/tests/test-webapps/test-jetty-webapp/src/main/config/demo-base/webapps/test.xml +++ b/tests/test-webapps/test-jetty-webapp/src/main/config/demo-base/webapps/test.xml @@ -11,7 +11,7 @@ directory, additional configuration may be specified and hot deployments detected. ===================================================================== --> - + @@ -41,6 +41,13 @@ detected. + + + + + + +