Skip to content

Commit

Permalink
Refine ApplicationHome detection logic
Browse files Browse the repository at this point in the history
Update the detection logic used in ApplicationHome to:
- Deal with `!/` elements in URLs so that `BOOT-INF/classes` packaging
  works as expected.
- Use the `start-class` when no explicit source class is provided to
  prevent accidentally picking a home next to a `spring-boot.jar` that
  happens to be on the classpath.
- Ignore search logic when running from a unit test.

Fixes spring-projectsgh-6129
  • Loading branch information
philwebb committed Jun 10, 2016
1 parent 17dfec7 commit c66da65
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@ overridedb.*
.DS_Store
.factorypath
dump.rdb
transaction-logs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2014 the original author or authors.
* Copyright 2012-2016 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.
Expand All @@ -18,12 +18,17 @@

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.JarURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.security.CodeSource;
import java.security.ProtectionDomain;
import java.util.Enumeration;
import java.util.jar.JarFile;
import java.util.jar.Manifest;

import org.springframework.util.ClassUtils;
import org.springframework.util.StringUtils;

/**
Expand Down Expand Up @@ -51,17 +56,51 @@ public ApplicationHome() {
* @param sourceClass the source class or {@code null}
*/
public ApplicationHome(Class<?> sourceClass) {
this.source = findSource(sourceClass == null ? getClass() : sourceClass);
this.source = findSource(sourceClass == null ? getStartClass() : sourceClass);
this.dir = findHomeDir(this.source);
}

private Class<?> getStartClass() {
try {
ClassLoader classLoader = getClass().getClassLoader();
return getStartClass(classLoader.getResources("META-INF/MANIFEST.MF"));
}
catch (Exception ex) {
return null;
}
}

private Class<?> getStartClass(Enumeration<URL> manifestResources) {
while (manifestResources.hasMoreElements()) {
try {
InputStream inputStream = manifestResources.nextElement().openStream();
try {
Manifest manifest = new Manifest(inputStream);
String startClass = manifest.getMainAttributes()
.getValue("Start-Class");
if (startClass != null) {
return ClassUtils.forName(startClass,
getClass().getClassLoader());
}
}
finally {
inputStream.close();
}
}
catch (Exception ex) {
}
}
return null;
}

private File findSource(Class<?> sourceClass) {
try {
ProtectionDomain protectionDomain = sourceClass.getProtectionDomain();
CodeSource codeSource = protectionDomain.getCodeSource();
ProtectionDomain domain = (sourceClass == null ? null
: sourceClass.getProtectionDomain());
CodeSource codeSource = (domain == null ? null : domain.getCodeSource());
URL location = (codeSource == null ? null : codeSource.getLocation());
File source = (location == null ? null : findSource(location));
if (source != null && source.exists()) {
if (source != null && source.exists() && !isUnitTest()) {
return source.getAbsoluteFile();
}
return null;
Expand All @@ -71,14 +110,36 @@ private File findSource(Class<?> sourceClass) {
}
}

private boolean isUnitTest() {
try {
for (StackTraceElement element : Thread.currentThread().getStackTrace()) {
if (element.getClassName().startsWith("org.junit.")) {
return true;
}
}
}
catch (Exception ex) {
}
return false;
}

private File findSource(URL location) throws IOException {
URLConnection connection = location.openConnection();
if (connection instanceof JarURLConnection) {
return new File(((JarURLConnection) connection).getJarFile().getName());
return getRootJarFile(((JarURLConnection) connection).getJarFile());
}
return new File(location.getPath());
}

private File getRootJarFile(JarFile jarFile) {
String name = jarFile.getName();
int separator = name.indexOf("!/");
if (separator > 0) {
name = name.substring(0, separator);
}
return new File(name);
}

private File findHomeDir(File source) {
File homeDir = source;
homeDir = (homeDir == null ? findDefaultHomeDir() : homeDir);
Expand Down

0 comments on commit c66da65

Please sign in to comment.