jdk/src/java.logging/share/classes/java/util/logging/LogManager.java
author dfuchs
Thu, 06 Aug 2015 16:36:47 +0200
changeset 32035 073a449c007d
parent 31150 2aa1d300cd75
child 32037 ab4526f4ac10
permissions -rw-r--r--
8132550: java/util/logging/LoggingDeadlock2.java times out Summary: LogManager must also use the configurationLock when reading its primordial configuration. Reviewed-by: joehw
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
29739
3966f5f6a6fd 8075810: LogManager.readConfiguration may throw undocumented IllegalArgumentException
dfuchs
parents: 27932
diff changeset
     2
 * Copyright (c) 2000, 2015, 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;
29740
d9f64fdd3c97 7113878: LogManager - namedLoggers should be ConcurrentHashMap instead of Hashtable
dfuchs
parents: 29739
diff changeset
    34
import java.util.concurrent.ConcurrentHashMap;
27754
ee2f7768e1c9 8060132: Handlers configured on abstract nodes in logging.properties are not always properly closed
dfuchs
parents: 27191
diff changeset
    35
import java.util.concurrent.CopyOnWriteArrayList;
30643
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
    36
import java.util.concurrent.locks.ReentrantLock;
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
    37
import sun.misc.JavaAWTAccess;
29919
be906afc335b 8047149: Enhance thread contexts in core libraries
chegar
parents: 29740
diff changeset
    38
import sun.misc.ManagedLocalsThread;
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
    39
import sun.misc.SharedSecrets;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * There is a single global LogManager object that is used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * maintain a set of shared state about Loggers and log services.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * This LogManager object:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * <li> Manages a hierarchical namespace of Logger objects.  All
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *      named Loggers are stored in this namespace.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * <li> Manages a set of logging control properties.  These are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *      simple key-value pairs that can be used by Handlers and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *      other logging objects to configure themselves.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * The global LogManager object can be retrieved using LogManager.getLogManager().
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * The LogManager object is created during class initialization and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * cannot subsequently be changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * At startup the LogManager class is located using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * java.util.logging.manager system property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * <p>
14691
22056e110b04 8003949: LogManager, downgrade normative reference to ${java.home}/lib/logging.properties
alanb
parents: 14342
diff changeset
    61
 * 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
    62
 * the initial configuration:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * <li>"java.util.logging.config.class"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * <li>"java.util.logging.config.file"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * </ul>
14691
22056e110b04 8003949: LogManager, downgrade normative reference to ${java.home}/lib/logging.properties
alanb
parents: 14342
diff changeset
    67
 * 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
    68
 * command, or as system property definitions passed to JNI_CreateJavaVM.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * If the "java.util.logging.config.class" property is set, then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * property value is treated as a class name.  The given class will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * loaded, an object will be instantiated, and that object's constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * is responsible for reading in the initial configuration.  (That object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * may use other system properties to control its configuration.)  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * alternate configuration class can use <tt>readConfiguration(InputStream)</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * to define properties in the LogManager.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * If "java.util.logging.config.class" property is <b>not</b> set,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * then the "java.util.logging.config.file" system property can be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * to specify a properties file (in java.util.Properties format). The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * initial logging configuration will be read from this file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * <p>
14691
22056e110b04 8003949: LogManager, downgrade normative reference to ${java.home}/lib/logging.properties
alanb
parents: 14342
diff changeset
    83
 * 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
    84
 * default configuration. The default configuration is typically loaded from the
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27191
diff changeset
    85
 * properties file "{@code conf/logging.properties}" in the Java installation
14691
22056e110b04 8003949: LogManager, downgrade normative reference to ${java.home}/lib/logging.properties
alanb
parents: 14342
diff changeset
    86
 * directory.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * The properties for loggers and Handlers will have names starting
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * with the dot-separated name for the handler or logger.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * The global logging properties may include:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * <li>A property "handlers".  This defines a whitespace or comma separated
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * list of class names for handler classes to load and register as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * handlers on the root Logger (the Logger named "").  Each class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * name must be for a Handler class which has a default constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * Note that these Handlers may be created lazily, when they are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * first used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * <li>A property "&lt;logger&gt;.handlers". This defines a whitespace or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * comma separated list of class names for handlers classes to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * load and register as handlers to the specified logger. Each class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * name must be for a Handler class which has a default constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * Note that these Handlers may be created lazily, when they are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * first used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 *
27754
ee2f7768e1c9 8060132: Handlers configured on abstract nodes in logging.properties are not always properly closed
dfuchs
parents: 27191
diff changeset
   107
 * <li>A property "&lt;logger&gt;.handlers.ensureCloseOnReset". This defines a
ee2f7768e1c9 8060132: Handlers configured on abstract nodes in logging.properties are not always properly closed
dfuchs
parents: 27191
diff changeset
   108
 * a boolean value. If "&lt;logger&gt;.handlers" is not defined or is empty,
ee2f7768e1c9 8060132: Handlers configured on abstract nodes in logging.properties are not always properly closed
dfuchs
parents: 27191
diff changeset
   109
 * this property is ignored. Otherwise it defaults to {@code true}. When the
ee2f7768e1c9 8060132: Handlers configured on abstract nodes in logging.properties are not always properly closed
dfuchs
parents: 27191
diff changeset
   110
 * value is {@code true}, the handlers associated with the logger are guaranteed
ee2f7768e1c9 8060132: Handlers configured on abstract nodes in logging.properties are not always properly closed
dfuchs
parents: 27191
diff changeset
   111
 * to be closed on {@linkplain #reset} and shutdown. This can be turned off
ee2f7768e1c9 8060132: Handlers configured on abstract nodes in logging.properties are not always properly closed
dfuchs
parents: 27191
diff changeset
   112
 * by explicitly setting "&lt;logger&gt;.handlers.ensureCloseOnReset=false" in
ee2f7768e1c9 8060132: Handlers configured on abstract nodes in logging.properties are not always properly closed
dfuchs
parents: 27191
diff changeset
   113
 * the configuration. Note that turning this property off causes the risk of
ee2f7768e1c9 8060132: Handlers configured on abstract nodes in logging.properties are not always properly closed
dfuchs
parents: 27191
diff changeset
   114
 * introducing a resource leak, as the logger may get garbage collected before
ee2f7768e1c9 8060132: Handlers configured on abstract nodes in logging.properties are not always properly closed
dfuchs
parents: 27191
diff changeset
   115
 * {@code reset()} is called, thus preventing its handlers from being closed
ee2f7768e1c9 8060132: Handlers configured on abstract nodes in logging.properties are not always properly closed
dfuchs
parents: 27191
diff changeset
   116
 * on {@code reset()}. In that case it is the responsibility of the application
ee2f7768e1c9 8060132: Handlers configured on abstract nodes in logging.properties are not always properly closed
dfuchs
parents: 27191
diff changeset
   117
 * to ensure that the handlers are closed before the logger is garbage
ee2f7768e1c9 8060132: Handlers configured on abstract nodes in logging.properties are not always properly closed
dfuchs
parents: 27191
diff changeset
   118
 * collected.
ee2f7768e1c9 8060132: Handlers configured on abstract nodes in logging.properties are not always properly closed
dfuchs
parents: 27191
diff changeset
   119
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 * <li>A property "&lt;logger&gt;.useParentHandlers". This defines a boolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 * value. By default every logger calls its parent in addition to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 * handling the logging message itself, this often result in messages
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 * being handled by the root logger as well. When setting this property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 * to false a Handler needs to be configured for this logger otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 * no logging messages are delivered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 * <li>A property "config".  This property is intended to allow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 * arbitrary configuration code to be run.  The property defines a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 * whitespace or comma separated list of class names.  A new instance will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 * created for each named class.  The default constructor of each class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 * may execute arbitrary code to update the logging configuration, such as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 * setting logger levels, adding handlers, adding filters, etc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 * Note that all classes loaded during LogManager configuration are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 * first searched on the system class path before any user class path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 * That includes the LogManager class, any config classes, and any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
 * handler classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
 * Loggers are organized into a naming hierarchy based on their
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
 * dot separated names.  Thus "a.b.c" is a child of "a.b", but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
 * "a.b1" and a.b2" are peers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
 * All properties whose names end with ".level" are assumed to define
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
 * log levels for Loggers.  Thus "foo.level" defines a log level for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
 * the logger called "foo" and (recursively) for any of its children
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
 * in the naming hierarchy.  Log Levels are applied in the order they
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
 * are defined in the properties file.  Thus level settings for child
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
 * nodes in the tree should come after settings for their parents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
 * The property name ".level" can be used to set the level for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
 * root of the tree.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
 * All methods on the LogManager object are multi-thread safe.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
public class LogManager {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    // The global LogManager object
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   160
    private static final LogManager manager;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
21960
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
   162
    // 'props' is assigned within a lock but accessed without it.
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
   163
    // Declaring it volatile makes sure that another thread will not
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
   164
    // be able to see a partially constructed 'props' object.
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
   165
    // (seeing a partially constructed 'props' object can result in
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
   166
    // NPE being thrown in Hashtable.get(), because it leaves the door
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
   167
    // open for props.getProperties() to be called before the construcor
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
   168
    // of Hashtable is actually completed).
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
   169
    private volatile Properties props = new Properties();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    private final static Level defaultLevel = Level.INFO;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   172
    // LoggerContext for system loggers and user loggers
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   173
    private final LoggerContext systemContext = new SystemLoggerContext();
16105
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   174
    private final LoggerContext userContext = new LoggerContext();
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   175
    // 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
   176
    // will see the new value once ensureLogManagerInitialized() has finished
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   177
    // executing.
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   178
    private volatile Logger rootLogger;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    // Have we done the primordial reading of the configuration file?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    // (Must be done after a suitable amount of java.lang.System
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    // initialization has been done)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    private volatile boolean readPrimordialConfiguration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    // Have we initialized global (root) handlers yet?
30643
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
   184
    // This gets set to STATE_UNINITIALIZED in readConfiguration
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
   185
    private static final int
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
   186
            STATE_INITIALIZED = 0, // initial state
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
   187
            STATE_INITIALIZING = 1,
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
   188
            STATE_READING_CONFIG = 2,
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
   189
            STATE_UNINITIALIZED = 3,
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
   190
            STATE_SHUTDOWN = 4;    // terminal state
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
   191
    private volatile int globalHandlersState; // = STATE_INITIALIZED;
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
   192
    // A concurrency lock for reset(), readConfiguration() and Cleaner.
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
   193
    private final ReentrantLock configurationLock = new ReentrantLock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
27754
ee2f7768e1c9 8060132: Handlers configured on abstract nodes in logging.properties are not always properly closed
dfuchs
parents: 27191
diff changeset
   195
    // This list contains the loggers for which some handlers have been
ee2f7768e1c9 8060132: Handlers configured on abstract nodes in logging.properties are not always properly closed
dfuchs
parents: 27191
diff changeset
   196
    // explicitly configured in the configuration file.
ee2f7768e1c9 8060132: Handlers configured on abstract nodes in logging.properties are not always properly closed
dfuchs
parents: 27191
diff changeset
   197
    // It prevents these loggers from being arbitrarily garbage collected.
ee2f7768e1c9 8060132: Handlers configured on abstract nodes in logging.properties are not always properly closed
dfuchs
parents: 27191
diff changeset
   198
    private static final class CloseOnReset {
ee2f7768e1c9 8060132: Handlers configured on abstract nodes in logging.properties are not always properly closed
dfuchs
parents: 27191
diff changeset
   199
        private final Logger logger;
ee2f7768e1c9 8060132: Handlers configured on abstract nodes in logging.properties are not always properly closed
dfuchs
parents: 27191
diff changeset
   200
        private CloseOnReset(Logger ref) {
ee2f7768e1c9 8060132: Handlers configured on abstract nodes in logging.properties are not always properly closed
dfuchs
parents: 27191
diff changeset
   201
            this.logger = Objects.requireNonNull(ref);
ee2f7768e1c9 8060132: Handlers configured on abstract nodes in logging.properties are not always properly closed
dfuchs
parents: 27191
diff changeset
   202
        }
ee2f7768e1c9 8060132: Handlers configured on abstract nodes in logging.properties are not always properly closed
dfuchs
parents: 27191
diff changeset
   203
        @Override
ee2f7768e1c9 8060132: Handlers configured on abstract nodes in logging.properties are not always properly closed
dfuchs
parents: 27191
diff changeset
   204
        public boolean equals(Object other) {
ee2f7768e1c9 8060132: Handlers configured on abstract nodes in logging.properties are not always properly closed
dfuchs
parents: 27191
diff changeset
   205
            return (other instanceof CloseOnReset) && ((CloseOnReset)other).logger == logger;
ee2f7768e1c9 8060132: Handlers configured on abstract nodes in logging.properties are not always properly closed
dfuchs
parents: 27191
diff changeset
   206
        }
ee2f7768e1c9 8060132: Handlers configured on abstract nodes in logging.properties are not always properly closed
dfuchs
parents: 27191
diff changeset
   207
        @Override
ee2f7768e1c9 8060132: Handlers configured on abstract nodes in logging.properties are not always properly closed
dfuchs
parents: 27191
diff changeset
   208
        public int hashCode() {
ee2f7768e1c9 8060132: Handlers configured on abstract nodes in logging.properties are not always properly closed
dfuchs
parents: 27191
diff changeset
   209
            return System.identityHashCode(logger);
ee2f7768e1c9 8060132: Handlers configured on abstract nodes in logging.properties are not always properly closed
dfuchs
parents: 27191
diff changeset
   210
        }
ee2f7768e1c9 8060132: Handlers configured on abstract nodes in logging.properties are not always properly closed
dfuchs
parents: 27191
diff changeset
   211
        public Logger get() {
ee2f7768e1c9 8060132: Handlers configured on abstract nodes in logging.properties are not always properly closed
dfuchs
parents: 27191
diff changeset
   212
            return logger;
ee2f7768e1c9 8060132: Handlers configured on abstract nodes in logging.properties are not always properly closed
dfuchs
parents: 27191
diff changeset
   213
        }
ee2f7768e1c9 8060132: Handlers configured on abstract nodes in logging.properties are not always properly closed
dfuchs
parents: 27191
diff changeset
   214
        public static CloseOnReset create(Logger logger) {
ee2f7768e1c9 8060132: Handlers configured on abstract nodes in logging.properties are not always properly closed
dfuchs
parents: 27191
diff changeset
   215
            return new CloseOnReset(logger);
ee2f7768e1c9 8060132: Handlers configured on abstract nodes in logging.properties are not always properly closed
dfuchs
parents: 27191
diff changeset
   216
        }
ee2f7768e1c9 8060132: Handlers configured on abstract nodes in logging.properties are not always properly closed
dfuchs
parents: 27191
diff changeset
   217
    }
ee2f7768e1c9 8060132: Handlers configured on abstract nodes in logging.properties are not always properly closed
dfuchs
parents: 27191
diff changeset
   218
    private final CopyOnWriteArrayList<CloseOnReset> closeOnResetLoggers =
ee2f7768e1c9 8060132: Handlers configured on abstract nodes in logging.properties are not always properly closed
dfuchs
parents: 27191
diff changeset
   219
            new CopyOnWriteArrayList<>();
ee2f7768e1c9 8060132: Handlers configured on abstract nodes in logging.properties are not always properly closed
dfuchs
parents: 27191
diff changeset
   220
ee2f7768e1c9 8060132: Handlers configured on abstract nodes in logging.properties are not always properly closed
dfuchs
parents: 27191
diff changeset
   221
26859
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
   222
    private final Map<Object, Runnable> listeners =
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
   223
            Collections.synchronizedMap(new IdentityHashMap<>());
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
   224
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    static {
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   226
        manager = AccessController.doPrivileged(new PrivilegedAction<LogManager>() {
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   227
            @Override
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   228
            public LogManager run() {
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   229
                LogManager mgr = null;
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   230
                String cname = null;
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   231
                try {
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   232
                    cname = System.getProperty("java.util.logging.manager");
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   233
                    if (cname != null) {
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   234
                        try {
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   235
                            Class<?> clz = ClassLoader.getSystemClassLoader()
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   236
                                    .loadClass(cname);
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   237
                            mgr = (LogManager) clz.newInstance();
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   238
                        } catch (ClassNotFoundException ex) {
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   239
                            Class<?> clz = Thread.currentThread()
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   240
                                    .getContextClassLoader().loadClass(cname);
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   241
                            mgr = (LogManager) clz.newInstance();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                    }
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   244
                } catch (Exception ex) {
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   245
                    System.err.println("Could not load Logmanager \"" + cname + "\"");
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   246
                    ex.printStackTrace();
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   247
                }
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   248
                if (mgr == null) {
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   249
                    mgr = new LogManager();
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   250
                }
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   251
                return mgr;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   253
            }
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   254
        });
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    // This private class is used as a shutdown hook.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    // It does a "reset" to close all open handlers.
29919
be906afc335b 8047149: Enhance thread contexts in core libraries
chegar
parents: 29740
diff changeset
   259
    private class Cleaner extends ManagedLocalsThread {
2384
b3ba5fb77f89 6799583: LogManager shutdown hook may cause a memory leak.
bae
parents: 2
diff changeset
   260
b3ba5fb77f89 6799583: LogManager shutdown hook may cause a memory leak.
bae
parents: 2
diff changeset
   261
        private Cleaner() {
b3ba5fb77f89 6799583: LogManager shutdown hook may cause a memory leak.
bae
parents: 2
diff changeset
   262
            /* 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
   263
             * keeping a strong reference to an application classloader.
b3ba5fb77f89 6799583: LogManager shutdown hook may cause a memory leak.
bae
parents: 2
diff changeset
   264
             */
b3ba5fb77f89 6799583: LogManager shutdown hook may cause a memory leak.
bae
parents: 2
diff changeset
   265
            this.setContextClassLoader(null);
b3ba5fb77f89 6799583: LogManager shutdown hook may cause a memory leak.
bae
parents: 2
diff changeset
   266
        }
b3ba5fb77f89 6799583: LogManager shutdown hook may cause a memory leak.
bae
parents: 2
diff changeset
   267
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   268
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            // This is to ensure the LogManager.<clinit> is completed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            // before synchronized block. Otherwise deadlocks are possible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            LogManager mgr = manager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
30643
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
   274
            // set globalHandlersState to STATE_SHUTDOWN atomically so that
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
   275
            // no attempts are made to (re)initialize the handlers or (re)read
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
   276
            // the configuration again. This is terminal state.
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
   277
            configurationLock.lock();
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
   278
            globalHandlersState = STATE_SHUTDOWN;
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
   279
            configurationLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            // Do a reset to close all active handlers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * Protected constructor.  This is protected so that container applications
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * (such as J2EE containers) can subclass the object.  It is non-public as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * 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
   291
     * retrieved by calling LogManager.getLogManager.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    protected LogManager() {
22321
37f8c8b6ef7a 8024867: Enhance logging start up
dfuchs
parents: 19825
diff changeset
   294
        this(checkSubclassPermissions());
37f8c8b6ef7a 8024867: Enhance logging start up
dfuchs
parents: 19825
diff changeset
   295
    }
37f8c8b6ef7a 8024867: Enhance logging start up
dfuchs
parents: 19825
diff changeset
   296
37f8c8b6ef7a 8024867: Enhance logging start up
dfuchs
parents: 19825
diff changeset
   297
    private LogManager(Void checked) {
37f8c8b6ef7a 8024867: Enhance logging start up
dfuchs
parents: 19825
diff changeset
   298
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        // Add a shutdown hook to close the global handlers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            Runtime.getRuntime().addShutdownHook(new Cleaner());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        } catch (IllegalStateException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            // If the VM is already shutting down,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            // We do not need to register shutdownHook.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
22321
37f8c8b6ef7a 8024867: Enhance logging start up
dfuchs
parents: 19825
diff changeset
   308
    private static Void checkSubclassPermissions() {
37f8c8b6ef7a 8024867: Enhance logging start up
dfuchs
parents: 19825
diff changeset
   309
        final SecurityManager sm = System.getSecurityManager();
37f8c8b6ef7a 8024867: Enhance logging start up
dfuchs
parents: 19825
diff changeset
   310
        if (sm != null) {
37f8c8b6ef7a 8024867: Enhance logging start up
dfuchs
parents: 19825
diff changeset
   311
            // These permission will be checked in the LogManager constructor,
37f8c8b6ef7a 8024867: Enhance logging start up
dfuchs
parents: 19825
diff changeset
   312
            // in order to register the Cleaner() thread as a shutdown hook.
37f8c8b6ef7a 8024867: Enhance logging start up
dfuchs
parents: 19825
diff changeset
   313
            // Check them here to avoid the penalty of constructing the object
37f8c8b6ef7a 8024867: Enhance logging start up
dfuchs
parents: 19825
diff changeset
   314
            // etc...
37f8c8b6ef7a 8024867: Enhance logging start up
dfuchs
parents: 19825
diff changeset
   315
            sm.checkPermission(new RuntimePermission("shutdownHooks"));
37f8c8b6ef7a 8024867: Enhance logging start up
dfuchs
parents: 19825
diff changeset
   316
            sm.checkPermission(new RuntimePermission("setContextClassLoader"));
37f8c8b6ef7a 8024867: Enhance logging start up
dfuchs
parents: 19825
diff changeset
   317
        }
37f8c8b6ef7a 8024867: Enhance logging start up
dfuchs
parents: 19825
diff changeset
   318
        return null;
37f8c8b6ef7a 8024867: Enhance logging start up
dfuchs
parents: 19825
diff changeset
   319
    }
37f8c8b6ef7a 8024867: Enhance logging start up
dfuchs
parents: 19825
diff changeset
   320
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    /**
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   322
     * 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
   323
     * 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
   324
     * 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
   325
     *
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   326
     * 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
   327
     * 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
   328
     * the root and global logger *to the context tree*.
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   329
     *
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   330
     * 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
   331
     * 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
   332
     * added.
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   333
     *
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   334
     * This is why ensureLogManagerInitialized() needs to be called before
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   335
     * any logger is added to any logger context.
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   336
     *
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   337
     */
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   338
    private boolean initializedCalled = false;
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   339
    private volatile boolean initializationDone = false;
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   340
    final void ensureLogManagerInitialized() {
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   341
        final LogManager owner = this;
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   342
        if (initializationDone || owner != manager) {
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   343
            // 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
   344
            // this on private manager instances.
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   345
            return;
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   346
        }
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   347
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   348
        // Maybe another thread has called ensureLogManagerInitialized()
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   349
        // 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
   350
        // 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
   351
        // notice that initializationDone is now true and return.
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   352
        // 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
   353
        // see that initializationDone is still false, and perform the
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   354
        // initialization.
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   355
        //
32035
073a449c007d 8132550: java/util/logging/LoggingDeadlock2.java times out
dfuchs
parents: 31150
diff changeset
   356
        configurationLock.lock();
073a449c007d 8132550: java/util/logging/LoggingDeadlock2.java times out
dfuchs
parents: 31150
diff changeset
   357
        try {
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   358
            // 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
   359
            // the process of initializing the LogManager in this thread.
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   360
            // There has been a recursive call to ensureLogManagerInitialized().
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   361
            final boolean isRecursiveInitialization = (initializedCalled == true);
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
            assert initializedCalled || !initializationDone
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   364
                    : "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
   365
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   366
            if (isRecursiveInitialization || initializationDone) {
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   367
                // If isRecursiveInitialization is true it means that we're
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   368
                // already in the process of initializing the LogManager in
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   369
                // this thread. There has been a recursive call to
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   370
                // ensureLogManagerInitialized(). We should not proceed as
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   371
                // it would lead to infinite recursion.
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   372
                //
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   373
                // If initializationDone is true then it means the manager
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   374
                // has finished initializing; just return: we're done.
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   375
                return;
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   376
            }
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   377
            // Calling addLogger below will in turn call requiresDefaultLogger()
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   378
            // which will call ensureLogManagerInitialized().
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   379
            // We use initializedCalled to break the recursion.
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   380
            initializedCalled = true;
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   381
            try {
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   382
                AccessController.doPrivileged(new PrivilegedAction<Object>() {
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   383
                    @Override
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   384
                    public Object run() {
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   385
                        assert rootLogger == null;
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   386
                        assert initializedCalled && !initializationDone;
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   387
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   388
                        // Read configuration.
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   389
                        owner.readPrimordialConfiguration();
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   390
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   391
                        // 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
   392
                        owner.rootLogger = owner.new RootLogger();
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   393
                        owner.addLogger(owner.rootLogger);
22086
866b0a7d0127 8030850: Setting .level=FINEST in logging configuration file doesn't work
dfuchs
parents: 22078
diff changeset
   394
                        if (!owner.rootLogger.isLevelInitialized()) {
866b0a7d0127 8030850: Setting .level=FINEST in logging configuration file doesn't work
dfuchs
parents: 22078
diff changeset
   395
                            owner.rootLogger.setLevel(defaultLevel);
866b0a7d0127 8030850: Setting .level=FINEST in logging configuration file doesn't work
dfuchs
parents: 22078
diff changeset
   396
                        }
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   397
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   398
                        // Adding the global Logger.
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   399
                        // 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
   400
                        // subtle inter-dependency issues.
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   401
                        @SuppressWarnings("deprecation")
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   402
                        final Logger global = Logger.global;
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   403
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   404
                        // 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
   405
                        // global manager
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   406
                        owner.addLogger(global);
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   407
                        return null;
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   408
                    }
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   409
                });
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   410
            } finally {
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   411
                initializationDone = true;
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   412
            }
32035
073a449c007d 8132550: java/util/logging/LoggingDeadlock2.java times out
dfuchs
parents: 31150
diff changeset
   413
        } finally {
073a449c007d 8132550: java/util/logging/LoggingDeadlock2.java times out
dfuchs
parents: 31150
diff changeset
   414
            configurationLock.unlock();
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   415
        }
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   416
    }
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   417
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   418
    /**
18565
5025d43d3731 8019315: Fix doclint issues in java.util.logging
darcy
parents: 18269
diff changeset
   419
     * Returns the global LogManager object.
5025d43d3731 8019315: Fix doclint issues in java.util.logging
darcy
parents: 18269
diff changeset
   420
     * @return the global LogManager object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    public static LogManager getLogManager() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        if (manager != null) {
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   424
            manager.ensureLogManagerInitialized();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        return manager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
32035
073a449c007d 8132550: java/util/logging/LoggingDeadlock2.java times out
dfuchs
parents: 31150
diff changeset
   429
    private void readPrimordialConfiguration() { // must be called while holding configurationLock
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        if (!readPrimordialConfiguration) {
32035
073a449c007d 8132550: java/util/logging/LoggingDeadlock2.java times out
dfuchs
parents: 31150
diff changeset
   431
            // If System.in/out/err are null, it's a good
073a449c007d 8132550: java/util/logging/LoggingDeadlock2.java times out
dfuchs
parents: 31150
diff changeset
   432
            // indication that we're still in the
073a449c007d 8132550: java/util/logging/LoggingDeadlock2.java times out
dfuchs
parents: 31150
diff changeset
   433
            // bootstrapping phase
073a449c007d 8132550: java/util/logging/LoggingDeadlock2.java times out
dfuchs
parents: 31150
diff changeset
   434
            if (System.out == null) {
073a449c007d 8132550: java/util/logging/LoggingDeadlock2.java times out
dfuchs
parents: 31150
diff changeset
   435
                return;
073a449c007d 8132550: java/util/logging/LoggingDeadlock2.java times out
dfuchs
parents: 31150
diff changeset
   436
            }
073a449c007d 8132550: java/util/logging/LoggingDeadlock2.java times out
dfuchs
parents: 31150
diff changeset
   437
            readPrimordialConfiguration = true;
073a449c007d 8132550: java/util/logging/LoggingDeadlock2.java times out
dfuchs
parents: 31150
diff changeset
   438
            try {
073a449c007d 8132550: java/util/logging/LoggingDeadlock2.java times out
dfuchs
parents: 31150
diff changeset
   439
                readConfiguration();
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   440
32035
073a449c007d 8132550: java/util/logging/LoggingDeadlock2.java times out
dfuchs
parents: 31150
diff changeset
   441
                // Platform loggers begin to delegate to java.util.logging.Logger
073a449c007d 8132550: java/util/logging/LoggingDeadlock2.java times out
dfuchs
parents: 31150
diff changeset
   442
                sun.util.logging.PlatformLogger.redirectPlatformLoggers();
073a449c007d 8132550: java/util/logging/LoggingDeadlock2.java times out
dfuchs
parents: 31150
diff changeset
   443
            } catch (Exception ex) {
073a449c007d 8132550: java/util/logging/LoggingDeadlock2.java times out
dfuchs
parents: 31150
diff changeset
   444
                assert false : "Exception raised while reading logging configuration: " + ex;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
19809
dc0480c1b36c 8019853: Break logging and AWT circular dependency
dfuchs
parents: 18595
diff changeset
   449
    // LoggerContext maps from AppContext
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   450
    private WeakHashMap<Object, LoggerContext> contextsMap = null;
19809
dc0480c1b36c 8019853: Break logging and AWT circular dependency
dfuchs
parents: 18595
diff changeset
   451
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   452
    // Returns the LoggerContext for the user code (i.e. application or AppContext).
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   453
    // Loggers are isolated from each AppContext.
16105
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   454
    private LoggerContext getUserContext() {
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   455
        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
   456
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   457
        SecurityManager sm = System.getSecurityManager();
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   458
        JavaAWTAccess javaAwtAccess = SharedSecrets.getJavaAWTAccess();
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   459
        if (sm != null && javaAwtAccess != null) {
19809
dc0480c1b36c 8019853: Break logging and AWT circular dependency
dfuchs
parents: 18595
diff changeset
   460
            // for each applet, it has its own LoggerContext isolated from others
27932
654c0f23c1c0 8065991: LogManager unecessarily calls JavaAWTAccess from within a critical section
dfuchs
parents: 27799
diff changeset
   461
            final Object ecx = javaAwtAccess.getAppletContext();
654c0f23c1c0 8065991: LogManager unecessarily calls JavaAWTAccess from within a critical section
dfuchs
parents: 27799
diff changeset
   462
            if (ecx != null) {
654c0f23c1c0 8065991: LogManager unecessarily calls JavaAWTAccess from within a critical section
dfuchs
parents: 27799
diff changeset
   463
                synchronized (javaAwtAccess) {
654c0f23c1c0 8065991: LogManager unecessarily calls JavaAWTAccess from within a critical section
dfuchs
parents: 27799
diff changeset
   464
                    // find the AppContext of the applet code
654c0f23c1c0 8065991: LogManager unecessarily calls JavaAWTAccess from within a critical section
dfuchs
parents: 27799
diff changeset
   465
                    // will be null if we are in the main app context.
19809
dc0480c1b36c 8019853: Break logging and AWT circular dependency
dfuchs
parents: 18595
diff changeset
   466
                    if (contextsMap == null) {
dc0480c1b36c 8019853: Break logging and AWT circular dependency
dfuchs
parents: 18595
diff changeset
   467
                        contextsMap = new WeakHashMap<>();
dc0480c1b36c 8019853: Break logging and AWT circular dependency
dfuchs
parents: 18595
diff changeset
   468
                    }
dc0480c1b36c 8019853: Break logging and AWT circular dependency
dfuchs
parents: 18595
diff changeset
   469
                    context = contextsMap.get(ecx);
18202
e9a6d59491ab 8004584: Augment applet contextualization
leonidr
parents: 16117
diff changeset
   470
                    if (context == null) {
19809
dc0480c1b36c 8019853: Break logging and AWT circular dependency
dfuchs
parents: 18595
diff changeset
   471
                        // Create a new LoggerContext for the applet.
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   472
                        context = new LoggerContext();
19809
dc0480c1b36c 8019853: Break logging and AWT circular dependency
dfuchs
parents: 18595
diff changeset
   473
                        contextsMap.put(ecx, context);
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   474
                    }
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   475
                }
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   476
            }
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   477
        }
19809
dc0480c1b36c 8019853: Break logging and AWT circular dependency
dfuchs
parents: 18595
diff changeset
   478
        // for standalone app, return userContext
18202
e9a6d59491ab 8004584: Augment applet contextualization
leonidr
parents: 16117
diff changeset
   479
        return context != null ? context : userContext;
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   480
    }
9700
fdbacf68f185 7016208: 4/3 null sometimes returned by java.util.logging.Logger.getLogger(String name) in -server -Xcomp
dcubed
parents: 9013
diff changeset
   481
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   482
    // The system context.
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   483
    final LoggerContext getSystemContext() {
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   484
        return systemContext;
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   485
    }
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   486
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   487
    private List<LoggerContext> contexts() {
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   488
        List<LoggerContext> cxs = new ArrayList<>();
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   489
        cxs.add(getSystemContext());
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   490
        cxs.add(getUserContext());
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   491
        return cxs;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
16105
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   494
    // 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
   495
    // 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
   496
    // 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
   497
    // in the LogManager global namespace.
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   498
    // 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
   499
    // Synchronization is not required here. All synchronization for
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   500
    // 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
   501
    //
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   502
    // 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
   503
    // 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
   504
    // 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
   505
    // readConfiguration, and other methods.
17487
77566e5979d1 8013380: Removal of stack walk to find resource bundle breaks Glassfish startup
jgish
parents: 17164
diff changeset
   506
    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
   507
        Logger result = getLogger(name);
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   508
        if (result == null) {
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   509
            // only allocate the new logger once
23899
a926ecef7eb3 8029740: Enhance handling of loggers
dfuchs
parents: 23342
diff changeset
   510
            Logger newLogger = new Logger(name, resourceBundleName, caller, this, false);
16105
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   511
            do {
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   512
                if (addLogger(newLogger)) {
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   513
                    // We successfully added the new Logger that we
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   514
                    // created above so return it without refetching.
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   515
                    return newLogger;
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   516
                }
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   517
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   518
                // 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
   519
                // 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
   520
                // 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
   521
                // 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
   522
                // addLogger() returns a boolean instead of the Logger
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   523
                // reference itself. However, if the thread that created
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   524
                // 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
   525
                // 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
   526
                // 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
   527
                // 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
   528
                // we'll just loop around and try again.
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   529
                result = getLogger(name);
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   530
            } while (result == null);
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   531
        }
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   532
        return result;
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   533
    }
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   534
31150
2aa1d300cd75 8080933: LogManager.demandSystemLogger should accept a 'caller' argument.
dfuchs
parents: 30643
diff changeset
   535
    Logger demandSystemLogger(String name, String resourceBundleName, Class<?> caller) {
16111
5c5fc1d5bb38 8007393: Possible race condition after JDK-6664509
mchung
parents: 16105
diff changeset
   536
        // Add a system logger in the system context's namespace
31150
2aa1d300cd75 8080933: LogManager.demandSystemLogger should accept a 'caller' argument.
dfuchs
parents: 30643
diff changeset
   537
        final Logger sysLogger = getSystemContext()
2aa1d300cd75 8080933: LogManager.demandSystemLogger should accept a 'caller' argument.
dfuchs
parents: 30643
diff changeset
   538
                .demandLogger(name, resourceBundleName, caller);
16111
5c5fc1d5bb38 8007393: Possible race condition after JDK-6664509
mchung
parents: 16105
diff changeset
   539
5c5fc1d5bb38 8007393: Possible race condition after JDK-6664509
mchung
parents: 16105
diff changeset
   540
        // 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
   541
        // 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
   542
        // System loggers are visible to applications unless a logger of
5c5fc1d5bb38 8007393: Possible race condition after JDK-6664509
mchung
parents: 16105
diff changeset
   543
        // the same name has been added.
5c5fc1d5bb38 8007393: Possible race condition after JDK-6664509
mchung
parents: 16105
diff changeset
   544
        Logger logger;
5c5fc1d5bb38 8007393: Possible race condition after JDK-6664509
mchung
parents: 16105
diff changeset
   545
        do {
5c5fc1d5bb38 8007393: Possible race condition after JDK-6664509
mchung
parents: 16105
diff changeset
   546
            // First attempt to call addLogger instead of getLogger
5c5fc1d5bb38 8007393: Possible race condition after JDK-6664509
mchung
parents: 16105
diff changeset
   547
            // This would avoid potential bug in custom LogManager.getLogger
5c5fc1d5bb38 8007393: Possible race condition after JDK-6664509
mchung
parents: 16105
diff changeset
   548
            // implementation that adds a logger if does not exist
5c5fc1d5bb38 8007393: Possible race condition after JDK-6664509
mchung
parents: 16105
diff changeset
   549
            if (addLogger(sysLogger)) {
5c5fc1d5bb38 8007393: Possible race condition after JDK-6664509
mchung
parents: 16105
diff changeset
   550
                // successfully added the new system logger
5c5fc1d5bb38 8007393: Possible race condition after JDK-6664509
mchung
parents: 16105
diff changeset
   551
                logger = sysLogger;
5c5fc1d5bb38 8007393: Possible race condition after JDK-6664509
mchung
parents: 16105
diff changeset
   552
            } else {
5c5fc1d5bb38 8007393: Possible race condition after JDK-6664509
mchung
parents: 16105
diff changeset
   553
                logger = getLogger(name);
5c5fc1d5bb38 8007393: Possible race condition after JDK-6664509
mchung
parents: 16105
diff changeset
   554
            }
5c5fc1d5bb38 8007393: Possible race condition after JDK-6664509
mchung
parents: 16105
diff changeset
   555
        } while (logger == null);
5c5fc1d5bb38 8007393: Possible race condition after JDK-6664509
mchung
parents: 16105
diff changeset
   556
5c5fc1d5bb38 8007393: Possible race condition after JDK-6664509
mchung
parents: 16105
diff changeset
   557
        // LogManager will set the sysLogger's handlers via LogManager.addLogger method.
23899
a926ecef7eb3 8029740: Enhance handling of loggers
dfuchs
parents: 23342
diff changeset
   558
        if (logger != sysLogger && sysLogger.accessCheckedHandlers().length == 0) {
16111
5c5fc1d5bb38 8007393: Possible race condition after JDK-6664509
mchung
parents: 16105
diff changeset
   559
            // if logger already exists but handlers not set
5c5fc1d5bb38 8007393: Possible race condition after JDK-6664509
mchung
parents: 16105
diff changeset
   560
            final Logger l = logger;
5c5fc1d5bb38 8007393: Possible race condition after JDK-6664509
mchung
parents: 16105
diff changeset
   561
            AccessController.doPrivileged(new PrivilegedAction<Void>() {
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   562
                @Override
16111
5c5fc1d5bb38 8007393: Possible race condition after JDK-6664509
mchung
parents: 16105
diff changeset
   563
                public Void run() {
23899
a926ecef7eb3 8029740: Enhance handling of loggers
dfuchs
parents: 23342
diff changeset
   564
                    for (Handler hdl : l.accessCheckedHandlers()) {
16111
5c5fc1d5bb38 8007393: Possible race condition after JDK-6664509
mchung
parents: 16105
diff changeset
   565
                        sysLogger.addHandler(hdl);
5c5fc1d5bb38 8007393: Possible race condition after JDK-6664509
mchung
parents: 16105
diff changeset
   566
                    }
5c5fc1d5bb38 8007393: Possible race condition after JDK-6664509
mchung
parents: 16105
diff changeset
   567
                    return null;
5c5fc1d5bb38 8007393: Possible race condition after JDK-6664509
mchung
parents: 16105
diff changeset
   568
                }
5c5fc1d5bb38 8007393: Possible race condition after JDK-6664509
mchung
parents: 16105
diff changeset
   569
            });
5c5fc1d5bb38 8007393: Possible race condition after JDK-6664509
mchung
parents: 16105
diff changeset
   570
        }
5c5fc1d5bb38 8007393: Possible race condition after JDK-6664509
mchung
parents: 16105
diff changeset
   571
        return sysLogger;
16105
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   572
    }
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   573
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   574
    // LoggerContext maintains the logger namespace per context.
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   575
    // 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
   576
    // 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
   577
    // 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
   578
    // 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
   579
    // 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
   580
    // added in the user context.
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   581
    class LoggerContext {
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   582
        // Table of named Loggers that maps names to Loggers.
29740
d9f64fdd3c97 7113878: LogManager - namedLoggers should be ConcurrentHashMap instead of Hashtable
dfuchs
parents: 29739
diff changeset
   583
        private final ConcurrentHashMap<String,LoggerWeakRef> namedLoggers =
d9f64fdd3c97 7113878: LogManager - namedLoggers should be ConcurrentHashMap instead of Hashtable
dfuchs
parents: 29739
diff changeset
   584
                new ConcurrentHashMap<>();
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   585
        // Tree of named Loggers
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   586
        private final LogNode root;
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   587
        private LoggerContext() {
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   588
            this.root = new LogNode(null, this);
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   589
        }
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   590
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   591
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   592
        // Tells whether default loggers are required in this context.
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   593
        // If true, the default loggers will be lazily added.
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   594
        final boolean requiresDefaultLoggers() {
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   595
            final boolean requiresDefaultLoggers = (getOwner() == manager);
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   596
            if (requiresDefaultLoggers) {
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   597
                getOwner().ensureLogManagerInitialized();
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   598
            }
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   599
            return requiresDefaultLoggers;
18593
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   600
        }
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   601
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   602
        // This context's LogManager.
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   603
        final LogManager getOwner() {
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   604
            return LogManager.this;
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   605
        }
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   606
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   607
        // 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
   608
        // 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
   609
        // logger's tree.
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   610
        final Logger getRootLogger() {
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   611
            return getOwner().rootLogger;
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   612
        }
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   613
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   614
        // The global logger, which if not null, and if
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   615
        // 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
   616
        // logger's tree.
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   617
        final Logger getGlobalLogger() {
23342
d7a41da68b85 8036916: Deprecation warnings in LogManager
dfuchs
parents: 22342
diff changeset
   618
            @SuppressWarnings("deprecation") // avoids initialization cycles.
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   619
            final Logger global = Logger.global;
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   620
            return global;
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   621
        }
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   622
31150
2aa1d300cd75 8080933: LogManager.demandSystemLogger should accept a 'caller' argument.
dfuchs
parents: 30643
diff changeset
   623
        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
   624
            // 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
   625
            // 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
   626
            final LogManager owner = getOwner();
31150
2aa1d300cd75 8080933: LogManager.demandSystemLogger should accept a 'caller' argument.
dfuchs
parents: 30643
diff changeset
   627
            return owner.demandLogger(name, resourceBundleName, caller);
16105
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   628
        }
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   629
18593
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   630
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   631
        // Due to subtle deadlock issues getUserContext() no longer
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   632
        // calls addLocalLogger(rootLogger);
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   633
        // 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
   634
        // Checks that the context is properly initialized
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   635
        // 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
   636
        // or getLoggerNames()
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   637
        //
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   638
        private void ensureInitialized() {
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   639
            if (requiresDefaultLoggers()) {
18593
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   640
                // 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
   641
                ensureDefaultLogger(getRootLogger());
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   642
                ensureDefaultLogger(getGlobalLogger());
18593
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   643
            }
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   644
        }
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   645
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   646
29740
d9f64fdd3c97 7113878: LogManager - namedLoggers should be ConcurrentHashMap instead of Hashtable
dfuchs
parents: 29739
diff changeset
   647
        Logger findLogger(String name) {
d9f64fdd3c97 7113878: LogManager - namedLoggers should be ConcurrentHashMap instead of Hashtable
dfuchs
parents: 29739
diff changeset
   648
            // Attempt to find logger without locking.
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   649
            LoggerWeakRef ref = namedLoggers.get(name);
29740
d9f64fdd3c97 7113878: LogManager - namedLoggers should be ConcurrentHashMap instead of Hashtable
dfuchs
parents: 29739
diff changeset
   650
            Logger logger = ref == null ? null : ref.get();
d9f64fdd3c97 7113878: LogManager - namedLoggers should be ConcurrentHashMap instead of Hashtable
dfuchs
parents: 29739
diff changeset
   651
d9f64fdd3c97 7113878: LogManager - namedLoggers should be ConcurrentHashMap instead of Hashtable
dfuchs
parents: 29739
diff changeset
   652
            // if logger is not null, then we can return it right away.
d9f64fdd3c97 7113878: LogManager - namedLoggers should be ConcurrentHashMap instead of Hashtable
dfuchs
parents: 29739
diff changeset
   653
            // if name is "" or "global" and logger is null
d9f64fdd3c97 7113878: LogManager - namedLoggers should be ConcurrentHashMap instead of Hashtable
dfuchs
parents: 29739
diff changeset
   654
            // we need to fall through and check that this context is
d9f64fdd3c97 7113878: LogManager - namedLoggers should be ConcurrentHashMap instead of Hashtable
dfuchs
parents: 29739
diff changeset
   655
            // initialized.
d9f64fdd3c97 7113878: LogManager - namedLoggers should be ConcurrentHashMap instead of Hashtable
dfuchs
parents: 29739
diff changeset
   656
            // if ref is not null and logger is null we also need to
d9f64fdd3c97 7113878: LogManager - namedLoggers should be ConcurrentHashMap instead of Hashtable
dfuchs
parents: 29739
diff changeset
   657
            // fall through.
d9f64fdd3c97 7113878: LogManager - namedLoggers should be ConcurrentHashMap instead of Hashtable
dfuchs
parents: 29739
diff changeset
   658
            if (logger != null || (ref == null && !name.isEmpty()
d9f64fdd3c97 7113878: LogManager - namedLoggers should be ConcurrentHashMap instead of Hashtable
dfuchs
parents: 29739
diff changeset
   659
                    && !name.equals(Logger.GLOBAL_LOGGER_NAME))) {
d9f64fdd3c97 7113878: LogManager - namedLoggers should be ConcurrentHashMap instead of Hashtable
dfuchs
parents: 29739
diff changeset
   660
                return logger;
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   661
            }
29740
d9f64fdd3c97 7113878: LogManager - namedLoggers should be ConcurrentHashMap instead of Hashtable
dfuchs
parents: 29739
diff changeset
   662
d9f64fdd3c97 7113878: LogManager - namedLoggers should be ConcurrentHashMap instead of Hashtable
dfuchs
parents: 29739
diff changeset
   663
            // We either found a stale reference, or we were looking for
d9f64fdd3c97 7113878: LogManager - namedLoggers should be ConcurrentHashMap instead of Hashtable
dfuchs
parents: 29739
diff changeset
   664
            // "" or "global" and didn't find them.
d9f64fdd3c97 7113878: LogManager - namedLoggers should be ConcurrentHashMap instead of Hashtable
dfuchs
parents: 29739
diff changeset
   665
            // Make sure context is initialized (has the default loggers),
d9f64fdd3c97 7113878: LogManager - namedLoggers should be ConcurrentHashMap instead of Hashtable
dfuchs
parents: 29739
diff changeset
   666
            // and look up again, cleaning the stale reference if it hasn't
d9f64fdd3c97 7113878: LogManager - namedLoggers should be ConcurrentHashMap instead of Hashtable
dfuchs
parents: 29739
diff changeset
   667
            // been cleaned up in between. All this needs to be done inside
d9f64fdd3c97 7113878: LogManager - namedLoggers should be ConcurrentHashMap instead of Hashtable
dfuchs
parents: 29739
diff changeset
   668
            // a synchronized block.
d9f64fdd3c97 7113878: LogManager - namedLoggers should be ConcurrentHashMap instead of Hashtable
dfuchs
parents: 29739
diff changeset
   669
            synchronized(this) {
d9f64fdd3c97 7113878: LogManager - namedLoggers should be ConcurrentHashMap instead of Hashtable
dfuchs
parents: 29739
diff changeset
   670
                // ensure that this context is properly initialized before
d9f64fdd3c97 7113878: LogManager - namedLoggers should be ConcurrentHashMap instead of Hashtable
dfuchs
parents: 29739
diff changeset
   671
                // looking for loggers.
d9f64fdd3c97 7113878: LogManager - namedLoggers should be ConcurrentHashMap instead of Hashtable
dfuchs
parents: 29739
diff changeset
   672
                ensureInitialized();
d9f64fdd3c97 7113878: LogManager - namedLoggers should be ConcurrentHashMap instead of Hashtable
dfuchs
parents: 29739
diff changeset
   673
                ref = namedLoggers.get(name);
d9f64fdd3c97 7113878: LogManager - namedLoggers should be ConcurrentHashMap instead of Hashtable
dfuchs
parents: 29739
diff changeset
   674
                if (ref == null) {
d9f64fdd3c97 7113878: LogManager - namedLoggers should be ConcurrentHashMap instead of Hashtable
dfuchs
parents: 29739
diff changeset
   675
                    return null;
d9f64fdd3c97 7113878: LogManager - namedLoggers should be ConcurrentHashMap instead of Hashtable
dfuchs
parents: 29739
diff changeset
   676
                }
d9f64fdd3c97 7113878: LogManager - namedLoggers should be ConcurrentHashMap instead of Hashtable
dfuchs
parents: 29739
diff changeset
   677
                logger = ref.get();
d9f64fdd3c97 7113878: LogManager - namedLoggers should be ConcurrentHashMap instead of Hashtable
dfuchs
parents: 29739
diff changeset
   678
                if (logger == null) {
d9f64fdd3c97 7113878: LogManager - namedLoggers should be ConcurrentHashMap instead of Hashtable
dfuchs
parents: 29739
diff changeset
   679
                    // The namedLoggers map holds stale weak reference
d9f64fdd3c97 7113878: LogManager - namedLoggers should be ConcurrentHashMap instead of Hashtable
dfuchs
parents: 29739
diff changeset
   680
                    // to a logger which has been GC-ed.
d9f64fdd3c97 7113878: LogManager - namedLoggers should be ConcurrentHashMap instead of Hashtable
dfuchs
parents: 29739
diff changeset
   681
                    ref.dispose();
d9f64fdd3c97 7113878: LogManager - namedLoggers should be ConcurrentHashMap instead of Hashtable
dfuchs
parents: 29739
diff changeset
   682
                }
d9f64fdd3c97 7113878: LogManager - namedLoggers should be ConcurrentHashMap instead of Hashtable
dfuchs
parents: 29739
diff changeset
   683
                return logger;
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   684
            }
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   685
        }
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   686
18593
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   687
        // 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
   688
        // context.
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   689
        // 'logger' is the context that will be added.
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   690
        // 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
   691
        // before adding 'logger'.
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   692
        //
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   693
        private void ensureAllDefaultLoggers(Logger logger) {
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   694
            if (requiresDefaultLoggers()) {
18593
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   695
                final String name = logger.getName();
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   696
                if (!name.isEmpty()) {
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   697
                    ensureDefaultLogger(getRootLogger());
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   698
                    if (!Logger.GLOBAL_LOGGER_NAME.equals(name)) {
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   699
                        ensureDefaultLogger(getGlobalLogger());
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   700
                    }
18593
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   701
                }
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   702
            }
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   703
        }
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   704
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   705
        private void ensureDefaultLogger(Logger logger) {
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   706
            // 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
   707
            // to a LoggerContext.
18202
e9a6d59491ab 8004584: Augment applet contextualization
leonidr
parents: 16117
diff changeset
   708
18593
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   709
            // 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
   710
            // 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
   711
            // or owner.rootLogger.
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   712
            if (!requiresDefaultLoggers() || logger == null
23342
d7a41da68b85 8036916: Deprecation warnings in LogManager
dfuchs
parents: 22342
diff changeset
   713
                    || logger != getGlobalLogger() && logger != LogManager.this.rootLogger ) {
18593
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   714
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   715
                // 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
   716
                // Logger.global nor manager.rootLogger indicates a serious
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   717
                // issue - as ensureDefaultLogger should never be called
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   718
                // 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
   719
                // e.g manager.rootLogger is not yet initialized)...
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   720
                assert logger == null;
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   721
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   722
                return;
18202
e9a6d59491ab 8004584: Augment applet contextualization
leonidr
parents: 16117
diff changeset
   723
            }
18593
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   724
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   725
            // 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
   726
            if (!namedLoggers.containsKey(logger.getName())) {
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   727
                // It is important to prevent addLocalLogger to
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   728
                // call ensureAllDefaultLoggers when we're in the process
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   729
                // 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
   730
                // immediately cause a stack overflow.
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   731
                // Therefore we must pass addDefaultLoggersIfNeeded=false,
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   732
                // even if requiresDefaultLoggers is true.
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   733
                addLocalLogger(logger, false);
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   734
            }
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   735
        }
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   736
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   737
        boolean addLocalLogger(Logger logger) {
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   738
            // 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
   739
            return addLocalLogger(logger, requiresDefaultLoggers());
18202
e9a6d59491ab 8004584: Augment applet contextualization
leonidr
parents: 16117
diff changeset
   740
        }
e9a6d59491ab 8004584: Augment applet contextualization
leonidr
parents: 16117
diff changeset
   741
16105
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   742
        // 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
   743
        // 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
   744
        synchronized boolean addLocalLogger(Logger logger, boolean addDefaultLoggersIfNeeded) {
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   745
            // addDefaultLoggersIfNeeded serves to break recursion when adding
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   746
            // 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
   747
            // (we're being called from ensureDefaultLogger()) then
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   748
            // 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
   749
            // call ensureAllDefaultLoggers again.
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   750
            //
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   751
            // Note: addDefaultLoggersIfNeeded can also be false when
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   752
            //       requiresDefaultLoggers is false - since calling
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   753
            //       ensureAllDefaultLoggers would have no effect in this case.
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   754
            if (addDefaultLoggersIfNeeded) {
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   755
                ensureAllDefaultLoggers(logger);
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   756
            }
18202
e9a6d59491ab 8004584: Augment applet contextualization
leonidr
parents: 16117
diff changeset
   757
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   758
            final String name = logger.getName();
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   759
            if (name == null) {
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   760
                throw new NullPointerException();
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   761
            }
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   762
            LoggerWeakRef ref = namedLoggers.get(name);
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   763
            if (ref != null) {
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   764
                if (ref.get() == null) {
17164
1304199c8641 8010939: Deadlock in LogManager
jgish
parents: 16117
diff changeset
   765
                    // It's possible that the Logger was GC'ed after a
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   766
                    // drainLoggerRefQueueBounded() call above so allow
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   767
                    // a new one to be registered.
21960
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
   768
                    ref.dispose();
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   769
                } else {
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   770
                    // We already have a registered logger with the given name.
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   771
                    return false;
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   772
                }
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   773
            }
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   774
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   775
            // We're adding a new logger.
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   776
            // 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
   777
            final LogManager owner = getOwner();
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   778
            logger.setLogManager(owner);
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   779
            ref = owner.new LoggerWeakRef(logger);
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   780
21304
7971ecf0fbed 8026499: Root Logger level can be reset unexpectedly
dfuchs
parents: 19825
diff changeset
   781
            // Apply any initial level defined for the new logger, unless
7971ecf0fbed 8026499: Root Logger level can be reset unexpectedly
dfuchs
parents: 19825
diff changeset
   782
            // the logger's level is already initialized
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   783
            Level level = owner.getLevelProperty(name + ".level", null);
21304
7971ecf0fbed 8026499: Root Logger level can be reset unexpectedly
dfuchs
parents: 19825
diff changeset
   784
            if (level != null && !logger.isLevelInitialized()) {
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   785
                doSetLevel(logger, level);
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   786
            }
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   787
16105
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   788
            // 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
   789
            // 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
   790
            // subclass for the custom log manager case
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   791
            processParentHandlers(logger, name);
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   792
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   793
            // Find the new node and its parent.
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   794
            LogNode node = getNode(name);
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   795
            node.loggerRef = ref;
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   796
            Logger parent = null;
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   797
            LogNode nodep = node.parent;
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   798
            while (nodep != null) {
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   799
                LoggerWeakRef nodeRef = nodep.loggerRef;
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   800
                if (nodeRef != null) {
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   801
                    parent = nodeRef.get();
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   802
                    if (parent != null) {
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   803
                        break;
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   804
                    }
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   805
                }
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   806
                nodep = nodep.parent;
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   807
            }
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   808
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   809
            if (parent != null) {
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   810
                doSetParent(logger, parent);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
            }
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   812
            // Walk over the children and tell them we are their new parent.
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   813
            node.walkAndSetParent(logger);
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   814
            // new LogNode is ready so tell the LoggerWeakRef about it
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   815
            ref.setNode(node);
29740
d9f64fdd3c97 7113878: LogManager - namedLoggers should be ConcurrentHashMap instead of Hashtable
dfuchs
parents: 29739
diff changeset
   816
d9f64fdd3c97 7113878: LogManager - namedLoggers should be ConcurrentHashMap instead of Hashtable
dfuchs
parents: 29739
diff changeset
   817
            // Do not publish 'ref' in namedLoggers before the logger tree
d9f64fdd3c97 7113878: LogManager - namedLoggers should be ConcurrentHashMap instead of Hashtable
dfuchs
parents: 29739
diff changeset
   818
            // is fully updated - because the named logger will be visible as
d9f64fdd3c97 7113878: LogManager - namedLoggers should be ConcurrentHashMap instead of Hashtable
dfuchs
parents: 29739
diff changeset
   819
            // soon as it is published in namedLoggers (findLogger takes
d9f64fdd3c97 7113878: LogManager - namedLoggers should be ConcurrentHashMap instead of Hashtable
dfuchs
parents: 29739
diff changeset
   820
            // benefit of the ConcurrentHashMap implementation of namedLoggers
d9f64fdd3c97 7113878: LogManager - namedLoggers should be ConcurrentHashMap instead of Hashtable
dfuchs
parents: 29739
diff changeset
   821
            // to avoid synchronizing on retrieval when that is possible).
d9f64fdd3c97 7113878: LogManager - namedLoggers should be ConcurrentHashMap instead of Hashtable
dfuchs
parents: 29739
diff changeset
   822
            namedLoggers.put(name, ref);
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   823
            return true;
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   824
        }
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   825
29740
d9f64fdd3c97 7113878: LogManager - namedLoggers should be ConcurrentHashMap instead of Hashtable
dfuchs
parents: 29739
diff changeset
   826
        void removeLoggerRef(String name, LoggerWeakRef ref) {
21960
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
   827
            namedLoggers.remove(name, ref);
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   828
        }
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   829
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   830
        synchronized Enumeration<String> getLoggerNames() {
18593
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   831
            // ensure that this context is properly initialized before
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   832
            // returning logger names.
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   833
            ensureInitialized();
29740
d9f64fdd3c97 7113878: LogManager - namedLoggers should be ConcurrentHashMap instead of Hashtable
dfuchs
parents: 29739
diff changeset
   834
            return Collections.enumeration(namedLoggers.keySet());
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   835
        }
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   836
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   837
        // If logger.getUseParentHandlers() returns 'true' and any of the logger's
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   838
        // 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
   839
        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
   840
            final LogManager owner = getOwner();
16105
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   841
            AccessController.doPrivileged(new PrivilegedAction<Void>() {
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   842
                @Override
16105
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   843
                public Void run() {
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   844
                    if (logger != owner.rootLogger) {
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   845
                        boolean useParent = owner.getBooleanProperty(name + ".useParentHandlers", true);
16105
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   846
                        if (!useParent) {
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   847
                            logger.setUseParentHandlers(false);
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   848
                        }
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   849
                    }
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   850
                    return null;
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   851
                }
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   852
            });
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   853
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   854
            int ix = 1;
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   855
            for (;;) {
24685
215fa91e1b4c 8044461: Cleanup new Boolean and single character strings
rriggs
parents: 24196
diff changeset
   856
                int ix2 = name.indexOf('.', ix);
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   857
                if (ix2 < 0) {
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   858
                    break;
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   859
                }
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   860
                String pname = name.substring(0, ix2);
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   861
                if (owner.getProperty(pname + ".level") != null ||
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   862
                    owner.getProperty(pname + ".handlers") != null) {
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   863
                    // This pname has a level/handlers definition.
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   864
                    // Make sure it exists.
31150
2aa1d300cd75 8080933: LogManager.demandSystemLogger should accept a 'caller' argument.
dfuchs
parents: 30643
diff changeset
   865
                    demandLogger(pname, null, null);
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   866
                }
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   867
                ix = ix2+1;
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   868
            }
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   869
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   871
        // Gets a node in our tree of logger nodes.
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   872
        // If necessary, create it.
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   873
        LogNode getNode(String name) {
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   874
            if (name == null || name.equals("")) {
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   875
                return root;
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   876
            }
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   877
            LogNode node = root;
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   878
            while (name.length() > 0) {
24685
215fa91e1b4c 8044461: Cleanup new Boolean and single character strings
rriggs
parents: 24196
diff changeset
   879
                int ix = name.indexOf('.');
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   880
                String head;
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   881
                if (ix > 0) {
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   882
                    head = name.substring(0, ix);
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   883
                    name = name.substring(ix + 1);
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   884
                } else {
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   885
                    head = name;
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   886
                    name = "";
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   887
                }
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   888
                if (node.children == null) {
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   889
                    node.children = new HashMap<>();
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   890
                }
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   891
                LogNode child = node.children.get(head);
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   892
                if (child == null) {
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   893
                    child = new LogNode(node, this);
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   894
                    node.children.put(head, child);
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   895
                }
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   896
                node = child;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
            }
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   898
            return node;
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   899
        }
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   900
    }
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   901
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   902
    final class SystemLoggerContext extends LoggerContext {
16105
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   903
        // 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
   904
        // 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
   905
        // 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
   906
        // 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
   907
        @Override
31150
2aa1d300cd75 8080933: LogManager.demandSystemLogger should accept a 'caller' argument.
dfuchs
parents: 30643
diff changeset
   908
        Logger demandLogger(String name, String resourceBundleName, Class<?> caller) {
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   909
            Logger result = findLogger(name);
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   910
            if (result == null) {
16105
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   911
                // only allocate the new system logger once
31150
2aa1d300cd75 8080933: LogManager.demandSystemLogger should accept a 'caller' argument.
dfuchs
parents: 30643
diff changeset
   912
                Logger newLogger = new Logger(name, resourceBundleName, caller, getOwner(), true);
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   913
                do {
16105
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   914
                    if (addLocalLogger(newLogger)) {
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   915
                        // We successfully added the new Logger that we
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   916
                        // created above so return it without refetching.
16105
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   917
                        result = newLogger;
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   918
                    } else {
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   919
                        // 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
   920
                        // 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
   921
                        // 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
   922
                        // 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
   923
                        // addLogger() returns a boolean instead of the Logger
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   924
                        // reference itself. However, if the thread that created
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   925
                        // 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
   926
                        // 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
   927
                        // 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
   928
                        // 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
   929
                        // we'll just loop around and try again.
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   930
                        result = findLogger(name);
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   931
                    }
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   932
                } while (result == null);
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   933
            }
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   934
            return result;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
    // Add new per logger handlers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
    // We need to raise privilege here. All our decisions will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
    // be made based on the logging configuration, which can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
    // only be modified by trusted code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
    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
   943
                                    final String handlersPropertyName)
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   944
    {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
        AccessController.doPrivileged(new PrivilegedAction<Object>() {
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   946
            @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
            public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
                String names[] = parseClassNames(handlersPropertyName);
27754
ee2f7768e1c9 8060132: Handlers configured on abstract nodes in logging.properties are not always properly closed
dfuchs
parents: 27191
diff changeset
   949
                final boolean ensureCloseOnReset = names.length > 0
ee2f7768e1c9 8060132: Handlers configured on abstract nodes in logging.properties are not always properly closed
dfuchs
parents: 27191
diff changeset
   950
                    && getBooleanProperty(handlersPropertyName + ".ensureCloseOnReset",true);
ee2f7768e1c9 8060132: Handlers configured on abstract nodes in logging.properties are not always properly closed
dfuchs
parents: 27191
diff changeset
   951
ee2f7768e1c9 8060132: Handlers configured on abstract nodes in logging.properties are not always properly closed
dfuchs
parents: 27191
diff changeset
   952
                int count = 0;
ee2f7768e1c9 8060132: Handlers configured on abstract nodes in logging.properties are not always properly closed
dfuchs
parents: 27191
diff changeset
   953
                for (String type : names) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
                    try {
27754
ee2f7768e1c9 8060132: Handlers configured on abstract nodes in logging.properties are not always properly closed
dfuchs
parents: 27191
diff changeset
   955
                        Class<?> clz = ClassLoader.getSystemClassLoader().loadClass(type);
16105
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   956
                        Handler hdl = (Handler) clz.newInstance();
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   957
                        // Check if there is a property defining the
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   958
                        // this handler's level.
27754
ee2f7768e1c9 8060132: Handlers configured on abstract nodes in logging.properties are not always properly closed
dfuchs
parents: 27191
diff changeset
   959
                        String levs = getProperty(type + ".level");
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   960
                        if (levs != null) {
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   961
                            Level l = Level.findLevel(levs);
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   962
                            if (l != null) {
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   963
                                hdl.setLevel(l);
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   964
                            } else {
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   965
                                // Probably a bad level. Drop through.
27754
ee2f7768e1c9 8060132: Handlers configured on abstract nodes in logging.properties are not always properly closed
dfuchs
parents: 27191
diff changeset
   966
                                System.err.println("Can't set level for " + type);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
                        // Add this Handler to the logger
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
                        logger.addHandler(hdl);
27754
ee2f7768e1c9 8060132: Handlers configured on abstract nodes in logging.properties are not always properly closed
dfuchs
parents: 27191
diff changeset
   971
                        if (++count == 1 && ensureCloseOnReset) {
ee2f7768e1c9 8060132: Handlers configured on abstract nodes in logging.properties are not always properly closed
dfuchs
parents: 27191
diff changeset
   972
                            // add this logger to the closeOnResetLoggers list.
ee2f7768e1c9 8060132: Handlers configured on abstract nodes in logging.properties are not always properly closed
dfuchs
parents: 27191
diff changeset
   973
                            closeOnResetLoggers.addIfAbsent(CloseOnReset.create(logger));
ee2f7768e1c9 8060132: Handlers configured on abstract nodes in logging.properties are not always properly closed
dfuchs
parents: 27191
diff changeset
   974
                        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
                    } catch (Exception ex) {
27754
ee2f7768e1c9 8060132: Handlers configured on abstract nodes in logging.properties are not always properly closed
dfuchs
parents: 27191
diff changeset
   976
                        System.err.println("Can't load log handler \"" + type + "\"");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
                        System.err.println("" + ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
                        ex.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
                }
27754
ee2f7768e1c9 8060132: Handlers configured on abstract nodes in logging.properties are not always properly closed
dfuchs
parents: 27191
diff changeset
   981
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
                return null;
16105
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   983
            }
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   984
        });
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
5964
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   987
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   988
    // loggerRefQueue holds LoggerWeakRef objects for Logger objects
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   989
    // that have been GC'ed.
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   990
    private final ReferenceQueue<Logger> loggerRefQueue
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 6675
diff changeset
   991
        = new ReferenceQueue<>();
5964
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
    // Package-level inner class.
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   994
    // 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
   995
    //
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   996
    // LogManager.namedLoggers
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   997
    //     - has weak references to all named Loggers
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   998
    //     - 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
   999
    //       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
  1000
    //       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
  1001
    // LogManager.LogNode.loggerRef
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1002
    //     - 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
  1003
    //     - 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
  1004
    //       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
  1005
    // Logger.kids
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1006
    //     - 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
  1007
    //       includes anonymous and named Loggers
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1008
    //     - 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
  1009
    //       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
  1010
    //       LoggerWeakRef objects for the anonymous Loggers around
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1011
    //       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
  1012
    //
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1013
    final class LoggerWeakRef extends WeakReference<Logger> {
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1014
        private String                name;       // for namedLoggers cleanup
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1015
        private LogNode               node;       // for loggerRef cleanup
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1016
        private WeakReference<Logger> parentRef;  // for kids cleanup
21960
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
  1017
        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
  1018
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1019
        LoggerWeakRef(Logger logger) {
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1020
            super(logger, loggerRefQueue);
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1021
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1022
            name = logger.getName();  // save for namedLoggers cleanup
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1023
        }
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
        // dispose of this LoggerWeakRef object
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1026
        void dispose() {
21960
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
  1027
            // Avoid calling dispose twice. When a Logger is gc'ed, its
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
  1028
            // LoggerWeakRef will be enqueued.
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
  1029
            // 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
  1030
            // up) before the queue is drained. When that happens, dispose()
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
  1031
            // will be called by addLocalLogger() or findLogger().
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
  1032
            // Later when the queue is drained, dispose() will be called again
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
  1033
            // for the same LoggerWeakRef. Marking LoggerWeakRef as disposed
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
  1034
            // avoids processing the data twice (even though the code should
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
  1035
            // now be reentrant).
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
  1036
            synchronized(this) {
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
  1037
                // Note to maintainers:
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
  1038
                // Be careful not to call any method that tries to acquire
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
  1039
                // another lock from within this block - as this would surely
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
  1040
                // lead to deadlocks, given that dispose() can be called by
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
  1041
                // multiple threads, and from within different synchronized
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
  1042
                // methods/blocks.
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
  1043
                if (disposed) return;
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
  1044
                disposed = true;
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
  1045
            }
5964
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1046
21960
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
  1047
            final LogNode n = node;
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
  1048
            if (n != null) {
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
  1049
                // n.loggerRef can only be safely modified from within
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
  1050
                // a lock on LoggerContext. removeLoggerRef is already
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
  1051
                // synchronized on LoggerContext so calling
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
  1052
                // n.context.removeLoggerRef from within this lock is safe.
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
  1053
                synchronized (n.context) {
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
  1054
                    // if we have a LogNode, then we were a named Logger
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
  1055
                    // so clear namedLoggers weak ref to us
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
  1056
                    n.context.removeLoggerRef(name, this);
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
  1057
                    name = null;  // clear our ref to the Logger's name
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
  1058
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
  1059
                    // LogNode may have been reused - so only clear
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
  1060
                    // LogNode.loggerRef if LogNode.loggerRef == this
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
  1061
                    if (n.loggerRef == this) {
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
  1062
                        n.loggerRef = null;  // clear LogNode's weak ref to us
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
  1063
                    }
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
  1064
                    node = null;            // clear our ref to LogNode
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
  1065
                }
5964
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1066
            }
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1067
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1068
            if (parentRef != null) {
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1069
                // 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
  1070
                Logger parent = parentRef.get();
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1071
                if (parent != null) {
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1072
                    // 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
  1073
                    // parent Logger's weak ref to us
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1074
                    parent.removeChildLogger(this);
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1075
                }
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1076
                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
  1077
            }
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1078
        }
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1079
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1080
        // 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
  1081
        void setNode(LogNode node) {
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1082
            this.node = node;
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1083
        }
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1084
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1085
        // 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
  1086
        void setParentRef(WeakReference<Logger> parentRef) {
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1087
            this.parentRef = parentRef;
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1088
        }
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1089
    }
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1090
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1091
    // Package-level method.
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1092
    // 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
  1093
    //
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1094
    // drainLoggerRefQueueBounded() is called by addLogger() below
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1095
    // 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
  1096
    // 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
  1097
    //
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1098
    // 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
  1099
    // 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
  1100
    // 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
  1101
    // 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
  1102
    //   - test duration 1 minute
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1103
    //   - sample size of 125 sets of 400
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1104
    //   - average: 1.99 ms
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1105
    //   - minimum: 0.57 ms
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1106
    //   - maximum: 25.3 ms
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1107
    //
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1108
    // 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
  1109
    // 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
  1110
    // 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
  1111
    //   - test duration 2 minutes
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1112
    //   - sample size of 506 sets of 400
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1113
    //   - average: 0.57 ms
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1114
    //   - minimum: 0.02 ms
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1115
    //   - maximum: 10.9 ms
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1116
    //
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1117
    private final static int MAX_ITERATIONS = 400;
21960
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
  1118
    final void drainLoggerRefQueueBounded() {
5964
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1119
        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
  1120
            if (loggerRefQueue == null) {
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1121
                // haven't finished loading LogManager yet
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1122
                break;
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1123
            }
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1124
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1125
            LoggerWeakRef ref = (LoggerWeakRef) loggerRefQueue.poll();
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1126
            if (ref == null) {
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1127
                break;
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1128
            }
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1129
            // 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
  1130
            ref.dispose();
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1131
        }
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1132
    }
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1133
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
     * Add a named logger.  This does nothing and returns false if a logger
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
     * with the same name is already registered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
     * The Logger factory methods call this method to register each
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
     * newly created Logger.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
     * The application should retain its own reference to the Logger
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
     * object to avoid it being garbage collected.  The LogManager
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
     * may only retain a weak reference.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
     * @param   logger the new logger.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
     * @return  true if the argument logger was registered successfully,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
     *          false if a logger of that name already exists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
     * @exception NullPointerException if the logger name is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
     */
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
  1150
    public boolean addLogger(Logger logger) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
        final String name = logger.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
        if (name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
        }
17164
1304199c8641 8010939: Deadlock in LogManager
jgish
parents: 16117
diff changeset
  1155
        drainLoggerRefQueueBounded();
16105
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
  1156
        LoggerContext cx = getUserContext();
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
  1157
        if (cx.addLocalLogger(logger)) {
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
  1158
            // Do we have a per logger handler too?
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
  1159
            // Note: this will add a 200ms penalty
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
  1160
            loadLoggerHandlers(logger, name, name + ".handlers");
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
  1161
            return true;
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
  1162
        } else {
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
  1163
            return false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
    // Private method to set a level on a logger.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
    // If necessary, we raise privilege before doing the call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
    private static void doSetLevel(final Logger logger, final Level level) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
        if (sm == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
            // There is no security manager, so things are easy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
            logger.setLevel(level);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
        // There is a security manager.  Raise privilege before
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
        // calling setLevel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
        AccessController.doPrivileged(new PrivilegedAction<Object>() {
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
  1179
            @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
            public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
                logger.setLevel(level);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
            }});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
    // Private method to set a parent on a logger.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
    // If necessary, we raise privilege before doing the setParent call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
    private static void doSetParent(final Logger logger, final Logger parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
        if (sm == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
            // There is no security manager, so things are easy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
            logger.setParent(parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
        // There is a security manager.  Raise privilege before
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
        // calling setParent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
        AccessController.doPrivileged(new PrivilegedAction<Object>() {
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
  1198
            @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
            public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
                logger.setParent(parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
            }});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
     * Method to find a named logger.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
     * Note that since untrusted code may create loggers with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
     * arbitrary names this method should not be relied on to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
     * 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
  1211
     * 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
  1212
     * 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
  1213
     * 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
  1214
     * 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
  1215
     * the case where the Logger has been garbage collected.
24196
61c9885d76e2 8029451: Tidy warnings cleanup for java.util package; minor changes in java.nio, java.sql
yan
parents: 23899
diff changeset
  1216
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
     * @param name name of the logger
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
     * @return  matching logger or null if none is found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
     */
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
  1220
    public Logger getLogger(String name) {
16105
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
  1221
        return getUserContext().findLogger(name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
     * Get an enumeration of known logger names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
     * Note:  Loggers may be added dynamically as new classes are loaded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
     * 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
  1229
     * 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
  1230
     * 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
  1231
     * 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
  1232
     * 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
  1233
     * 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
  1234
     * 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
  1235
     * 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
  1236
     * time since its name was returned by this method.
24196
61c9885d76e2 8029451: Tidy warnings cleanup for java.util package; minor changes in java.nio, java.sql
yan
parents: 23899
diff changeset
  1237
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
     * @return  enumeration of logger name strings
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
     */
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
  1240
    public Enumeration<String> getLoggerNames() {
16105
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
  1241
        return getUserContext().getLoggerNames();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
     * Reinitialize the logging properties and reread the logging configuration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
     * The same rules are used for locating the configuration properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
     * as are used at startup.  So normally the logging properties will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
     * be re-read from the same file that was used at startup.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
     * Any log level definitions in the new configuration file will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
     * applied using Logger.setLevel(), if the target Logger exists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
     * <p>
26859
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1254
     * Any {@linkplain #addConfigurationListener registered configuration
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1255
     * listener} will be invoked after the properties are read.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
     * @exception  SecurityException  if a security manager exists and if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
     *             the caller does not have LoggingPermission("control").
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
     * @exception  IOException if there are IO problems reading the configuration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
    public void readConfiguration() throws IOException, SecurityException {
14216
23714b376286 7169884: LogManager checks do not work correctly for sub-types
alanb
parents: 13578
diff changeset
  1262
        checkPermission();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
        // if a configuration class is specified, load it and use it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
        String cname = System.getProperty("java.util.logging.config.class");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
        if (cname != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
                // Instantiate the named class.  It is its constructor's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
                // responsibility to initialize the logging configuration, by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
                // calling readConfiguration(InputStream) with a suitable stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
                try {
11274
7e7196757acd 7117249: fix warnings in java.util.jar, .logging, .prefs, .zip
smarks
parents: 9700
diff changeset
  1272
                    Class<?> clz = ClassLoader.getSystemClassLoader().loadClass(cname);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
                    clz.newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
                } catch (ClassNotFoundException ex) {
11274
7e7196757acd 7117249: fix warnings in java.util.jar, .logging, .prefs, .zip
smarks
parents: 9700
diff changeset
  1276
                    Class<?> clz = Thread.currentThread().getContextClassLoader().loadClass(cname);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
                    clz.newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
            } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
                System.err.println("Logging configuration class \"" + cname + "\" failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
                System.err.println("" + ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
                // keep going and useful config file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
        String fname = System.getProperty("java.util.logging.config.file");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
        if (fname == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
            fname = System.getProperty("java.home");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
            if (fname == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
                throw new Error("Can't find java.home ??");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
            }
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27191
diff changeset
  1293
            File f = new File(fname, "conf");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
            f = new File(f, "logging.properties");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
            fname = f.getCanonicalPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
        }
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
  1297
        try (final InputStream in = new FileInputStream(fname)) {
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
  1298
            final BufferedInputStream bin = new BufferedInputStream(in);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
            readConfiguration(bin);
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
     * Reset the logging configuration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
     * For all named loggers, the reset operation removes and closes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
     * all Handlers and (except for the root logger) sets the level
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
     * to null.  The root logger's level is set to Level.INFO.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
     * @exception  SecurityException  if a security manager exists and if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
     *             the caller does not have LoggingPermission("control").
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
    public void reset() throws SecurityException {
14216
23714b376286 7169884: LogManager checks do not work correctly for sub-types
alanb
parents: 13578
diff changeset
  1315
        checkPermission();
30643
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1316
27754
ee2f7768e1c9 8060132: Handlers configured on abstract nodes in logging.properties are not always properly closed
dfuchs
parents: 27191
diff changeset
  1317
        List<CloseOnReset> persistent;
30643
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1318
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1319
        // We don't want reset() and readConfiguration()
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1320
        // to run in parallel
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1321
        configurationLock.lock();
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1322
        try {
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1323
            // install new empty properties
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
            props = new Properties();
27754
ee2f7768e1c9 8060132: Handlers configured on abstract nodes in logging.properties are not always properly closed
dfuchs
parents: 27191
diff changeset
  1325
            // make sure we keep the loggers persistent until reset is done.
ee2f7768e1c9 8060132: Handlers configured on abstract nodes in logging.properties are not always properly closed
dfuchs
parents: 27191
diff changeset
  1326
            // Those are the loggers for which we previously created a
ee2f7768e1c9 8060132: Handlers configured on abstract nodes in logging.properties are not always properly closed
dfuchs
parents: 27191
diff changeset
  1327
            // handler from the configuration, and we need to prevent them
ee2f7768e1c9 8060132: Handlers configured on abstract nodes in logging.properties are not always properly closed
dfuchs
parents: 27191
diff changeset
  1328
            // from being gc'ed until those handlers are closed.
ee2f7768e1c9 8060132: Handlers configured on abstract nodes in logging.properties are not always properly closed
dfuchs
parents: 27191
diff changeset
  1329
            persistent = new ArrayList<>(closeOnResetLoggers);
ee2f7768e1c9 8060132: Handlers configured on abstract nodes in logging.properties are not always properly closed
dfuchs
parents: 27191
diff changeset
  1330
            closeOnResetLoggers.clear();
30643
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1331
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1332
            // if reset has been called from shutdown-hook (Cleaner),
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1333
            // or if reset has been called from readConfiguration() which
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1334
            // already holds the lock and will change the state itself,
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1335
            // then do not change state here...
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1336
            if (globalHandlersState != STATE_SHUTDOWN &&
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1337
                globalHandlersState != STATE_READING_CONFIG) {
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1338
                // ...else user called reset()...
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1339
                // Since we are doing a reset we no longer want to initialize
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1340
                // the global handlers, if they haven't been initialized yet.
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1341
                globalHandlersState = STATE_INITIALIZED;
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1342
            }
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1343
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1344
            for (LoggerContext cx : contexts()) {
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1345
                resetLoggerContext(cx);
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1346
            }
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1347
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1348
            persistent.clear();
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1349
        } finally {
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1350
            configurationLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
        }
30643
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1352
    }
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1353
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1354
    private void resetLoggerContext(LoggerContext cx) {
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1355
        Enumeration<String> enum_ = cx.getLoggerNames();
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1356
        while (enum_.hasMoreElements()) {
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1357
            String name = enum_.nextElement();
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1358
            Logger logger = cx.findLogger(name);
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1359
            if (logger != null) {
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1360
                resetLogger(logger);
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
  1361
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
30643
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1365
    private void closeHandlers(Logger logger) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
        Handler[] targets = logger.getHandlers();
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 22046
diff changeset
  1367
        for (Handler h : targets) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
            logger.removeHandler(h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
                h.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
            } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
                // Problems closing a handler?  Keep going...
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
        }
30643
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1375
    }
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1376
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1377
    // Private method to reset an individual target logger.
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1378
    private void resetLogger(Logger logger) {
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1379
        // Close all the Logger handlers.
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1380
        closeHandlers(logger);
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1381
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1382
        // Reset Logger level
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
  1383
        String name = logger.getName();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
        if (name != null && name.equals("")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
            // This is the root logger.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
            logger.setLevel(defaultLevel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
            logger.setLevel(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
    // get a list of whitespace separated classnames from a property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
    private String[] parseClassNames(String propertyName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
        String hands = getProperty(propertyName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
        if (hands == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
            return new String[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
        hands = hands.trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
        int ix = 0;
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
  1400
        final List<String> result = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
        while (ix < hands.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
            int end = ix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
            while (end < hands.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
                if (Character.isWhitespace(hands.charAt(end))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
                if (hands.charAt(end) == ',') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
                end++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
            String word = hands.substring(ix, end);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
            ix = end+1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
            word = word.trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
            if (word.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
            result.add(word);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
        return result.toArray(new String[result.size()]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
     * Reinitialize the logging properties and reread the logging configuration
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
     * from the given stream, which should be in java.util.Properties format.
26859
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1426
     * Any {@linkplain #addConfigurationListener registered configuration
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1427
     * listener} will be invoked after the properties are read.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
     * Any log level definitions in the new configuration file will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
     * applied using Logger.setLevel(), if the target Logger exists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
     * @param ins       stream to read properties from
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
     * @exception  SecurityException  if a security manager exists and if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
     *             the caller does not have LoggingPermission("control").
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
     * @exception  IOException if there are problems reading from the stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
    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
  1438
        checkPermission();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
30643
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1440
        // We don't want reset() and readConfiguration() to run
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1441
        // in parallel.
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1442
        configurationLock.lock();
29739
3966f5f6a6fd 8075810: LogManager.readConfiguration may throw undocumented IllegalArgumentException
dfuchs
parents: 27932
diff changeset
  1443
        try {
30643
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1444
            if (globalHandlersState == STATE_SHUTDOWN) {
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1445
                // already in terminal state: don't even bother
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1446
                // to read the configuration
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1447
                return;
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1448
            }
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1449
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1450
            // change state to STATE_READING_CONFIG to signal reset() to not change it
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1451
            globalHandlersState = STATE_READING_CONFIG;
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1452
            try {
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1453
                // reset configuration which leaves globalHandlersState at STATE_READING_CONFIG
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1454
                // so that while reading configuration, any ongoing logging requests block and
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1455
                // wait for the outcome (see the end of this try statement)
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1456
                reset();
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1457
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1458
                try {
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1459
                    // Load the properties
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1460
                    props.load(ins);
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1461
                } catch (IllegalArgumentException x) {
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1462
                    // props.load may throw an IllegalArgumentException if the stream
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1463
                    // contains malformed Unicode escape sequences.
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1464
                    // We wrap that in an IOException as readConfiguration is
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1465
                    // specified to throw IOException if there are problems reading
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1466
                    // from the stream.
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1467
                    // Note: new IOException(x.getMessage(), x) allow us to get a more
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1468
                    // concise error message than new IOException(x);
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1469
                    throw new IOException(x.getMessage(), x);
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1470
                }
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1471
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1472
                // Instantiate new configuration objects.
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1473
                String names[] = parseClassNames("config");
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1474
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1475
                for (String word : names) {
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1476
                    try {
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1477
                        Class<?> clz = ClassLoader.getSystemClassLoader().loadClass(word);
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1478
                        clz.newInstance();
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1479
                    } catch (Exception ex) {
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1480
                        System.err.println("Can't load config class \"" + word + "\"");
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1481
                        System.err.println("" + ex);
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1482
                        // ex.printStackTrace();
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1483
                    }
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1484
                }
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1485
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1486
                // Set levels on any pre-existing loggers, based on the new properties.
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1487
                setLevelsOnExistingLoggers();
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1488
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1489
                // Note that we need to reinitialize global handles when
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1490
                // they are first referenced.
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1491
                globalHandlersState = STATE_UNINITIALIZED;
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1492
            } catch (Throwable t) {
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1493
                // If there were any trouble, then set state to STATE_INITIALIZED
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1494
                // so that no global handlers reinitialization is performed on not fully
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1495
                // initialized configuration.
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1496
                globalHandlersState = STATE_INITIALIZED;
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1497
                // re-throw
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1498
                throw t;
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1499
            }
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1500
        } finally {
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1501
            configurationLock.unlock();
29739
3966f5f6a6fd 8075810: LogManager.readConfiguration may throw undocumented IllegalArgumentException
dfuchs
parents: 27932
diff changeset
  1502
        }
3966f5f6a6fd 8075810: LogManager.readConfiguration may throw undocumented IllegalArgumentException
dfuchs
parents: 27932
diff changeset
  1503
30643
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1504
        // should be called out of lock to avoid dead-lock situations
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1505
        // when user code is involved
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1506
        invokeConfigurationListeners();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
     * Get the value of a logging property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
     * The method returns null if the property is not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
     * @param name      property name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
     * @return          property value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
    public String getProperty(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
        return props.getProperty(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
    // Package private method to get a String property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
    // If the property is not defined we return the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
    // default value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
    String getStringProperty(String name, String defaultValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
        String val = getProperty(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
        if (val == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
            return defaultValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
        return val.trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
    // Package private method to get an integer property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
    // If the property is not defined or cannot be parsed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
    // we return the given default value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
    int getIntProperty(String name, int defaultValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
        String val = getProperty(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
        if (val == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
            return defaultValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
            return Integer.parseInt(val.trim());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
        } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
            return defaultValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
27191
45a3002de56f 8059767: FileHandler should allow 'long' limits and handle overflow of MeteredStream.written.
dfuchs
parents: 26859
diff changeset
  1545
    // Package private method to get a long property.
45a3002de56f 8059767: FileHandler should allow 'long' limits and handle overflow of MeteredStream.written.
dfuchs
parents: 26859
diff changeset
  1546
    // If the property is not defined or cannot be parsed
45a3002de56f 8059767: FileHandler should allow 'long' limits and handle overflow of MeteredStream.written.
dfuchs
parents: 26859
diff changeset
  1547
    // we return the given default value.
45a3002de56f 8059767: FileHandler should allow 'long' limits and handle overflow of MeteredStream.written.
dfuchs
parents: 26859
diff changeset
  1548
    long getLongProperty(String name, long defaultValue) {
45a3002de56f 8059767: FileHandler should allow 'long' limits and handle overflow of MeteredStream.written.
dfuchs
parents: 26859
diff changeset
  1549
        String val = getProperty(name);
45a3002de56f 8059767: FileHandler should allow 'long' limits and handle overflow of MeteredStream.written.
dfuchs
parents: 26859
diff changeset
  1550
        if (val == null) {
45a3002de56f 8059767: FileHandler should allow 'long' limits and handle overflow of MeteredStream.written.
dfuchs
parents: 26859
diff changeset
  1551
            return defaultValue;
45a3002de56f 8059767: FileHandler should allow 'long' limits and handle overflow of MeteredStream.written.
dfuchs
parents: 26859
diff changeset
  1552
        }
45a3002de56f 8059767: FileHandler should allow 'long' limits and handle overflow of MeteredStream.written.
dfuchs
parents: 26859
diff changeset
  1553
        try {
45a3002de56f 8059767: FileHandler should allow 'long' limits and handle overflow of MeteredStream.written.
dfuchs
parents: 26859
diff changeset
  1554
            return Long.parseLong(val.trim());
45a3002de56f 8059767: FileHandler should allow 'long' limits and handle overflow of MeteredStream.written.
dfuchs
parents: 26859
diff changeset
  1555
        } catch (Exception ex) {
45a3002de56f 8059767: FileHandler should allow 'long' limits and handle overflow of MeteredStream.written.
dfuchs
parents: 26859
diff changeset
  1556
            return defaultValue;
45a3002de56f 8059767: FileHandler should allow 'long' limits and handle overflow of MeteredStream.written.
dfuchs
parents: 26859
diff changeset
  1557
        }
45a3002de56f 8059767: FileHandler should allow 'long' limits and handle overflow of MeteredStream.written.
dfuchs
parents: 26859
diff changeset
  1558
    }
45a3002de56f 8059767: FileHandler should allow 'long' limits and handle overflow of MeteredStream.written.
dfuchs
parents: 26859
diff changeset
  1559
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
    // Package private method to get a boolean property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
    // If the property is not defined or cannot be parsed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
    // we return the given default value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
    boolean getBooleanProperty(String name, boolean defaultValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
        String val = getProperty(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
        if (val == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
            return defaultValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
        val = val.toLowerCase();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
        if (val.equals("true") || val.equals("1")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
        } else if (val.equals("false") || val.equals("0")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
        return defaultValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
    // Package private method to get a Level property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
    // If the property is not defined or cannot be parsed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
    // we return the given default value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
    Level getLevelProperty(String name, Level defaultValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
        String val = getProperty(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
        if (val == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
            return defaultValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
        }
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
  1585
        Level l = Level.findLevel(val.trim());
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
  1586
        return l != null ? l : defaultValue;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
    // Package private method to get a filter property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
    // We return an instance of the class named by the "name"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
    // property. If the property is not defined or has problems
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
    // we return the defaultValue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
    Filter getFilterProperty(String name, Filter defaultValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
        String val = getProperty(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
            if (val != null) {
11274
7e7196757acd 7117249: fix warnings in java.util.jar, .logging, .prefs, .zip
smarks
parents: 9700
diff changeset
  1597
                Class<?> clz = ClassLoader.getSystemClassLoader().loadClass(val);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
                return (Filter) clz.newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
        } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
            // We got one of a variety of exceptions in creating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
            // class or creating an instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
            // Drop through.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
        // We got an exception.  Return the defaultValue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
        return defaultValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
    // Package private method to get a formatter property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
    // We return an instance of the class named by the "name"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
    // property. If the property is not defined or has problems
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
    // we return the defaultValue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
    Formatter getFormatterProperty(String name, Formatter defaultValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
        String val = getProperty(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
            if (val != null) {
11274
7e7196757acd 7117249: fix warnings in java.util.jar, .logging, .prefs, .zip
smarks
parents: 9700
diff changeset
  1618
                Class<?> clz = ClassLoader.getSystemClassLoader().loadClass(val);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
                return (Formatter) clz.newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
        } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
            // We got one of a variety of exceptions in creating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
            // class or creating an instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
            // Drop through.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
        // We got an exception.  Return the defaultValue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
        return defaultValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
    // Private method to load the global handlers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
    // We do the real work lazily, when the global handlers
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
    // are first used.
30643
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1633
    private void initializeGlobalHandlers() {
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1634
        int state = globalHandlersState;
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1635
        if (state == STATE_INITIALIZED ||
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1636
            state == STATE_SHUTDOWN) {
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1637
            // Nothing to do: return.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
30643
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1641
        // If we have not initialized global handlers yet (or need to
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1642
        // reinitialize them), lets do it now (this case is indicated by
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1643
        // globalHandlersState == STATE_UNINITIALIZED).
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1644
        // If we are in the process of initializing global handlers we
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1645
        // also need to lock & wait (this case is indicated by
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1646
        // globalHandlersState == STATE_INITIALIZING).
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1647
        // If we are in the process of reading configuration we also need to
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1648
        // wait to see what the outcome will be (this case
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1649
        // is indicated by globalHandlersState == STATE_READING_CONFIG)
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1650
        // So in either case we need to wait for the lock.
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1651
        configurationLock.lock();
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1652
        try {
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1653
            if (globalHandlersState != STATE_UNINITIALIZED) {
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1654
                return; // recursive call or nothing to do
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1655
            }
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1656
            // set globalHandlersState to STATE_INITIALIZING first to avoid
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1657
            // getting an infinite recursion when loadLoggerHandlers(...)
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1658
            // is going to call addHandler(...)
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1659
            globalHandlersState = STATE_INITIALIZING;
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1660
            try {
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1661
                loadLoggerHandlers(rootLogger, null, "handlers");
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1662
            } finally {
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1663
                globalHandlersState = STATE_INITIALIZED;
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1664
            }
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1665
        } finally {
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1666
            configurationLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
22110
06e486bc20b6 8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents: 22086
diff changeset
  1670
    static final Permission controlPermission = new LoggingPermission("control", null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
14216
23714b376286 7169884: LogManager checks do not work correctly for sub-types
alanb
parents: 13578
diff changeset
  1672
    void checkPermission() {
23714b376286 7169884: LogManager checks do not work correctly for sub-types
alanb
parents: 13578
diff changeset
  1673
        SecurityManager sm = System.getSecurityManager();
23714b376286 7169884: LogManager checks do not work correctly for sub-types
alanb
parents: 13578
diff changeset
  1674
        if (sm != null)
23714b376286 7169884: LogManager checks do not work correctly for sub-types
alanb
parents: 13578
diff changeset
  1675
            sm.checkPermission(controlPermission);
23714b376286 7169884: LogManager checks do not work correctly for sub-types
alanb
parents: 13578
diff changeset
  1676
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
     * Check that the current context is trusted to modify the logging
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
     * configuration.  This requires LoggingPermission("control").
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
     * If the check fails we throw a SecurityException, otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
     * we return normally.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
     * @exception  SecurityException  if a security manager exists and if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
     *             the caller does not have LoggingPermission("control").
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
    public void checkAccess() throws SecurityException {
14216
23714b376286 7169884: LogManager checks do not work correctly for sub-types
alanb
parents: 13578
diff changeset
  1689
        checkPermission();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
    // Nested class to represent a node in our tree of named loggers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
    private static class LogNode {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
        HashMap<String,LogNode> children;
5964
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1695
        LoggerWeakRef loggerRef;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
        LogNode parent;
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
  1697
        final LoggerContext context;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
  1699
        LogNode(LogNode parent, LoggerContext context) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
            this.parent = parent;
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
  1701
            this.context = context;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
        // Recursive method to walk the tree below a node and set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
        // a new parent logger.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
        void walkAndSetParent(Logger parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
            if (children == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
            }
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 22046
diff changeset
  1710
            for (LogNode node : children.values()) {
5964
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1711
                LoggerWeakRef ref = node.loggerRef;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
                Logger logger = (ref == null) ? null : ref.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
                if (logger == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
                    node.walkAndSetParent(parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
                    doSetParent(logger, parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
    // We use a subclass of Logger for the root logger, so
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
    // that we only instantiate the global handlers when they
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
    // are first needed.
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
  1725
    private final class RootLogger extends Logger {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
        private RootLogger() {
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
  1727
            // 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
  1728
            // to avoid calling LogManager.getLogManager() from within the
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
  1729
            // RootLogger constructor.
23899
a926ecef7eb3 8029740: Enhance handling of loggers
dfuchs
parents: 23342
diff changeset
  1730
            super("", null, null, LogManager.this, true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
  1733
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
        public void log(LogRecord record) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
            // Make sure that the global handlers have been instantiated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
            initializeGlobalHandlers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
            super.log(record);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
  1740
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
        public void addHandler(Handler h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
            initializeGlobalHandlers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
            super.addHandler(h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
  1746
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
        public void removeHandler(Handler h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
            initializeGlobalHandlers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
            super.removeHandler(h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
  1752
        @Override
23899
a926ecef7eb3 8029740: Enhance handling of loggers
dfuchs
parents: 23342
diff changeset
  1753
        Handler[] accessCheckedHandlers() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
            initializeGlobalHandlers();
23899
a926ecef7eb3 8029740: Enhance handling of loggers
dfuchs
parents: 23342
diff changeset
  1755
            return super.accessCheckedHandlers();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
    // Private method to be called when the configuration has
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
    // changed to apply any level settings to any pre-existing loggers.
30643
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1762
    private void setLevelsOnExistingLoggers() {
11274
7e7196757acd 7117249: fix warnings in java.util.jar, .logging, .prefs, .zip
smarks
parents: 9700
diff changeset
  1763
        Enumeration<?> enum_ = props.propertyNames();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
        while (enum_.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
            String key = (String)enum_.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
            if (!key.endsWith(".level")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
                // Not a level definition.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
            int ix = key.length() - 6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
            String name = key.substring(0, ix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
            Level level = getLevelProperty(key, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
            if (level == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
                System.err.println("Bad level value for property: " + key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
            }
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
  1777
            for (LoggerContext cx : contexts()) {
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
  1778
                Logger l = cx.findLogger(name);
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
  1779
                if (l == null) {
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
  1780
                    continue;
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
  1781
                }
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
  1782
                l.setLevel(level);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
    // Management Support
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
    private static LoggingMXBean loggingMXBean = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
     * String representation of the
9013
eedac0b9f552 7024172: Move BufferPoolMXBean and PlatformLoggingMXBean java.lang.management
mchung
parents: 7803
diff changeset
  1791
     * {@link javax.management.ObjectName} for the management interface
eedac0b9f552 7024172: Move BufferPoolMXBean and PlatformLoggingMXBean java.lang.management
mchung
parents: 7803
diff changeset
  1792
     * for the logging facility.
eedac0b9f552 7024172: Move BufferPoolMXBean and PlatformLoggingMXBean java.lang.management
mchung
parents: 7803
diff changeset
  1793
     *
eedac0b9f552 7024172: Move BufferPoolMXBean and PlatformLoggingMXBean java.lang.management
mchung
parents: 7803
diff changeset
  1794
     * @see java.lang.management.PlatformLoggingMXBean
eedac0b9f552 7024172: Move BufferPoolMXBean and PlatformLoggingMXBean java.lang.management
mchung
parents: 7803
diff changeset
  1795
     * @see java.util.logging.LoggingMXBean
eedac0b9f552 7024172: Move BufferPoolMXBean and PlatformLoggingMXBean java.lang.management
mchung
parents: 7803
diff changeset
  1796
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
    public final static String LOGGING_MXBEAN_NAME
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
        = "java.util.logging:type=Logging";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
     * Returns <tt>LoggingMXBean</tt> for managing loggers.
9013
eedac0b9f552 7024172: Move BufferPoolMXBean and PlatformLoggingMXBean java.lang.management
mchung
parents: 7803
diff changeset
  1804
     * An alternative way to manage loggers is through the
eedac0b9f552 7024172: Move BufferPoolMXBean and PlatformLoggingMXBean java.lang.management
mchung
parents: 7803
diff changeset
  1805
     * {@link java.lang.management.PlatformLoggingMXBean} interface
eedac0b9f552 7024172: Move BufferPoolMXBean and PlatformLoggingMXBean java.lang.management
mchung
parents: 7803
diff changeset
  1806
     * that can be obtained by calling:
4172
bceea7bc12b7 6876135: Add PlatformLoggingMXBean to eliminate the dependency on JMX from logging
mchung
parents: 3861
diff changeset
  1807
     * <pre>
9013
eedac0b9f552 7024172: Move BufferPoolMXBean and PlatformLoggingMXBean java.lang.management
mchung
parents: 7803
diff changeset
  1808
     *     PlatformLoggingMXBean logging = {@link java.lang.management.ManagementFactory#getPlatformMXBean(Class)
eedac0b9f552 7024172: Move BufferPoolMXBean and PlatformLoggingMXBean java.lang.management
mchung
parents: 7803
diff changeset
  1809
     *         ManagementFactory.getPlatformMXBean}(PlatformLoggingMXBean.class);
4172
bceea7bc12b7 6876135: Add PlatformLoggingMXBean to eliminate the dependency on JMX from logging
mchung
parents: 3861
diff changeset
  1810
     * </pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
     * @return a {@link LoggingMXBean} object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
     *
9013
eedac0b9f552 7024172: Move BufferPoolMXBean and PlatformLoggingMXBean java.lang.management
mchung
parents: 7803
diff changeset
  1814
     * @see java.lang.management.PlatformLoggingMXBean
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
     */
9013
eedac0b9f552 7024172: Move BufferPoolMXBean and PlatformLoggingMXBean java.lang.management
mchung
parents: 7803
diff changeset
  1817
    public static synchronized LoggingMXBean getLoggingMXBean() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
        if (loggingMXBean == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
            loggingMXBean =  new Logging();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
        return loggingMXBean;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
    }
26859
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1823
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1824
    /**
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1825
     * Adds a configuration listener to be invoked each time the logging
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1826
     * configuration is read.
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1827
     * If the listener is already registered the method does nothing.
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1828
     * <p>
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1829
     * The listener is invoked with privileges that are restricted by the
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1830
     * calling context of this method.
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1831
     * The order in which the listeners are invoked is unspecified.
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1832
     * <p>
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1833
     * It is recommended that listeners do not throw errors or exceptions.
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1834
     *
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1835
     * If a listener terminates with an uncaught error or exception then
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1836
     * the first exception will be propagated to the caller of
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1837
     * {@link #readConfiguration()} (or {@link #readConfiguration(java.io.InputStream)})
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1838
     * after all listeners have been invoked.
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1839
     *
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1840
     * @implNote If more than one listener terminates with an uncaught error or
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1841
     * exception, an implementation may record the additional errors or
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1842
     * exceptions as {@linkplain Throwable#addSuppressed(java.lang.Throwable)
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1843
     * suppressed exceptions}.
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1844
     *
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1845
     * @param listener A configuration listener that will be invoked after the
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1846
     *        configuration changed.
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1847
     * @return This LogManager.
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1848
     * @throws SecurityException if a security manager exists and if the
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1849
     * caller does not have LoggingPermission("control").
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1850
     * @throws NullPointerException if the listener is null.
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1851
     *
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1852
     * @since 1.9
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1853
     */
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1854
    public LogManager addConfigurationListener(Runnable listener) {
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1855
        final Runnable r = Objects.requireNonNull(listener);
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1856
        checkPermission();
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1857
        final SecurityManager sm = System.getSecurityManager();
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1858
        final AccessControlContext acc =
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1859
                sm == null ? null : AccessController.getContext();
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1860
        final PrivilegedAction<Void> pa =
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1861
                acc == null ? null : () -> { r.run() ; return null; };
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1862
        final Runnable pr =
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1863
                acc == null ? r : () -> AccessController.doPrivileged(pa, acc);
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1864
        // Will do nothing if already registered.
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1865
        listeners.putIfAbsent(r, pr);
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1866
        return this;
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1867
    }
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1868
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1869
    /**
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1870
     * Removes a previously registered configuration listener.
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1871
     *
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1872
     * Returns silently if the listener is not found.
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1873
     *
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1874
     * @param listener the configuration listener to remove.
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1875
     * @throws NullPointerException if the listener is null.
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1876
     * @throws SecurityException if a security manager exists and if the
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1877
     * caller does not have LoggingPermission("control").
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1878
     *
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1879
     * @since 1.9
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1880
     */
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1881
    public void removeConfigurationListener(Runnable listener) {
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1882
        final Runnable key = Objects.requireNonNull(listener);
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1883
        checkPermission();
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1884
        listeners.remove(key);
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1885
    }
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1886
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1887
    private void invokeConfigurationListeners() {
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1888
        Throwable t = null;
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1889
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1890
        // We're using an IdentityHashMap because we want to compare
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1891
        // keys using identity (==).
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1892
        // We don't want to loop within a block synchronized on 'listeners'
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1893
        // to avoid invoking listeners from yet another synchronized block.
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1894
        // So we're taking a snapshot of the values list to avoid the risk of
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1895
        // ConcurrentModificationException while looping.
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1896
        //
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1897
        for (Runnable c : listeners.values().toArray(new Runnable[0])) {
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1898
            try {
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1899
                c.run();
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1900
            } catch (ThreadDeath death) {
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1901
                throw death;
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1902
            } catch (Error | RuntimeException x) {
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1903
                if (t == null) t = x;
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1904
                else t.addSuppressed(x);
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1905
            }
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1906
        }
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1907
        // Listeners are not supposed to throw exceptions, but if that
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1908
        // happens, we will rethrow the first error or exception that is raised
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1909
        // after all listeners have been invoked.
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1910
        if (t instanceof Error) throw (Error)t;
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1911
        if (t instanceof RuntimeException) throw (RuntimeException)t;
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1912
    }
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1913
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
}