From c22018aaa12b0113ff85e07f613e50535e00e357 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Sun, 14 Dec 2014 13:40:46 -0800 Subject: [PATCH 1/2] Polish --- .../boot/StartupInfoLogger.java | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/spring-boot/src/main/java/org/springframework/boot/StartupInfoLogger.java b/spring-boot/src/main/java/org/springframework/boot/StartupInfoLogger.java index 01057bd6f7..85f1253a52 100644 --- a/spring-boot/src/main/java/org/springframework/boot/StartupInfoLogger.java +++ b/spring-boot/src/main/java/org/springframework/boot/StartupInfoLogger.java @@ -17,11 +17,13 @@ package org.springframework.boot; import java.io.File; +import java.io.IOException; import java.lang.management.ManagementFactory; import java.net.InetAddress; import java.net.JarURLConnection; import java.net.URL; import java.net.URLConnection; +import java.security.CodeSource; import java.security.ProtectionDomain; import java.util.concurrent.Callable; @@ -162,15 +164,13 @@ class StartupInfoLogger { try { ProtectionDomain protectionDomain = (this.sourceClass == null ? getClass() : this.sourceClass).getProtectionDomain(); + CodeSource codeSource = protectionDomain.getCodeSource(); + if (codeSource == null) { + return null; + } 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()); - } + File file = getFile(location, connection); if (file.exists()) { return file; } @@ -180,6 +180,13 @@ class StartupInfoLogger { return null; } + private File getFile(URL location, URLConnection connection) throws IOException { + if (connection instanceof JarURLConnection) { + return new File(((JarURLConnection) connection).getJarFile().getName()); + } + return new File(location.getPath()); + } + private String getValue(String prefix, Callable call) { return getValue(prefix, call, ""); } From 1ef77d7d97a75b042f610967ae876518ea94413f Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Sun, 14 Dec 2014 13:43:00 -0800 Subject: [PATCH 2/2] Protect against null CodeSource location Update LogbackLoggingSystem to protect against a potential null CodeSource result. Fixes gh-2149 --- .../logging/logback/LogbackLoggingSystem.java | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/spring-boot/src/main/java/org/springframework/boot/logging/logback/LogbackLoggingSystem.java b/spring-boot/src/main/java/org/springframework/boot/logging/logback/LogbackLoggingSystem.java index 85699bef30..d6338397ea 100644 --- a/spring-boot/src/main/java/org/springframework/boot/logging/logback/LogbackLoggingSystem.java +++ b/spring-boot/src/main/java/org/springframework/boot/logging/logback/LogbackLoggingSystem.java @@ -17,6 +17,8 @@ package org.springframework.boot.logging.logback; import java.net.URL; +import java.security.CodeSource; +import java.security.ProtectionDomain; import java.util.Collections; import java.util.HashMap; import java.util.Map; @@ -107,8 +109,7 @@ public class LogbackLoggingSystem extends AbstractLoggingSystem { "LoggerFactory is not a Logback LoggerContext but Logback is on " + "the classpath. Either remove Logback or the competing " + "implementation (%s loaded from %s).", - factory.getClass(), factory.getClass().getProtectionDomain() - .getCodeSource().getLocation())); + factory.getClass(), getLocation(factory))); LoggerContext context = (LoggerContext) factory; context.stop(); @@ -123,6 +124,20 @@ public class LogbackLoggingSystem extends AbstractLoggingSystem { } } + private Object getLocation(ILoggerFactory factory) { + try { + ProtectionDomain protectionDomain = factory.getClass().getProtectionDomain(); + CodeSource codeSource = protectionDomain.getCodeSource(); + if (codeSource != null) { + return codeSource.getLocation(); + } + } + catch (SecurityException ex) { + // Unable to determine location + } + return "unknown location"; + } + @Override public void setLogLevel(String loggerName, LogLevel level) { ILoggerFactory factory = StaticLoggerBinder.getSingleton().getLoggerFactory();