diff options
author | Greg Wilkins | 2010-07-28 13:48:54 +0000 |
---|---|---|
committer | Greg Wilkins | 2010-07-28 13:48:54 +0000 |
commit | cbc47fca80e3440b5e3fce5922f757e7d1a90531 (patch) | |
tree | d3f80fba2718f58605506f4d5437258ce6b36495 /jetty-deploy/src/test | |
parent | e025cc3ca4af1241c8e4e6c30d0a072842bbf702 (diff) | |
download | org.eclipse.jetty.project-cbc47fca80e3440b5e3fce5922f757e7d1a90531.tar.gz org.eclipse.jetty.project-cbc47fca80e3440b5e3fce5922f757e7d1a90531.tar.xz org.eclipse.jetty.project-cbc47fca80e3440b5e3fce5922f757e7d1a90531.zip |
320073 local loader
git-svn-id: svn+ssh://dev.eclipse.org/svnroot/rt/org.eclipse.jetty/jetty/trunk@2191 7e9141cc-0065-0410-87d8-b60c137991c4
Diffstat (limited to 'jetty-deploy/src/test')
-rw-r--r-- | jetty-deploy/src/test/java/org/eclipse/jetty/deploy/CloudLoader.java | 116 | ||||
-rw-r--r-- | jetty-deploy/src/test/java/org/eclipse/jetty/deploy/CloudServer.java | 147 |
2 files changed, 177 insertions, 86 deletions
diff --git a/jetty-deploy/src/test/java/org/eclipse/jetty/deploy/CloudLoader.java b/jetty-deploy/src/test/java/org/eclipse/jetty/deploy/CloudLoader.java index f540666fb2..04e4c1ab42 100644 --- a/jetty-deploy/src/test/java/org/eclipse/jetty/deploy/CloudLoader.java +++ b/jetty-deploy/src/test/java/org/eclipse/jetty/deploy/CloudLoader.java @@ -1,63 +1,121 @@ package org.eclipse.jetty.deploy; import java.io.IOException; -import java.io.InputStream; import java.lang.reflect.Field; import java.lang.reflect.Modifier; +import java.net.URL; +import java.net.URLClassLoader; +import java.util.Enumeration; -import org.eclipse.jetty.util.IO; import org.eclipse.jetty.util.log.Log; +import org.eclipse.jetty.webapp.ClasspathPattern; import org.eclipse.jetty.webapp.WebAppClassLoader; -public class CloudLoader extends WebAppClassLoader +public class CloudLoader extends URLClassLoader { - final WebAppClassLoader.Context _context; + final WebAppClassLoader _parent; + ClasspathPattern _localPaths = new ClasspathPattern(); /* ------------------------------------------------------------ */ - public CloudLoader(WebAppClassLoader.Context context) + public CloudLoader(WebAppClassLoader parent) throws IOException { - super(context); - _context=context; + super(parent.getURLs(),parent); + _parent=parent; } /* ------------------------------------------------------------ */ - public CloudLoader(ClassLoader parent,WebAppClassLoader.Context context) - throws IOException + /** Add a local class pattern + * <p>Add a pattern as defined by {@link ClasspathPattern}. + */ + public void addPattern(String pattern) { - super(parent,context); - _context=context; + _localPaths.addPattern(pattern); + } + + /* ------------------------------------------------------------ */ + public boolean isLocal(String name) + { + return _localPaths.match(name) && !_parent.getContext().isSystemClass(name); } /* ------------------------------------------------------------ */ - /** - * @see java.net.URLClassLoader#findClass(java.lang.String) - */ @Override - protected Class<?> findClass(String name) throws ClassNotFoundException + public Enumeration<URL> getResources(String name) throws IOException { - Class<?> clazz = super.findClass(name); - - if (clazz!=null && clazz.getClassLoader()==this) + return isLocal(name)?findResources(name):_parent.getResources(name); + } + + /* ------------------------------------------------------------ */ + @Override + public URL getResource(String name) + { + if (isLocal(name)) + { + URL url= this.findResource(name); + + if (url == null && name.startsWith("/")) + { + if (Log.isDebugEnabled()) + Log.debug("HACK leading / off " + name); + url= this.findResource(name.substring(1)); + } + if (url!=null) + return url; + } + return _parent.getResource(name); + } + + + /* ------------------------------------------------------------ */ + @Override + protected synchronized Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException + { + Class<?> c= findLoadedClass(name); + + if (c==null && isLocal(name)) { - boolean has_non_final_static_fields=false; - for (Field field : clazz.getFields()) + try + { + c= this.findClass(name); + } + catch (ClassNotFoundException e) { - int mods = field.getModifiers(); - if (Modifier.isStatic(mods) && !Modifier.isFinal(mods)) - { - has_non_final_static_fields=true; - } + Log.ignore(e); } + } - if (has_non_final_static_fields && !_context.isSystemClass(name)) + if (c == null) + c= _parent.loadClass(name); + + if (resolve) + resolveClass(c); + + if (Log.isDebugEnabled()) + Log.debug("loaded " + c+ " from "+c.getClassLoader()); + + // if loaded from direct parent, then scan for non final statics + // look for non final static fields + boolean has_non_final_static_fields=false; + for (Field field : c.getDeclaredFields()) + { + int mods = field.getModifiers(); + if (Modifier.isStatic(mods) && !Modifier.isFinal(mods)) { - Log.info("Has non-final static fields: "+name); + has_non_final_static_fields=true; } } - return clazz; + + if (has_non_final_static_fields) + { + if (c.getClassLoader()==_parent) + Log.warn(name+" loaded from "+c.getClassLoader()+" has non-final static fields"); + else + Log.debug(name+" loaded from "+c.getClassLoader()+" has non-final static fields"); + } + + return c; } - } diff --git a/jetty-deploy/src/test/java/org/eclipse/jetty/deploy/CloudServer.java b/jetty-deploy/src/test/java/org/eclipse/jetty/deploy/CloudServer.java index 7c7a799308..75d39384fb 100644 --- a/jetty-deploy/src/test/java/org/eclipse/jetty/deploy/CloudServer.java +++ b/jetty-deploy/src/test/java/org/eclipse/jetty/deploy/CloudServer.java @@ -113,12 +113,11 @@ public class CloudServer } } }; - ResourceCache cache = new ResourceCache(resources,new MimeTypes(),true); WebAppClassLoader.Context loaderContext = new WebAppClassLoader.Context() { - private ClasspathPattern _systemClasses = ClasspathPattern.fromArray(WebAppContext.__dftSystemClasses); - private ClasspathPattern _serverClasses = ClasspathPattern.fromArray(WebAppContext.__dftServerClasses); + private ClasspathPattern _systemClasses = new ClasspathPattern(WebAppContext.__dftSystemClasses); + private ClasspathPattern _serverClasses = new ClasspathPattern(WebAppContext.__dftServerClasses); public Resource newResource(String urlOrPath) throws IOException { @@ -142,7 +141,7 @@ public class CloudServer public boolean isParentLoaderPriority() { - return true; + return false; } public String getExtraClasspath() @@ -151,76 +150,110 @@ public class CloudServer } }; - WebAppClassLoader loader = new CloudLoader(loaderContext); + WebAppClassLoader loader = new WebAppClassLoader(loaderContext); + loader.setName("template"); loader.addClassPath("../test-jetty-webapp/target/classes"); loader.addJars(Resource.newResource("../test-jetty-webapp/target/test-jetty-webapp-7.2.0-SNAPSHOT/WEB-INF/lib")); - // Create a base configuration - /* Cloud deploy */ - boolean cloud=!Boolean.getBoolean("nocloud"); - if (cloud) + // Non cloud deployment first + for (int i=0;i<10;i++) { - Log.info("Cload deploy"); - final WebAppContext template = new WebAppContext(); - template.setClassLoader(loader); - template.setBaseResource(baseResource); - template.setAttribute("instance","-1"); - template.setServer(server); - template.preConfigure(); - template.configure(); - template.postConfigure(); - - for (int i=0;i<10;i++) - { - final WebAppContext webapp = new WebAppContext(template); - webapp.setAttribute("resourceCache",cache); - webapp.setAttribute("instance",i); + final WebAppContext webapp= new WebAppContext(); + webapp.setWar("../test-jetty-webapp/target/test-jetty-webapp-7.2.0-SNAPSHOT.war"); + webapp.setAttribute("instance",i); - if (i>0) - webapp.setVirtualHosts(new String[] {"127.0.0."+i}); - contexts.addHandler(webapp); - } + if (i>0) + webapp.setVirtualHosts(new String[] {"127.0.0."+i}); + contexts.addHandler(webapp); } - else - { - /* Normal deploy */ + + server.start(); + load(); + + Runtime.getRuntime().gc(); + Runtime.getRuntime().gc(); + + long used_normal = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory(); + System.err.println(used_normal); + + server.stop(); + contexts.setHandlers(null); - for (int i=0;i<10;i++) - { - final WebAppContext webapp= new WebAppContext(); - webapp.setWar("../test-jetty-webapp/target/test-jetty-webapp-7.2.0-SNAPSHOT.war"); - webapp.setAttribute("instance",i); + Runtime.getRuntime().gc(); + Runtime.getRuntime().gc(); + + long used_stopped = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory(); + System.err.println(used_stopped); - if (i>0) - webapp.setVirtualHosts(new String[] {"127.0.0."+i}); - contexts.addHandler(webapp); - } - } + /* Cloud deploy */ + boolean cloud=!Boolean.getBoolean("nocloud"); + Log.info("Cloud deploy"); + final WebAppContext template = new WebAppContext(); + template.setClassLoader(loader); + template.setBaseResource(baseResource); + template.setAttribute("instance","-1"); + template.setServer(server); + template.preConfigure(); + template.configure(); + template.postConfigure(); + + ResourceCache cache = new ResourceCache(resources,new MimeTypes(),true); + + for (int i=0;i<10;i++) + { + final WebAppContext webapp = new WebAppContext(template); + webapp.setAttribute("resourceCache",cache); + webapp.setAttribute("instance",i); + CloudLoader cloud_loader = new CloudLoader((WebAppClassLoader)webapp.getClassLoader()); + // cloud_loader.addPattern("com.acme."); + // cloud_loader.addPattern("org.eclipse.jetty.util."); + webapp.setClassLoader(cloud_loader); + if (i>0) + webapp.setVirtualHosts(new String[] {"127.0.0."+i}); + contexts.addHandler(webapp); + } server.start(); - System.err.println(server.dump()); + load(); - for (int i=0;i<10;i++) + + Runtime.getRuntime().gc(); + Runtime.getRuntime().gc(); + + long used_cloud = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory(); + System.err.println(used_cloud); + + server.stop(); + + System.err.println(used_normal-used_cloud); + } + + private static void load() throws Exception + { + + for (int j=0;j<10;j++) { - // generate some load - for (String uri : new String[] { - "/", - "/d.txt", - "/da.txt", - "/dat.txt", - "/data.txt", - "/data.txt.gz", - "/dump/info" - }) + for (int i=0;i<10;i++) { - URL url = new URL("http://127.0.0."+(i==0?10:i)+":8080"+uri); - System.err.println("GOT "+url+" "+String.valueOf(IO.toString(url.openStream())).length()); + // generate some load + for (String uri : new String[] { + "/", + "/d.txt", + "/da.txt", + "/dat.txt", + "/data.txt", + "/data.txt.gz", + "/dump/info" + }) + { + URL url = new URL("http://127.0.0."+(i==0?10:i)+":8080"+uri); + String content = String.valueOf(IO.toString(url.openStream())); + // System.err.println("GOT "+url+" "+content.length()); + } } } - - server.join(); } } |