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:
parent
afac085318
commit
8168e8a327
|
|
@ -21,6 +21,8 @@ import java.net.URL;
|
||||||
import java.net.URLClassLoader;
|
import java.net.URLClassLoader;
|
||||||
import java.security.AccessController;
|
import java.security.AccessController;
|
||||||
import java.security.PrivilegedExceptionAction;
|
import java.security.PrivilegedExceptionAction;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.Enumeration;
|
import java.util.Enumeration;
|
||||||
|
|
||||||
import org.springframework.boot.loader.jar.JarFile;
|
import org.springframework.boot.loader.jar.JarFile;
|
||||||
|
|
@ -63,6 +65,28 @@ public class LaunchedURLClassLoader extends URLClassLoader {
|
||||||
return (url == null ? findResource(name) : url);
|
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
|
@Override
|
||||||
public Enumeration<URL> getResources(String name) throws IOException {
|
public Enumeration<URL> getResources(String name) throws IOException {
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -75,6 +75,8 @@ public abstract class Launcher {
|
||||||
protected ClassLoader createClassLoader(List<Archive> archives) throws Exception {
|
protected ClassLoader createClassLoader(List<Archive> archives) throws Exception {
|
||||||
List<URL> urls = new ArrayList<URL>(archives.size());
|
List<URL> urls = new ArrayList<URL>(archives.size());
|
||||||
for (Archive archive : archives) {
|
for (Archive archive : archives) {
|
||||||
|
// Add the current archive at end (it will be reversed and end up taking
|
||||||
|
// precedence)
|
||||||
urls.add(archive.getUrl());
|
urls.add(archive.getUrl());
|
||||||
}
|
}
|
||||||
return createClassLoader(urls.toArray(new URL[urls.size()]));
|
return createClassLoader(urls.toArray(new URL[urls.size()]));
|
||||||
|
|
|
||||||
|
|
@ -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());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -126,7 +126,6 @@ public class ExplodedArchiveTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@SuppressWarnings("resource")
|
|
||||||
public void getFilteredArchive() throws Exception {
|
public void getFilteredArchive() throws Exception {
|
||||||
Archive filteredArchive = this.archive
|
Archive filteredArchive = this.archive
|
||||||
.getFilteredArchive(new Archive.EntryRenameFilter() {
|
.getFilteredArchive(new Archive.EntryRenameFilter() {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue