Special case for root of classpath resource in archive

This turns out to affect JPA, but only because it looks for a URL for the
root of the classpath using ClassLoader.getResource("") which barfs in
an app launched from an executable JAR. It's easy to make a special case
for "" in the class loader, so I went ahead and did that. Possibly need
to think what the implication of getResources("") is as well (not
tested in an app yet).

Fixes gh-420
This commit is contained in:
Dave Syer 2014-03-06 11:36:47 +00:00
parent afac085318
commit 8168e8a327
4 changed files with 89 additions and 1 deletions

View File

@ -21,6 +21,8 @@ import java.net.URL;
import java.net.URLClassLoader;
import java.security.AccessController;
import java.security.PrivilegedExceptionAction;
import java.util.Arrays;
import java.util.Collections;
import java.util.Enumeration;
import org.springframework.boot.loader.jar.JarFile;
@ -63,6 +65,28 @@ public class LaunchedURLClassLoader extends URLClassLoader {
return (url == null ? findResource(name) : url);
}
@Override
public URL findResource(String name) {
if (name.equals("")) {
URL[] urls = getURLs();
if (urls.length > 0) {
return urls[0];
}
}
return super.findResource(name);
}
@Override
public Enumeration<URL> findResources(String name) throws IOException {
if (name.equals("")) {
URL[] urls = getURLs();
if (urls.length > 0) {
return Collections.enumeration(Arrays.asList(urls));
}
}
return super.findResources(name);
}
@Override
public Enumeration<URL> getResources(String name) throws IOException {

View File

@ -75,6 +75,8 @@ public abstract class Launcher {
protected ClassLoader createClassLoader(List<Archive> archives) throws Exception {
List<URL> urls = new ArrayList<URL>(archives.size());
for (Archive archive : archives) {
// Add the current archive at end (it will be reversed and end up taking
// precedence)
urls.add(archive.getUrl());
}
return createClassLoader(urls.toArray(new URL[urls.size()]));

View File

@ -0,0 +1,63 @@
/*
* Copyright 2012-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.loader;
import java.net.URL;
import org.junit.Test;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
/**
* @author Dave Syer
*/
public class LaunchedURLClassLoaderTests {
@Test
public void resolveResourceFromArchive() throws Exception {
LaunchedURLClassLoader loader = new LaunchedURLClassLoader(new URL[] { new URL(
"jar:file:src/test/resources/jars/app.jar!/") }, getClass()
.getClassLoader());
assertNotNull(loader.getResource("demo/Application.java"));
}
@Test
public void resolveResourcesFromArchive() throws Exception {
LaunchedURLClassLoader loader = new LaunchedURLClassLoader(new URL[] { new URL(
"jar:file:src/test/resources/jars/app.jar!/") }, getClass()
.getClassLoader());
assertTrue(loader.getResources("demo/Application.java").hasMoreElements());
}
@Test
public void resolveRootPathFromArchive() throws Exception {
LaunchedURLClassLoader loader = new LaunchedURLClassLoader(new URL[] { new URL(
"jar:file:src/test/resources/jars/app.jar!/") }, getClass()
.getClassLoader());
assertNotNull(loader.getResource(""));
}
@Test
public void resolveRootResourcesFromArchive() throws Exception {
LaunchedURLClassLoader loader = new LaunchedURLClassLoader(new URL[] { new URL(
"jar:file:src/test/resources/jars/app.jar!/") }, getClass()
.getClassLoader());
assertTrue(loader.getResources("").hasMoreElements());
}
}

View File

@ -126,7 +126,6 @@ public class ExplodedArchiveTests {
}
@Test
@SuppressWarnings("resource")
public void getFilteredArchive() throws Exception {
Archive filteredArchive = this.archive
.getFilteredArchive(new Archive.EntryRenameFilter() {