Allow PropertyLauncher loader.path to be configured using manifest

Closes gh-7178
This commit is contained in:
Andy Wilkinson
2016-10-24 22:14:56 +01:00
parent a638dcd51b
commit ee7141cf63
4 changed files with 68 additions and 21 deletions

View File

@@ -132,7 +132,7 @@ public class PropertiesLauncher extends Launcher {
public PropertiesLauncher() {
try {
this.home = getHomeDirectory();
initializeProperties(this.home);
initializeProperties();
initializePaths();
this.parent = createArchive();
}
@@ -146,7 +146,7 @@ public class PropertiesLauncher extends Launcher {
.resolvePlaceholders(System.getProperty(HOME, "${user.dir}")));
}
private void initializeProperties(File home) throws Exception, IOException {
private void initializeProperties() throws Exception, IOException {
String config = "classpath:BOOT-INF/classes/"
+ SystemPropertyUtils.resolvePlaceholders(
SystemPropertyUtils.getProperty(CONFIG_NAME, "application"))
@@ -273,14 +273,10 @@ public class PropertiesLauncher extends Launcher {
}
}
private void initializePaths() throws IOException {
String path = SystemPropertyUtils.getProperty(PATH);
if (path == null) {
path = this.properties.getProperty(PATH);
}
private void initializePaths() throws Exception {
String path = getProperty(PATH);
if (path != null) {
this.paths = parsePathsProperty(
SystemPropertyUtils.resolvePlaceholders(path));
this.paths = parsePathsProperty(path);
}
log("Nested archive paths: " + this.paths);
}

View File

@@ -17,16 +17,21 @@
package org.springframework.boot.loader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.jar.Attributes;
import java.util.jar.Manifest;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.mockito.MockitoAnnotations;
import org.springframework.boot.loader.archive.Archive;
@@ -45,6 +50,9 @@ public class PropertiesLauncherTests {
@Rule
public InternalOutputCapture output = new InternalOutputCapture();
@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();
@Before
public void setup() throws IOException {
MockitoAnnotations.initMocks(this);
@@ -207,6 +215,23 @@ public class PropertiesLauncherTests {
.isEqualTo("[foo, bar]");
}
@SuppressWarnings("unchecked")
@Test
public void testLoadPathCustomizedUsingManifest() throws Exception {
System.setProperty("loader.home",
this.temporaryFolder.getRoot().getAbsolutePath());
Manifest manifest = new Manifest();
manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
manifest.getMainAttributes().putValue("Loader-Path", "/foo.jar, /bar");
File manifestFile = new File(this.temporaryFolder.getRoot(),
"META-INF/MANIFEST.MF");
manifestFile.getParentFile().mkdirs();
manifest.write(new FileOutputStream(manifestFile));
PropertiesLauncher launcher = new PropertiesLauncher();
assertThat((List<String>) ReflectionTestUtils.getField(launcher, "paths"))
.containsExactly("/foo.jar", "/bar/");
}
private void waitFor(String value) throws Exception {
int count = 0;
boolean timeout = false;

View File

@@ -1,2 +1 @@
loader.main: demo.Application
loader.path: etc/,lib,.