diff --git a/spring-boot-tools/spring-boot-loader/README.md b/spring-boot-tools/spring-boot-loader/README.md index 9b72bcd7ace..b0ae8d3533d 100644 --- a/spring-boot-tools/spring-boot-loader/README.md +++ b/spring-boot-tools/spring-boot-loader/README.md @@ -162,25 +162,6 @@ $ java org.springframework.boot.loader.JarLauncher There are a number of restrictions that you need to consider when working with a Spring Boot Loader packaged application. -### URLs -URLs for nested jar entries intentionally look and behave like standard jar URLs, -You cannot, however, directly create a nested jar URL from a string: - -``` -URL url = classLoader.getResoure("/a/b.txt"); -String s = url.toString(); // In the form 'jar:file:/file.jar!/nested.jar!/a/b.txt' -new URL(s); // This will fail -``` - -If you need to obtain URL using a String, ensure that you always provide a context URL -to the constructor. This will ensure that the custom `URLStreamHandler` used to support -nested jars is used. - -``` -URL url = classLoader.getResoure("/a"); -new URL(url, "b.txt"); -``` - ### Zip entry compression The `ZipEntry` for a nested jar must be saved using the `ZipEntry.STORED` method. This is required so that we can seek directly to individual content within the nested jar. diff --git a/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/Launcher.java b/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/Launcher.java index 7d38a476fca..6ad66cff8bf 100644 --- a/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/Launcher.java +++ b/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/Launcher.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2013 the original author or authors. + * Copyright 2012-2014 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. @@ -23,6 +23,7 @@ import java.util.List; import java.util.logging.Logger; import org.springframework.boot.loader.archive.Archive; +import org.springframework.boot.loader.jar.JarFile; /** * Base class for launchers that can start an application with a fully configured @@ -49,6 +50,7 @@ public abstract class Launcher { */ protected void launch(String[] args) { try { + JarFile.registerUrlProtocolHandler(); ClassLoader classLoader = createClassLoader(getClassPathArchives()); launch(args, getMainClass(), classLoader); } diff --git a/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/Handler.java b/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/Handler.java new file mode 100644 index 00000000000..a08d9aadf1b --- /dev/null +++ b/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/Handler.java @@ -0,0 +1,93 @@ +/* + * Copyright 2012-2014 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.jar; + +import java.io.File; +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URISyntaxException; +import java.net.URL; +import java.net.URLConnection; +import java.net.URLStreamHandler; + +/** + * {@link URLStreamHandler} for Spring Boot loader {@link JarFile}s. + * + * @author Phillip Webb + * @see JarFile#registerUrlProtocolHandler() + */ +public class Handler extends URLStreamHandler { + + // NOTE: in order to be found as a URL protocol hander, this class must be public, + // must be named Handler and must be in a package ending '.jar' + + private static final String SEPARATOR = JarURLConnection.SEPARATOR; + + private final JarFile jarFile; + + public Handler() { + this(null); + } + + public Handler(JarFile jarFile) { + this.jarFile = jarFile; + } + + @Override + protected URLConnection openConnection(URL url) throws IOException { + JarFile jarFile = (this.jarFile != null ? this.jarFile : getJarFileFromUrl(url)); + return new JarURLConnection(url, jarFile); + } + + public JarFile getJarFileFromUrl(URL url) throws IOException { + + String spec = url.getFile(); + + int separatorIndex = spec.indexOf(SEPARATOR); + if (separatorIndex == -1) { + throw new MalformedURLException("Jar URL does not contain !/ separator"); + } + + JarFile jar = null; + while (separatorIndex != -1) { + String name = spec.substring(0, separatorIndex); + jar = (jar == null ? getRootJarFile(name) : getNestedJarFile(jar, name)); + spec = spec.substring(separatorIndex + SEPARATOR.length()); + separatorIndex = spec.indexOf(SEPARATOR); + } + + return jar; + } + + private JarFile getRootJarFile(String name) throws IOException { + try { + return new JarFile(new File(new URL(name).toURI())); + } + catch (URISyntaxException ex) { + throw new IOException("Unable to open root Jar file '" + name + "'", ex); + } + } + + private JarFile getNestedJarFile(JarFile jarFile, String name) throws IOException { + JarEntry jarEntry = jarFile.getJarEntry(name); + if (jarEntry == null) { + throw new IOException("Unable to find nested jar '" + name + "' from '" + + jarFile + "'"); + } + return jarFile.getNestedJarFile(jarEntry); + } +} diff --git a/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/JarEntry.java b/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/JarEntry.java index f8145c412aa..239d504f7f6 100644 --- a/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/JarEntry.java +++ b/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/JarEntry.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2013 the original author or authors. + * Copyright 2012-2014 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. @@ -17,6 +17,8 @@ package org.springframework.boot.loader.jar; import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; import java.security.CodeSigner; import java.security.cert.Certificate; import java.util.jar.Attributes; @@ -47,6 +49,13 @@ public class JarEntry extends java.util.jar.JarEntry { return this.source; } + /** + * Return a {@link URL} for this {@link JarEntry}. + */ + public URL getUrl() throws MalformedURLException { + return new URL(this.source.getSource().getUrl(), getName()); + } + @Override public Attributes getAttributes() throws IOException { Manifest manifest = this.source.getSource().getManifest(); diff --git a/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/JarFile.java b/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/JarFile.java index fec690ba445..3953235fa53 100644 --- a/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/JarFile.java +++ b/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/JarFile.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2013 the original author or authors. + * Copyright 2012-2014 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. @@ -22,6 +22,8 @@ import java.io.InputStream; import java.lang.ref.SoftReference; import java.net.MalformedURLException; import java.net.URL; +import java.net.URLStreamHandler; +import java.net.URLStreamHandlerFactory; import java.util.ArrayList; import java.util.Enumeration; import java.util.HashMap; @@ -63,6 +65,10 @@ public class JarFile extends java.util.jar.JarFile implements Iterable