Be defensive with possibly secured APIs

This commit is contained in:
Dave Syer 2014-03-18 14:13:31 +00:00
parent 38585bf3b6
commit 433f998659
4 changed files with 34 additions and 13 deletions

View File

@ -17,6 +17,7 @@
package org.springframework.boot;
import java.lang.reflect.Constructor;
import java.security.AccessControlException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
@ -281,7 +282,12 @@ public class SpringApplication {
// Create, load, refresh and run the ApplicationContext
context = createApplicationContext();
if (this.registerShutdownHook) {
context.registerShutdownHook();
try {
context.registerShutdownHook();
}
catch (AccessControlException e) {
// Not allowed in some environments.
}
}
context.setEnvironment(environment);
postProcessApplicationContext(context);

View File

@ -89,7 +89,12 @@ class StartupInfoLogger {
message.append(" in ");
message.append(stopWatch.getTotalTimeSeconds());
message.append(" seconds (JVM running for ");
message.append(ManagementFactory.getRuntimeMXBean().getUptime() / 1000.0);
try {
message.append(ManagementFactory.getRuntimeMXBean().getUptime() / 1000.0);
}
catch (Throwable e) {
message.append("?");
}
message.append(")");
return message;
}

View File

@ -191,9 +191,13 @@ public class LoggingApplicationListener implements SmartApplicationListener {
}
private String getPid() {
String name = ManagementFactory.getRuntimeMXBean().getName();
if (name != null) {
return name.split("@")[0];
try {
String name = ManagementFactory.getRuntimeMXBean().getName();
if (name != null) {
return name.split("@")[0];
}
}
catch (Throwable e) {
}
return "????";
}

View File

@ -66,15 +66,21 @@ public class LogbackLoggingSystem extends AbstractLoggingSystem {
@Override
public void beforeInitialize() {
super.beforeInitialize();
if (ClassUtils.isPresent("org.slf4j.bridge.SLF4JBridgeHandler", getClassLoader())) {
try {
SLF4JBridgeHandler.removeHandlersForRootLogger();
try {
if (ClassUtils.isPresent("org.slf4j.bridge.SLF4JBridgeHandler",
getClassLoader())) {
try {
SLF4JBridgeHandler.removeHandlersForRootLogger();
}
catch (NoSuchMethodError ex) {
// Method missing in older versions of SLF4J like in JBoss AS 7.1
SLF4JBridgeHandler.uninstall();
}
SLF4JBridgeHandler.install();
}
catch (NoSuchMethodError ex) {
// Method missing in older versions of SLF4J like in JBoss AS 7.1
SLF4JBridgeHandler.uninstall();
}
SLF4JBridgeHandler.install();
}
catch (Throwable e) {
// Ignore. No java.util.logging bridge is installed.
}
}