jdk/src/share/classes/java/util/logging/LogManager.java
author dfuchs
Wed, 12 Mar 2014 20:18:47 +0100
changeset 23342 d7a41da68b85
parent 22342 c83795c442b7
child 23899 a926ecef7eb3
permissions -rw-r--r--
8036916: Deprecation warnings in LogManager Reviewed-by: mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
17164
1304199c8641 8010939: Deadlock in LogManager
jgish
parents: 16117
diff changeset
     2
 * Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4172
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4172
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4172
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4172
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4172
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
package java.util.logging;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.*;
5964
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
    32
import java.lang.ref.ReferenceQueue;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.lang.ref.WeakReference;
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
    34
import sun.misc.JavaAWTAccess;
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
    35
import sun.misc.SharedSecrets;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * There is a single global LogManager object that is used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * maintain a set of shared state about Loggers and log services.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * This LogManager object:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <li> Manages a hierarchical namespace of Logger objects.  All
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *      named Loggers are stored in this namespace.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <li> Manages a set of logging control properties.  These are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *      simple key-value pairs that can be used by Handlers and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *      other logging objects to configure themselves.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * The global LogManager object can be retrieved using LogManager.getLogManager().
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * The LogManager object is created during class initialization and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * cannot subsequently be changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * At startup the LogManager class is located using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * java.util.logging.manager system property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * <p>
14691
22056e110b04 8003949: LogManager, downgrade normative reference to ${java.home}/lib/logging.properties
alanb
parents: 14342
diff changeset
    57
 * The LogManager defines two optional system properties that allow control over
22056e110b04 8003949: LogManager, downgrade normative reference to ${java.home}/lib/logging.properties
alanb
parents: 14342
diff changeset
    58
 * the initial configuration:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * <li>"java.util.logging.config.class"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * <li>"java.util.logging.config.file"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * </ul>
14691
22056e110b04 8003949: LogManager, downgrade normative reference to ${java.home}/lib/logging.properties
alanb
parents: 14342
diff changeset
    63
 * These two properties may be specified on the command line to the "java"
22056e110b04 8003949: LogManager, downgrade normative reference to ${java.home}/lib/logging.properties
alanb
parents: 14342
diff changeset
    64
 * command, or as system property definitions passed to JNI_CreateJavaVM.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * If the "java.util.logging.config.class" property is set, then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * property value is treated as a class name.  The given class will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * loaded, an object will be instantiated, and that object's constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * is responsible for reading in the initial configuration.  (That object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * may use other system properties to control its configuration.)  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * alternate configuration class can use <tt>readConfiguration(InputStream)</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * to define properties in the LogManager.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * If "java.util.logging.config.class" property is <b>not</b> set,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * then the "java.util.logging.config.file" system property can be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * to specify a properties file (in java.util.Properties format). The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * initial logging configuration will be read from this file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * <p>
14691
22056e110b04 8003949: LogManager, downgrade normative reference to ${java.home}/lib/logging.properties
alanb
parents: 14342
diff changeset
    79
 * If neither of these properties is defined then the LogManager uses its
22056e110b04 8003949: LogManager, downgrade normative reference to ${java.home}/lib/logging.properties
alanb
parents: 14342
diff changeset
    80
 * default configuration. The default configuration is typically loaded from the
22056e110b04 8003949: LogManager, downgrade normative reference to ${java.home}/lib/logging.properties
alanb
parents: 14342
diff changeset
    81
 * properties file "{@code lib/logging.properties}" in the Java installation
