From 28dad44e2d81b69b818c9ec1b7829213132a00a8 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Mon, 24 Jul 2017 12:36:34 -0700 Subject: [PATCH] Be defensive about JUL calls from JAR Handler Update nested JAR support to only obtain JUL loggers when absolutely necessary and to defensively deal with failures. Prior to this commit it was not possible to override `java.util.logging.manager` to use a nested JAR as the logger implementation. Fixes gh-9848 --- .../boot/loader/jar/Handler.java | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/Handler.java b/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/Handler.java index 949504b4ff..4a64223cc0 100644 --- a/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/Handler.java +++ b/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/Handler.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2016 the original author or authors. + * Copyright 2012-2017 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. @@ -71,8 +71,6 @@ public class Handler extends URLStreamHandler { rootFileCache = new SoftReference>(null); } - private final Logger logger = Logger.getLogger(getClass().getName()); - private final JarFile jarFile; private URLStreamHandler fallbackHandler; @@ -105,10 +103,10 @@ public class Handler extends URLStreamHandler { } catch (Exception ex) { if (reason instanceof IOException) { - this.logger.log(Level.FINEST, "Unable to open fallback handler", ex); + log(false, "Unable to open fallback handler", ex); throw (IOException) reason; } - this.logger.log(Level.WARNING, "Unable to open fallback handler", ex); + log(true, "Unable to open fallback handler", ex); if (reason instanceof RuntimeException) { throw (RuntimeException) reason; } @@ -116,6 +114,18 @@ public class Handler extends URLStreamHandler { } } + private void log(boolean warning, String message, Exception cause) { + try { + Logger.getLogger(getClass().getName()) + .log((warning ? Level.WARNING : Level.FINEST), message, cause); + } + catch (Exception ex) { + if (warning) { + System.err.println("WARNING: " + message); + } + } + } + private URLStreamHandler getFallbackHandler() { if (this.fallbackHandler != null) { return this.fallbackHandler;