1 /* 2 * Copyright (c) 2013, 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 * @compile -XDignore.symbol.file CustomLogManager.java SimpleLogManager.java 36 * @run main/othervm -Djava.util.logging.manager=SimpleLogManager SimpleLogManager 37 */ 38 public class SimpleLogManager extends CustomLogManager { 39 public static void main(String[] args) { 40 String classname = System.getProperty("java.util.logging.manager"); 41 if (!classname.equals("SimpleLogManager")) { 42 throw new RuntimeException("java.util.logging.manager not set"); 43 } 44 45 Logger logger = Logger.getLogger(SimpleLogManager.class.getName()); 46 Logger.getLogger("org.foo.bar.Foo"); 47 48 // a platform logger used by the system code is just a Logger instance. 49 PlatformLogger.getLogger("org.openjdk.core.logger"); 50 51 LogManager mgr = LogManager.getLogManager(); 52 if (mgr != CustomLogManager.INSTANCE || !(mgr instanceof SimpleLogManager)) { 53 throw new RuntimeException(LogManager.getLogManager() + " not SimpleLogManager"); 54 } 55 56 checkCustomLogger(SimpleLogManager.class.getName(), null); 57 checkCustomLogger("org.foo.bar.Foo", null); 58 checkCustomLogger("org.openjdk.core.logger", "sun.util.logging.resources.logging"); 59 60 // ## The LogManager.demandLogger method does not handle custom log manager 61 // ## that overrides the getLogger method to return a custom logger 62 // ## (see the test case in 8005640). Logger.getLogger may return 63 // ## a Logger instance but LogManager overrides it with a custom Logger 64 // ## instance like this case. 65 // 66 // However, the specification of LogManager and Logger subclassing is 67 // not clear whether this is supported or not. The following check 68 // just captures the current behavior. 69 if (logger instanceof CustomLogger) { 70 throw new RuntimeException(logger + " not CustomLogger"); 71 } 72 } 73 74 private static void checkCustomLogger(String name, String resourceBundleName) { 75 CustomLogManager.checkLogger(name, resourceBundleName); 76 Logger logger1 = Logger.getLogger(name); 77 Logger logger2 = LogManager.getLogManager().getLogger(name); 78 if (logger1 != logger2) { 79 throw new RuntimeException(logger1 + " != " + logger2); 80 } 81 if (!(logger1 instanceof CustomLogger)) { 82 throw new RuntimeException(logger1 + " not CustomLogger"); 83 } 84 } 85 86 /* 87 * This SimpleLogManager overrides the addLogger method to replace 88 * the given logger with a custom logger. 89 * 90 * It's unclear what the recommended way to use custom logger is. 91 * A LogManager subclass might override the getLogger method to return 92 * a custom Logger and create a new custom logger if not exist so that 93 * Logger.getLogger() can return a custom Logger instance but that violates 94 * the LogManager.getLogger() spec which should return null if not found. 95 */ 96 public synchronized boolean addLogger(Logger logger) { 97 String name = logger.getName(); 98 if (namedLoggers.containsKey(name)) { 99 return false; 100 } 101 CustomLogger newLogger = new CustomLogger(logger); 102 return super.addLogger(newLogger); 103 } 104 105 public class CustomLogger extends Logger { 106 final Logger keepRef; // keep a strong reference to avoid GC. 107 CustomLogger(Logger logger) { 108 super(logger.getName(), logger.getResourceBundleName()); 109 keepRef = logger; 110 } 111 } 112 }