Create ApplicationHome class
Extract useful logic from StartupInfoLogger to a new ApplicationHome class. Fixes gh-1414
This commit is contained in:
parent
91e493fd0a
commit
bd4eda383f
|
|
@ -0,0 +1,118 @@
|
||||||
|
/*
|
||||||
|
* 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;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.JarURLConnection;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.net.URLConnection;
|
||||||
|
import java.security.CodeSource;
|
||||||
|
import java.security.ProtectionDomain;
|
||||||
|
|
||||||
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Provides access to the application home directory. Attempts to pick a sensible home for
|
||||||
|
* both Jar Files, Exploded Archives and directly running applications.
|
||||||
|
*
|
||||||
|
* @author Phillip Webb
|
||||||
|
* @since 1.1.2
|
||||||
|
*/
|
||||||
|
public class ApplicationHome {
|
||||||
|
|
||||||
|
private final File source;
|
||||||
|
|
||||||
|
private final File dir;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new {@link ApplicationHome} instance.
|
||||||
|
*/
|
||||||
|
public ApplicationHome() {
|
||||||
|
this(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new {@link ApplicationHome} instance for the specified source class.
|
||||||
|
* @param sourceClass the source class or {@code null}
|
||||||
|
*/
|
||||||
|
public ApplicationHome(Class<?> sourceClass) {
|
||||||
|
this.source = findSource(sourceClass == null ? getClass() : sourceClass);
|
||||||
|
this.dir = findHomeDir(this.source);
|
||||||
|
}
|
||||||
|
|
||||||
|
private File findSource(Class<?> sourceClass) {
|
||||||
|
try {
|
||||||
|
ProtectionDomain protectionDomain = sourceClass.getProtectionDomain();
|
||||||
|
CodeSource codeSource = protectionDomain.getCodeSource();
|
||||||
|
URL location = (codeSource == null ? null : codeSource.getLocation());
|
||||||
|
File source = (location == null ? null : findSource(location));
|
||||||
|
if (source != null && source.exists()) {
|
||||||
|
return source.getAbsoluteFile();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex) {
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private File findSource(URL location) throws IOException {
|
||||||
|
URLConnection connection = location.openConnection();
|
||||||
|
if (connection instanceof JarURLConnection) {
|
||||||
|
return new File(((JarURLConnection) connection).getJarFile().getName());
|
||||||
|
}
|
||||||
|
return new File(location.getPath());
|
||||||
|
}
|
||||||
|
|
||||||
|
private File findHomeDir(File source) {
|
||||||
|
File homeDir = source;
|
||||||
|
homeDir = (homeDir == null ? findDefaultHomeDir() : homeDir);
|
||||||
|
if (homeDir.isFile()) {
|
||||||
|
homeDir = homeDir.getParentFile();
|
||||||
|
}
|
||||||
|
homeDir = (homeDir.exists() ? homeDir : new File("."));
|
||||||
|
return homeDir.getAbsoluteFile();
|
||||||
|
}
|
||||||
|
|
||||||
|
private File findDefaultHomeDir() {
|
||||||
|
String userDir = System.getProperty("user.dir");
|
||||||
|
return new File(StringUtils.hasLength(userDir) ? userDir : ".");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the underlying source used to find the home folder. This is usually the jar
|
||||||
|
* file or a directory. Can return {@code null} if the source cannot be determined.
|
||||||
|
* @return the underlying source or {@code null}
|
||||||
|
*/
|
||||||
|
public File getSource() {
|
||||||
|
return this.source;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the application home folder.
|
||||||
|
* @return the home folder (never {@code null})
|
||||||
|
*/
|
||||||
|
public File getDir() {
|
||||||
|
return this.dir;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return getDir().toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -16,13 +16,8 @@
|
||||||
|
|
||||||
package org.springframework.boot;
|
package org.springframework.boot;
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.lang.management.ManagementFactory;
|
import java.lang.management.ManagementFactory;
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
import java.net.JarURLConnection;
|
|
||||||
import java.net.URL;
|
|
||||||
import java.net.URLConnection;
|
|
||||||
import java.security.ProtectionDomain;
|
|
||||||
import java.util.concurrent.Callable;
|
import java.util.concurrent.Callable;
|
||||||
|
|
||||||
import org.apache.commons.logging.Log;
|
import org.apache.commons.logging.Log;
|
||||||
|
|
@ -143,10 +138,9 @@ class StartupInfoLogger {
|
||||||
return System.getProperty("user.dir");
|
return System.getProperty("user.dir");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
File codeSourceLocation = getCodeSourceLocation();
|
ApplicationHome home = new ApplicationHome(this.sourceClass);
|
||||||
String path = (codeSourceLocation == null ? "" : codeSourceLocation
|
String path = (home.getSource() == null ? "" : home.getSource().getAbsolutePath());
|
||||||
.getAbsolutePath());
|
if (startedBy == null && path == null) {
|
||||||
if (startedBy == null && codeSourceLocation == null) {
|
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
if (StringUtils.hasLength(startedBy) && StringUtils.hasLength(path)) {
|
if (StringUtils.hasLength(startedBy) && StringUtils.hasLength(path)) {
|
||||||
|
|
@ -158,28 +152,6 @@ class StartupInfoLogger {
|
||||||
return " (" + path + startedBy + in + ")";
|
return " (" + path + startedBy + in + ")";
|
||||||
}
|
}
|
||||||
|
|
||||||
private File getCodeSourceLocation() {
|
|
||||||
try {
|
|
||||||
ProtectionDomain protectionDomain = (this.sourceClass == null ? getClass()
|
|
||||||
: this.sourceClass).getProtectionDomain();
|
|
||||||
URL location = protectionDomain.getCodeSource().getLocation();
|
|
||||||
File file;
|
|
||||||
URLConnection connection = location.openConnection();
|
|
||||||
if (connection instanceof JarURLConnection) {
|
|
||||||
file = new File(((JarURLConnection) connection).getJarFile().getName());
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
file = new File(location.getPath());
|
|
||||||
}
|
|
||||||
if (file.exists()) {
|
|
||||||
return file;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (Exception ex) {
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
private String getValue(String prefix, Callable<Object> call) {
|
private String getValue(String prefix, Callable<Object> call) {
|
||||||
return getValue(prefix, call, "");
|
return getValue(prefix, call, "");
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue