Add escape hatch for ClassLoader.findResource() for invalid path

The source of the exception is in sun.misc (so hard to track down precisely)
but it's clear that the LaunchedJarURLClassLoader needs to be more
defensive and return null from findResource() if it can't find it.

Fixes gh-486
This commit is contained in:
Dave Syer 2014-03-13 09:03:11 +00:00
parent 08aacf72e0
commit a71c9b5de7
2 changed files with 20 additions and 1 deletions

View File

@ -73,7 +73,12 @@ public class LaunchedURLClassLoader extends URLClassLoader {
return urls[0];
}
}
return super.findResource(name);
try {
return super.findResource(name);
}
catch (IllegalArgumentException e) {
return null;
}
}
@Override

View File

@ -21,6 +21,7 @@ import java.net.URL;
import org.junit.Test;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
/**
@ -28,6 +29,19 @@ import static org.junit.Assert.assertTrue;
*/
public class LaunchedURLClassLoaderTests {
@Test
public void resolveResourceFromWindowsFilesystem() throws Exception {
// This path is invalid - it should return null even on Windows.
// A regular URLClassLoader will deal with it gracefully.
assertNull(getClass().getClassLoader().getResource(
"c:\\Users\\user\\bar.properties"));
LaunchedURLClassLoader loader = new LaunchedURLClassLoader(new URL[] { new URL(
"jar:file:src/test/resources/jars/app.jar!/") }, getClass()
.getClassLoader());
// So we should too...
assertNull(loader.getResource("c:\\Users\\user\\bar.properties"));
}
@Test
public void resolveResourceFromArchive() throws Exception {
LaunchedURLClassLoader loader = new LaunchedURLClassLoader(new URL[] { new URL(