Skip to content

Commit

Permalink
Special case for root of classpath resource in archive
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Dave Syer committed Mar 6, 2014
1 parent afac085 commit 8168e8a
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
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;
Expand Down Expand Up @@ -63,6 +65,28 @@ public URL getResource(String name) {
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 {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ protected void launch(String[] args) {
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()]));
Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ public void nestedDirArchive() throws Exception {
}

@Test
@SuppressWarnings("resource")
public void getFilteredArchive() throws Exception {
Archive filteredArchive = this.archive
.getFilteredArchive(new Archive.EntryRenameFilter() {
Expand Down

0 comments on commit 8168e8a

Please sign in to comment.