8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
8029781: Theoretical data race on java.util.logging.Handler.sealed
Summary: Use privileged actions instead of racy boolean field to elevate privilege when constructing logging handlers
Reviewed-by: mchung, dfuchs
--- a/jdk/src/share/classes/java/util/logging/ConsoleHandler.java Mon Jan 06 13:54:54 2014 -0800
+++ b/jdk/src/share/classes/java/util/logging/ConsoleHandler.java Tue Jan 07 09:54:16 2014 +0100
@@ -66,27 +66,6 @@
* @since 1.4
*/
public class ConsoleHandler extends StreamHandler {
- // Private method to configure a ConsoleHandler from LogManager
- // properties and/or default values as specified in the class
- // javadoc.
- private void configure() {
- LogManager manager = LogManager.getLogManager();
- String cname = getClass().getName();
-
- setLevel(manager.getLevelProperty(cname +".level", Level.INFO));
- setFilter(manager.getFilterProperty(cname +".filter", null));
- setFormatter(manager.getFormatterProperty(cname +".formatter", new SimpleFormatter()));
- try {
- setEncoding(manager.getStringProperty(cname +".encoding", null));
- } catch (Exception ex) {
- try {
- setEncoding(null);
- } catch (Exception ex2) {
- // doing a setEncoding with null should always work.
- // assert false;
- }
- }
- }
/**
* Create a <tt>ConsoleHandler</tt> for <tt>System.err</tt>.
@@ -96,10 +75,10 @@
*
*/
public ConsoleHandler() {
- sealed = false;
- configure();
- setOutputStream(System.err);
- sealed = true;
+ // configure with specific defaults for ConsoleHandler
+ super(Level.INFO, new SimpleFormatter(), null);
+
+ setOutputStreamPrivileged(System.err);
}
/**
--- a/jdk/src/share/classes/java/util/logging/Handler.java Mon Jan 06 13:54:54 2014 -0800
+++ b/jdk/src/share/classes/java/util/logging/Handler.java Tue Jan 07 09:54:16 2014 +0100
@@ -27,6 +27,9 @@
package java.util.logging;
import java.io.UnsupportedEncodingException;
+import java.security.AccessController;
+import java.security.PrivilegedAction;
+
/**
* A <tt>Handler</tt> object takes log messages from a <tt>Logger</tt> and
* exports them. It might for example, write them to a console
@@ -62,10 +65,6 @@
private volatile ErrorManager errorManager = new ErrorManager();
private volatile String encoding;
- // Package private support for security checking. When sealed
- // is true, we access check updates to the class.
- boolean sealed = true;
-
/**
* Default constructor. The resulting <tt>Handler</tt> has a log
* level of <tt>Level.ALL</tt>, no <tt>Formatter</tt>, and no
@@ -76,6 +75,52 @@
}
/**
+ * Package-private constructor for chaining from subclass constructors
+ * that wish to configure the handler with specific default and/or
+ * specified values.
+ *
+ * @param defaultLevel a default {@link Level} to configure if one is
+ * not found in LogManager configuration properties
+ * @param defaultFormatter a default {@link Formatter} to configure if one is
+ * not specified by {@code specifiedFormatter} parameter
+ * nor found in LogManager configuration properties
+ * @param specifiedFormatter if not null, this is the formatter to configure
+ */
+ Handler(Level defaultLevel, Formatter defaultFormatter,
+ Formatter specifiedFormatter) {
+
+ LogManager manager = LogManager.getLogManager();
+ String cname = getClass().getName();
+
+ final Level level = manager.getLevelProperty(cname + ".level", defaultLevel);
+ final Filter filter = manager.getFilterProperty(cname + ".filter", null);
+ final Formatter formatter = specifiedFormatter == null
+ ? manager.getFormatterProperty(cname + ".formatter", defaultFormatter)
+ : specifiedFormatter;
+ final String encoding = manager.getStringProperty(cname + ".encoding", null);
+
+ AccessController.doPrivileged(new PrivilegedAction<Void>() {
+ @Override
+ public Void run() {
+ setLevel(level);
+ setFilter(filter);
+ setFormatter(formatter);
+ try {
+ setEncoding(encoding);
+ } catch (Exception ex) {
+ try {
+ setEncoding(null);
+ } catch (Exception ex2) {
+ // doing a setEncoding with null should always work.
+ // assert false;
+ }
+ }
+ return null;
+ }
+ }, null, LogManager.controlPermission);
+ }
+
+ /**
* Publish a <tt>LogRecord</tt>.
* <p>
* The logging request was made initially to a <tt>Logger</tt> object,
@@ -302,12 +347,9 @@
}
// Package-private support method for security checks.
- // If "sealed" is true, we check that the caller has
- // appropriate security privileges to update Handler
- // state and if not throw a SecurityException.
+ // We check that the caller has appropriate security privileges
+ // to update Handler state and if not throw a SecurityException.
void checkPermission() throws SecurityException {
- if (sealed) {
- manager.checkPermission();
- }
+ manager.checkPermission();
}
}
--- a/jdk/src/share/classes/java/util/logging/LogManager.java Mon Jan 06 13:54:54 2014 -0800
+++ b/jdk/src/share/classes/java/util/logging/LogManager.java Tue Jan 07 09:54:16 2014 +0100
@@ -1449,7 +1449,7 @@
loadLoggerHandlers(rootLogger, null, "handlers");
}
- private final Permission controlPermission = new LoggingPermission("control", null);
+ static final Permission controlPermission = new LoggingPermission("control", null);
void checkPermission() {
SecurityManager sm = System.getSecurityManager();
--- a/jdk/src/share/classes/java/util/logging/MemoryHandler.java Mon Jan 06 13:54:54 2014 -0800
+++ b/jdk/src/share/classes/java/util/logging/MemoryHandler.java Tue Jan 07 09:54:16 2014 +0100
@@ -94,37 +94,24 @@
private LogRecord buffer[];
int start, count;
- // Private method to configure a MemoryHandler from LogManager
- // properties and/or default values as specified in the class
- // javadoc.
- private void configure() {
+ /**
+ * Create a <tt>MemoryHandler</tt> and configure it based on
+ * <tt>LogManager</tt> configuration properties.
+ */
+ public MemoryHandler() {
+ // configure with specific defaults for MemoryHandler
+ super(Level.ALL, new SimpleFormatter(), null);
+
LogManager manager = LogManager.getLogManager();
String cname = getClass().getName();
-
pushLevel = manager.getLevelProperty(cname +".push", Level.SEVERE);
size = manager.getIntProperty(cname + ".size", DEFAULT_SIZE);
if (size <= 0) {
size = DEFAULT_SIZE;
}
- setLevel(manager.getLevelProperty(cname +".level", Level.ALL));
- setFilter(manager.getFilterProperty(cname +".filter", null));
- setFormatter(manager.getFormatterProperty(cname +".formatter", new SimpleFormatter()));
- }
-
- /**
- * Create a <tt>MemoryHandler</tt> and configure it based on
- * <tt>LogManager</tt> configuration properties.
- */
- public MemoryHandler() {
- sealed = false;
- configure();
- sealed = true;
-
- LogManager manager = LogManager.getLogManager();
- String handlerName = getClass().getName();
- String targetName = manager.getProperty(handlerName+".target");
+ String targetName = manager.getProperty(cname+".target");
if (targetName == null) {
- throw new RuntimeException("The handler " + handlerName
+ throw new RuntimeException("The handler " + cname
+ " does not specify a target");
}
Class<?> clz;
@@ -158,15 +145,15 @@
* @throws IllegalArgumentException if {@code size is <= 0}
*/
public MemoryHandler(Handler target, int size, Level pushLevel) {
+ // configure with specific defaults for MemoryHandler
+ super(Level.ALL, new SimpleFormatter(), null);
+
if (target == null || pushLevel == null) {
throw new NullPointerException();
}
if (size <= 0) {
throw new IllegalArgumentException();
}
- sealed = false;
- configure();
- sealed = true;
this.target = target;
this.pushLevel = pushLevel;
this.size = size;
--- a/jdk/src/share/classes/java/util/logging/SocketHandler.java Mon Jan 06 13:54:54 2014 -0800
+++ b/jdk/src/share/classes/java/util/logging/SocketHandler.java Tue Jan 07 09:54:16 2014 +0100
@@ -83,31 +83,6 @@
private String host;
private int port;
- // Private method to configure a SocketHandler from LogManager
- // properties and/or default values as specified in the class
- // javadoc.
- private void configure() {
- LogManager manager = LogManager.getLogManager();
- String cname = getClass().getName();
-
- setLevel(manager.getLevelProperty(cname +".level", Level.ALL));
- setFilter(manager.getFilterProperty(cname +".filter", null));
- setFormatter(manager.getFormatterProperty(cname +".formatter", new XMLFormatter()));
- try {
- setEncoding(manager.getStringProperty(cname +".encoding", null));
- } catch (Exception ex) {
- try {
- setEncoding(null);
- } catch (Exception ex2) {
- // doing a setEncoding with null should always work.
- // assert false;
- }
- }
- port = manager.getIntProperty(cname + ".port", 0);
- host = manager.getStringProperty(cname + ".host", null);
- }
-
-
/**
* Create a <tt>SocketHandler</tt>, using only <tt>LogManager</tt> properties
* (or their defaults).
@@ -117,9 +92,13 @@
* host and port.
*/
public SocketHandler() throws IOException {
- // We are going to use the logging defaults.
- sealed = false;
- configure();
+ // configure with specific defaults for SocketHandler
+ super(Level.ALL, new XMLFormatter(), null);
+
+ LogManager manager = LogManager.getLogManager();
+ String cname = getClass().getName();
+ port = manager.getIntProperty(cname + ".port", 0);
+ host = manager.getStringProperty(cname + ".host", null);
try {
connect();
@@ -127,7 +106,6 @@
System.err.println("SocketHandler: connect failed to " + host + ":" + port);
throw ix;
}
- sealed = true;
}
/**
@@ -146,11 +124,12 @@
* host and port.
*/
public SocketHandler(String host, int port) throws IOException {
- sealed = false;
- configure();
- sealed = true;
+ // configure with specific defaults for SocketHandler
+ super(Level.ALL, new XMLFormatter(), null);
+
this.port = port;
this.host = host;
+
connect();
}
@@ -167,7 +146,7 @@
sock = new Socket(host, port);
OutputStream out = sock.getOutputStream();
BufferedOutputStream bout = new BufferedOutputStream(out);
- setOutputStream(bout);
+ setOutputStreamPrivileged(bout);
}
/**
--- a/jdk/src/share/classes/java/util/logging/StreamHandler.java Mon Jan 06 13:54:54 2014 -0800
+++ b/jdk/src/share/classes/java/util/logging/StreamHandler.java Tue Jan 07 09:54:16 2014 +0100
@@ -27,6 +27,9 @@
package java.util.logging;
import java.io.*;
+import java.security.AccessController;
+import java.security.PrivilegedAction;
+import java.util.Objects;
/**
* Stream based logging <tt>Handler</tt>.
@@ -77,35 +80,12 @@
private boolean doneHeader;
private volatile Writer writer;
- // Private method to configure a StreamHandler from LogManager
- // properties and/or default values as specified in the class
- // javadoc.
- private void configure() {
- LogManager manager = LogManager.getLogManager();
- String cname = getClass().getName();
-
- setLevel(manager.getLevelProperty(cname +".level", Level.INFO));
- setFilter(manager.getFilterProperty(cname +".filter", null));
- setFormatter(manager.getFormatterProperty(cname +".formatter", new SimpleFormatter()));
- try {
- setEncoding(manager.getStringProperty(cname +".encoding", null));
- } catch (Exception ex) {
- try {
- setEncoding(null);
- } catch (Exception ex2) {
- // doing a setEncoding with null should always work.
- // assert false;
- }
- }
- }
-
/**
* Create a <tt>StreamHandler</tt>, with no current output stream.
*/
public StreamHandler() {
- sealed = false;
- configure();
- sealed = true;
+ // configure with specific defaults for StreamHandler
+ super(Level.INFO, new SimpleFormatter(), null);
}
/**
@@ -116,11 +96,19 @@
* @param formatter Formatter to be used to format output
*/
public StreamHandler(OutputStream out, Formatter formatter) {
- sealed = false;
- configure();
- setFormatter(formatter);
- setOutputStream(out);
- sealed = true;
+ // configure with default level but use specified formatter
+ super(Level.INFO, null, Objects.requireNonNull(formatter));
+
+ setOutputStreamPrivileged(out);
+ }
+
+ /**
+ * @see Handler#Handler(Level, Formatter, Formatter)
+ */
+ StreamHandler(Level defaultLevel,
+ Formatter defaultFormatter,
+ Formatter specifiedFormatter) {
+ super(defaultLevel, defaultFormatter, specifiedFormatter);
}
/**
@@ -301,4 +289,16 @@
public synchronized void close() throws SecurityException {
flushAndClose();
}
+
+ // Package-private support for setting OutputStream
+ // with elevated privilege.
+ final void setOutputStreamPrivileged(final OutputStream out) {
+ AccessController.doPrivileged(new PrivilegedAction<Void>() {
+ @Override
+ public Void run() {
+ setOutputStream(out);
+ return null;
+ }
+ }, null, LogManager.controlPermission);
+ }
}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/util/logging/HandlersConfigTest$Configured.props Tue Jan 07 09:54:16 2014 +0100
@@ -0,0 +1,29 @@
+# mandatory Default properties...
+
+.level= INFO
+handlers=HandlersConfigTest$ConfiguredHandler
+
+java.util.logging.MemoryHandler.target=HandlersConfigTest$ConfiguredHandler
+
+# in addition to Default, Configured adds the following...
+
+java.util.logging.MemoryHandler.level=FINE
+java.util.logging.MemoryHandler.filter=HandlersConfigTest$ConfiguredFilter
+java.util.logging.MemoryHandler.formatter=HandlersConfigTest$ConfiguredFormatter
+java.util.logging.MemoryHandler.size=123
+java.util.logging.MemoryHandler.push=FINE
+
+java.util.logging.ConsoleHandler.level=FINE
+java.util.logging.ConsoleHandler.encoding=ASCII
+java.util.logging.ConsoleHandler.filter=HandlersConfigTest$ConfiguredFilter
+java.util.logging.ConsoleHandler.formatter=HandlersConfigTest$ConfiguredFormatter
+
+java.util.logging.StreamHandler.level=FINE
+java.util.logging.StreamHandler.encoding=ASCII
+java.util.logging.StreamHandler.filter=HandlersConfigTest$ConfiguredFilter
+java.util.logging.StreamHandler.formatter=HandlersConfigTest$ConfiguredFormatter
+
+java.util.logging.SocketHandler.level=FINE
+java.util.logging.SocketHandler.encoding=ASCII
+java.util.logging.SocketHandler.filter=HandlersConfigTest$ConfiguredFilter
+java.util.logging.SocketHandler.formatter=HandlersConfigTest$ConfiguredFormatter
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/util/logging/HandlersConfigTest$Default.props Tue Jan 07 09:54:16 2014 +0100
@@ -0,0 +1,6 @@
+# mandatory Default properties...
+
+.level= INFO
+handlers=HandlersConfigTest$ConfiguredHandler
+
+java.util.logging.MemoryHandler.target=HandlersConfigTest$ConfiguredHandler
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/util/logging/HandlersConfigTest.java Tue Jan 07 09:54:16 2014 +0100
@@ -0,0 +1,330 @@
+/*
+ * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * @bug 8029781 8030801
+ * @summary Test which verifies that various JDK logging Handlers are
+ * configured correctly from defaults and/or LogManager properties
+ * as specified in javadoc and that no special
+ * logging permission is required for instantiating them.
+ * @run main/othervm HandlersConfigTest default
+ * @run main/othervm HandlersConfigTest configured
+ */
+
+import java.io.IOException;
+import java.io.OutputStream;
+import java.lang.reflect.Field;
+import java.net.ServerSocket;
+import java.net.URL;
+import java.util.Objects;
+import java.util.logging.ConsoleHandler;
+import java.util.logging.Filter;
+import java.util.logging.Formatter;
+import java.util.logging.Handler;
+import java.util.logging.Level;
+import java.util.logging.LogManager;
+import java.util.logging.LogRecord;
+import java.util.logging.MemoryHandler;
+import java.util.logging.SimpleFormatter;
+import java.util.logging.SocketHandler;
+import java.util.logging.StreamHandler;
+import java.util.logging.XMLFormatter;
+
+public abstract class HandlersConfigTest implements Runnable {
+
+ public static void main(String[] args) {
+ switch (args.length == 1 ? args[0] : "usage") {
+ case "default":
+ new Default().run();
+ break;
+ case "configured":
+ new Configured().run();
+ break;
+ default:
+ System.err.println("Usage: HandlersConfigTest [default|configured]");
+ break;
+ }
+ }
+
+ static final String CONFIG_FILE_PROPERTY = "java.util.logging.config.file";
+ final Field memoryHandlerTarget, memoryHandlerSize, streamHandlerOutput;
+ final ServerSocket serverSocket;
+
+ HandlersConfigTest() {
+ // establish access to private fields
+ try {
+ memoryHandlerTarget = MemoryHandler.class.getDeclaredField("target");
+ memoryHandlerTarget.setAccessible(true);
+ memoryHandlerSize = MemoryHandler.class.getDeclaredField("size");
+ memoryHandlerSize.setAccessible(true);
+ streamHandlerOutput = StreamHandler.class.getDeclaredField("output");
+ streamHandlerOutput.setAccessible(true);
+ } catch (NoSuchFieldException e) {
+ throw new AssertionError(e);
+ }
+
+ // load logging.propertes for the test
+ String rname = getClass().getName().replace('.', '/') + ".props";
+ URL url = getClass().getClassLoader().getResource(rname);
+ if (url == null || !"file".equals(url.getProtocol())) {
+ throw new IllegalStateException("Resource: " + rname + " not found or not on file: " + url);
+ }
+ System.setProperty(CONFIG_FILE_PROPERTY, url.getFile());
+
+ // create ServerSocket as a target for SocketHandler
+ try {
+ serverSocket = new ServerSocket(0); // auto allocated port
+ } catch (IOException e) {
+ throw new AssertionError(e);
+ }
+
+ // activate security
+ System.setSecurityManager(new SecurityManager() {
+ @Override
+ public void checkConnect(String host, int port) {
+ // allow socket connections
+ }
+ });
+
+ // initialize logging system
+ LogManager.getLogManager();
+ }
+
+ // check that defaults are used as specified by javadoc
+
+ public static class Default extends HandlersConfigTest {
+ public static void main(String[] args) {
+ new Default().run();
+ }
+
+ @Override
+ public void run() {
+ // MemoryHandler
+
+ check(new MemoryHandler(),
+ Level.ALL, null, null, SimpleFormatter.class,
+ ConfiguredHandler.class, 1000, Level.SEVERE);
+
+ check(new MemoryHandler(new SpecifiedHandler(), 100, Level.WARNING),
+ Level.ALL, null, null, SimpleFormatter.class,
+ SpecifiedHandler.class, 100, Level.WARNING);
+
+ // StreamHandler
+
+ check(new StreamHandler(),
+ Level.INFO, null, null, SimpleFormatter.class,
+ null);
+
+ check(new StreamHandler(System.out, new SpecifiedFormatter()),
+ Level.INFO, null, null, SpecifiedFormatter.class,
+ System.out);
+
+ // ConsoleHandler
+
+ check(new ConsoleHandler(),
+ Level.INFO, null, null, SimpleFormatter.class,
+ System.err);
+
+ // SocketHandler (use the ServerSocket's port)
+
+ try {
+ check(new SocketHandler("localhost", serverSocket.getLocalPort()),
+ Level.ALL, null, null, XMLFormatter.class);
+ } catch (IOException e) {
+ throw new RuntimeException("Can't connect to localhost:" + serverSocket.getLocalPort(), e);
+ }
+ }
+ }
+
+ // check that LogManager properties configuration is respected
+
+ public static class Configured extends HandlersConfigTest {
+ public static void main(String[] args) {
+ new Configured().run();
+ }
+
+ @Override
+ public void run() {
+ // MemoryHandler
+
+ check(new MemoryHandler(),
+ Level.FINE, null, ConfiguredFilter.class, ConfiguredFormatter.class,
+ ConfiguredHandler.class, 123, Level.FINE);
+
+ check(new MemoryHandler(new SpecifiedHandler(), 100, Level.WARNING),
+ Level.FINE, null, ConfiguredFilter.class, ConfiguredFormatter.class,
+ SpecifiedHandler.class, 100, Level.WARNING);
+
+ // StreamHandler
+
+ check(new StreamHandler(),
+ Level.FINE, "ASCII", ConfiguredFilter.class, ConfiguredFormatter.class,
+ null);
+
+ check(new StreamHandler(System.out, new SpecifiedFormatter()),
+ Level.FINE, "ASCII", ConfiguredFilter.class, SpecifiedFormatter.class,
+ System.out);
+
+ // ConsoleHandler
+
+ check(new ConsoleHandler(),
+ Level.FINE, "ASCII", ConfiguredFilter.class, ConfiguredFormatter.class,
+ System.err);
+
+ // SocketHandler (use the ServerSocket's port)
+
+ try {
+ check(new SocketHandler("localhost", serverSocket.getLocalPort()),
+ Level.FINE, "ASCII", ConfiguredFilter.class, ConfiguredFormatter.class);
+ } catch (Exception e) {
+ throw new RuntimeException("Can't connect to localhost:" + serverSocket.getLocalPort(), e);
+ }
+ }
+ }
+
+ // test infrastructure
+
+ void check(Handler handler,
+ Level expectedLevel,
+ String expectedEncoding,
+ Class<? extends Filter> expectedFilterType,
+ Class<? extends Formatter> expectedFormatterType) {
+ checkEquals(handler, "level", handler.getLevel(), expectedLevel);
+ checkEquals(handler, "encoding", handler.getEncoding(), expectedEncoding);
+ checkType(handler, "filter", handler.getFilter(), expectedFilterType);
+ checkType(handler, "formatter", handler.getFormatter(), expectedFormatterType);
+ }
+
+ void check(MemoryHandler handler,
+ Level expectedLevel,
+ String expectedEncoding,
+ Class<? extends Filter> expectedFilterType,
+ Class<? extends Formatter> expectedFormatterType,
+ Class<? extends Handler> expextedTargetType,
+ int expextedSize,
+ Level expectedPushLevel) {
+ checkType(handler, "target", getTarget(handler), expextedTargetType);
+ checkEquals(handler, "size", getSize(handler), expextedSize);
+ checkEquals(handler, "pushLevel", handler.getPushLevel(), expectedPushLevel);
+ check(handler, expectedLevel, expectedEncoding, expectedFilterType, expectedFormatterType);
+ }
+
+ void check(StreamHandler handler,
+ Level expectedLevel,
+ String expectedEncoding,
+ Class<? extends Filter> expectedFilterType,
+ Class<? extends Formatter> expectedFormatterType,
+ OutputStream expectedOutputStream) {
+ checkEquals(handler, "outputStream", getOutput(handler), expectedOutputStream);
+ check(handler, expectedLevel, expectedEncoding, expectedFilterType, expectedFormatterType);
+ }
+
+ <T> void checkEquals(Handler handler, String property, T value, T expectedValue) {
+ if (!Objects.equals(value, expectedValue)) {
+ fail(handler, property + ": " + value + ", expected " + property + ": " + expectedValue);
+ }
+ }
+
+ <T> void checkType(Handler handler, String property, T value, Class<? extends T> expectedType) {
+ if (!(expectedType == null && value == null || expectedType != null && expectedType.isInstance(value))) {
+ Class<?> type = value == null ? null : value.getClass();
+ fail(handler, property + " type: " + type + ", expected " + property + " type: " + expectedType);
+ }
+ }
+
+ void fail(Handler handler, String message) {
+ throw new AssertionError("Handler: " + handler.getClass().getName() +
+ ", configured with: " + getClass().getName() +
+ ", " + message);
+ }
+
+ Handler getTarget(MemoryHandler memoryHandler) {
+ try {
+ return (Handler) memoryHandlerTarget.get(memoryHandler);
+ } catch (IllegalAccessException e) {
+ throw new IllegalAccessError(e.getMessage());
+ }
+ }
+
+ int getSize(MemoryHandler memoryHandler) {
+ try {
+ return (int) memoryHandlerSize.get(memoryHandler);
+ } catch (IllegalAccessException e) {
+ throw new IllegalAccessError(e.getMessage());
+ }
+ }
+
+ OutputStream getOutput(StreamHandler streamHandler) {
+ try {
+ return (OutputStream) streamHandlerOutput.get(streamHandler);
+ } catch (IllegalAccessException e) {
+ throw new IllegalAccessError(e.getMessage());
+ }
+ }
+
+ // various independent types of Formatters, Filters, Handlers...
+
+ public static class SpecifiedFormatter extends Formatter {
+ @Override
+ public String format(LogRecord record) {
+ return String.valueOf(record);
+ }
+ }
+
+ public static class SpecifiedHandler extends Handler {
+ @Override
+ public void publish(LogRecord record) { }
+
+ @Override
+ public void flush() { }
+
+ @Override
+ public void close() throws SecurityException { }
+ }
+
+ public static class ConfiguredFormatter extends Formatter {
+ @Override
+ public String format(LogRecord record) {
+ return String.valueOf(record);
+ }
+ }
+
+ public static class ConfiguredFilter implements Filter {
+ @Override
+ public boolean isLoggable(LogRecord record) {
+ return true;
+ }
+ }
+
+ public static class ConfiguredHandler extends Handler {
+ @Override
+ public void publish(LogRecord record) { }
+
+ @Override
+ public void flush() { }
+
+ @Override
+ public void close() throws SecurityException { }
+ }
+}