297421 Hide server/system classes from WebAppClassLoader.getResources

git-svn-id: svn+ssh://dev.eclipse.org/svnroot/rt/org.eclipse.jetty/jetty/trunk@1139 7e9141cc-0065-0410-87d8-b60c137991c4
This commit is contained in:
Greg Wilkins 2009-12-10 21:54:06 +00:00
parent 6def823a49
commit 9825a4282b
8 changed files with 222 additions and 3 deletions

View File

@ -1,6 +1,7 @@
jetty-7.0.2-SNAPSHOT
+ JSON parses NaN as null
+ 290765 Reset input for HttpExchange retry.
+ 297421 Hide server/system classes from WebAppClassLoader.getResources
jetty-7.0.1.v20091125 25 November 2009
+ 274251 DefaultServlet supports exact match mode.

View File

@ -19,7 +19,11 @@ import java.net.URL;
import java.net.URLClassLoader;
import java.security.CodeSource;
import java.security.PermissionCollection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.List;
import java.util.StringTokenizer;
import org.eclipse.jetty.server.handler.ContextHandler;
@ -221,12 +225,45 @@ public class WebAppClassLoader extends URLClassLoader
return pc;
}
/* ------------------------------------------------------------ */
public Enumeration<URL> getResources(String name) throws IOException
{
boolean system_class=_context.isSystemClass(name);
boolean server_class=_context.isServerClass(name);
List<URL> from_parent = toList(server_class?null:_parent.getResources(name));
List<URL> from_webapp = toList((system_class&&!from_parent.isEmpty())?null:this.findResources(name));
if (_context.isParentLoaderPriority())
{
from_parent.addAll(from_webapp);
return Collections.enumeration(from_parent);
}
from_webapp.addAll(from_parent);
return Collections.enumeration(from_webapp);
}
private List<URL> toList(Enumeration<URL> e)
{
List<URL> list = new ArrayList<URL>();
while (e!=null && e.hasMoreElements())
list.add(e.nextElement());
return list;
}
/* ------------------------------------------------------------ */
public URL getResource(String name)
{
URL url= null;
boolean tried_parent= false;
if (_context.isParentLoaderPriority() || _context.isSystemClass(name))
boolean system_class=_context.isSystemClass(name);
boolean server_class=_context.isServerClass(name);
if (system_class && server_class)
return null;
if (_parent!=null &&(_context.isParentLoaderPriority() || system_class ) && !server_class)
{
tried_parent= true;
@ -246,7 +283,7 @@ public class WebAppClassLoader extends URLClassLoader
}
}
if (url == null && !tried_parent && !_context.isServerClass(name) )
if (url == null && !tried_parent && !server_class )
{
if (_parent!=null)
url= _parent.getResource(name);
@ -282,7 +319,7 @@ public class WebAppClassLoader extends URLClassLoader
return null;
}
if (c == null && _parent!=null && (_context.isParentLoaderPriority() || system_class) )
if (c == null && _parent!=null && (_context.isParentLoaderPriority() || system_class) && !server_class)
{
tried_parent= true;
try

View File

@ -0,0 +1,6 @@
package org.acme.webapp;
public class ClassInJarA
{
public static final boolean FROM_PARENT=true;
}

View File

@ -0,0 +1,173 @@
package org.eclipse.jetty.webapp;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.List;
import org.eclipse.jetty.util.resource.Resource;
import junit.framework.TestCase;
public class WebAppClassLoaderTest extends TestCase
{
WebAppContext _context;
WebAppClassLoader _loader;
/* ------------------------------------------------------------ */
/**
* @see junit.framework.TestCase#setUp()
*/
@Override
protected void setUp() throws Exception
{
Resource webapp = Resource.newResource("./src/test/webapp");
_context = new WebAppContext();
_context.setBaseResource(webapp);
_context.setContextPath("/test");
_loader = new WebAppClassLoader(_context);
_loader.addJars(webapp.addPath("WEB-INF/lib"));
_loader.addClassPath(webapp.addPath("WEB-INF/classes").toString());
_loader.setName("test");
}
public void testParentLoad() throws Exception
{
_context.setParentLoaderPriority(true);
assertTrue(canLoadClass("org.acme.webapp.ClassInJarA"));
assertTrue(canLoadClass("org.acme.webapp.ClassInJarB"));
assertTrue(canLoadClass("org.acme.other.ClassInClassesC"));
assertFalse(canLoadClass("org.eclipse.jetty.webapp.Configuration"));
Class clazzA = _loader.loadClass("org.acme.webapp.ClassInJarA");
assertTrue(clazzA.getField("FROM_PARENT")!=null);
}
public void testWebAppLoad() throws Exception
{
_context.setParentLoaderPriority(false);
assertTrue(canLoadClass("org.acme.webapp.ClassInJarA"));
assertTrue(canLoadClass("org.acme.webapp.ClassInJarB"));
assertTrue(canLoadClass("org.acme.other.ClassInClassesC"));
assertFalse(canLoadClass("org.eclipse.jetty.webapp.Configuration"));
Class<?> clazzA = _loader.loadClass("org.acme.webapp.ClassInJarA");
try
{
clazzA.getField("FROM_PARENT");
assertTrue(false);
}
catch(NoSuchFieldException e)
{
assertTrue(true);
}
}
public void testExposedClass() throws Exception
{
String[] oldSC=_context.getServerClasses();
String[] newSC=new String[oldSC.length+1];
newSC[0]="-org.eclipse.jetty.webapp.Configuration";
System.arraycopy(oldSC,0,newSC,1,oldSC.length);
_context.setServerClasses(newSC);
assertTrue(canLoadClass("org.acme.webapp.ClassInJarA"));
assertTrue(canLoadClass("org.acme.webapp.ClassInJarB"));
assertTrue(canLoadClass("org.acme.other.ClassInClassesC"));
assertTrue(canLoadClass("org.eclipse.jetty.webapp.Configuration"));
assertFalse(canLoadClass("org.eclipse.jetty.webapp.JarScanner"));
}
public void testSystemServerClass() throws Exception
{
String[] oldServC=_context.getServerClasses();
String[] newServC=new String[oldServC.length+1];
newServC[0]="org.eclipse.jetty.webapp.Configuration";
System.arraycopy(oldServC,0,newServC,1,oldServC.length);
_context.setServerClasses(newServC);
String[] oldSysC=_context.getSystemClasses();
String[] newSysC=new String[oldSysC.length+1];
newSysC[0]="org.eclipse.jetty.webapp.";
System.arraycopy(oldSysC,0,newSysC,1,oldSysC.length);
_context.setSystemClasses(newSysC);
assertTrue(canLoadClass("org.acme.webapp.ClassInJarA"));
assertTrue(canLoadClass("org.acme.webapp.ClassInJarB"));
assertTrue(canLoadClass("org.acme.other.ClassInClassesC"));
assertFalse(canLoadClass("org.eclipse.jetty.webapp.Configuration"));
assertFalse(canLoadClass("org.eclipse.jetty.webapp.JarScanner"));
}
public void testResources() throws Exception
{
List<URL> resources;
_context.setParentLoaderPriority(false);
resources =toList( _loader.getResources("org/acme/resource.txt"));
assertEquals(3,resources.size());
assertEquals(0,resources.get(0).toString().indexOf("jar:file:"));
assertEquals(-1,resources.get(1).toString().indexOf("test-classes"));
assertEquals(0,resources.get(2).toString().indexOf("file:"));
_context.setParentLoaderPriority(true);
resources =toList( _loader.getResources("org/acme/resource.txt"));
assertEquals(3,resources.size());
assertEquals(0,resources.get(0).toString().indexOf("file:"));
assertEquals(0,resources.get(1).toString().indexOf("jar:file:"));
assertEquals(-1,resources.get(2).toString().indexOf("test-classes"));
String[] oldServC=_context.getServerClasses();
String[] newServC=new String[oldServC.length+1];
newServC[0]="org.acme.";
System.arraycopy(oldServC,0,newServC,1,oldServC.length);
_context.setServerClasses(newServC);
_context.setParentLoaderPriority(true);
resources =toList( _loader.getResources("org/acme/resource.txt"));
assertEquals(2,resources.size());
assertEquals(0,resources.get(0).toString().indexOf("jar:file:"));
assertEquals(0,resources.get(1).toString().indexOf("file:"));
_context.setServerClasses(oldServC);
String[] oldSysC=_context.getSystemClasses();
String[] newSysC=new String[oldSysC.length+1];
newSysC[0]="org.acme.";
System.arraycopy(oldSysC,0,newSysC,1,oldSysC.length);
_context.setSystemClasses(newSysC);
_context.setParentLoaderPriority(true);
resources =toList( _loader.getResources("org/acme/resource.txt"));
assertEquals(1,resources.size());
assertEquals(0,resources.get(0).toString().indexOf("file:"));
}
private List<URL> toList(Enumeration<URL> e)
{
List<URL> list = new ArrayList<URL>();
while (e!=null && e.hasMoreElements())
list.add(e.nextElement());
return list;
}
private boolean canLoadClass(String clazz)
{
try
{
return _loader.loadClass(clazz)!=null;
}
catch(ClassNotFoundException e)
{
return false;
}
}
}

View File

@ -0,0 +1 @@
resource

View File

@ -0,0 +1 @@
resource

Binary file not shown.