1 /* 2 * Copyright (c) 2013, 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 import java.util.*; 25 import java.util.logging.*; 26 import sun.util.logging.PlatformLogger; 27 28 /* 29 * @test 30 * @bug 8005615 31 * @summary A LogManager subclass overrides its own implementation of named 32 * logger (see the subclassing information in the Logger class specification) 33 * 34 * @modules java.base/sun.util.logging 35 * java.logging 36 * @compile -XDignore.symbol.file CustomLogManager.java SimpleLogManager.java 37 * @run main/othervm -Djava.util.logging.manager=SimpleLogManager SimpleLogManager 38 */ 39 public class SimpleLogManager extends CustomLogManager { main(String[] args)40 public static void main(String[] args) { 41 String classname = System.getProperty("java.util.logging.manager"); 42 if (!classname.equals("SimpleLogManager")) { 43 throw new RuntimeException("java.util.logging.manager not set"); 44 } 45 46 Logger logger = Logger.getLogger(SimpleLogManager.class.getName()); 47 Logger.getLogger("org.foo.bar.Foo"); 48 49 // a platform logger used by the system code is just a Logger instance. 50 PlatformLogger.getLogger("org.openjdk.core.logger"); 51 52 LogManager mgr = LogManager.getLogManager(); 53 if (mgr != CustomLogManager.INSTANCE || !(mgr instanceof SimpleLogManager)) { 54 throw new RuntimeException(LogManager.getLogManager() + " not SimpleLogManager"); 55 } 56 57 checkCustomLogger(SimpleLogManager.class.getName(), null); 58 checkCustomLogger("org.foo.bar.Foo", null); 59 checkCustomLogger("org.openjdk.core.logger", "sun.util.logging.resources.logging"); 60 61 // ## The LogManager.demandLogger method does not handle custom log manager 62 // ## that overrides the getLogger method to return a custom logger 63 // ## (see the test case in 8005640). Logger.getLogger may return 64 // ## a Logger instance but LogManager overrides it with a custom Logger 65 // ## instance like this case. 66 // 67 // However, the specification of LogManager and Logger subclassing is 68 // not clear whether this is supported or not. The following check 69 // just captures the current behavior. 70 if (logger instanceof CustomLogger) { 71 throw new RuntimeException(logger + " not CustomLogger"); 72 } 73 } 74 checkCustomLogger(String name, String resourceBundleName)75 private static void checkCustomLogger(String name, String resourceBundleName) { 76 CustomLogManager.checkLogger(name, resourceBundleName); 77 Logger logger1 = Logger.getLogger(name); 78 Logger logger2 = LogManager.getLogManager().getLogger(name); 79 if (logger1 != logger2) { 80 throw new RuntimeException(logger1 + " != " + logger2); 81 } 82 if (!(logger1 instanceof CustomLogger)) { 83 throw new RuntimeException(logger1 + " not CustomLogger"); 84 } 85 } 86 87 /* 88 * This SimpleLogManager overrides the addLogger method to replace 89 * the given logger with a custom logger. 90 * 91 * It's unclear what the recommended way to use custom logger is. 92 * A LogManager subclass might override the getLogger method to return 93 * a custom Logger and create a new custom logger if not exist so that 94 * Logger.getLogger() can return a custom Logger instance but that violates 95 * the LogManager.getLogger() spec which should return null if not found. 96 */ addLogger(Logger logger)97 public synchronized boolean addLogger(Logger logger) { 98 String name = logger.getName(); 99 if (namedLoggers.containsKey(name)) { 100 return false; 101 } 102 CustomLogger newLogger = new CustomLogger(logger); 103 return super.addLogger(newLogger); 104 } 105 106 public class CustomLogger extends Logger { 107 final Logger keepRef; // keep a strong reference to avoid GC. CustomLogger(Logger logger)108 CustomLogger(Logger logger) { 109 super(logger.getName(), logger.getResourceBundleName()); 110 keepRef = logger; 111 } 112 } 113 } 114