22056e110b04 8003949: LogManager, downgrade normative reference to ${java.home}/lib/logging.properties
alanb
parents: 14342
diff changeset
    82
 * directory.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * The properties for loggers and Handlers will have names starting
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * with the dot-separated name for the handler or logger.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * The global logging properties may include:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * <li>A property "handlers".  This defines a whitespace or comma separated
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * list of class names for handler classes to load and register as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * handlers on the root Logger (the Logger named "").  Each class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * name must be for a Handler class which has a default constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * Note that these Handlers may be created lazily, when they are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * first used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * <li>A property "&lt;logger&gt;.handlers". This defines a whitespace or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * comma separated list of class names for handlers classes to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * load and register as handlers to the specified logger. Each class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * name must be for a Handler class which has a default constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * Note that these Handlers may be created lazily, when they are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * first used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * <li>A property "&lt;logger&gt;.useParentHandlers". This defines a boolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * value. By default every logger calls its parent in addition to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * handling the logging message itself, this often result in messages
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * being handled by the root logger as well. When setting this property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * to false a Handler needs to be configured for this logger otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * no logging messages are delivered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * <li>A property "config".  This property is intended to allow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * arbitrary configuration code to be run.  The property defines a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * whitespace or comma separated list of class names.  A new instance will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 * created for each named class.  The default constructor of each class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 * may execute arbitrary code to update the logging configuration, such as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * setting logger levels, adding handlers, adding filters, etc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 * Note that all classes loaded during LogManager configuration are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 * first searched on the system class path before any user class path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 * That includes the LogManager class, any config classes, and any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 * handler classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 * Loggers are organized into a naming hierarchy based on their
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 * dot separated names.  Thus "a.b.c" is a child of "a.b", but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 * "a.b1" and a.b2" are peers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 * All properties whose names end with ".level" are assumed to define
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 * log levels for Loggers.  Thus "foo.level" defines a log level for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 * the logger called "foo" and (recursively) for any of its children
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 * in the naming hierarchy.  Log Levels are applied in the order they
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 * are defined in the properties file.  Thus level settings for child
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 * nodes in the tree should come after settings for their parents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 * The property name ".level" can be used to set the level for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 * root of the tree.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 * All methods on the LogManager object are multi-thread safe.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
public class LogManager {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    // The global LogManager object
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   143
    private static final LogManager manager;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
21960
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
   145
    // 'props' is assigned within a lock but accessed without it.
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
   146
    // Declaring it volatile makes sure that another thread will not
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
   147
    // be able to see a partially constructed 'props' object.
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
   148
    // (seeing a partially constructed 'props' object can result in
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
   149
    // NPE being thrown in Hashtable.get(), because it leaves the door
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
   150
    // open for props.getProperties() to be called before the construcor
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
   151
    // of Hashtable is actually completed).
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
   152
    private volatile Properties props = new Properties();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    private final static Level defaultLevel = Level.INFO;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   155
    // LoggerContext for system loggers and user loggers
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   156
    private final LoggerContext systemContext = new SystemLoggerContext();
16105
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   157
    private final LoggerContext userContext = new LoggerContext();
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   158
    // non final field - make it volatile to make sure that other threads
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   159
    // will see the new value once ensureLogManagerInitialized() has finished
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   160
    // executing.
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   161
    private volatile Logger rootLogger;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    // Have we done the primordial reading of the configuration file?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    // (Must be done after a suitable amount of java.lang.System
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    // initialization has been done)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    private volatile boolean readPrimordialConfiguration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    // Have we initialized global (root) handlers yet?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    // This gets set to false in readConfiguration
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    private boolean initializedGlobalHandlers = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    // True if JVM death is imminent and the exit hook has been called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    private boolean deathImminent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    static {
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   173
        manager = AccessController.doPrivileged(new PrivilegedAction<LogManager>() {
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   174
            @Override
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   175
            public LogManager run() {
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   176
                LogManager mgr = null;
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   177
                String cname = null;
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   178
                try {
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   179
                    cname = System.getProperty("java.util.logging.manager");
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   180
                    if (cname != null) {
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   181
                        try {
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   182
                            Class<?> clz = ClassLoader.getSystemClassLoader()
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   183
                                    .loadClass(cname);
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   184
                            mgr = (LogManager) clz.newInstance();
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   185
                        } catch (ClassNotFoundException ex) {
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   186
                            Class<?> clz = Thread.currentThread()
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   187
                                    .getContextClassLoader().loadClass(cname);
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   188
                            mgr = (LogManager) clz.newInstance();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                    }
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   191
                } catch (Exception ex) {
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   192
                    System.err.println("Could not load Logmanager \"" + cname + "\"");
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   193
                    ex.printStackTrace();
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   194
                }
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   195
                if (mgr == null) {
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   196
                    mgr = new LogManager();
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   197
                }
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   198
                return mgr;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   200
            }
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   201
        });
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    // This private class is used as a shutdown hook.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    // It does a "reset" to close all open handlers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    private class Cleaner extends Thread {
2384
b3ba5fb77f89 6799583: LogManager shutdown hook may cause a memory leak.
bae
parents: 2
diff changeset
   208
b3ba5fb77f89 6799583: LogManager shutdown hook may cause a memory leak.
bae
parents: 2
diff changeset
   209
        private Cleaner() {
b3ba5fb77f89 6799583: LogManager shutdown hook may cause a memory leak.
bae
parents: 2
diff changeset
   210
            /* Set context class loader to null in order to avoid
b3ba5fb77f89 6799583: LogManager shutdown hook may cause a memory leak.
bae
parents: 2
diff changeset
   211
             * keeping a strong reference to an application classloader.
b3ba5fb77f89 6799583: LogManager shutdown hook may cause a memory leak.
bae
parents: 2
diff changeset
   212
             */
b3ba5fb77f89 6799583: LogManager shutdown hook may cause a memory leak.
bae
parents: 2
diff changeset
   213
            this.setContextClassLoader(null);
b3ba5fb77f89 6799583: LogManager shutdown hook may cause a memory leak.
bae
parents: 2
diff changeset
   214
        }
b3ba5fb77f89 6799583: LogManager shutdown hook may cause a memory leak.
bae
parents: 2
diff changeset
   215
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   216
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            // This is to ensure the LogManager.<clinit> is completed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            // before synchronized block. Otherwise deadlocks are possible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            LogManager mgr = manager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            // If the global handlers haven't been initialized yet, we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            // don't want to initialize them just so we can close them!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            synchronized (LogManager.this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                // Note that death is imminent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                deathImminent = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                initializedGlobalHandlers = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            // Do a reset to close all active handlers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * Protected constructor.  This is protected so that container applications
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * (such as J2EE containers) can subclass the object.  It is non-public as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * it is intended that there only be one LogManager object, whose value is
14767
b64db1d425e5 4819681: Typo in http://java.sun.com/j2se/1.4.1/docs/api/java/util/logging/LogManager.html
mchung
parents: 14691
diff changeset
   240
     * retrieved by calling LogManager.getLogManager.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    protected LogManager() {
22321
37f8c8b6ef7a 8024867: Enhance logging start up
dfuchs
parents: 19825
diff changeset
   243
        this(checkSubclassPermissions());
37f8c8b6ef7a 8024867: Enhance logging start up
dfuchs
parents: 19825
diff changeset
   244
    }
37f8c8b6ef7a 8024867: Enhance logging start up
dfuchs
parents: 19825
diff changeset
   245
37f8c8b6ef7a 8024867: Enhance logging start up
dfuchs
parents: 19825
diff changeset
   246
    private LogManager(Void checked) {
37f8c8b6ef7a 8024867: Enhance logging start up
dfuchs
parents: 19825
diff changeset
   247
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        // Add a shutdown hook to close the global handlers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            Runtime.getRuntime().addShutdownHook(new Cleaner());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        } catch (IllegalStateException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            // If the VM is already shutting down,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            // We do not need to register shutdownHook.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
22321
37f8c8b6ef7a 8024867: Enhance logging start up
dfuchs
parents: 19825
diff changeset
   257
    private static Void checkSubclassPermissions() {
37f8c8b6ef7a 8024867: Enhance logging start up
dfuchs
parents: 19825
diff changeset
   258
        final SecurityManager sm = System.getSecurityManager();
37f8c8b6ef7a 8024867: Enhance logging start up
dfuchs
parents: 19825
diff changeset
   259
        if (sm != null) {
37f8c8b6ef7a 8024867: Enhance logging start up
dfuchs
parents: 19825
diff changeset
   260
            // These permission will be checked in the LogManager constructor,
37f8c8b6ef7a 8024867: Enhance logging start up
dfuchs
parents: 19825
diff changeset
   261
            // in order to register the Cleaner() thread as a shutdown hook.
37f8c8b6ef7a 8024867: Enhance logging start up
dfuchs
parents: 19825
diff changeset
   262
            // Check them here to avoid the penalty of constructing the object
37f8c8b6ef7a 8024867: Enhance logging start up
dfuchs
parents: 19825
diff changeset
   263
            // etc...
37f8c8b6ef7a 8024867: Enhance logging start up
dfuchs
parents: 19825
diff changeset
   264
            sm.checkPermission(new RuntimePermission("shutdownHooks"));
37f8c8b6ef7a 8024867: Enhance logging start up
dfuchs
parents: 19825
diff changeset
   265
            sm.checkPermission(new RuntimePermission("setContextClassLoader"));
37f8c8b6ef7a 8024867: Enhance logging start up
dfuchs
parents: 19825
diff changeset
   266
        }
37f8c8b6ef7a 8024867: Enhance logging start up
dfuchs
parents: 19825
diff changeset
   267
        return null;
37f8c8b6ef7a 8024867: Enhance logging start up
dfuchs
parents: 19825
diff changeset
   268
    }
37f8c8b6ef7a 8024867: Enhance logging start up
dfuchs
parents: 19825
diff changeset
   269
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    /**
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   271
     * Lazy initialization: if this instance of manager is the global
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   272
     * manager then this method will read the initial configuration and
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   273
     * add the root logger and global logger by calling addLogger().
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   274
     *
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   275
     * Note that it is subtly different from what we do in LoggerContext.
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   276
     * In LoggerContext we're patching up the logger context tree in order to add
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   277
     * the root and global logger *to the context tree*.
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   278
     *
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   279
     * For this to work, addLogger() must have already have been called
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   280
     * once on the LogManager instance for the default logger being
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   281
     * added.
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   282
     *
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   283
     * This is why ensureLogManagerInitialized() needs to be called before
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   284
     * any logger is added to any logger context.
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   285
     *
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   286
     */
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   287
    private boolean initializedCalled = false;
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   288
    private volatile boolean initializationDone = false;
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   289
    final void ensureLogManagerInitialized() {
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   290
        final LogManager owner = this;
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   291
        if (initializationDone || owner != manager) {
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   292
            // we don't want to do this twice, and we don't want to do
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   293
            // this on private manager instances.
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   294
            return;
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   295
        }
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   296
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   297
        // Maybe another thread has called ensureLogManagerInitialized()
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   298
        // before us and is still executing it. If so we will block until
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   299
        // the log manager has finished initialized, then acquire the monitor,
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   300
        // notice that initializationDone is now true and return.
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   301
        // Otherwise - we have come here first! We will acquire the monitor,
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   302
        // see that initializationDone is still false, and perform the
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   303
        // initialization.
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   304
        //
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   305
        synchronized(this) {
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   306
            // If initializedCalled is true it means that we're already in
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   307
            // the process of initializing the LogManager in this thread.
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   308
            // There has been a recursive call to ensureLogManagerInitialized().
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   309
            final boolean isRecursiveInitialization = (initializedCalled == true);
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   310
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   311
            assert initializedCalled || !initializationDone
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   312
                    : "Initialization can't be done if initialized has not been called!";
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   313
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   314
            if (isRecursiveInitialization || initializationDone) {
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   315
                // If isRecursiveInitialization is true it means that we're
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   316
                // already in the process of initializing the LogManager in
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   317
                // this thread. There has been a recursive call to
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   318
                // ensureLogManagerInitialized(). We should not proceed as
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   319
                // it would lead to infinite recursion.
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   320
                //
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   321
                // If initializationDone is true then it means the manager
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   322
                // has finished initializing; just return: we're done.
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   323
                return;
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   324
            }
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   325
            // Calling addLogger below will in turn call requiresDefaultLogger()
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   326
            // which will call ensureLogManagerInitialized().
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   327
            // We use initializedCalled to break the recursion.
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   328
            initializedCalled = true;
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   329
            try {
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   330
                AccessController.doPrivileged(new PrivilegedAction<Object>() {
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   331
                    @Override
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   332
                    public Object run() {
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   333
                        assert rootLogger == null;
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   334
                        assert initializedCalled && !initializationDone;
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   335
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   336
                        // Read configuration.
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   337
                        owner.readPrimordialConfiguration();
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   338
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   339
                        // Create and retain Logger for the root of the namespace.
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   340
                        owner.rootLogger = owner.new RootLogger();
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   341
                        owner.addLogger(owner.rootLogger);
22086
866b0a7d0127 8030850: Setting .level=FINEST in logging configuration file doesn't work
dfuchs
parents: 22078
diff changeset
   342
                        if (!owner.rootLogger.isLevelInitialized()) {
866b0a7d0127 8030850: Setting .level=FINEST in logging configuration file doesn't work
dfuchs
parents: 22078
diff changeset
   343
                            owner.rootLogger.setLevel(defaultLevel);
866b0a7d0127 8030850: Setting .level=FINEST in logging configuration file doesn't work
dfuchs
parents: 22078
diff changeset
   344
                        }
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   345
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   346
                        // Adding the global Logger.
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   347
                        // Do not call Logger.getGlobal() here as this might trigger
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   348
                        // subtle inter-dependency issues.
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   349
                        @SuppressWarnings("deprecation")
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   350
                        final Logger global = Logger.global;
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   351
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   352
                        // Make sure the global logger will be registered in the
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   353
                        // global manager
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   354
                        owner.addLogger(global);
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   355
                        return null;
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   356
                    }
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   357
                });
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   358
            } finally {
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   359
                initializationDone = true;
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   360
            }
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   361
        }
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   362
    }
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   363
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   364
    /**
18565
5025d43d3731 8019315: Fix doclint issues in java.util.logging
darcy
parents: 18269
diff changeset
   365
     * Returns the global LogManager object.
5025d43d3731 8019315: Fix doclint issues in java.util.logging
darcy
parents: 18269
diff changeset
   366
     * @return the global LogManager object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    public static LogManager getLogManager() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        if (manager != null) {
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   370
            manager.ensureLogManagerInitialized();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        return manager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    private void readPrimordialConfiguration() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        if (!readPrimordialConfiguration) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                if (!readPrimordialConfiguration) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                    // If System.in/out/err are null, it's a good
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                    // indication that we're still in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                    // bootstrapping phase
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                    if (System.out == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                    readPrimordialConfiguration = true;
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   386
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                    try {
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   388
                        AccessController.doPrivileged(new PrivilegedExceptionAction<Void>() {
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   389
                                @Override
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   390
                                public Void run() throws Exception {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                                    readConfiguration();
3861
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents: 3319
diff changeset
   392
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents: 3319
diff changeset
   393
                                    // Platform loggers begin to delegate to java.util.logging.Logger
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents: 3319
diff changeset
   394
                                    sun.util.logging.PlatformLogger.redirectPlatformLoggers();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                    } catch (Exception ex) {
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   399
                        assert false : "Exception raised while reading logging configuration: " + ex;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
19809
dc0480c1b36c 8019853: Break logging and AWT circular dependency
dfuchs
parents: 18595
diff changeset
   406
    // LoggerContext maps from AppContext
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   407
    private WeakHashMap<Object, LoggerContext> contextsMap = null;
19809
dc0480c1b36c 8019853: Break logging and AWT circular dependency
dfuchs
parents: 18595
diff changeset
   408
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   409
    // Returns the LoggerContext for the user code (i.e. application or AppContext).
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   410
    // Loggers are isolated from each AppContext.
16105
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   411
    private LoggerContext getUserContext() {
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   412
        LoggerContext context = null;
9700
fdbacf68f185 7016208: 4/3 null sometimes returned by java.util.logging.Logger.getLogger(String name) in -server -Xcomp
dcubed
parents: 9013
diff changeset
   413
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   414
        SecurityManager sm = System.getSecurityManager();
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   415
        JavaAWTAccess javaAwtAccess = SharedSecrets.getJavaAWTAccess();
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   416
        if (sm != null && javaAwtAccess != null) {
19809
dc0480c1b36c 8019853: Break logging and AWT circular dependency
dfuchs
parents: 18595
diff changeset
   417
            // for each applet, it has its own LoggerContext isolated from others
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   418
            synchronized (javaAwtAccess) {
19809
dc0480c1b36c 8019853: Break logging and AWT circular dependency
dfuchs
parents: 18595
diff changeset
   419
                // find the AppContext of the applet code
dc0480c1b36c 8019853: Break logging and AWT circular dependency
dfuchs
parents: 18595
diff changeset
   420
                // will be null if we are in the main app context.
dc0480c1b36c 8019853: Break logging and AWT circular dependency
dfuchs
parents: 18595
diff changeset
   421
                final Object ecx = javaAwtAccess.getAppletContext();
18202
e9a6d59491ab 8004584: Augment applet contextualization
leonidr
parents: 16117
diff changeset
   422
                if (ecx != null) {
19809
dc0480c1b36c 8019853: Break logging and AWT circular dependency
dfuchs
parents: 18595
diff changeset
   423
                    if (contextsMap == null) {
dc0480c1b36c 8019853: Break logging and AWT circular dependency
dfuchs
parents: 18595
diff changeset
   424
                        contextsMap = new WeakHashMap<>();
dc0480c1b36c 8019853: Break logging and AWT circular dependency
dfuchs
parents: 18595
diff changeset
   425
                    }
dc0480c1b36c 8019853: Break logging and AWT circular dependency
dfuchs
parents: 18595
diff changeset
   426
                    context = contextsMap.get(ecx);
18202
e9a6d59491ab 8004584: Augment applet contextualization
leonidr
parents: 16117
diff changeset
   427
                    if (context == null) {
19809
dc0480c1b36c 8019853: Break logging and AWT circular dependency
dfuchs
parents: 18595
diff changeset
   428
                        // Create a new LoggerContext for the applet.
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   429
                        context = new LoggerContext();
19809
dc0480c1b36c 8019853: Break logging and AWT circular dependency
dfuchs
parents: 18595
diff changeset
   430
                        contextsMap.put(ecx, context);
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   431
                    }
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   432
                }
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   433
            }
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   434
        }
19809
dc0480c1b36c 8019853: Break logging and AWT circular dependency
dfuchs
parents: 18595
diff changeset
   435
        // for standalone app, return userContext
18202
e9a6d59491ab 8004584: Augment applet contextualization
leonidr
parents: 16117
diff changeset
   436
        return context != null ? context : userContext;
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   437
    }
9700
fdbacf68f185 7016208: 4/3 null sometimes returned by java.util.logging.Logger.getLogger(String name) in -server -Xcomp
dcubed
parents: 9013
diff changeset
   438
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   439
    // The system context.
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   440
    final LoggerContext getSystemContext() {
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   441
        return systemContext;
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   442
    }
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   443
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   444
    private List<LoggerContext> contexts() {
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   445
        List<LoggerContext> cxs = new ArrayList<>();
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   446
        cxs.add(getSystemContext());
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   447
        cxs.add(getUserContext());
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   448
        return cxs;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
16105
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   451
    // Find or create a specified logger instance. If a logger has
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   452
    // already been created with the given name it is returned.
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   453
    // Otherwise a new logger instance is created and registered
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   454
    // in the LogManager global namespace.
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   455
    // This method will always return a non-null Logger object.
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   456
    // Synchronization is not required here. All synchronization for
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   457
    // adding a new Logger object is handled by addLogger().
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   458
    //
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   459
    // This method must delegate to the LogManager implementation to
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   460
    // add a new Logger or return the one that has been added previously
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   461
    // as a LogManager subclass may override the addLogger, getLogger,
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   462
    // readConfiguration, and other methods.
17487
77566e5979d1 8013380: Removal of stack walk to find resource bundle breaks Glassfish startup
jgish
parents: 17164
diff changeset
   463
    Logger demandLogger(String name, String resourceBundleName, Class<?> caller) {
16105
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   464
        Logger result = getLogger(name);
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   465
        if (result == null) {
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   466
            // only allocate the new logger once
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   467
            Logger newLogger = new Logger(name, resourceBundleName, caller, this);
16105
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   468
            do {
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   469
                if (addLogger(newLogger)) {
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   470
                    // We successfully added the new Logger that we
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   471
                    // created above so return it without refetching.
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   472
                    return newLogger;
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   473
                }
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   474
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   475
                // We didn't add the new Logger that we created above
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   476
                // because another thread added a Logger with the same
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   477
                // name after our null check above and before our call
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   478
                // to addLogger(). We have to refetch the Logger because
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   479
                // addLogger() returns a boolean instead of the Logger
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   480
                // reference itself. However, if the thread that created
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   481
                // the other Logger is not holding a strong reference to
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   482
                // the other Logger, then it is possible for the other
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   483
                // Logger to be GC'ed after we saw it in addLogger() and
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   484
                // before we can refetch it. If it has been GC'ed then
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   485
                // we'll just loop around and try again.
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   486
                result = getLogger(name);
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   487
            } while (result == null);
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   488
        }
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   489
        return result;
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   490
    }
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   491
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   492
    Logger demandSystemLogger(String name, String resourceBundleName) {
16111
5c5fc1d5bb38 8007393: Possible race condition after JDK-6664509
mchung
parents: 16105
diff changeset
   493
        // Add a system logger in the system context's namespace
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   494
        final Logger sysLogger = getSystemContext().demandLogger(name, resourceBundleName);
16111
5c5fc1d5bb38 8007393: Possible race condition after JDK-6664509
mchung
parents: 16105
diff changeset
   495
5c5fc1d5bb38 8007393: Possible race condition after JDK-6664509
mchung
parents: 16105
diff changeset
   496
        // Add the system logger to the LogManager's namespace if not exist
5c5fc1d5bb38 8007393: Possible race condition after JDK-6664509
mchung
parents: 16105
diff changeset
   497
        // so that there is only one single logger of the given name.
5c5fc1d5bb38 8007393: Possible race condition after JDK-6664509
mchung
parents: 16105
diff changeset
   498
        // System loggers are visible to applications unless a logger of
5c5fc1d5bb38 8007393: Possible race condition after JDK-6664509
mchung
parents: 16105
diff changeset
   499
        // the same name has been added.
5c5fc1d5bb38 8007393: Possible race condition after JDK-6664509
mchung
parents: 16105
diff changeset
   500
        Logger logger;
5c5fc1d5bb38 8007393: Possible race condition after JDK-6664509
mchung
parents: 16105
diff changeset
   501
        do {
5c5fc1d5bb38 8007393: Possible race condition after JDK-6664509
mchung
parents: 16105
diff changeset
   502
            // First attempt to call addLogger instead of getLogger
5c5fc1d5bb38 8007393: Possible race condition after JDK-6664509
mchung
parents: 16105
diff changeset
   503
            // This would avoid potential bug in custom LogManager.getLogger
5c5fc1d5bb38 8007393: Possible race condition after JDK-6664509
mchung
parents: 16105
diff changeset
   504
            // implementation that adds a logger if does not exist
5c5fc1d5bb38 8007393: Possible race condition after JDK-6664509
mchung
parents: 16105
diff changeset
   505
            if (addLogger(sysLogger)) {
5c5fc1d5bb38 8007393: Possible race condition after JDK-6664509
mchung
parents: 16105
diff changeset
   506
                // successfully added the new system logger
5c5fc1d5bb38 8007393: Possible race condition after JDK-6664509
mchung
parents: 16105
diff changeset
   507
                logger = sysLogger;
5c5fc1d5bb38 8007393: Possible race condition after JDK-6664509
mchung
parents: 16105
diff changeset
   508
            } else {
5c5fc1d5bb38 8007393: Possible race condition after JDK-6664509
mchung
parents: 16105
diff changeset
   509
                logger = getLogger(name);
5c5fc1d5bb38 8007393: Possible race condition after JDK-6664509
mchung
parents: 16105
diff changeset
   510
            }
5c5fc1d5bb38 8007393: Possible race condition after JDK-6664509
mchung
parents: 16105
diff changeset
   511
        } while (logger == null);
5c5fc1d5bb38 8007393: Possible race condition after JDK-6664509
mchung
parents: 16105
diff changeset
   512
5c5fc1d5bb38 8007393: Possible race condition after JDK-6664509
mchung
parents: 16105
diff changeset
   513
        // LogManager will set the sysLogger's handlers via LogManager.addLogger method.
5c5fc1d5bb38 8007393: Possible race condition after JDK-6664509
mchung
parents: 16105
diff changeset
   514
        if (logger != sysLogger && sysLogger.getHandlers().length == 0) {
5c5fc1d5bb38 8007393: Possible race condition after JDK-6664509
mchung
parents: 16105
diff changeset
   515
            // if logger already exists but handlers not set
5c5fc1d5bb38 8007393: Possible race condition after JDK-6664509
mchung
parents: 16105
diff changeset
   516
            final Logger l = logger;
5c5fc1d5bb38 8007393: Possible race condition after JDK-6664509
mchung
parents: 16105
diff changeset
   517
            AccessController.doPrivileged(new PrivilegedAction<Void>() {
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   518
                @Override
16111
5c5fc1d5bb38 8007393: Possible race condition after JDK-6664509
mchung
parents: 16105
diff changeset
   519
                public Void run() {
5c5fc1d5bb38 8007393: Possible race condition after JDK-6664509
mchung
parents: 16105
diff changeset
   520
                    for (Handler hdl : l.getHandlers()) {
5c5fc1d5bb38 8007393: Possible race condition after JDK-6664509
mchung
parents: 16105
diff changeset
   521
                        sysLogger.addHandler(hdl);
5c5fc1d5bb38 8007393: Possible race condition after JDK-6664509
mchung
parents: 16105
diff changeset
   522
                    }
5c5fc1d5bb38 8007393: Possible race condition after JDK-6664509
mchung
parents: 16105
diff changeset
   523
                    return null;
5c5fc1d5bb38 8007393: Possible race condition after JDK-6664509
mchung
parents: 16105
diff changeset
   524
                }
5c5fc1d5bb38 8007393: Possible race condition after JDK-6664509
mchung
parents: 16105
diff changeset
   525
            });
5c5fc1d5bb38 8007393: Possible race condition after JDK-6664509
mchung
parents: 16105
diff changeset
   526
        }
5c5fc1d5bb38 8007393: Possible race condition after JDK-6664509
mchung
parents: 16105
diff changeset
   527
        return sysLogger;
16105
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   528
    }
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   529
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   530
    // LoggerContext maintains the logger namespace per context.
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   531
    // The default LogManager implementation has one system context and user
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   532
    // context.  The system context is used to maintain the namespace for
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   533
    // all system loggers and is queried by the system code.  If a system logger
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   534
    // doesn't exist in the user context, it'll also be added to the user context.
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   535
    // The user context is queried by the user code and all other loggers are
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   536
    // added in the user context.
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   537
    class LoggerContext {
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   538
        // Table of named Loggers that maps names to Loggers.
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   539
        private final Hashtable<String,LoggerWeakRef> namedLoggers = new Hashtable<>();
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   540
        // Tree of named Loggers
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   541
        private final LogNode root;
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   542
        private LoggerContext() {
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   543
            this.root = new LogNode(null, this);
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   544
        }
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   545
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   546
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   547
        // Tells whether default loggers are required in this context.
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   548
        // If true, the default loggers will be lazily added.
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   549
        final boolean requiresDefaultLoggers() {
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   550
            final boolean requiresDefaultLoggers = (getOwner() == manager);
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   551
            if (requiresDefaultLoggers) {
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   552
                getOwner().ensureLogManagerInitialized();
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   553
            }
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   554
            return requiresDefaultLoggers;
18593
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   555
        }
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   556
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   557
        // This context's LogManager.
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   558
        final LogManager getOwner() {
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   559
            return LogManager.this;
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   560
        }
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   561
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   562
        // This context owner's root logger, which if not null, and if
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   563
        // the context requires default loggers, will be added to the context
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   564
        // logger's tree.
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   565
        final Logger getRootLogger() {
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   566
            return getOwner().rootLogger;
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   567
        }
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   568
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   569
        // The global logger, which if not null, and if
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   570
        // the context requires default loggers, will be added to the context
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   571
        // logger's tree.
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   572
        final Logger getGlobalLogger() {
23342
d7a41da68b85 8036916: Deprecation warnings in LogManager
dfuchs
parents: 22342
diff changeset
   573
            @SuppressWarnings("deprecation") // avoids initialization cycles.
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   574
            final Logger global = Logger.global;
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   575
            return global;
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   576
        }
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   577
16105
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   578
        Logger demandLogger(String name, String resourceBundleName) {
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   579
            // a LogManager subclass may have its own implementation to add and
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   580
            // get a Logger.  So delegate to the LogManager to do the work.
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   581
            final LogManager owner = getOwner();
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   582
            return owner.demandLogger(name, resourceBundleName, null);
16105
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   583
        }
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   584
18593
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   585
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   586
        // Due to subtle deadlock issues getUserContext() no longer
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   587
        // calls addLocalLogger(rootLogger);
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   588
        // Therefore - we need to add the default loggers later on.
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   589
        // Checks that the context is properly initialized
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   590
        // This is necessary before calling e.g. find(name)
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   591
        // or getLoggerNames()
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   592
        //
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   593
        private void ensureInitialized() {
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   594
            if (requiresDefaultLoggers()) {
18593
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   595
                // Ensure that the root and global loggers are set.
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   596
                ensureDefaultLogger(getRootLogger());
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   597
                ensureDefaultLogger(getGlobalLogger());
18593
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   598
            }
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   599
        }
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   600
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   601
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   602
        synchronized Logger findLogger(String name) {
18593
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   603
            // ensure that this context is properly initialized before
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   604
            // looking for loggers.
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   605
            ensureInitialized();
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   606
            LoggerWeakRef ref = namedLoggers.get(name);
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   607
            if (ref == null) {
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   608
                return null;
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   609
            }
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   610
            Logger logger = ref.get();
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   611
            if (logger == null) {
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   612
                // Hashtable holds stale weak reference
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   613
                // to a logger which has been GC-ed.
21960
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
   614
                ref.dispose();
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   615
            }
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   616
            return logger;
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   617
        }
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   618
18593
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   619
        // This method is called before adding a logger to the
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   620
        // context.
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   621
        // 'logger' is the context that will be added.
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   622
        // This method will ensure that the defaults loggers are added
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   623
        // before adding 'logger'.
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   624
        //
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   625
        private void ensureAllDefaultLoggers(Logger logger) {
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   626
            if (requiresDefaultLoggers()) {
18593
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   627
                final String name = logger.getName();
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   628
                if (!name.isEmpty()) {
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   629
                    ensureDefaultLogger(getRootLogger());
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   630
                    if (!Logger.GLOBAL_LOGGER_NAME.equals(name)) {
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   631
                        ensureDefaultLogger(getGlobalLogger());
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   632
                    }
18593
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   633
                }
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   634
            }
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   635
        }
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   636
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   637
        private void ensureDefaultLogger(Logger logger) {
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   638
            // Used for lazy addition of root logger and global logger
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   639
            // to a LoggerContext.
18202
e9a6d59491ab 8004584: Augment applet contextualization
leonidr
parents: 16117
diff changeset
   640
18593
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   641
            // This check is simple sanity: we do not want that this
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   642
            // method be called for anything else than Logger.global
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   643
            // or owner.rootLogger.
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   644
            if (!requiresDefaultLoggers() || logger == null
23342
d7a41da68b85 8036916: Deprecation warnings in LogManager
dfuchs
parents: 22342
diff changeset
   645
                    || logger != getGlobalLogger() && logger != LogManager.this.rootLogger ) {
18593
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   646
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   647
                // the case where we have a non null logger which is neither
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   648
                // Logger.global nor manager.rootLogger indicates a serious
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   649
                // issue - as ensureDefaultLogger should never be called
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   650
                // with any other loggers than one of these two (or null - if
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   651
                // e.g manager.rootLogger is not yet initialized)...
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   652
                assert logger == null;
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   653
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   654
                return;
18202
e9a6d59491ab 8004584: Augment applet contextualization
leonidr
parents: 16117
diff changeset
   655
            }
18593
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   656
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   657
            // Adds the logger if it's not already there.
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   658
            if (!namedLoggers.containsKey(logger.getName())) {
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   659
                // It is important to prevent addLocalLogger to
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   660
                // call ensureAllDefaultLoggers when we're in the process
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   661
                // off adding one of those default loggers - as this would
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   662
                // immediately cause a stack overflow.
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   663
                // Therefore we must pass addDefaultLoggersIfNeeded=false,
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   664
                // even if requiresDefaultLoggers is true.
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   665
                addLocalLogger(logger, false);
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   666
            }
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   667
        }
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   668
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   669
        boolean addLocalLogger(Logger logger) {
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   670
            // no need to add default loggers if it's not required
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   671
            return addLocalLogger(logger, requiresDefaultLoggers());
18202
e9a6d59491ab 8004584: Augment applet contextualization
leonidr
parents: 16117
diff changeset
   672
        }
e9a6d59491ab 8004584: Augment applet contextualization
leonidr
parents: 16117
diff changeset
   673
16105
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   674
        // Add a logger to this context.  This method will only set its level
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   675
        // and process parent loggers.  It doesn't set its handlers.
18593
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   676
        synchronized boolean addLocalLogger(Logger logger, boolean addDefaultLoggersIfNeeded) {
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   677
            // addDefaultLoggersIfNeeded serves to break recursion when adding
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   678
            // default loggers. If we're adding one of the default loggers
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   679
            // (we're being called from ensureDefaultLogger()) then
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   680
            // addDefaultLoggersIfNeeded will be false: we don't want to
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   681
            // call ensureAllDefaultLoggers again.
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   682
            //
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   683
            // Note: addDefaultLoggersIfNeeded can also be false when
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   684
            //       requiresDefaultLoggers is false - since calling
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   685
            //       ensureAllDefaultLoggers would have no effect in this case.
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   686
            if (addDefaultLoggersIfNeeded) {
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   687
                ensureAllDefaultLoggers(logger);
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   688
            }
18202
e9a6d59491ab 8004584: Augment applet contextualization
leonidr
parents: 16117
diff changeset
   689
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   690
            final String name = logger.getName();
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   691
            if (name == null) {
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   692
                throw new NullPointerException();
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   693
            }
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   694
            LoggerWeakRef ref = namedLoggers.get(name);
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   695
            if (ref != null) {
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   696
                if (ref.get() == null) {
17164
1304199c8641 8010939: Deadlock in LogManager
jgish
parents: 16117
diff changeset
   697
                    // It's possible that the Logger was GC'ed after a
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   698
                    // drainLoggerRefQueueBounded() call above so allow
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   699
                    // a new one to be registered.
21960
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
   700
                    ref.dispose();
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   701
                } else {
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   702
                    // We already have a registered logger with the given name.
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   703
                    return false;
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   704
                }
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   705
            }
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   706
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   707
            // We're adding a new logger.
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   708
            // Note that we are creating a weak reference here.
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   709
            final LogManager owner = getOwner();
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   710
            logger.setLogManager(owner);
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   711
            ref = owner.new LoggerWeakRef(logger);
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   712
            namedLoggers.put(name, ref);
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   713
21304
7971ecf0fbed 8026499: Root Logger level can be reset unexpectedly
dfuchs
parents: 19825
diff changeset
   714
            // Apply any initial level defined for the new logger, unless
7971ecf0fbed 8026499: Root Logger level can be reset unexpectedly
dfuchs
parents: 19825
diff changeset
   715
            // the logger's level is already initialized
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   716
            Level level = owner.getLevelProperty(name + ".level", null);
21304
7971ecf0fbed 8026499: Root Logger level can be reset unexpectedly
dfuchs
parents: 19825
diff changeset
   717
            if (level != null && !logger.isLevelInitialized()) {
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   718
                doSetLevel(logger, level);
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   719
            }
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   720
16105
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   721
            // instantiation of the handler is done in the LogManager.addLogger
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   722
            // implementation as a handler class may be only visible to LogManager
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   723
            // subclass for the custom log manager case
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   724
            processParentHandlers(logger, name);
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   725
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   726
            // Find the new node and its parent.
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   727
            LogNode node = getNode(name);
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   728
            node.loggerRef = ref;
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   729
            Logger parent = null;
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   730
            LogNode nodep = node.parent;
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   731
            while (nodep != null) {
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   732
                LoggerWeakRef nodeRef = nodep.loggerRef;
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   733
                if (nodeRef != null) {
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   734
                    parent = nodeRef.get();
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   735
                    if (parent != null) {
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   736
                        break;
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   737
                    }
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   738
                }
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   739
                nodep = nodep.parent;
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   740
            }
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   741
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   742
            if (parent != null) {
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   743
                doSetParent(logger, parent);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
            }
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   745
            // Walk over the children and tell them we are their new parent.
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   746
            node.walkAndSetParent(logger);
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   747
            // new LogNode is ready so tell the LoggerWeakRef about it
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   748
            ref.setNode(node);
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   749
            return true;
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   750
        }
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   751
21960
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
   752
        synchronized void removeLoggerRef(String name, LoggerWeakRef ref) {
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
   753
            namedLoggers.remove(name, ref);
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   754
        }
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   755
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   756
        synchronized Enumeration<String> getLoggerNames() {
18593
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   757
            // ensure that this context is properly initialized before
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   758
            // returning logger names.
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   759
            ensureInitialized();
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   760
            return namedLoggers.keys();
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   761
        }
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   762
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   763
        // If logger.getUseParentHandlers() returns 'true' and any of the logger's
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   764
        // parents have levels or handlers defined, make sure they are instantiated.
16105
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   765
        private void processParentHandlers(final Logger logger, final String name) {
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   766
            final LogManager owner = getOwner();
16105
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   767
            AccessController.doPrivileged(new PrivilegedAction<Void>() {
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   768
                @Override
16105
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   769
                public Void run() {
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   770
                    if (logger != owner.rootLogger) {
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   771
                        boolean useParent = owner.getBooleanProperty(name + ".useParentHandlers", true);
16105
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   772
                        if (!useParent) {
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   773
                            logger.setUseParentHandlers(false);
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   774
                        }
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   775
                    }
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   776
                    return null;
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   777
                }
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   778
            });
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   779
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   780
            int ix = 1;
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   781
            for (;;) {
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   782
                int ix2 = name.indexOf(".", ix);
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   783
                if (ix2 < 0) {
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   784
                    break;
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   785
                }
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   786
                String pname = name.substring(0, ix2);
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   787
                if (owner.getProperty(pname + ".level") != null ||
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   788
                    owner.getProperty(pname + ".handlers") != null) {
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   789
                    // This pname has a level/handlers definition.
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   790
                    // Make sure it exists.
16105
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   791
                    demandLogger(pname, null);
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   792
                }
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   793
                ix = ix2+1;
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   794
            }
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   795
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   797
        // Gets a node in our tree of logger nodes.
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   798
        // If necessary, create it.
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   799
        LogNode getNode(String name) {
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   800
            if (name == null || name.equals("")) {
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   801
                return root;
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   802
            }
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   803
            LogNode node = root;
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   804
            while (name.length() > 0) {
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   805
                int ix = name.indexOf(".");
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   806
                String head;
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   807
                if (ix > 0) {
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   808
                    head = name.substring(0, ix);
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   809
                    name = name.substring(ix + 1);
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   810
                } else {
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   811
                    head = name;
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   812
                    name = "";
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   813
                }
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   814
                if (node.children == null) {
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   815
                    node.children = new HashMap<>();
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   816
                }
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   817
                LogNode child = node.children.get(head);
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   818
                if (child == null) {
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   819
                    child = new LogNode(node, this);
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   820
                    node.children.put(head, child);
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   821
                }
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   822
                node = child;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
            }
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   824
            return node;
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   825
        }
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   826
    }
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   827
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   828
    final class SystemLoggerContext extends LoggerContext {
16105
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   829
        // Add a system logger in the system context's namespace as well as
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   830
        // in the LogManager's namespace if not exist so that there is only
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   831
        // one single logger of the given name.  System loggers are visible
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   832
        // to applications unless a logger of the same name has been added.
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   833
        @Override
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   834
        Logger demandLogger(String name, String resourceBundleName) {
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   835
            Logger result = findLogger(name);
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   836
            if (result == null) {
16105
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   837
                // only allocate the new system logger once
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   838
                Logger newLogger = new Logger(name, resourceBundleName, null, getOwner());
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   839
                do {
16105
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   840
                    if (addLocalLogger(newLogger)) {
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   841
                        // We successfully added the new Logger that we
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   842
                        // created above so return it without refetching.
16105
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   843
                        result = newLogger;
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   844
                    } else {
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   845
                        // We didn't add the new Logger that we created above
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   846
                        // because another thread added a Logger with the same
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   847
                        // name after our null check above and before our call
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   848
                        // to addLogger(). We have to refetch the Logger because
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   849
                        // addLogger() returns a boolean instead of the Logger
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   850
                        // reference itself. However, if the thread that created
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   851
                        // the other Logger is not holding a strong reference to
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   852
                        // the other Logger, then it is possible for the other
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   853
                        // Logger to be GC'ed after we saw it in addLogger() and
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   854
                        // before we can refetch it. If it has been GC'ed then
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   855
                        // we'll just loop around and try again.
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   856
                        result = findLogger(name);
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   857
                    }
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   858
                } while (result == null);
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   859
            }
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   860
            return result;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
    // Add new per logger handlers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
    // We need to raise privilege here. All our decisions will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
    // be made based on the logging configuration, which can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
    // only be modified by trusted code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
    private void loadLoggerHandlers(final Logger logger, final String name,
16105
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   869
                                    final String handlersPropertyName)
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   870
    {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
        AccessController.doPrivileged(new PrivilegedAction<Object>() {
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   872
            @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
            public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
                String names[] = parseClassNames(handlersPropertyName);
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 22046
diff changeset
   875
                for (String word : names) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
                    try {
11274
7e7196757acd 7117249: fix warnings in java.util.jar, .logging, .prefs, .zip
smarks
parents: 9700
diff changeset
   877
                        Class<?> clz = ClassLoader.getSystemClassLoader().loadClass(word);
16105
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   878
                        Handler hdl = (Handler) clz.newInstance();
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   879
                        // Check if there is a property defining the
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   880
                        // this handler's level.
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   881
                        String levs = getProperty(word + ".level");
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   882
                        if (levs != null) {
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   883
                            Level l = Level.findLevel(levs);
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   884
                            if (l != null) {
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   885
                                hdl.setLevel(l);
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   886
                            } else {
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   887
                                // Probably a bad level. Drop through.
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   888
                                System.err.println("Can't set level for " + word);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
                        // Add this Handler to the logger
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
                        logger.addHandler(hdl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
                    } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
                        System.err.println("Can't load log handler \"" + word + "\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
                        System.err.println("" + ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
                        ex.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
                return null;
16105
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   900
            }
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   901
        });
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
5964
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   904
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   905
    // loggerRefQueue holds LoggerWeakRef objects for Logger objects
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   906
    // that have been GC'ed.
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   907
    private final ReferenceQueue<Logger> loggerRefQueue
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 6675
diff changeset
   908
        = new ReferenceQueue<>();
5964
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   909
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   910
    // Package-level inner class.
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   911
    // Helper class for managing WeakReferences to Logger objects.
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   912
    //
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   913
    // LogManager.namedLoggers
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   914
    //     - has weak references to all named Loggers
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   915
    //     - namedLoggers keeps the LoggerWeakRef objects for the named
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   916
    //       Loggers around until we can deal with the book keeping for
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   917
    //       the named Logger that is being GC'ed.
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   918
    // LogManager.LogNode.loggerRef
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   919
    //     - has a weak reference to a named Logger
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   920
    //     - the LogNode will also keep the LoggerWeakRef objects for
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   921
    //       the named Loggers around; currently LogNodes never go away.
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   922
    // Logger.kids
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   923
    //     - has a weak reference to each direct child Logger; this
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   924
    //       includes anonymous and named Loggers
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   925
    //     - anonymous Loggers are always children of the rootLogger
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   926
    //       which is a strong reference; rootLogger.kids keeps the
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   927
    //       LoggerWeakRef objects for the anonymous Loggers around
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   928
    //       until we can deal with the book keeping.
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   929
    //
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   930
    final class LoggerWeakRef extends WeakReference<Logger> {
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   931
        private String                name;       // for namedLoggers cleanup
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   932
        private LogNode               node;       // for loggerRef cleanup
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   933
        private WeakReference<Logger> parentRef;  // for kids cleanup
21960
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
   934
        private boolean disposed = false;         // avoid calling dispose twice
5964
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   935
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   936
        LoggerWeakRef(Logger logger) {
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   937
            super(logger, loggerRefQueue);
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   938
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   939
            name = logger.getName();  // save for namedLoggers cleanup
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   940
        }
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   941
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   942
        // dispose of this LoggerWeakRef object
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   943
        void dispose() {
21960
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
   944
            // Avoid calling dispose twice. When a Logger is gc'ed, its
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
   945
            // LoggerWeakRef will be enqueued.
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
   946
            // However, a new logger of the same name may be added (or looked
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
   947
            // up) before the queue is drained. When that happens, dispose()
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
   948
            // will be called by addLocalLogger() or findLogger().
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
   949
            // Later when the queue is drained, dispose() will be called again
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
   950
            // for the same LoggerWeakRef. Marking LoggerWeakRef as disposed
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
   951
            // avoids processing the data twice (even though the code should
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
   952
            // now be reentrant).
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
   953
            synchronized(this) {
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
   954
                // Note to maintainers:
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
   955
                // Be careful not to call any method that tries to acquire
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
   956
                // another lock from within this block - as this would surely
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
   957
                // lead to deadlocks, given that dispose() can be called by
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
   958
                // multiple threads, and from within different synchronized
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
   959
                // methods/blocks.
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
   960
                if (disposed) return;
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
   961
                disposed = true;
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
   962
            }
5964
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   963
21960
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
   964
            final LogNode n = node;
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
   965
            if (n != null) {
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
   966
                // n.loggerRef can only be safely modified from within
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
   967
                // a lock on LoggerContext. removeLoggerRef is already
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
   968
                // synchronized on LoggerContext so calling
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
   969
                // n.context.removeLoggerRef from within this lock is safe.
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
   970
                synchronized (n.context) {
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
   971
                    // if we have a LogNode, then we were a named Logger
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
   972
                    // so clear namedLoggers weak ref to us
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
   973
                    n.context.removeLoggerRef(name, this);
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
   974
                    name = null;  // clear our ref to the Logger's name
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
   975
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
   976
                    // LogNode may have been reused - so only clear
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
   977
                    // LogNode.loggerRef if LogNode.loggerRef == this
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
   978
                    if (n.loggerRef == this) {
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
   979
                        n.loggerRef = null;  // clear LogNode's weak ref to us
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
   980
                    }
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
   981
                    node = null;            // clear our ref to LogNode
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
   982
                }
5964
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   983
            }
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   984
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   985
            if (parentRef != null) {
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   986
                // this LoggerWeakRef has or had a parent Logger
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   987
                Logger parent = parentRef.get();
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   988
                if (parent != null) {
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   989
                    // the parent Logger is still there so clear the
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   990
                    // parent Logger's weak ref to us
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   991
                    parent.removeChildLogger(this);
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   992
                }
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   993
                parentRef = null;  // clear our weak ref to the parent Logger
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   994
            }
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   995
        }
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   996
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   997
        // set the node field to the specified value
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   998
        void setNode(LogNode node) {
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   999
            this.node = node;
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1000
        }
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1001
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1002
        // set the parentRef field to the specified value
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1003
        void setParentRef(WeakReference<Logger> parentRef) {
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1004
            this.parentRef = parentRef;
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1005
        }
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1006
    }
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1007
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1008
    // Package-level method.
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1009
    // Drain some Logger objects that have been GC'ed.
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1010
    //
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1011
    // drainLoggerRefQueueBounded() is called by addLogger() below
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1012
    // and by Logger.getAnonymousLogger(String) so we'll drain up to
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1013
    // MAX_ITERATIONS GC'ed Loggers for every Logger we add.
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1014
    //
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1015
    // On a WinXP VMware client, a MAX_ITERATIONS value of 400 gives
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1016
    // us about a 50/50 mix in increased weak ref counts versus
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1017
    // decreased weak ref counts in the AnonLoggerWeakRefLeak test.
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1018
    // Here are stats for cleaning up sets of 400 anonymous Loggers:
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1019
    //   - test duration 1 minute
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1020
    //   - sample size of 125 sets of 400
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1021
    //   - average: 1.99 ms
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1022
    //   - minimum: 0.57 ms
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1023
    //   - maximum: 25.3 ms
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1024
    //
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1025
    // The same config gives us a better decreased weak ref count
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1026
    // than increased weak ref count in the LoggerWeakRefLeak test.
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1027
    // Here are stats for cleaning up sets of 400 named Loggers:
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1028
    //   - test duration 2 minutes
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1029
    //   - sample size of 506 sets of 400
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1030
    //   - average: 0.57 ms
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1031
    //   - minimum: 0.02 ms
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1032
    //   - maximum: 10.9 ms
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1033
    //
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1034
    private final static int MAX_ITERATIONS = 400;
21960
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
  1035
    final void drainLoggerRefQueueBounded() {
5964
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1036
        for (int i = 0; i < MAX_ITERATIONS; i++) {
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1037
            if (loggerRefQueue == null) {
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1038
                // haven't finished loading LogManager yet
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1039
                break;
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1040
            }
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1041
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1042
            LoggerWeakRef ref = (LoggerWeakRef) loggerRefQueue.poll();
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1043
            if (ref == null) {
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1044
                break;
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1045
            }
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1046
            // a Logger object has been GC'ed so clean it up
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1047
            ref.dispose();
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1048
        }
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1049
    }
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1050
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
     * Add a named logger.  This does nothing and returns false if a logger
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
     * with the same name is already registered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
     * The Logger factory methods call this method to register each
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
     * newly created Logger.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
     * The application should retain its own reference to the Logger
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
     * object to avoid it being garbage collected.  The LogManager
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
     * may only retain a weak reference.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
     * @param   logger the new logger.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
     * @return  true if the argument logger was registered successfully,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
     *          false if a logger of that name already exists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
     * @exception NullPointerException if the logger name is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
     */
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
  1067
    public boolean addLogger(Logger logger) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
        final String name = logger.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
        if (name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
        }
17164
1304199c8641 8010939: Deadlock in LogManager
jgish
parents: 16117
diff changeset
  1072
        drainLoggerRefQueueBounded();
16105
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
  1073
        LoggerContext cx = getUserContext();
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
  1074
        if (cx.addLocalLogger(logger)) {
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
  1075
            // Do we have a per logger handler too?
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
  1076
            // Note: this will add a 200ms penalty
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
  1077
            loadLoggerHandlers(logger, name, name + ".handlers");
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
  1078
            return true;
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
  1079
        } else {
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
  1080
            return false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
    // Private method to set a level on a logger.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
    // If necessary, we raise privilege before doing the call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
    private static void doSetLevel(final Logger logger, final Level level) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
        if (sm == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
            // There is no security manager, so things are easy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
            logger.setLevel(level);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
        // There is a security manager.  Raise privilege before
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
        // calling setLevel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
        AccessController.doPrivileged(new PrivilegedAction<Object>() {
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
  1096
            @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
            public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
                logger.setLevel(level);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
            }});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
    // Private method to set a parent on a logger.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
    // If necessary, we raise privilege before doing the setParent call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
    private static void doSetParent(final Logger logger, final Logger parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
        if (sm == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
            // There is no security manager, so things are easy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
            logger.setParent(parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
        // There is a security manager.  Raise privilege before
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
        // calling setParent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
        AccessController.doPrivileged(new PrivilegedAction<Object>() {
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
  1115
            @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
            public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
                logger.setParent(parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
            }});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
     * Method to find a named logger.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
     * Note that since untrusted code may create loggers with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
     * arbitrary names this method should not be relied on to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
     * find Loggers for security sensitive logging.
6675
c86763d8f1c7 6949710: 3/3 the GC'able nature of Logging objects needs to be made brutally clear
dcubed
parents: 5964
diff changeset
  1128
     * It is also important to note that the Logger associated with the
c86763d8f1c7 6949710: 3/3 the GC'able nature of Logging objects needs to be made brutally clear
dcubed
parents: 5964
diff changeset
  1129
     * String {@code name} may be garbage collected at any time if there
c86763d8f1c7 6949710: 3/3 the GC'able nature of Logging objects needs to be made brutally clear
dcubed
parents: 5964
diff changeset
  1130
     * is no strong reference to the Logger. The caller of this method
c86763d8f1c7 6949710: 3/3 the GC'able nature of Logging objects needs to be made brutally clear
dcubed
parents: 5964
diff changeset
  1131
     * must check the return value for null in order to properly handle
c86763d8f1c7 6949710: 3/3 the GC'able nature of Logging objects needs to be made brutally clear
dcubed
parents: 5964
diff changeset
  1132
     * the case where the Logger has been garbage collected.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
     * @param name name of the logger
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
     * @return  matching logger or null if none is found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
     */
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
  1137
    public Logger getLogger(String name) {
16105
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
  1138
        return getUserContext().findLogger(name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
     * Get an enumeration of known logger names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
     * Note:  Loggers may be added dynamically as new classes are loaded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
     * This method only reports on the loggers that are currently registered.
6675
c86763d8f1c7 6949710: 3/3 the GC'able nature of Logging objects needs to be made brutally clear
dcubed
parents: 5964
diff changeset
  1146
     * It is also important to note that this method only returns the name
c86763d8f1c7 6949710: 3/3 the GC'able nature of Logging objects needs to be made brutally clear
dcubed
parents: 5964
diff changeset
  1147
     * of a Logger, not a strong reference to the Logger itself.
c86763d8f1c7 6949710: 3/3 the GC'able nature of Logging objects needs to be made brutally clear
dcubed
parents: 5964
diff changeset
  1148
     * The returned String does nothing to prevent the Logger from being
c86763d8f1c7 6949710: 3/3 the GC'able nature of Logging objects needs to be made brutally clear
dcubed
parents: 5964
diff changeset
  1149
     * garbage collected. In particular, if the returned name is passed
c86763d8f1c7 6949710: 3/3 the GC'able nature of Logging objects needs to be made brutally clear
dcubed
parents: 5964
diff changeset
  1150
     * to {@code LogManager.getLogger()}, then the caller must check the
c86763d8f1c7 6949710: 3/3 the GC'able nature of Logging objects needs to be made brutally clear
dcubed
parents: 5964
diff changeset
  1151
     * return value from {@code LogManager.getLogger()} for null to properly
c86763d8f1c7 6949710: 3/3 the GC'able nature of Logging objects needs to be made brutally clear
dcubed
parents: 5964
diff changeset
  1152
     * handle the case where the Logger has been garbage collected in the
c86763d8f1c7 6949710: 3/3 the GC'able nature of Logging objects needs to be made brutally clear
dcubed
parents: 5964
diff changeset
  1153
     * time since its name was returned by this method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
     * @return  enumeration of logger name strings
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
     */
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
  1157
    public Enumeration<String> getLoggerNames() {
16105
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
  1158
        return getUserContext().getLoggerNames();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
     * Reinitialize the logging properties and reread the logging configuration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
     * The same rules are used for locating the configuration properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
     * as are used at startup.  So normally the logging properties will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
     * be re-read from the same file that was used at startup.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
     * Any log level definitions in the new configuration file will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
     * applied using Logger.setLevel(), if the target Logger exists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
     * A PropertyChangeEvent will be fired after the properties are read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
     * @exception  SecurityException  if a security manager exists and if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
     *             the caller does not have LoggingPermission("control").
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
     * @exception  IOException if there are IO problems reading the configuration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
    public void readConfiguration() throws IOException, SecurityException {
14216
23714b376286 7169884: LogManager checks do not work correctly for sub-types
alanb
parents: 13578
diff changeset
  1178
        checkPermission();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
        // if a configuration class is specified, load it and use it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
        String cname = System.getProperty("java.util.logging.config.class");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
        if (cname != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
                // Instantiate the named class.  It is its constructor's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
                // responsibility to initialize the logging configuration, by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
                // calling readConfiguration(InputStream) with a suitable stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
                try {
11274
7e7196757acd 7117249: fix warnings in java.util.jar, .logging, .prefs, .zip
smarks
parents: 9700
diff changeset
  1188
                    Class<?> clz = ClassLoader.getSystemClassLoader().loadClass(cname);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
                    clz.newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
                } catch (ClassNotFoundException ex) {
11274
7e7196757acd 7117249: fix warnings in java.util.jar, .logging, .prefs, .zip
smarks
parents: 9700
diff changeset
  1192
                    Class<?> clz = Thread.currentThread().getContextClassLoader().loadClass(cname);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
                    clz.newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
            } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
                System.err.println("Logging configuration class \"" + cname + "\" failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
                System.err.println("" + ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
                // keep going and useful config file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
        String fname = System.getProperty("java.util.logging.config.file");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
        if (fname == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
            fname = System.getProperty("java.home");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
            if (fname == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
                throw new Error("Can't find java.home ??");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
            File f = new File(fname, "lib");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
            f = new File(f, "logging.properties");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
            fname = f.getCanonicalPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
        }
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
  1213
        try (final InputStream in = new FileInputStream(fname)) {
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
  1214
            final BufferedInputStream bin = new BufferedInputStream(in);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
            readConfiguration(bin);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
     * Reset the logging configuration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
     * For all named loggers, the reset operation removes and closes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
     * all Handlers and (except for the root logger) sets the level
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
     * to null.  The root logger's level is set to Level.INFO.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
     * @exception  SecurityException  if a security manager exists and if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
     *             the caller does not have LoggingPermission("control").
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
    public void reset() throws SecurityException {
14216
23714b376286 7169884: LogManager checks do not work correctly for sub-types
alanb
parents: 13578
diff changeset
  1231
        checkPermission();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
            props = new Properties();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
            // Since we are doing a reset we no longer want to initialize
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
            // the global handlers, if they haven't been initialized yet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
            initializedGlobalHandlers = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
        }
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
  1238
        for (LoggerContext cx : contexts()) {
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
  1239
            Enumeration<String> enum_ = cx.getLoggerNames();
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
  1240
            while (enum_.hasMoreElements()) {
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
  1241
                String name = enum_.nextElement();
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
  1242
                Logger logger = cx.findLogger(name);
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
  1243
                if (logger != null) {
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
  1244
                    resetLogger(logger);
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
  1245
                }
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
  1246
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
    // Private method to reset an individual target logger.
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
  1251
    private void resetLogger(Logger logger) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
        // Close all the Logger's handlers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
        Handler[] targets = logger.getHandlers();
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 22046
diff changeset
  1254
        for (Handler h : targets) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
            logger.removeHandler(h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
                h.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
            } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
                // Problems closing a handler?  Keep going...
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
        }
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
  1262
        String name = logger.getName();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
        if (name != null && name.equals("")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
            // This is the root logger.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
            logger.setLevel(defaultLevel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
            logger.setLevel(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
    // get a list of whitespace separated classnames from a property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
    private String[] parseClassNames(String propertyName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
        String hands = getProperty(propertyName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
        if (hands == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
            return new String[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
        hands = hands.trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
        int ix = 0;
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
  1279
        final List<String> result = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
        while (ix < hands.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
            int end = ix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
            while (end < hands.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
                if (Character.isWhitespace(hands.charAt(end))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
                if (hands.charAt(end) == ',') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
                end++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
            String word = hands.substring(ix, end);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
            ix = end+1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
            word = word.trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
            if (word.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
            result.add(word);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
        return result.toArray(new String[result.size()]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
     * Reinitialize the logging properties and reread the logging configuration
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
     * from the given stream, which should be in java.util.Properties format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
     * A PropertyChangeEvent will be fired after the properties are read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
     * Any log level definitions in the new configuration file will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
     * applied using Logger.setLevel(), if the target Logger exists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
     * @param ins       stream to read properties from
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
     * @exception  SecurityException  if a security manager exists and if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
     *             the caller does not have LoggingPermission("control").
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
     * @exception  IOException if there are problems reading from the stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
    public void readConfiguration(InputStream ins) throws IOException, SecurityException {
14216
23714b376286 7169884: LogManager checks do not work correctly for sub-types
alanb
parents: 13578
diff changeset
  1316
        checkPermission();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
        reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
        // Load the properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
        props.load(ins);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
        // Instantiate new configuration objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
        String names[] = parseClassNames("config");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 22046
diff changeset
  1324
        for (String word : names) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
            try {
11274
7e7196757acd 7117249: fix warnings in java.util.jar, .logging, .prefs, .zip
smarks
parents: 9700
diff changeset
  1326
                Class<?> clz = ClassLoader.getSystemClassLoader().loadClass(word);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
                clz.newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
            } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
                System.err.println("Can't load config class \"" + word + "\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
                System.err.println("" + ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
                // ex.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
        // Set levels on any pre-existing loggers, based on the new properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
        setLevelsOnExistingLoggers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
        // Note that we need to reinitialize global handles when
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
        // they are first referenced.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
            initializedGlobalHandlers = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
     * Get the value of a logging property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
     * The method returns null if the property is not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
     * @param name      property name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
     * @return          property value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
    public String getProperty(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
        return props.getProperty(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
    // Package private method to get a String property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
    // If the property is not defined we return the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
    // default value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
    String getStringProperty(String name, String defaultValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
        String val = getProperty(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
        if (val == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
            return defaultValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
        return val.trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
    // Package private method to get an integer property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
    // If the property is not defined or cannot be parsed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
    // we return the given default value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
    int getIntProperty(String name, int defaultValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
        String val = getProperty(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
        if (val == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
            return defaultValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
            return Integer.parseInt(val.trim());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
        } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
            return defaultValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
    // Package private method to get a boolean property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
    // If the property is not defined or cannot be parsed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
    // we return the given default value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
    boolean getBooleanProperty(String name, boolean defaultValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
        String val = getProperty(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
        if (val == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
            return defaultValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
        val = val.toLowerCase();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
        if (val.equals("true") || val.equals("1")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
        } else if (val.equals("false") || val.equals("0")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
        return defaultValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
    // Package private method to get a Level property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
    // If the property is not defined or cannot be parsed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
    // we return the given default value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
    Level getLevelProperty(String name, Level defaultValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
        String val = getProperty(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
        if (val == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
            return defaultValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
        }
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
  1406
        Level l = Level.findLevel(val.trim());
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
  1407
        return l != null ? l : defaultValue;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
    // Package private method to get a filter property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
    // We return an instance of the class named by the "name"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
    // property. If the property is not defined or has problems
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
    // we return the defaultValue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
    Filter getFilterProperty(String name, Filter defaultValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
        String val = getProperty(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
            if (val != null) {
11274
7e7196757acd 7117249: fix warnings in java.util.jar, .logging, .prefs, .zip
smarks
parents: 9700
diff changeset
  1418
                Class<?> clz = ClassLoader.getSystemClassLoader().loadClass(val);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
                return (Filter) clz.newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
        } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
            // We got one of a variety of exceptions in creating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
            // class or creating an instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
            // Drop through.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
        // We got an exception.  Return the defaultValue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
        return defaultValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
    // Package private method to get a formatter property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
    // We return an instance of the class named by the "name"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
    // property. If the property is not defined or has problems
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
    // we return the defaultValue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
    Formatter getFormatterProperty(String name, Formatter defaultValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
        String val = getProperty(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
            if (val != null) {
11274
7e7196757acd 7117249: fix warnings in java.util.jar, .logging, .prefs, .zip
smarks
parents: 9700
diff changeset
  1439
                Class<?> clz = ClassLoader.getSystemClassLoader().loadClass(val);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
                return (Formatter) clz.newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
        } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
            // We got one of a variety of exceptions in creating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
            // class or creating an instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
            // Drop through.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
        // We got an exception.  Return the defaultValue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
        return defaultValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
    // Private method to load the global handlers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
    // We do the real work lazily, when the global handlers
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
    // are first used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
    private synchronized void initializeGlobalHandlers() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
        if (initializedGlobalHandlers) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
        initializedGlobalHandlers = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
        if (deathImminent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
            // Aaargh...
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
            // The VM is shutting down and our exit hook has been called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
            // Avoid allocating global handlers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
        loadLoggerHandlers(rootLogger, null, "handlers");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
22110
06e486bc20b6 8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents: 22086
diff changeset
  1470
    static final Permission controlPermission = new LoggingPermission("control", null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
14216
23714b376286 7169884: LogManager checks do not work correctly for sub-types
alanb
parents: 13578
diff changeset
  1472
    void checkPermission() {
23714b376286 7169884: LogManager checks do not work correctly for sub-types
alanb
parents: 13578
diff changeset
  1473
        SecurityManager sm = System.getSecurityManager();
23714b376286 7169884: LogManager checks do not work correctly for sub-types
alanb
parents: 13578
diff changeset
  1474
        if (sm != null)
23714b376286 7169884: LogManager checks do not work correctly for sub-types
alanb
parents: 13578
diff changeset
  1475
            sm.checkPermission(controlPermission);
23714b376286 7169884: LogManager checks do not work correctly for sub-types
alanb
parents: 13578
diff changeset
  1476
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
     * Check that the current context is trusted to modify the logging
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
     * configuration.  This requires LoggingPermission("control").
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
     * If the check fails we throw a SecurityException, otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
     * we return normally.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
     * @exception  SecurityException  if a security manager exists and if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
     *             the caller does not have LoggingPermission("control").
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
    public void checkAccess() throws SecurityException {
14216
23714b376286 7169884: LogManager checks do not work correctly for sub-types
alanb
parents: 13578
diff changeset
  1489
        checkPermission();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
    // Nested class to represent a node in our tree of named loggers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
    private static class LogNode {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
        HashMap<String,LogNode> children;
5964
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1495
        LoggerWeakRef loggerRef;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
        LogNode parent;
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
  1497
        final LoggerContext context;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
  1499
        LogNode(LogNode parent, LoggerContext context) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
            this.parent = parent;
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
  1501
            this.context = context;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
        // Recursive method to walk the tree below a node and set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
        // a new parent logger.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
        void walkAndSetParent(Logger parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
            if (children == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
            }
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 22046
diff changeset
  1510
            for (LogNode node : children.values()) {
5964
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1511
                LoggerWeakRef ref = node.loggerRef;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
                Logger logger = (ref == null) ? null : ref.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
                if (logger == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
                    node.walkAndSetParent(parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
                    doSetParent(logger, parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
    // We use a subclass of Logger for the root logger, so
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
    // that we only instantiate the global handlers when they
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
    // are first needed.
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
  1525
    private final class RootLogger extends Logger {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
        private RootLogger() {
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
  1527
            // We do not call the protected Logger two args constructor here,
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
  1528
            // to avoid calling LogManager.getLogManager() from within the
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
  1529
            // RootLogger constructor.
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
  1530
            super("", null, null, LogManager.this);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
  1533
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
        public void log(LogRecord record) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
            // Make sure that the global handlers have been instantiated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
            initializeGlobalHandlers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
            super.log(record);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
  1540
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
        public void addHandler(Handler h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
            initializeGlobalHandlers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
            super.addHandler(h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
  1546
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
        public void removeHandler(Handler h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
            initializeGlobalHandlers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
            super.removeHandler(h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
  1552
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
        public Handler[] getHandlers() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
            initializeGlobalHandlers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
            return super.getHandlers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
    // Private method to be called when the configuration has
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
    // changed to apply any level settings to any pre-existing loggers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
    synchronized private void setLevelsOnExistingLoggers() {
11274
7e7196757acd 7117249: fix warnings in java.util.jar, .logging, .prefs, .zip
smarks
parents: 9700
diff changeset
  1563
        Enumeration<?> enum_ = props.propertyNames();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
        while (enum_.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
            String key = (String)enum_.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
            if (!key.endsWith(".level")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
                // Not a level definition.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
            int ix = key.length() - 6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
            String name = key.substring(0, ix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
            Level level = getLevelProperty(key, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
            if (level == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
                System.err.println("Bad level value for property: " + key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
            }
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
  1577
            for (LoggerContext cx : contexts()) {
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
  1578
                Logger l = cx.findLogger(name);
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
  1579
                if (l == null) {
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
  1580
                    continue;
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
  1581
                }
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
  1582
                l.setLevel(level);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
    // Management Support
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
    private static LoggingMXBean loggingMXBean = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
     * String representation of the
9013
eedac0b9f552 7024172: Move BufferPoolMXBean and PlatformLoggingMXBean java.lang.management
mchung
parents: 7803
diff changeset
  1591
     * {@link javax.management.ObjectName} for the management interface
eedac0b9f552 7024172: Move BufferPoolMXBean and PlatformLoggingMXBean java.lang.management
mchung
parents: 7803
diff changeset
  1592
     * for the logging facility.
eedac0b9f552 7024172: Move BufferPoolMXBean and PlatformLoggingMXBean java.lang.management
mchung
parents: 7803
diff changeset
  1593
     *
eedac0b9f552 7024172: Move BufferPoolMXBean and PlatformLoggingMXBean java.lang.management
mchung
parents: 7803
diff changeset
  1594
     * @see java.lang.management.PlatformLoggingMXBean
eedac0b9f552 7024172: Move BufferPoolMXBean and PlatformLoggingMXBean java.lang.management
mchung
parents: 7803
diff changeset
  1595
     * @see java.util.logging.LoggingMXBean
eedac0b9f552 7024172: Move BufferPoolMXBean and PlatformLoggingMXBean java.lang.management
mchung
parents: 7803
diff changeset
  1596
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
    public final static String LOGGING_MXBEAN_NAME
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
        = "java.util.logging:type=Logging";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
     * Returns <tt>LoggingMXBean</tt> for managing loggers.
9013
eedac0b9f552 7024172: Move BufferPoolMXBean and PlatformLoggingMXBean java.lang.management
mchung
parents: 7803
diff changeset
  1604
     * An alternative way to manage loggers is through the
eedac0b9f552 7024172: Move BufferPoolMXBean and PlatformLoggingMXBean java.lang.management
mchung
parents: 7803
diff changeset
  1605
     * {@link java.lang.management.PlatformLoggingMXBean} interface
eedac0b9f552 7024172: Move BufferPoolMXBean and PlatformLoggingMXBean java.lang.management
mchung
parents: 7803
diff changeset
  1606
     * that can be obtained by calling:
4172
bceea7bc12b7 6876135: Add PlatformLoggingMXBean to eliminate the dependency on JMX from logging
mchung
parents: 3861
diff changeset
  1607
     * <pre>
9013
eedac0b9f552 7024172: Move BufferPoolMXBean and PlatformLoggingMXBean java.lang.management
mchung
parents: 7803
diff changeset
  1608
     *     PlatformLoggingMXBean logging = {@link java.lang.management.ManagementFactory#getPlatformMXBean(Class)
eedac0b9f552 7024172: Move BufferPoolMXBean and PlatformLoggingMXBean java.lang.management
mchung
parents: 7803
diff changeset
  1609
     *         ManagementFactory.getPlatformMXBean}(PlatformLoggingMXBean.class);
4172
bceea7bc12b7 6876135: Add PlatformLoggingMXBean to eliminate the dependency on JMX from logging
mchung
parents: 3861
diff changeset
  1610
     * </pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
     * @return a {@link LoggingMXBean} object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
     *
9013
eedac0b9f552 7024172: Move BufferPoolMXBean and PlatformLoggingMXBean java.lang.management
mchung
parents: 7803
diff changeset
  1614
     * @see java.lang.management.PlatformLoggingMXBean
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
     */
9013
eedac0b9f552 7024172: Move BufferPoolMXBean and PlatformLoggingMXBean java.lang.management
mchung
parents: 7803
diff changeset
  1617
    public static synchronized LoggingMXBean getLoggingMXBean() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
        if (loggingMXBean == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
            loggingMXBean =  new Logging();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
        return loggingMXBean;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
}