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