From 1ef77d7d97a75b042f610967ae876518ea94413f Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Sun, 14 Dec 2014 13:43:00 -0800 Subject: [PATCH] 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();