Protect against null CodeSource location

Update LogbackLoggingSystem to protect against a potential null
CodeSource result.

Fixes gh-2149
This commit is contained in:
Phillip Webb 2014-12-14 13:43:00 -08:00
parent c22018aaa1
commit 1ef77d7d97
1 changed files with 17 additions and 2 deletions

View File

@ -17,6 +17,8 @@
package org.springframework.boot.logging.logback; package org.springframework.boot.logging.logback;
import java.net.URL; import java.net.URL;
import java.security.CodeSource;
import java.security.ProtectionDomain;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -107,8 +109,7 @@ public class LogbackLoggingSystem extends AbstractLoggingSystem {
"LoggerFactory is not a Logback LoggerContext but Logback is on " "LoggerFactory is not a Logback LoggerContext but Logback is on "
+ "the classpath. Either remove Logback or the competing " + "the classpath. Either remove Logback or the competing "
+ "implementation (%s loaded from %s).", + "implementation (%s loaded from %s).",
factory.getClass(), factory.getClass().getProtectionDomain() factory.getClass(), getLocation(factory)));
.getCodeSource().getLocation()));
LoggerContext context = (LoggerContext) factory; LoggerContext context = (LoggerContext) factory;
context.stop(); 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 @Override
public void setLogLevel(String loggerName, LogLevel level) { public void setLogLevel(String loggerName, LogLevel level) {
ILoggerFactory factory = StaticLoggerBinder.getSingleton().getLoggerFactory(); ILoggerFactory factory = StaticLoggerBinder.getSingleton().getLoggerFactory();