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