Merge branch '1.1.x'

pull/1433/head
Dave Syer 10 years ago
commit 399238384f

@ -456,7 +456,7 @@ public class PropertiesLauncher extends Launcher {
String root = cleanupPath(stripFileUrlPrefix(path)); String root = cleanupPath(stripFileUrlPrefix(path));
List<Archive> lib = new ArrayList<Archive>(); List<Archive> lib = new ArrayList<Archive>();
File file = new File(root); File file = new File(root);
if (!root.startsWith("/")) { if (!isAbsolutePath(root)) {
file = new File(this.home, root); file = new File(this.home, root);
} }
if (file.isDirectory()) { if (file.isDirectory()) {
@ -479,6 +479,11 @@ public class PropertiesLauncher extends Launcher {
return lib; return lib;
} }
private boolean isAbsolutePath(String root) {
// Windows contains ":" others start with "/"
return root.contains(":") || root.startsWith("/");
}
private Archive getArchive(File file) throws IOException { private Archive getArchive(File file) throws IOException {
String name = file.getName().toLowerCase(); String name = file.getName().toLowerCase();
if (name.endsWith(".jar") || name.endsWith(".zip")) { if (name.endsWith(".jar") || name.endsWith(".zip")) {
@ -534,6 +539,10 @@ public class PropertiesLauncher extends Launcher {
private String cleanupPath(String path) { private String cleanupPath(String path) {
path = path.trim(); path = path.trim();
// No need for current dir path
if (path.startsWith("./")) {
path = path.substring(2);
}
if (path.toLowerCase().endsWith(".jar") || path.toLowerCase().endsWith(".zip")) { if (path.toLowerCase().endsWith(".jar") || path.toLowerCase().endsWith(".zip")) {
return path; return path;
} }
@ -542,14 +551,10 @@ public class PropertiesLauncher extends Launcher {
} }
else { else {
// It's a directory // It's a directory
if (!path.endsWith("/")) { if (!path.endsWith("/") && !path.equals(".")) {
path = path + "/"; path = path + "/";
} }
} }
// No need for current dir path
if (path.startsWith("./")) {
path = path.substring(2);
}
return path; return path;
} }
@ -593,8 +598,7 @@ public class PropertiesLauncher extends Launcher {
@Override @Override
public boolean matches(Entry entry) { public boolean matches(Entry entry) {
return entry.isDirectory() || entry.getName().endsWith(DOT_JAR) return entry.getName().endsWith(DOT_JAR) || entry.getName().endsWith(DOT_ZIP);
|| entry.getName().endsWith(DOT_ZIP);
} }
} }

@ -56,7 +56,7 @@ public class ExplodedArchive extends Archive {
private Manifest manifest; private Manifest manifest;
private boolean recursive = true; private boolean filtered = false;
/** /**
* Create a new {@link ExplodedArchive} instance. * Create a new {@link ExplodedArchive} instance.
@ -78,17 +78,18 @@ public class ExplodedArchive extends Archive {
throw new IllegalArgumentException("Invalid source folder " + root); throw new IllegalArgumentException("Invalid source folder " + root);
} }
this.root = root; this.root = root;
this.recursive = recursive; buildEntries(root, recursive);
buildEntries(root);
this.entries = Collections.unmodifiableMap(this.entries); this.entries = Collections.unmodifiableMap(this.entries);
} }
private ExplodedArchive(File root, Map<AsciiBytes, Entry> entries) { private ExplodedArchive(File root, Map<AsciiBytes, Entry> entries) {
this.root = root; this.root = root;
// The entries are pre-filtered
this.filtered = true;
this.entries = Collections.unmodifiableMap(entries); this.entries = Collections.unmodifiableMap(entries);
} }
private void buildEntries(File file) { private void buildEntries(File file, boolean recursive) {
if (!file.equals(this.root)) { if (!file.equals(this.root)) {
String name = file.toURI().getPath() String name = file.toURI().getPath()
.substring(this.root.toURI().getPath().length()); .substring(this.root.toURI().getPath().length());
@ -102,9 +103,9 @@ public class ExplodedArchive extends Archive {
} }
for (File child : files) { for (File child : files) {
if (!SKIPPED_NAMES.contains(child.getName())) { if (!SKIPPED_NAMES.contains(child.getName())) {
if (file.equals(this.root) || this.recursive if (file.equals(this.root) || recursive
|| file.getName().equals("META-INF")) { || file.getName().equals("META-INF")) {
buildEntries(child); buildEntries(child, recursive);
} }
} }
} }
@ -113,7 +114,8 @@ public class ExplodedArchive extends Archive {
@Override @Override
public URL getUrl() throws MalformedURLException { public URL getUrl() throws MalformedURLException {
FilteredURLStreamHandler handler = new FilteredURLStreamHandler(); FilteredURLStreamHandler handler = this.filtered ? new FilteredURLStreamHandler()
: null;
return new URL("file", "", -1, this.root.toURI().getPath(), handler); return new URL("file", "", -1, this.root.toURI().getPath(), handler);
} }

@ -112,6 +112,17 @@ public class PropertiesLauncherTests {
waitFor("Hello World"); waitFor("Hello World");
} }
@Test
public void testUserSpecifiedJarPathWithDot() throws Exception {
System.setProperty("loader.path", "./jars/app.jar");
System.setProperty("loader.main", "demo.Application");
PropertiesLauncher launcher = new PropertiesLauncher();
assertEquals("[jars/app.jar]", ReflectionTestUtils.getField(launcher, "paths")
.toString());
launcher.launch(new String[0]);
waitFor("Hello World");
}
@Test @Test
public void testUserSpecifiedClassLoader() throws Exception { public void testUserSpecifiedClassLoader() throws Exception {
System.setProperty("loader.path", "jars/app.jar"); System.setProperty("loader.path", "jars/app.jar");

@ -147,6 +147,7 @@ public class ExplodedArchiveTests {
new URL[] { filteredArchive.getUrl() }); new URL[] { filteredArchive.getUrl() });
assertThat(classLoader.getResourceAsStream("1.dat").read(), equalTo(1)); assertThat(classLoader.getResourceAsStream("1.dat").read(), equalTo(1));
assertThat(classLoader.getResourceAsStream("2.dat"), nullValue()); assertThat(classLoader.getResourceAsStream("2.dat"), nullValue());
classLoader.close();
} }
@Test @Test
@ -173,6 +174,25 @@ public class ExplodedArchiveTests {
assertThat(entries.size(), equalTo(3)); assertThat(entries.size(), equalTo(3));
} }
@Test
public void getResourceAsStream() throws Exception {
ExplodedArchive archive = new ExplodedArchive(new File("src/test/resources/root"));
assertNotNull(archive.getManifest());
URLClassLoader loader = new URLClassLoader(new URL[] { archive.getUrl() });
assertNotNull(loader.getResourceAsStream("META-INF/spring/application.xml"));
loader.close();
}
@Test
public void getResourceAsStreamNonRecursive() throws Exception {
ExplodedArchive archive = new ExplodedArchive(
new File("src/test/resources/root"), false);
assertNotNull(archive.getManifest());
URLClassLoader loader = new URLClassLoader(new URL[] { archive.getUrl() });
assertNotNull(loader.getResourceAsStream("META-INF/spring/application.xml"));
loader.close();
}
private Map<String, Archive.Entry> getEntriesMap(Archive archive) { private Map<String, Archive.Entry> getEntriesMap(Archive archive) {
Map<String, Archive.Entry> entries = new HashMap<String, Archive.Entry>(); Map<String, Archive.Entry> entries = new HashMap<String, Archive.Entry>();
for (Archive.Entry entry : archive.getEntries()) { for (Archive.Entry entry : archive.getEntries()) {

@ -97,6 +97,7 @@ public class JarFileTests {
assertThat(urlClassLoader.getResource("special/\u00EB.dat"), notNullValue()); assertThat(urlClassLoader.getResource("special/\u00EB.dat"), notNullValue());
assertThat(urlClassLoader.getResource("d/9.dat"), notNullValue()); assertThat(urlClassLoader.getResource("d/9.dat"), notNullValue());
jarFile.close(); jarFile.close();
urlClassLoader.close();
} }
@Test @Test
@ -139,6 +140,7 @@ public class JarFileTests {
URLClassLoader urlClassLoader = new URLClassLoader( URLClassLoader urlClassLoader = new URLClassLoader(
new URL[] { this.jarFile.getUrl() }); new URL[] { this.jarFile.getUrl() });
assertThat(urlClassLoader.getResource("special/\u00EB.dat"), notNullValue()); assertThat(urlClassLoader.getResource("special/\u00EB.dat"), notNullValue());
urlClassLoader.close();
} }
@Test @Test

Loading…
Cancel
Save