jdk/src/java.logging/share/classes/java/util/logging/LogManager.java
author dfuchs
Thu, 06 Apr 2017 14:38:15 +0100
changeset 44538 a603c475d649
parent 44263 7a9297d467e7
child 44545 83b611b88ac8
permissions -rw-r--r--
8178139: Minor typo in API documentation of java.util.logging.Logger Reviewed-by: lancea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
39634
812020bcb9f1 8159245: Loggers created by system classes are not initialized correctly when configured programmatically from application code.
dfuchs
parents: 38370
diff changeset
     2
 * Copyright (c) 2000, 2016, 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;
32984
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
    35
import java.nio.file.Paths;
27754
ee2f7768e1c9 8060132: Handlers configured on abstract nodes in logging.properties are not always properly closed
dfuchs
parents: 27191
diff changeset
    36
import java.util.concurrent.CopyOnWriteArrayList;
30643
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
    37
import java.util.concurrent.locks.ReentrantLock;
32984
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
    38
import java.util.function.BiFunction;
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
    39
import java.util.function.Function;
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
    40
import java.util.function.Predicate;
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
    41
import java.util.stream.Collectors;
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
    42
import java.util.stream.Stream;
32834
e1dca5fe4de3 8137056: Move SharedSecrets and interface friends out of sun.misc
chegar
parents: 32037
diff changeset
    43
import jdk.internal.misc.JavaAWTAccess;
e1dca5fe4de3 8137056: Move SharedSecrets and interface friends out of sun.misc
chegar
parents: 32037
diff changeset
    44
import jdk.internal.misc.SharedSecrets;
33875
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents: 32984
diff changeset
    45
import sun.util.logging.internal.LoggingProviderImpl;
37672
03684934dc09 8148568: LoggerFinder.getLogger and LoggerFinder.getLocalizedLogger should take a Module argument instead of a Class.
dfuchs
parents: 37520
diff changeset
    46
import java.lang.reflect.Module;
03684934dc09 8148568: LoggerFinder.getLogger and LoggerFinder.getLocalizedLogger should take a Module argument instead of a Class.
dfuchs
parents: 37520
diff changeset
    47
import static jdk.internal.logger.DefaultLoggerFinder.isSystem;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * There is a single global LogManager object that is used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * maintain a set of shared state about Loggers and log services.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * This LogManager object:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * <li> Manages a hierarchical namespace of Logger objects.  All
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *      named Loggers are stored in this namespace.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * <li> Manages a set of logging control properties.  These are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *      simple key-value pairs that can be used by Handlers and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *      other logging objects to configure themselves.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * The global LogManager object can be retrieved using LogManager.getLogManager().
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * The LogManager object is created during class initialization and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * cannot subsequently be changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * At startup the LogManager class is located using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * java.util.logging.manager system property.
32984
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
    68
 *
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
    69
 * <h3>LogManager Configuration</h3>
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
    70
 *
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
    71
 * A LogManager initializes the logging configuration via
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
    72
 * the {@link #readConfiguration()} method during LogManager initialization.
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
    73
 * By default, LogManager default configuration is used.
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
    74
 * The logging configuration read by LogManager must be in the
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
    75
 * {@linkplain Properties properties file} format.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * <p>
14691
22056e110b04 8003949: LogManager, downgrade normative reference to ${java.home}/lib/logging.properties
alanb
parents: 14342
diff changeset
    77
 * The LogManager defines two optional system properties that allow control over
32984
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
    78
 * the initial configuration, as specified in the {@link #readConfiguration()}
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
    79
 * method:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * <li>"java.util.logging.config.class"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * <li>"java.util.logging.config.file"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * </ul>
32984
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
    84
 * <p>
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
    85
 * These two system properties may be specified on the command line to the "java"
14691
22056e110b04 8003949: LogManager, downgrade normative reference to ${java.home}/lib/logging.properties
alanb
parents: 14342
diff changeset
    86
 * command, or as system property definitions passed to JNI_CreateJavaVM.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * <p>
32984
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
    88
 * The {@linkplain Properties properties} for loggers and Handlers will have
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
    89
 * names starting with the dot-separated name for the handler or logger.<br>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * The global logging properties may include:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * <li>A property "handlers".  This defines a whitespace or comma separated
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * list of class names for handler classes to load and register as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * handlers on the root Logger (the Logger named "").  Each class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * name must be for a Handler class which has a default constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * Note that these Handlers may be created lazily, when they are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * first used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * <li>A property "&lt;logger&gt;.handlers". This defines a whitespace or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * comma separated list of class names for handlers classes to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * load and register as handlers to the specified logger. Each class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * name must be for a Handler class which has a default constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * Note that these Handlers may be created lazily, when they are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * first used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 *
27754
ee2f7768e1c9 8060132: Handlers configured on abstract nodes in logging.properties are not always properly closed
dfuchs
parents: 27191
diff changeset
   106
 * <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
   107
 * 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
   108
 * 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
   109
 * 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
   110
 * 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
   111
 * 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
   112
 * 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
   113
 * 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
   114
 * {@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
   115
 * 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
   116
 * 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
   117
 * collected.
ee2f7768e1c9 8060132: Handlers configured on abstract nodes in logging.properties are not always properly closed
dfuchs
parents: 27191
diff changeset
   118
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 * <li>A property "&lt;logger&gt;.useParentHandlers". This defines a boolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 * value. By default every logger calls its parent in addition to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 * handling the logging message itself, this often result in messages
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 * being handled by the root logger as well. When setting this property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 * to false a Handler needs to be configured for this logger otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 * no logging messages are delivered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 * <li>A property "config".  This property is intended to allow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 * arbitrary configuration code to be run.  The property defines a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 * whitespace or comma separated list of class names.  A new instance will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 * created for each named class.  The default constructor of each class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 * may execute arbitrary code to update the logging configuration, such as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 * setting logger levels, adding handlers, adding filters, etc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 * Note that all classes loaded during LogManager configuration are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 * first searched on the system class path before any user class path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 * That includes the LogManager class, any config classes, and any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 * handler classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
 * Loggers are organized into a naming hierarchy based on their
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
 * dot separated names.  Thus "a.b.c" is a child of "a.b", but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
 * "a.b1" and a.b2" are peers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
 * All properties whose names end with ".level" are assumed to define
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
 * log levels for Loggers.  Thus "foo.level" defines a log level for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
 * the logger called "foo" and (recursively) for any of its children
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
 * in the naming hierarchy.  Log Levels are applied in the order they
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
 * are defined in the properties file.  Thus level settings for child
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
 * nodes in the tree should come after settings for their parents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
 * The property name ".level" can be used to set the level for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
 * root of the tree.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
 * All methods on the LogManager object are multi-thread safe.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
public class LogManager {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    // The global LogManager object
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   159
    private static final LogManager manager;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
21960
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
   161
    // 'props' is assigned within a lock but accessed without it.
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
   162
    // Declaring it volatile makes sure that another thread will not
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
   163
    // be able to see a partially constructed 'props' object.
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
   164
    // (seeing a partially constructed 'props' object can result in
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
   165
    // NPE being thrown in Hashtable.get(), because it leaves the door
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
   166
    // open for props.getProperties() to be called before the construcor
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
   167
    // of Hashtable is actually completed).
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
   168
    private volatile Properties props = new Properties();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    private final static Level defaultLevel = Level.INFO;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   171
    // LoggerContext for system loggers and user loggers
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   172
    private final LoggerContext systemContext = new SystemLoggerContext();
16105
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   173
    private final LoggerContext userContext = new LoggerContext();
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   174
    // 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
   175
    // will see the new value once ensureLogManagerInitialized() has finished
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   176
    // executing.
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   177
    private volatile Logger rootLogger;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    // Have we done the primordial reading of the configuration file?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    // (Must be done after a suitable amount of java.lang.System
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    // initialization has been done)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    private volatile boolean readPrimordialConfiguration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    // Have we initialized global (root) handlers yet?
30643
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
   183
    // This gets set to STATE_UNINITIALIZED in readConfiguration
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
   184
    private static final int
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
   185
            STATE_INITIALIZED = 0, // initial state
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
   186
            STATE_INITIALIZING = 1,
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
   187
            STATE_READING_CONFIG = 2,
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
   188
            STATE_UNINITIALIZED = 3,
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
   189
            STATE_SHUTDOWN = 4;    // terminal state
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
   190
    private volatile int globalHandlersState; // = STATE_INITIALIZED;
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
   191
    // A concurrency lock for reset(), readConfiguration() and Cleaner.
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
   192
    private final ReentrantLock configurationLock = new ReentrantLock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
27754
ee2f7768e1c9 8060132: Handlers configured on abstract nodes in logging.properties are not always properly closed
dfuchs
parents: 27191
diff changeset
   194
    // 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
   195
    // 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
   196
    // 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
   197
    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
   198
        private final Logger logger;
ee2f7768e1c9 8060132: Handlers configured on abstract nodes in logging.properties are not always properly closed
dfuchs
parents: 27191
diff changeset
   199
        private CloseOnReset(Logger ref) {
ee2f7768e1c9 8060132: Handlers configured on abstract nodes in logging.properties are not always properly closed
dfuchs
parents: 27191
diff changeset
   200
            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
   201
        }
ee2f7768e1c9 8060132: Handlers configured on abstract nodes in logging.properties are not always properly closed
dfuchs
parents: 27191
diff changeset
   202
        @Override
ee2f7768e1c9 8060132: Handlers configured on abstract nodes in logging.properties are not always properly closed
dfuchs
parents: 27191
diff changeset
   203
        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
   204
            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
   205
        }
ee2f7768e1c9 8060132: Handlers configured on abstract nodes in logging.properties are not always properly closed
dfuchs
parents: 27191
diff changeset
   206
        @Override
ee2f7768e1c9 8060132: Handlers configured on abstract nodes in logging.properties are not always properly closed
dfuchs
parents: 27191
diff changeset
   207
        public int hashCode() {
ee2f7768e1c9 8060132: Handlers configured on abstract nodes in logging.properties are not always properly closed
dfuchs
parents: 27191
diff changeset
   208
            return System.identityHashCode(logger);
ee2f7768e1c9 8060132: Handlers configured on abstract nodes in logging.properties are not always properly closed
dfuchs
parents: 27191
diff changeset
   209
        }
ee2f7768e1c9 8060132: Handlers configured on abstract nodes in logging.properties are not always properly closed
dfuchs
parents: 27191
diff changeset
   210
        public Logger get() {
ee2f7768e1c9 8060132: Handlers configured on abstract nodes in logging.properties are not always properly closed
dfuchs
parents: 27191
diff changeset
   211
            return logger;
ee2f7768e1c9 8060132: Handlers configured on abstract nodes in logging.properties are not always properly closed
dfuchs
parents: 27191
diff changeset
   212
        }
ee2f7768e1c9 8060132: Handlers configured on abstract nodes in logging.properties are not always properly closed
dfuchs
parents: 27191
diff changeset
   213
        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
   214
            return new CloseOnReset(logger);
ee2f7768e1c9 8060132: Handlers configured on abstract nodes in logging.properties are not always properly closed
dfuchs
parents: 27191
diff changeset
   215
        }
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
    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
   218
            new CopyOnWriteArrayList<>();
ee2f7768e1c9 8060132: Handlers configured on abstract nodes in logging.properties are not always properly closed
dfuchs
parents: 27191
diff changeset
   219
ee2f7768e1c9 8060132: Handlers configured on abstract nodes in logging.properties are not always properly closed
dfuchs
parents: 27191
diff changeset
   220
26859
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
   221
    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
   222
            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
   223
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    static {
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   225
        manager = AccessController.doPrivileged(new PrivilegedAction<LogManager>() {
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   226
            @Override
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   227
            public LogManager run() {
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   228
                LogManager mgr = null;
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   229
                String cname = null;
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   230
                try {
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   231
                    cname = System.getProperty("java.util.logging.manager");
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   232
                    if (cname != null) {
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   233
                        try {
37782
ad8fe7507ecc 6850612: Deprecate Class.newInstance since it violates the checked exception language contract
darcy
parents: 37672
diff changeset
   234
                            @SuppressWarnings("deprecation")
ad8fe7507ecc 6850612: Deprecate Class.newInstance since it violates the checked exception language contract
darcy
parents: 37672
diff changeset
   235
                            Object tmp = ClassLoader.getSystemClassLoader()
ad8fe7507ecc 6850612: Deprecate Class.newInstance since it violates the checked exception language contract
darcy
parents: 37672
diff changeset
   236
                                .loadClass(cname).newInstance();
ad8fe7507ecc 6850612: Deprecate Class.newInstance since it violates the checked exception language contract
darcy
parents: 37672
diff changeset
   237
                            mgr = (LogManager) tmp;
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   238
                        } catch (ClassNotFoundException ex) {
37782
ad8fe7507ecc 6850612: Deprecate Class.newInstance since it violates the checked exception language contract
darcy
parents: 37672
diff changeset
   239
                            @SuppressWarnings("deprecation")
ad8fe7507ecc 6850612: Deprecate Class.newInstance since it violates the checked exception language contract
darcy
parents: 37672
diff changeset
   240
                            Object tmp = Thread.currentThread()
ad8fe7507ecc 6850612: Deprecate Class.newInstance since it violates the checked exception language contract
darcy
parents: 37672
diff changeset
   241
                                .getContextClassLoader().loadClass(cname).newInstance();
ad8fe7507ecc 6850612: Deprecate Class.newInstance since it violates the checked exception language contract
darcy
parents: 37672
diff changeset
   242
                            mgr = (LogManager) tmp;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                    }
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   245
                } catch (Exception ex) {
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   246
                    System.err.println("Could not load Logmanager \"" + cname + "\"");
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   247
                    ex.printStackTrace();
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   248
                }
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   249
                if (mgr == null) {
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   250
                    mgr = new LogManager();
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   251
                }
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   252
                return mgr;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   254
            }
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   255
        });
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    // This private class is used as a shutdown hook.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    // It does a "reset" to close all open handlers.
37520
f308aaf25d07 8153158: Remove sun.misc.ManagedLocalsThread from java.logging
chegar
parents: 35302
diff changeset
   260
    private class Cleaner extends Thread {
2384
b3ba5fb77f89 6799583: LogManager shutdown hook may cause a memory leak.
bae
parents: 2
diff changeset
   261
b3ba5fb77f89 6799583: LogManager shutdown hook may cause a memory leak.
bae
parents: 2
diff changeset
   262
        private Cleaner() {
37520
f308aaf25d07 8153158: Remove sun.misc.ManagedLocalsThread from java.logging
chegar
parents: 35302
diff changeset
   263
            super(null, null, "Logging-Cleaner", 0, false);
2384
b3ba5fb77f89 6799583: LogManager shutdown hook may cause a memory leak.
bae
parents: 2
diff changeset
   264
            /* 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
   265
             * keeping a strong reference to an application classloader.
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
            this.setContextClassLoader(null);
b3ba5fb77f89 6799583: LogManager shutdown hook may cause a memory leak.
bae
parents: 2
diff changeset
   268
        }
b3ba5fb77f89 6799583: LogManager shutdown hook may cause a memory leak.
bae
parents: 2
diff changeset
   269
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   270
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            // This is to ensure the LogManager.<clinit> is completed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            // before synchronized block. Otherwise deadlocks are possible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            LogManager mgr = manager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
30643
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
   276
            // set globalHandlersState to STATE_SHUTDOWN atomically so that
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
   277
            // 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
   278
            // the configuration again. This is terminal state.
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
   279
            configurationLock.lock();
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
   280
            globalHandlersState = STATE_SHUTDOWN;
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
   281
            configurationLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            // Do a reset to close all active handlers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            reset();
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * Protected constructor.  This is protected so that container applications
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * (such as J2EE containers) can subclass the object.  It is non-public as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * 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
   293
     * retrieved by calling LogManager.getLogManager.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    protected LogManager() {
22321
37f8c8b6ef7a 8024867: Enhance logging start up
dfuchs
parents: 19825
diff changeset
   296
        this(checkSubclassPermissions());
37f8c8b6ef7a 8024867: Enhance logging start up
dfuchs
parents: 19825
diff changeset
   297
    }
37f8c8b6ef7a 8024867: Enhance logging start up
dfuchs
parents: 19825
diff changeset
   298
37f8c8b6ef7a 8024867: Enhance logging start up
dfuchs
parents: 19825
diff changeset
   299
    private LogManager(Void checked) {
37f8c8b6ef7a 8024867: Enhance logging start up
dfuchs
parents: 19825
diff changeset
   300
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        // Add a shutdown hook to close the global handlers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            Runtime.getRuntime().addShutdownHook(new Cleaner());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        } catch (IllegalStateException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            // If the VM is already shutting down,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            // We do not need to register shutdownHook.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
22321
37f8c8b6ef7a 8024867: Enhance logging start up
dfuchs
parents: 19825
diff changeset
   310
    private static Void checkSubclassPermissions() {
37f8c8b6ef7a 8024867: Enhance logging start up
dfuchs
parents: 19825
diff changeset
   311
        final SecurityManager sm = System.getSecurityManager();
37f8c8b6ef7a 8024867: Enhance logging start up
dfuchs
parents: 19825
diff changeset
   312
        if (sm != null) {
37f8c8b6ef7a 8024867: Enhance logging start up
dfuchs
parents: 19825
diff changeset
   313
            // These permission will be checked in the LogManager constructor,
37f8c8b6ef7a 8024867: Enhance logging start up
dfuchs
parents: 19825
diff changeset
   314
            // in order to register the Cleaner() thread as a shutdown hook.
37f8c8b6ef7a 8024867: Enhance logging start up
dfuchs
parents: 19825
diff changeset
   315
            // Check them here to avoid the penalty of constructing the object
37f8c8b6ef7a 8024867: Enhance logging start up
dfuchs
parents: 19825
diff changeset
   316
            // etc...
37f8c8b6ef7a 8024867: Enhance logging start up
dfuchs
parents: 19825
diff changeset
   317
            sm.checkPermission(new RuntimePermission("shutdownHooks"));
37f8c8b6ef7a 8024867: Enhance logging start up
dfuchs
parents: 19825
diff changeset
   318
            sm.checkPermission(new RuntimePermission("setContextClassLoader"));
37f8c8b6ef7a 8024867: Enhance logging start up
dfuchs
parents: 19825
diff changeset
   319
        }
37f8c8b6ef7a 8024867: Enhance logging start up
dfuchs
parents: 19825
diff changeset
   320
        return null;
37f8c8b6ef7a 8024867: Enhance logging start up
dfuchs
parents: 19825
diff changeset
   321
    }
37f8c8b6ef7a 8024867: Enhance logging start up
dfuchs
parents: 19825
diff changeset
   322
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    /**
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   324
     * 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
   325
     * 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
   326
     * 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
   327
     *
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   328
     * 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
   329
     * 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
   330
     * the root and global logger *to the context tree*.
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   331
     *
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   332
     * 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
   333
     * 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
   334
     * added.
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   335
     *
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   336
     * This is why ensureLogManagerInitialized() needs to be called before
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   337
     * any logger is added to any logger context.
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   338
     *
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   339
     */
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   340
    private boolean initializedCalled = false;
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   341
    private volatile boolean initializationDone = false;
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   342
    final void ensureLogManagerInitialized() {
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   343
        final LogManager owner = this;
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   344
        if (initializationDone || owner != manager) {
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   345
            // 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
   346
            // this on private manager instances.
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   347
            return;
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   348
        }
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   349
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   350
        // Maybe another thread has called ensureLogManagerInitialized()
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   351
        // 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
   352
        // 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
   353
        // notice that initializationDone is now true and return.
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   354
        // 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
   355
        // see that initializationDone is still false, and perform the
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   356
        // initialization.
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   357
        //
32035
073a449c007d 8132550: java/util/logging/LoggingDeadlock2.java times out
dfuchs
parents: 31150
diff changeset
   358
        configurationLock.lock();
073a449c007d 8132550: java/util/logging/LoggingDeadlock2.java times out
dfuchs
parents: 31150
diff changeset
   359
        try {
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   360
            // 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
   361
            // the process of initializing the LogManager in this thread.
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   362
            // There has been a recursive call to ensureLogManagerInitialized().
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   363
            final boolean isRecursiveInitialization = (initializedCalled == true);
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   364
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   365
            assert initializedCalled || !initializationDone
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   366
                    : "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
   367
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   368
            if (isRecursiveInitialization || initializationDone) {
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   369
                // If isRecursiveInitialization is true it means that we're
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   370
                // already in the process of initializing the LogManager in
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   371
                // this thread. There has been a recursive call to
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   372
                // ensureLogManagerInitialized(). We should not proceed as
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   373
                // it would lead to infinite recursion.
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   374
                //
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   375
                // If initializationDone is true then it means the manager
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   376
                // has finished initializing; just return: we're done.
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   377
                return;
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   378
            }
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   379
            // Calling addLogger below will in turn call requiresDefaultLogger()
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   380
            // which will call ensureLogManagerInitialized().
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   381
            // We use initializedCalled to break the recursion.
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   382
            initializedCalled = true;
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   383
            try {
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   384
                AccessController.doPrivileged(new PrivilegedAction<Object>() {
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   385
                    @Override
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   386
                    public Object run() {
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   387
                        assert rootLogger == null;
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   388
                        assert initializedCalled && !initializationDone;
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   389
39634
812020bcb9f1 8159245: Loggers created by system classes are not initialized correctly when configured programmatically from application code.
dfuchs
parents: 38370
diff changeset
   390
                        // create root logger before reading primordial
812020bcb9f1 8159245: Loggers created by system classes are not initialized correctly when configured programmatically from application code.
dfuchs
parents: 38370
diff changeset
   391
                        // configuration - to ensure that it will be added
812020bcb9f1 8159245: Loggers created by system classes are not initialized correctly when configured programmatically from application code.
dfuchs
parents: 38370
diff changeset
   392
                        // before the global logger, and not after.
812020bcb9f1 8159245: Loggers created by system classes are not initialized correctly when configured programmatically from application code.
dfuchs
parents: 38370
diff changeset
   393
                        owner.rootLogger = owner.new RootLogger();
812020bcb9f1 8159245: Loggers created by system classes are not initialized correctly when configured programmatically from application code.
dfuchs
parents: 38370
diff changeset
   394
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   395
                        // Read configuration.
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   396
                        owner.readPrimordialConfiguration();
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
                        // 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
   399
                        owner.addLogger(owner.rootLogger);
22086
866b0a7d0127 8030850: Setting .level=FINEST in logging configuration file doesn't work
dfuchs
parents: 22078
diff changeset
   400
                        if (!owner.rootLogger.isLevelInitialized()) {
866b0a7d0127 8030850: Setting .level=FINEST in logging configuration file doesn't work
dfuchs
parents: 22078
diff changeset
   401
                            owner.rootLogger.setLevel(defaultLevel);
866b0a7d0127 8030850: Setting .level=FINEST in logging configuration file doesn't work
dfuchs
parents: 22078
diff changeset
   402
                        }
19825
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
                        // Adding the global Logger.
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   405
                        // 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
   406
                        // subtle inter-dependency issues.
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   407
                        @SuppressWarnings("deprecation")
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   408
                        final Logger global = Logger.global;
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
                        // 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
   411
                        // global manager
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   412
                        owner.addLogger(global);
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   413
                        return null;
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   414
                    }
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
            } finally {
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   417
                initializationDone = true;
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   418
            }
32035
073a449c007d 8132550: java/util/logging/LoggingDeadlock2.java times out
dfuchs
parents: 31150
diff changeset
   419
        } finally {
073a449c007d 8132550: java/util/logging/LoggingDeadlock2.java times out
dfuchs
parents: 31150
diff changeset
   420
            configurationLock.unlock();
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   421
        }
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   422
    }
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   423
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   424
    /**
18565
5025d43d3731 8019315: Fix doclint issues in java.util.logging
darcy
parents: 18269
diff changeset
   425
     * Returns the global LogManager object.
5025d43d3731 8019315: Fix doclint issues in java.util.logging
darcy
parents: 18269
diff changeset
   426
     * @return the global LogManager object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    public static LogManager getLogManager() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        if (manager != null) {
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   430
            manager.ensureLogManagerInitialized();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        return manager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
32035
073a449c007d 8132550: java/util/logging/LoggingDeadlock2.java times out
dfuchs
parents: 31150
diff changeset
   435
    private void readPrimordialConfiguration() { // must be called while holding configurationLock
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        if (!readPrimordialConfiguration) {
32035
073a449c007d 8132550: java/util/logging/LoggingDeadlock2.java times out
dfuchs
parents: 31150
diff changeset
   437
            // 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
   438
            // indication that we're still in the
073a449c007d 8132550: java/util/logging/LoggingDeadlock2.java times out
dfuchs
parents: 31150
diff changeset
   439
            // bootstrapping phase
073a449c007d 8132550: java/util/logging/LoggingDeadlock2.java times out
dfuchs
parents: 31150
diff changeset
   440
            if (System.out == null) {
073a449c007d 8132550: java/util/logging/LoggingDeadlock2.java times out
dfuchs
parents: 31150
diff changeset
   441
                return;
073a449c007d 8132550: java/util/logging/LoggingDeadlock2.java times out
dfuchs
parents: 31150
diff changeset
   442
            }
073a449c007d 8132550: java/util/logging/LoggingDeadlock2.java times out
dfuchs
parents: 31150
diff changeset
   443
            readPrimordialConfiguration = true;
073a449c007d 8132550: java/util/logging/LoggingDeadlock2.java times out
dfuchs
parents: 31150
diff changeset
   444
            try {
073a449c007d 8132550: java/util/logging/LoggingDeadlock2.java times out
dfuchs
parents: 31150
diff changeset
   445
                readConfiguration();
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   446
32035
073a449c007d 8132550: java/util/logging/LoggingDeadlock2.java times out
dfuchs
parents: 31150
diff changeset
   447
                // Platform loggers begin to delegate to java.util.logging.Logger
33875
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents: 32984
diff changeset
   448
                jdk.internal.logger.BootstrapLogger.redirectTemporaryLoggers();
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents: 32984
diff changeset
   449
32035
073a449c007d 8132550: java/util/logging/LoggingDeadlock2.java times out
dfuchs
parents: 31150
diff changeset
   450
            } catch (Exception ex) {
073a449c007d 8132550: java/util/logging/LoggingDeadlock2.java times out
dfuchs
parents: 31150
diff changeset
   451
                assert false : "Exception raised while reading logging configuration: " + ex;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
19809
dc0480c1b36c 8019853: Break logging and AWT circular dependency
dfuchs
parents: 18595
diff changeset
   456
    // LoggerContext maps from AppContext
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   457
    private WeakHashMap<Object, LoggerContext> contextsMap = null;
19809
dc0480c1b36c 8019853: Break logging and AWT circular dependency
dfuchs
parents: 18595
diff changeset
   458
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   459
    // Returns the LoggerContext for the user code (i.e. application or AppContext).
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   460
    // Loggers are isolated from each AppContext.
16105
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   461
    private LoggerContext getUserContext() {
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   462
        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
   463
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   464
        SecurityManager sm = System.getSecurityManager();
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   465
        JavaAWTAccess javaAwtAccess = SharedSecrets.getJavaAWTAccess();
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   466
        if (sm != null && javaAwtAccess != null) {
19809
dc0480c1b36c 8019853: Break logging and AWT circular dependency
dfuchs
parents: 18595
diff changeset
   467
            // 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
   468
            final Object ecx = javaAwtAccess.getAppletContext();
654c0f23c1c0 8065991: LogManager unecessarily calls JavaAWTAccess from within a critical section
dfuchs
parents: 27799
diff changeset
   469
            if (ecx != null) {
654c0f23c1c0 8065991: LogManager unecessarily calls JavaAWTAccess from within a critical section
dfuchs
parents: 27799
diff changeset
   470
                synchronized (javaAwtAccess) {
654c0f23c1c0 8065991: LogManager unecessarily calls JavaAWTAccess from within a critical section
dfuchs
parents: 27799
diff changeset
   471
                    // find the AppContext of the applet code
654c0f23c1c0 8065991: LogManager unecessarily calls JavaAWTAccess from within a critical section
dfuchs
parents: 27799
diff changeset
   472
                    // 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
   473
                    if (contextsMap == null) {
dc0480c1b36c 8019853: Break logging and AWT circular dependency
dfuchs
parents: 18595
diff changeset
   474
                        contextsMap = new WeakHashMap<>();
dc0480c1b36c 8019853: Break logging and AWT circular dependency
dfuchs
parents: 18595
diff changeset
   475
                    }
dc0480c1b36c 8019853: Break logging and AWT circular dependency
dfuchs
parents: 18595
diff changeset
   476
                    context = contextsMap.get(ecx);
18202
e9a6d59491ab 8004584: Augment applet contextualization
leonidr
parents: 16117
diff changeset
   477
                    if (context == null) {
19809
dc0480c1b36c 8019853: Break logging and AWT circular dependency
dfuchs
parents: 18595
diff changeset
   478
                        // Create a new LoggerContext for the applet.
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   479
                        context = new LoggerContext();
19809
dc0480c1b36c 8019853: Break logging and AWT circular dependency
dfuchs
parents: 18595
diff changeset
   480
                        contextsMap.put(ecx, context);
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   481
                    }
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   482
                }
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   483
            }
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   484
        }
19809
dc0480c1b36c 8019853: Break logging and AWT circular dependency
dfuchs
parents: 18595
diff changeset
   485
        // for standalone app, return userContext
18202
e9a6d59491ab 8004584: Augment applet contextualization
leonidr
parents: 16117
diff changeset
   486
        return context != null ? context : userContext;
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   487
    }
9700
fdbacf68f185 7016208: 4/3 null sometimes returned by java.util.logging.Logger.getLogger(String name) in -server -Xcomp
dcubed
parents: 9013
diff changeset
   488
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   489
    // The system context.
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   490
    final LoggerContext getSystemContext() {
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   491
        return systemContext;
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   492
    }
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   493
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   494
    private List<LoggerContext> contexts() {
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   495
        List<LoggerContext> cxs = new ArrayList<>();
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   496
        cxs.add(getSystemContext());
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   497
        cxs.add(getUserContext());
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   498
        return cxs;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
16105
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   501
    // 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
   502
    // 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
   503
    // 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
   504
    // in the LogManager global namespace.
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   505
    // 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
   506
    // Synchronization is not required here. All synchronization for
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   507
    // 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
   508
    //
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   509
    // 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
   510
    // 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
   511
    // 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
   512
    // readConfiguration, and other methods.
17487
77566e5979d1 8013380: Removal of stack walk to find resource bundle breaks Glassfish startup
jgish
parents: 17164
diff changeset
   513
    Logger demandLogger(String name, String resourceBundleName, Class<?> caller) {
37672
03684934dc09 8148568: LoggerFinder.getLogger and LoggerFinder.getLocalizedLogger should take a Module argument instead of a Class.
dfuchs
parents: 37520
diff changeset
   514
        final Module module = caller == null ? null : caller.getModule();
03684934dc09 8148568: LoggerFinder.getLogger and LoggerFinder.getLocalizedLogger should take a Module argument instead of a Class.
dfuchs
parents: 37520
diff changeset
   515
        return demandLogger(name, resourceBundleName, module);
03684934dc09 8148568: LoggerFinder.getLogger and LoggerFinder.getLocalizedLogger should take a Module argument instead of a Class.
dfuchs
parents: 37520
diff changeset
   516
    }
03684934dc09 8148568: LoggerFinder.getLogger and LoggerFinder.getLocalizedLogger should take a Module argument instead of a Class.
dfuchs
parents: 37520
diff changeset
   517
03684934dc09 8148568: LoggerFinder.getLogger and LoggerFinder.getLocalizedLogger should take a Module argument instead of a Class.
dfuchs
parents: 37520
diff changeset
   518
    Logger demandLogger(String name, String resourceBundleName, Module module) {
16105
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   519
        Logger result = getLogger(name);
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   520
        if (result == null) {
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   521
            // only allocate the new logger once
37672
03684934dc09 8148568: LoggerFinder.getLogger and LoggerFinder.getLocalizedLogger should take a Module argument instead of a Class.
dfuchs
parents: 37520
diff changeset
   522
            Logger newLogger = new Logger(name, resourceBundleName,
39634
812020bcb9f1 8159245: Loggers created by system classes are not initialized correctly when configured programmatically from application code.
dfuchs
parents: 38370
diff changeset
   523
                                          module, this, false);
16105
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   524
            do {
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   525
                if (addLogger(newLogger)) {
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   526
                    // We successfully added the new Logger that we
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   527
                    // created above so return it without refetching.
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   528
                    return newLogger;
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   529
                }
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   530
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   531
                // 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
   532
                // 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
   533
                // 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
   534
                // 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
   535
                // addLogger() returns a boolean instead of the Logger
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   536
                // reference itself. However, if the thread that created
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   537
                // 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
   538
                // 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
   539
                // 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
   540
                // 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
   541
                // we'll just loop around and try again.
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   542
                result = getLogger(name);
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   543
            } while (result == null);
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   544
        }
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   545
        return result;
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   546
    }
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   547
31150
2aa1d300cd75 8080933: LogManager.demandSystemLogger should accept a 'caller' argument.
dfuchs
parents: 30643
diff changeset
   548
    Logger demandSystemLogger(String name, String resourceBundleName, Class<?> caller) {
37672
03684934dc09 8148568: LoggerFinder.getLogger and LoggerFinder.getLocalizedLogger should take a Module argument instead of a Class.
dfuchs
parents: 37520
diff changeset
   549
        final Module module = caller == null ? null : caller.getModule();
03684934dc09 8148568: LoggerFinder.getLogger and LoggerFinder.getLocalizedLogger should take a Module argument instead of a Class.
dfuchs
parents: 37520
diff changeset
   550
        return demandSystemLogger(name, resourceBundleName, module);
03684934dc09 8148568: LoggerFinder.getLogger and LoggerFinder.getLocalizedLogger should take a Module argument instead of a Class.
dfuchs
parents: 37520
diff changeset
   551
    }
03684934dc09 8148568: LoggerFinder.getLogger and LoggerFinder.getLocalizedLogger should take a Module argument instead of a Class.
dfuchs
parents: 37520
diff changeset
   552
03684934dc09 8148568: LoggerFinder.getLogger and LoggerFinder.getLocalizedLogger should take a Module argument instead of a Class.
dfuchs
parents: 37520
diff changeset
   553
    Logger demandSystemLogger(String name, String resourceBundleName, Module module) {
16111
5c5fc1d5bb38 8007393: Possible race condition after JDK-6664509
mchung
parents: 16105
diff changeset
   554
        // 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
   555
        final Logger sysLogger = getSystemContext()
37672
03684934dc09 8148568: LoggerFinder.getLogger and LoggerFinder.getLocalizedLogger should take a Module argument instead of a Class.
dfuchs
parents: 37520
diff changeset
   556
                .demandLogger(name, resourceBundleName, module);
16111
5c5fc1d5bb38 8007393: Possible race condition after JDK-6664509
mchung
parents: 16105
diff changeset
   557
5c5fc1d5bb38 8007393: Possible race condition after JDK-6664509
mchung
parents: 16105
diff changeset
   558
        // 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
   559
        // 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
   560
        // System loggers are visible to applications unless a logger of
5c5fc1d5bb38 8007393: Possible race condition after JDK-6664509
mchung
parents: 16105
diff changeset
   561
        // the same name has been added.
5c5fc1d5bb38 8007393: Possible race condition after JDK-6664509
mchung
parents: 16105
diff changeset
   562
        Logger logger;
5c5fc1d5bb38 8007393: Possible race condition after JDK-6664509
mchung
parents: 16105
diff changeset
   563
        do {
5c5fc1d5bb38 8007393: Possible race condition after JDK-6664509
mchung
parents: 16105
diff changeset
   564
            // First attempt to call addLogger instead of getLogger
5c5fc1d5bb38 8007393: Possible race condition after JDK-6664509
mchung
parents: 16105
diff changeset
   565
            // This would avoid potential bug in custom LogManager.getLogger
5c5fc1d5bb38 8007393: Possible race condition after JDK-6664509
mchung
parents: 16105
diff changeset
   566
            // implementation that adds a logger if does not exist
5c5fc1d5bb38 8007393: Possible race condition after JDK-6664509
mchung
parents: 16105
diff changeset
   567
            if (addLogger(sysLogger)) {
5c5fc1d5bb38 8007393: Possible race condition after JDK-6664509
mchung
parents: 16105
diff changeset
   568
                // successfully added the new system logger
5c5fc1d5bb38 8007393: Possible race condition after JDK-6664509
mchung
parents: 16105
diff changeset
   569
                logger = sysLogger;
5c5fc1d5bb38 8007393: Possible race condition after JDK-6664509
mchung
parents: 16105
diff changeset
   570
            } else {
5c5fc1d5bb38 8007393: Possible race condition after JDK-6664509
mchung
parents: 16105
diff changeset
   571
                logger = getLogger(name);
5c5fc1d5bb38 8007393: Possible race condition after JDK-6664509
mchung
parents: 16105
diff changeset
   572
            }
5c5fc1d5bb38 8007393: Possible race condition after JDK-6664509
mchung
parents: 16105
diff changeset
   573
        } while (logger == null);
5c5fc1d5bb38 8007393: Possible race condition after JDK-6664509
mchung
parents: 16105
diff changeset
   574
5c5fc1d5bb38 8007393: Possible race condition after JDK-6664509
mchung
parents: 16105
diff changeset
   575
        // LogManager will set the sysLogger's handlers via LogManager.addLogger method.
39634
812020bcb9f1 8159245: Loggers created by system classes are not initialized correctly when configured programmatically from application code.
dfuchs
parents: 38370
diff changeset
   576
        if (logger != sysLogger) {
812020bcb9f1 8159245: Loggers created by system classes are not initialized correctly when configured programmatically from application code.
dfuchs
parents: 38370
diff changeset
   577
            // if logger already exists we merge the two logger configurations.
16111
5c5fc1d5bb38 8007393: Possible race condition after JDK-6664509
mchung
parents: 16105
diff changeset
   578
            final Logger l = logger;
5c5fc1d5bb38 8007393: Possible race condition after JDK-6664509
mchung
parents: 16105
diff changeset
   579
            AccessController.doPrivileged(new PrivilegedAction<Void>() {
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   580
                @Override
16111
5c5fc1d5bb38 8007393: Possible race condition after JDK-6664509
mchung
parents: 16105
diff changeset
   581
                public Void run() {
39634
812020bcb9f1 8159245: Loggers created by system classes are not initialized correctly when configured programmatically from application code.
dfuchs
parents: 38370
diff changeset
   582
                    l.mergeWithSystemLogger(sysLogger);
16111
5c5fc1d5bb38 8007393: Possible race condition after JDK-6664509
mchung
parents: 16105
diff changeset
   583
                    return null;
5c5fc1d5bb38 8007393: Possible race condition after JDK-6664509
mchung
parents: 16105
diff changeset
   584
                }
5c5fc1d5bb38 8007393: Possible race condition after JDK-6664509
mchung
parents: 16105
diff changeset
   585
            });
5c5fc1d5bb38 8007393: Possible race condition after JDK-6664509
mchung
parents: 16105
diff changeset
   586
        }
5c5fc1d5bb38 8007393: Possible race condition after JDK-6664509
mchung
parents: 16105
diff changeset
   587
        return sysLogger;
16105
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   588
    }
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   589
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   590
    // LoggerContext maintains the logger namespace per context.
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   591
    // 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
   592
    // 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
   593
    // 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
   594
    // 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
   595
    // 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
   596
    // added in the user context.
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   597
    class LoggerContext {
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   598
        // 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
   599
        private final ConcurrentHashMap<String,LoggerWeakRef> namedLoggers =
d9f64fdd3c97 7113878: LogManager - namedLoggers should be ConcurrentHashMap instead of Hashtable
dfuchs
parents: 29739
diff changeset
   600
                new ConcurrentHashMap<>();
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   601
        // Tree of named Loggers
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   602
        private final LogNode root;
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   603
        private LoggerContext() {
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   604
            this.root = new LogNode(null, 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
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   608
        // Tells whether default loggers are required in this context.
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   609
        // If true, the default loggers will be lazily added.
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   610
        final boolean requiresDefaultLoggers() {
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   611
            final boolean requiresDefaultLoggers = (getOwner() == manager);
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   612
            if (requiresDefaultLoggers) {
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   613
                getOwner().ensureLogManagerInitialized();
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   614
            }
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   615
            return requiresDefaultLoggers;
18593
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   616
        }
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   617
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   618
        // This context's LogManager.
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   619
        final LogManager getOwner() {
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   620
            return LogManager.this;
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   621
        }
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   622
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   623
        // 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
   624
        // 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
   625
        // logger's tree.
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   626
        final Logger getRootLogger() {
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   627
            return getOwner().rootLogger;
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   628
        }
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   629
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   630
        // The global logger, which if not null, and if
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   631
        // 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
   632
        // logger's tree.
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   633
        final Logger getGlobalLogger() {
23342
d7a41da68b85 8036916: Deprecation warnings in LogManager
dfuchs
parents: 22342
diff changeset
   634
            @SuppressWarnings("deprecation") // avoids initialization cycles.
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   635
            final Logger global = Logger.global;
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   636
            return global;
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   637
        }
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   638
37672
03684934dc09 8148568: LoggerFinder.getLogger and LoggerFinder.getLocalizedLogger should take a Module argument instead of a Class.
dfuchs
parents: 37520
diff changeset
   639
        Logger demandLogger(String name, String resourceBundleName, Module module) {
16105
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   640
            // 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
   641
            // 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
   642
            final LogManager owner = getOwner();
37672
03684934dc09 8148568: LoggerFinder.getLogger and LoggerFinder.getLocalizedLogger should take a Module argument instead of a Class.
dfuchs
parents: 37520
diff changeset
   643
            return owner.demandLogger(name, resourceBundleName, module);
16105
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   644
        }
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   645
18593
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   646
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   647
        // Due to subtle deadlock issues getUserContext() no longer
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   648
        // calls addLocalLogger(rootLogger);
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   649
        // 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
   650
        // Checks that the context is properly initialized
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   651
        // 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
   652
        // or getLoggerNames()
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   653
        //
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   654
        private void ensureInitialized() {
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   655
            if (requiresDefaultLoggers()) {
18593
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   656
                // 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
   657
                ensureDefaultLogger(getRootLogger());
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   658
                ensureDefaultLogger(getGlobalLogger());
18593
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   659
            }
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   660
        }
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   661
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   662
29740
d9f64fdd3c97 7113878: LogManager - namedLoggers should be ConcurrentHashMap instead of Hashtable
dfuchs
parents: 29739
diff changeset
   663
        Logger findLogger(String name) {
d9f64fdd3c97 7113878: LogManager - namedLoggers should be ConcurrentHashMap instead of Hashtable
dfuchs
parents: 29739
diff changeset
   664
            // Attempt to find logger without locking.
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   665
            LoggerWeakRef ref = namedLoggers.get(name);
29740
d9f64fdd3c97 7113878: LogManager - namedLoggers should be ConcurrentHashMap instead of Hashtable
dfuchs
parents: 29739
diff changeset
   666
            Logger logger = ref == null ? null : ref.get();
d9f64fdd3c97 7113878: LogManager - namedLoggers should be ConcurrentHashMap instead of Hashtable
dfuchs
parents: 29739
diff changeset
   667
d9f64fdd3c97 7113878: LogManager - namedLoggers should be ConcurrentHashMap instead of Hashtable
dfuchs
parents: 29739
diff changeset
   668
            // 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
   669
            // if name is "" or "global" and logger is null
d9f64fdd3c97 7113878: LogManager - namedLoggers should be ConcurrentHashMap instead of Hashtable
dfuchs
parents: 29739
diff changeset
   670
            // 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
   671
            // initialized.
d9f64fdd3c97 7113878: LogManager - namedLoggers should be ConcurrentHashMap instead of Hashtable
dfuchs
parents: 29739
diff changeset
   672
            // 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
   673
            // fall through.
d9f64fdd3c97 7113878: LogManager - namedLoggers should be ConcurrentHashMap instead of Hashtable
dfuchs
parents: 29739
diff changeset
   674
            if (logger != null || (ref == null && !name.isEmpty()
d9f64fdd3c97 7113878: LogManager - namedLoggers should be ConcurrentHashMap instead of Hashtable
dfuchs
parents: 29739
diff changeset
   675
                    && !name.equals(Logger.GLOBAL_LOGGER_NAME))) {
d9f64fdd3c97 7113878: LogManager - namedLoggers should be ConcurrentHashMap instead of Hashtable
dfuchs
parents: 29739
diff changeset
   676
                return logger;
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   677
            }
29740
d9f64fdd3c97 7113878: LogManager - namedLoggers should be ConcurrentHashMap instead of Hashtable
dfuchs
parents: 29739
diff changeset
   678
d9f64fdd3c97 7113878: LogManager - namedLoggers should be ConcurrentHashMap instead of Hashtable
dfuchs
parents: 29739
diff changeset
   679
            // 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
   680
            // "" or "global" and didn't find them.
d9f64fdd3c97 7113878: LogManager - namedLoggers should be ConcurrentHashMap instead of Hashtable
dfuchs
parents: 29739
diff changeset
   681
            // Make sure context is initialized (has the default loggers),
d9f64fdd3c97 7113878: LogManager - namedLoggers should be ConcurrentHashMap instead of Hashtable
dfuchs
parents: 29739
diff changeset
   682
            // 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
   683
            // 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
   684
            // a synchronized block.
d9f64fdd3c97 7113878: LogManager - namedLoggers should be ConcurrentHashMap instead of Hashtable
dfuchs
parents: 29739
diff changeset
   685
            synchronized(this) {
d9f64fdd3c97 7113878: LogManager - namedLoggers should be ConcurrentHashMap instead of Hashtable
dfuchs
parents: 29739
diff changeset
   686
                // ensure that this context is properly initialized before
d9f64fdd3c97 7113878: LogManager - namedLoggers should be ConcurrentHashMap instead of Hashtable
dfuchs
parents: 29739
diff changeset
   687
                // looking for loggers.
d9f64fdd3c97 7113878: LogManager - namedLoggers should be ConcurrentHashMap instead of Hashtable
dfuchs
parents: 29739
diff changeset
   688
                ensureInitialized();
d9f64fdd3c97 7113878: LogManager - namedLoggers should be ConcurrentHashMap instead of Hashtable
dfuchs
parents: 29739
diff changeset
   689
                ref = namedLoggers.get(name);
d9f64fdd3c97 7113878: LogManager - namedLoggers should be ConcurrentHashMap instead of Hashtable
dfuchs
parents: 29739
diff changeset
   690
                if (ref == null) {
d9f64fdd3c97 7113878: LogManager - namedLoggers should be ConcurrentHashMap instead of Hashtable
dfuchs
parents: 29739
diff changeset
   691
                    return null;
d9f64fdd3c97 7113878: LogManager - namedLoggers should be ConcurrentHashMap instead of Hashtable
dfuchs
parents: 29739
diff changeset
   692
                }
d9f64fdd3c97 7113878: LogManager - namedLoggers should be ConcurrentHashMap instead of Hashtable
dfuchs
parents: 29739
diff changeset
   693
                logger = ref.get();
d9f64fdd3c97 7113878: LogManager - namedLoggers should be ConcurrentHashMap instead of Hashtable
dfuchs
parents: 29739
diff changeset
   694
                if (logger == null) {
d9f64fdd3c97 7113878: LogManager - namedLoggers should be ConcurrentHashMap instead of Hashtable
dfuchs
parents: 29739
diff changeset
   695
                    // The namedLoggers map holds stale weak reference
d9f64fdd3c97 7113878: LogManager - namedLoggers should be ConcurrentHashMap instead of Hashtable
dfuchs
parents: 29739
diff changeset
   696
                    // to a logger which has been GC-ed.
d9f64fdd3c97 7113878: LogManager - namedLoggers should be ConcurrentHashMap instead of Hashtable
dfuchs
parents: 29739
diff changeset
   697
                    ref.dispose();
d9f64fdd3c97 7113878: LogManager - namedLoggers should be ConcurrentHashMap instead of Hashtable
dfuchs
parents: 29739
diff changeset
   698
                }
d9f64fdd3c97 7113878: LogManager - namedLoggers should be ConcurrentHashMap instead of Hashtable
dfuchs
parents: 29739
diff changeset
   699
                return logger;
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   700
            }
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   701
        }
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   702
18593
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   703
        // 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
   704
        // context.
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   705
        // 'logger' is the context that will be added.
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   706
        // 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
   707
        // before adding 'logger'.
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   708
        //
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   709
        private void ensureAllDefaultLoggers(Logger logger) {
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   710
            if (requiresDefaultLoggers()) {
18593
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   711
                final String name = logger.getName();
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   712
                if (!name.isEmpty()) {
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   713
                    ensureDefaultLogger(getRootLogger());
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   714
                    if (!Logger.GLOBAL_LOGGER_NAME.equals(name)) {
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   715
                        ensureDefaultLogger(getGlobalLogger());
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   716
                    }
18593
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   717
                }
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   718
            }
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   719
        }
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   720
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   721
        private void ensureDefaultLogger(Logger logger) {
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   722
            // 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
   723
            // to a LoggerContext.
18202
e9a6d59491ab 8004584: Augment applet contextualization
leonidr
parents: 16117
diff changeset
   724
18593
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   725
            // 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
   726
            // 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
   727
            // or owner.rootLogger.
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   728
            if (!requiresDefaultLoggers() || logger == null
23342
d7a41da68b85 8036916: Deprecation warnings in LogManager
dfuchs
parents: 22342
diff changeset
   729
                    || logger != getGlobalLogger() && logger != LogManager.this.rootLogger ) {
18593
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   730
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   731
                // 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
   732
                // Logger.global nor manager.rootLogger indicates a serious
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   733
                // issue - as ensureDefaultLogger should never be called
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   734
                // 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
   735
                // e.g manager.rootLogger is not yet initialized)...
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   736
                assert logger == null;
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   737
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   738
                return;
18202
e9a6d59491ab 8004584: Augment applet contextualization
leonidr
parents: 16117
diff changeset
   739
            }
18593
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   740
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   741
            // 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
   742
            if (!namedLoggers.containsKey(logger.getName())) {
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   743
                // It is important to prevent addLocalLogger to
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   744
                // call ensureAllDefaultLoggers when we're in the process
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   745
                // 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
   746
                // immediately cause a stack overflow.
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   747
                // Therefore we must pass addDefaultLoggersIfNeeded=false,
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   748
                // even if requiresDefaultLoggers is true.
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   749
                addLocalLogger(logger, false);
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
        }
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   752
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   753
        boolean addLocalLogger(Logger logger) {
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   754
            // 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
   755
            return addLocalLogger(logger, requiresDefaultLoggers());
18202
e9a6d59491ab 8004584: Augment applet contextualization
leonidr
parents: 16117
diff changeset
   756
        }
e9a6d59491ab 8004584: Augment applet contextualization
leonidr
parents: 16117
diff changeset
   757
16105
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   758
        // 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
   759
        // 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
   760
        synchronized boolean addLocalLogger(Logger logger, boolean addDefaultLoggersIfNeeded) {
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   761
            // addDefaultLoggersIfNeeded serves to break recursion when adding
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   762
            // 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
   763
            // (we're being called from ensureDefaultLogger()) then
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   764
            // 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
   765
            // call ensureAllDefaultLoggers again.
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   766
            //
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   767
            // Note: addDefaultLoggersIfNeeded can also be false when
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   768
            //       requiresDefaultLoggers is false - since calling
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   769
            //       ensureAllDefaultLoggers would have no effect in this case.
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   770
            if (addDefaultLoggersIfNeeded) {
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   771
                ensureAllDefaultLoggers(logger);
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   772
            }
18202
e9a6d59491ab 8004584: Augment applet contextualization
leonidr
parents: 16117
diff changeset
   773
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   774
            final String name = logger.getName();
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   775
            if (name == null) {
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   776
                throw new NullPointerException();
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   777
            }
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   778
            LoggerWeakRef ref = namedLoggers.get(name);
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   779
            if (ref != null) {
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   780
                if (ref.get() == null) {
17164
1304199c8641 8010939: Deadlock in LogManager
jgish
parents: 16117
diff changeset
   781
                    // It's possible that the Logger was GC'ed after a
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   782
                    // drainLoggerRefQueueBounded() call above so allow
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   783
                    // a new one to be registered.
21960
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
   784
                    ref.dispose();
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   785
                } else {
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   786
                    // We already have a registered logger with the given name.
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   787
                    return false;
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   788
                }
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   789
            }
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   790
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   791
            // We're adding a new logger.
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   792
            // 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
   793
            final LogManager owner = getOwner();
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   794
            logger.setLogManager(owner);
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   795
            ref = owner.new LoggerWeakRef(logger);
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   796
21304
7971ecf0fbed 8026499: Root Logger level can be reset unexpectedly
dfuchs
parents: 19825
diff changeset
   797
            // Apply any initial level defined for the new logger, unless
7971ecf0fbed 8026499: Root Logger level can be reset unexpectedly
dfuchs
parents: 19825
diff changeset
   798
            // the logger's level is already initialized
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   799
            Level level = owner.getLevelProperty(name + ".level", null);
21304
7971ecf0fbed 8026499: Root Logger level can be reset unexpectedly
dfuchs
parents: 19825
diff changeset
   800
            if (level != null && !logger.isLevelInitialized()) {
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   801
                doSetLevel(logger, level);
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   802
            }
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   803
16105
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   804
            // 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
   805
            // 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
   806
            // subclass for the custom log manager case
32984
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
   807
            processParentHandlers(logger, name, VisitedLoggers.NEVER);
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   808
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   809
            // Find the new node and its parent.
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   810
            LogNode node = getNode(name);
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   811
            node.loggerRef = ref;
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   812
            Logger parent = null;
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   813
            LogNode nodep = node.parent;
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   814
            while (nodep != null) {
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   815
                LoggerWeakRef nodeRef = nodep.loggerRef;
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   816
                if (nodeRef != null) {
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   817
                    parent = nodeRef.get();
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   818
                    if (parent != null) {
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   819
                        break;
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   820
                    }
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   821
                }
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   822
                nodep = nodep.parent;
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   823
            }
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   824
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   825
            if (parent != null) {
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   826
                doSetParent(logger, parent);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
            }
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   828
            // Walk over the children and tell them we are their new parent.
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   829
            node.walkAndSetParent(logger);
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   830
            // new LogNode is ready so tell the LoggerWeakRef about it
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   831
            ref.setNode(node);
29740
d9f64fdd3c97 7113878: LogManager - namedLoggers should be ConcurrentHashMap instead of Hashtable
dfuchs
parents: 29739
diff changeset
   832
d9f64fdd3c97 7113878: LogManager - namedLoggers should be ConcurrentHashMap instead of Hashtable
dfuchs
parents: 29739
diff changeset
   833
            // 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
   834
            // 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
   835
            // soon as it is published in namedLoggers (findLogger takes
d9f64fdd3c97 7113878: LogManager - namedLoggers should be ConcurrentHashMap instead of Hashtable
dfuchs
parents: 29739
diff changeset
   836
            // benefit of the ConcurrentHashMap implementation of namedLoggers
d9f64fdd3c97 7113878: LogManager - namedLoggers should be ConcurrentHashMap instead of Hashtable
dfuchs
parents: 29739
diff changeset
   837
            // to avoid synchronizing on retrieval when that is possible).
d9f64fdd3c97 7113878: LogManager - namedLoggers should be ConcurrentHashMap instead of Hashtable
dfuchs
parents: 29739
diff changeset
   838
            namedLoggers.put(name, ref);
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   839
            return true;
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   840
        }
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   841
29740
d9f64fdd3c97 7113878: LogManager - namedLoggers should be ConcurrentHashMap instead of Hashtable
dfuchs
parents: 29739
diff changeset
   842
        void removeLoggerRef(String name, LoggerWeakRef ref) {
21960
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
   843
            namedLoggers.remove(name, ref);
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   844
        }
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   845
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   846
        synchronized Enumeration<String> getLoggerNames() {
18593
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   847
            // ensure that this context is properly initialized before
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   848
            // returning logger names.
40715ebb6681 8017174: NPE when using Logger.getAnonymousLogger or LogManager.getLogManager().getLogger
dfuchs
parents: 18565
diff changeset
   849
            ensureInitialized();
29740
d9f64fdd3c97 7113878: LogManager - namedLoggers should be ConcurrentHashMap instead of Hashtable
dfuchs
parents: 29739
diff changeset
   850
            return Collections.enumeration(namedLoggers.keySet());
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   851
        }
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   852
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   853
        // If logger.getUseParentHandlers() returns 'true' and any of the logger's
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   854
        // parents have levels or handlers defined, make sure they are instantiated.
32984
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
   855
        private void processParentHandlers(final Logger logger, final String name,
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
   856
               Predicate<Logger> visited) {
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   857
            final LogManager owner = getOwner();
16105
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   858
            AccessController.doPrivileged(new PrivilegedAction<Void>() {
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   859
                @Override
16105
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   860
                public Void run() {
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   861
                    if (logger != owner.rootLogger) {
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   862
                        boolean useParent = owner.getBooleanProperty(name + ".useParentHandlers", true);
16105
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   863
                        if (!useParent) {
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   864
                            logger.setUseParentHandlers(false);
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   865
                        }
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   866
                    }
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   867
                    return null;
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   868
                }
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   869
            });
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   870
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   871
            int ix = 1;
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   872
            for (;;) {
24685
215fa91e1b4c 8044461: Cleanup new Boolean and single character strings
rriggs
parents: 24196
diff changeset
   873
                int ix2 = name.indexOf('.', ix);
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   874
                if (ix2 < 0) {
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   875
                    break;
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   876
                }
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   877
                String pname = name.substring(0, ix2);
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   878
                if (owner.getProperty(pname + ".level") != null ||
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   879
                    owner.getProperty(pname + ".handlers") != null) {
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   880
                    // This pname has a level/handlers definition.
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   881
                    // Make sure it exists.
32984
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
   882
                    if (visited.test(demandLogger(pname, null, null))) {
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
   883
                        break;
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
   884
                    }
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   885
                }
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   886
                ix = ix2+1;
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   887
            }
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   888
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   890
        // Gets a node in our tree of logger nodes.
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   891
        // If necessary, create it.
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   892
        LogNode getNode(String name) {
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   893
            if (name == null || name.equals("")) {
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   894
                return root;
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   895
            }
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   896
            LogNode node = root;
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   897
            while (name.length() > 0) {
24685
215fa91e1b4c 8044461: Cleanup new Boolean and single character strings
rriggs
parents: 24196
diff changeset
   898
                int ix = name.indexOf('.');
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   899
                String head;
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   900
                if (ix > 0) {
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   901
                    head = name.substring(0, ix);
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   902
                    name = name.substring(ix + 1);
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   903
                } else {
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   904
                    head = name;
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   905
                    name = "";
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   906
                }
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   907
                if (node.children == null) {
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   908
                    node.children = new HashMap<>();
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   909
                }
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   910
                LogNode child = node.children.get(head);
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   911
                if (child == null) {
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   912
                    child = new LogNode(node, this);
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   913
                    node.children.put(head, child);
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   914
                }
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   915
                node = child;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
            }
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   917
            return node;
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   918
        }
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   919
    }
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   920
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   921
    final class SystemLoggerContext extends LoggerContext {
16105
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   922
        // 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
   923
        // 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
   924
        // 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
   925
        // 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
   926
        @Override
37672
03684934dc09 8148568: LoggerFinder.getLogger and LoggerFinder.getLocalizedLogger should take a Module argument instead of a Class.
dfuchs
parents: 37520
diff changeset
   927
        Logger demandLogger(String name, String resourceBundleName,
03684934dc09 8148568: LoggerFinder.getLogger and LoggerFinder.getLocalizedLogger should take a Module argument instead of a Class.
dfuchs
parents: 37520
diff changeset
   928
                            Module module) {
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   929
            Logger result = findLogger(name);
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   930
            if (result == null) {
16105
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   931
                // only allocate the new system logger once
37672
03684934dc09 8148568: LoggerFinder.getLogger and LoggerFinder.getLocalizedLogger should take a Module argument instead of a Class.
dfuchs
parents: 37520
diff changeset
   932
                Logger newLogger = new Logger(name, resourceBundleName,
03684934dc09 8148568: LoggerFinder.getLogger and LoggerFinder.getLocalizedLogger should take a Module argument instead of a Class.
dfuchs
parents: 37520
diff changeset
   933
                                              module, getOwner(), true);
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   934
                do {
16105
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   935
                    if (addLocalLogger(newLogger)) {
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   936
                        // We successfully added the new Logger that we
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   937
                        // created above so return it without refetching.
16105
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   938
                        result = newLogger;
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   939
                    } else {
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   940
                        // 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
   941
                        // 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
   942
                        // 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
   943
                        // 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
   944
                        // addLogger() returns a boolean instead of the Logger
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   945
                        // reference itself. However, if the thread that created
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   946
                        // 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
   947
                        // 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
   948
                        // 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
   949
                        // 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
   950
                        // we'll just loop around and try again.
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   951
                        result = findLogger(name);
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   952
                    }
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   953
                } while (result == null);
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   954
            }
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
   955
            return result;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
    // Add new per logger handlers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
    // We need to raise privilege here. All our decisions will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
    // be made based on the logging configuration, which can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
    // only be modified by trusted code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
    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
   964
                                    final String handlersPropertyName)
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   965
    {
32984
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
   966
        AccessController.doPrivileged(new PrivilegedAction<Void>() {
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
   967
            @Override
32984
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
   968
            public Void run() {
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
   969
                setLoggerHandlers(logger, name, handlersPropertyName,
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
   970
                    createLoggerHandlers(name, handlersPropertyName));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
                return null;
16105
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   972
            }
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
   973
        });
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
32984
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
   976
    private void setLoggerHandlers(final Logger logger, final String name,
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
   977
                                   final String handlersPropertyName,
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
   978
                                   List<Handler> handlers)
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
   979
    {
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
   980
        final boolean ensureCloseOnReset = ! handlers.isEmpty()
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
   981
                    && getBooleanProperty(handlersPropertyName + ".ensureCloseOnReset",true);
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
   982
        int count = 0;
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
   983
        for (Handler hdl : handlers) {
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
   984
            logger.addHandler(hdl);
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
   985
            if (++count == 1 && ensureCloseOnReset) {
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
   986
                // add this logger to the closeOnResetLoggers list.
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
   987
                closeOnResetLoggers.addIfAbsent(CloseOnReset.create(logger));
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
   988
            }
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
   989
        }
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
   990
    }
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
   991
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
   992
    private List<Handler> createLoggerHandlers(final String name, final String handlersPropertyName)
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
   993
    {
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
   994
        String names[] = parseClassNames(handlersPropertyName);
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
   995
        List<Handler> handlers = new ArrayList<>(names.length);
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
   996
        for (String type : names) {
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
   997
            try {
37782
ad8fe7507ecc 6850612: Deprecate Class.newInstance since it violates the checked exception language contract
darcy
parents: 37672
diff changeset
   998
                @SuppressWarnings("deprecation")
ad8fe7507ecc 6850612: Deprecate Class.newInstance since it violates the checked exception language contract
darcy
parents: 37672
diff changeset
   999
                Object o = ClassLoader.getSystemClassLoader().loadClass(type).newInstance();
ad8fe7507ecc 6850612: Deprecate Class.newInstance since it violates the checked exception language contract
darcy
parents: 37672
diff changeset
  1000
                Handler hdl = (Handler) o;
32984
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1001
                // Check if there is a property defining the
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1002
                // this handler's level.
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1003
                String levs = getProperty(type + ".level");
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1004
                if (levs != null) {
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1005
                    Level l = Level.findLevel(levs);
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1006
                    if (l != null) {
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1007
                        hdl.setLevel(l);
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1008
                    } else {
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1009
                        // Probably a bad level. Drop through.
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1010
                        System.err.println("Can't set level for " + type);
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1011
                    }
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1012
                }
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1013
                // Add this Handler to the logger
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1014
                handlers.add(hdl);
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1015
            } catch (Exception ex) {
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1016
                System.err.println("Can't load log handler \"" + type + "\"");
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1017
                System.err.println("" + ex);
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1018
                ex.printStackTrace();
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1019
            }
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1020
        }
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1021
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1022
        return handlers;
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1023
    }
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1024
5964
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1025
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1026
    // loggerRefQueue holds LoggerWeakRef objects for Logger objects
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1027
    // that have been GC'ed.
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1028
    private final ReferenceQueue<Logger> loggerRefQueue
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 6675
diff changeset
  1029
        = new ReferenceQueue<>();
5964
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1030
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1031
    // Package-level inner class.
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1032
    // 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
  1033
    //
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1034
    // LogManager.namedLoggers
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1035
    //     - has weak references to all named Loggers
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1036
    //     - 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
  1037
    //       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
  1038
    //       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
  1039
    // LogManager.LogNode.loggerRef
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1040
    //     - 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
  1041
    //     - 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
  1042
    //       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
  1043
    // Logger.kids
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1044
    //     - 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
  1045
    //       includes anonymous and named Loggers
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1046
    //     - 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
  1047
    //       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
  1048
    //       LoggerWeakRef objects for the anonymous Loggers around
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1049
    //       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
  1050
    //
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1051
    final class LoggerWeakRef extends WeakReference<Logger> {
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1052
        private String                name;       // for namedLoggers cleanup
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1053
        private LogNode               node;       // for loggerRef cleanup
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1054
        private WeakReference<Logger> parentRef;  // for kids cleanup
21960
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
  1055
        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
  1056
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1057
        LoggerWeakRef(Logger logger) {
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1058
            super(logger, loggerRefQueue);
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1059
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1060
            name = logger.getName();  // save for namedLoggers cleanup
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1061
        }
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1062
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1063
        // dispose of this LoggerWeakRef object
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1064
        void dispose() {
21960
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
  1065
            // Avoid calling dispose twice. When a Logger is gc'ed, its
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
  1066
            // LoggerWeakRef will be enqueued.
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
  1067
            // 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
  1068
            // up) before the queue is drained. When that happens, dispose()
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
  1069
            // will be called by addLocalLogger() or findLogger().
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
  1070
            // Later when the queue is drained, dispose() will be called again
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
  1071
            // for the same LoggerWeakRef. Marking LoggerWeakRef as disposed
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
  1072
            // avoids processing the data twice (even though the code should
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
  1073
            // now be reentrant).
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
  1074
            synchronized(this) {
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
  1075
                // Note to maintainers:
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
  1076
                // Be careful not to call any method that tries to acquire
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
  1077
                // another lock from within this block - as this would surely
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
  1078
                // lead to deadlocks, given that dispose() can be called by
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
  1079
                // multiple threads, and from within different synchronized
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
  1080
                // methods/blocks.
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
  1081
                if (disposed) return;
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
  1082
                disposed = true;
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
  1083
            }
5964
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1084
21960
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
  1085
            final LogNode n = node;
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
  1086
            if (n != null) {
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
  1087
                // n.loggerRef can only be safely modified from within
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
  1088
                // a lock on LoggerContext. removeLoggerRef is already
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
  1089
                // synchronized on LoggerContext so calling
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
  1090
                // n.context.removeLoggerRef from within this lock is safe.
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
  1091
                synchronized (n.context) {
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
  1092
                    // if we have a LogNode, then we were a named Logger
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
  1093
                    // so clear namedLoggers weak ref to us
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
  1094
                    n.context.removeLoggerRef(name, this);
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
  1095
                    name = null;  // clear our ref to the Logger's name
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
  1096
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
  1097
                    // LogNode may have been reused - so only clear
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
  1098
                    // LogNode.loggerRef if LogNode.loggerRef == this
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
  1099
                    if (n.loggerRef == this) {
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
  1100
                        n.loggerRef = null;  // clear LogNode's weak ref to us
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
  1101
                    }
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
  1102
                    node = null;            // clear our ref to LogNode
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
  1103
                }
5964
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1104
            }
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1105
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1106
            if (parentRef != null) {
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1107
                // 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
  1108
                Logger parent = parentRef.get();
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1109
                if (parent != null) {
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1110
                    // 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
  1111
                    // parent Logger's weak ref to us
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1112
                    parent.removeChildLogger(this);
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1113
                }
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1114
                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
  1115
            }
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
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1118
        // 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
  1119
        void setNode(LogNode node) {
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1120
            this.node = node;
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1121
        }
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1122
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1123
        // 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
  1124
        void setParentRef(WeakReference<Logger> parentRef) {
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1125
            this.parentRef = parentRef;
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1126
        }
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1127
    }
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
    // Package-level method.
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1130
    // 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
  1131
    //
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1132
    // drainLoggerRefQueueBounded() is called by addLogger() below
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1133
    // 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
  1134
    // 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
  1135
    //
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1136
    // 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
  1137
    // 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
  1138
    // 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
  1139
    // 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
  1140
    //   - test duration 1 minute
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1141
    //   - sample size of 125 sets of 400
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1142
    //   - average: 1.99 ms
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1143
    //   - minimum: 0.57 ms
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1144
    //   - maximum: 25.3 ms
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1145
    //
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1146
    // 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
  1147
    // 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
  1148
    // 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
  1149
    //   - test duration 2 minutes
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1150
    //   - sample size of 506 sets of 400
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1151
    //   - average: 0.57 ms
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1152
    //   - minimum: 0.02 ms
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1153
    //   - maximum: 10.9 ms
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1154
    //
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1155
    private final static int MAX_ITERATIONS = 400;
21960
277d5c6b2172 8029281: Synchronization issues in Logger and LogManager
dfuchs
parents: 21304
diff changeset
  1156
    final void drainLoggerRefQueueBounded() {
5964
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1157
        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
  1158
            if (loggerRefQueue == null) {
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1159
                // haven't finished loading LogManager yet
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1160
                break;
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1161
            }
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1162
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1163
            LoggerWeakRef ref = (LoggerWeakRef) loggerRefQueue.poll();
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1164
            if (ref == null) {
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1165
                break;
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1166
            }
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1167
            // 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
  1168
            ref.dispose();
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1169
        }
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1170
    }
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1171
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
     * Add a named logger.  This does nothing and returns false if a logger
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
     * with the same name is already registered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
     * The Logger factory methods call this method to register each
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
     * newly created Logger.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
     * The application should retain its own reference to the Logger
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
     * object to avoid it being garbage collected.  The LogManager
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
     * may only retain a weak reference.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
     * @param   logger the new logger.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
     * @return  true if the argument logger was registered successfully,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
     *          false if a logger of that name already exists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
     * @exception NullPointerException if the logger name is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
     */
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
  1188
    public boolean addLogger(Logger logger) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
        final String name = logger.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
        if (name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
        }
17164
1304199c8641 8010939: Deadlock in LogManager
jgish
parents: 16117
diff changeset
  1193
        drainLoggerRefQueueBounded();
16105
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
  1194
        LoggerContext cx = getUserContext();
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
  1195
        if (cx.addLocalLogger(logger)) {
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
  1196
            // Do we have a per logger handler too?
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
  1197
            // Note: this will add a 200ms penalty
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
  1198
            loadLoggerHandlers(logger, name, name + ".handlers");
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
  1199
            return true;
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
  1200
        } else {
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
  1201
            return false;
2
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
    // Private method to set a level on a logger.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
    // If necessary, we raise privilege before doing the call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
    private static void doSetLevel(final Logger logger, final Level level) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
        if (sm == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
            // There is no security manager, so things are easy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
            logger.setLevel(level);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
        // There is a security manager.  Raise privilege before
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
        // calling setLevel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
        AccessController.doPrivileged(new PrivilegedAction<Object>() {
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
  1217
            @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
            public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
                logger.setLevel(level);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
            }});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
    // Private method to set a parent on a logger.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
    // If necessary, we raise privilege before doing the setParent call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
    private static void doSetParent(final Logger logger, final Logger parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
        if (sm == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
            // There is no security manager, so things are easy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
            logger.setParent(parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
        // There is a security manager.  Raise privilege before
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
        // calling setParent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
        AccessController.doPrivileged(new PrivilegedAction<Object>() {
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
  1236
            @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
            public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
                logger.setParent(parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
            }});
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
     * Method to find a named logger.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
     * Note that since untrusted code may create loggers with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
     * arbitrary names this method should not be relied on to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
     * 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
  1249
     * 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
  1250
     * 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
  1251
     * 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
  1252
     * 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
  1253
     * 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
  1254
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
     * @param name name of the logger
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
     * @return  matching logger or null if none is found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
     */
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
  1258
    public Logger getLogger(String name) {
16105
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
  1259
        return getUserContext().findLogger(name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
     * Get an enumeration of known logger names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
     * Note:  Loggers may be added dynamically as new classes are loaded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
     * 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
  1267
     * 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
  1268
     * 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
  1269
     * 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
  1270
     * 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
  1271
     * 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
  1272
     * 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
  1273
     * 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
  1274
     * 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
  1275
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
     * @return  enumeration of logger name strings
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
     */
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
  1278
    public Enumeration<String> getLoggerNames() {
16105
fe7392acb767 8005615: Java Logger fails to load tomcat logger implementation (JULI)
mchung
parents: 16100
diff changeset
  1279
        return getUserContext().getLoggerNames();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
    /**
32984
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1283
     * Reads and initializes the logging configuration.
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1284
     * <p>
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1285
     * If the "java.util.logging.config.class" system property is set, then the
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1286
     * property value is treated as a class name.  The given class will be
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1287
     * loaded, an object will be instantiated, and that object's constructor
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1288
     * is responsible for reading in the initial configuration.  (That object
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1289
     * may use other system properties to control its configuration.)  The
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1290
     * alternate configuration class can use {@code readConfiguration(InputStream)}
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1291
     * to define properties in the LogManager.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
     * <p>
32984
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1293
     * If "java.util.logging.config.class" system property is <b>not</b> set,
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1294
     * then this method will read the initial configuration from a properties
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1295
     * file and calls the {@link #readConfiguration(InputStream)} method to initialize
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1296
     * the configuration. The "java.util.logging.config.file" system property can be used
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1297
     * to specify the properties file that will be read as the initial configuration;
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1298
     * if not set, then the LogManager default configuration is used.
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1299
     * The default configuration is typically loaded from the
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1300
     * properties file "{@code conf/logging.properties}" in the Java installation
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1301
     * directory.
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1302
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
     * <p>
26859
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1304
     * 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
  1305
     * listener} will be invoked after the properties are read.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
     *
32984
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1307
     * @apiNote This {@code readConfiguration} method should only be used for
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1308
     * initializing the configuration during LogManager initialization or
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1309
     * used with the "java.util.logging.config.class" property.
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1310
     * When this method is called after loggers have been created, and
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1311
     * the "java.util.logging.config.class" system property is not set, all
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1312
     * existing loggers will be {@linkplain #reset() reset}. Then any
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1313
     * existing loggers that have a level property specified in the new
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1314
     * configuration stream will be {@linkplain
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1315
     * Logger#setLevel(java.util.logging.Level) set} to the specified log level.
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1316
     * <p>
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1317
     * To properly update the logging configuration, use the
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1318
     * {@link #updateConfiguration(java.util.function.Function)} or
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1319
     * {@link #updateConfiguration(java.io.InputStream, java.util.function.Function)}
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1320
     * methods instead.
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1321
     *
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1322
     * @throws   SecurityException  if a security manager exists and if
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1323
     *              the caller does not have LoggingPermission("control").
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1324
     * @throws   IOException if there are IO problems reading the configuration.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
    public void readConfiguration() throws IOException, SecurityException {
14216
23714b376286 7169884: LogManager checks do not work correctly for sub-types
alanb
parents: 13578
diff changeset
  1327
        checkPermission();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
        // if a configuration class is specified, load it and use it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
        String cname = System.getProperty("java.util.logging.config.class");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
        if (cname != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
                // Instantiate the named class.  It is its constructor's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
                // responsibility to initialize the logging configuration, by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
                // calling readConfiguration(InputStream) with a suitable stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
                try {
11274
7e7196757acd 7117249: fix warnings in java.util.jar, .logging, .prefs, .zip
smarks
parents: 9700
diff changeset
  1337
                    Class<?> clz = ClassLoader.getSystemClassLoader().loadClass(cname);
37782
ad8fe7507ecc 6850612: Deprecate Class.newInstance since it violates the checked exception language contract
darcy
parents: 37672
diff changeset
  1338
                    @SuppressWarnings("deprecation")
ad8fe7507ecc 6850612: Deprecate Class.newInstance since it violates the checked exception language contract
darcy
parents: 37672
diff changeset
  1339
                    Object witness = clz.newInstance();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
                } catch (ClassNotFoundException ex) {
11274
7e7196757acd 7117249: fix warnings in java.util.jar, .logging, .prefs, .zip
smarks
parents: 9700
diff changeset
  1342
                    Class<?> clz = Thread.currentThread().getContextClassLoader().loadClass(cname);
37782
ad8fe7507ecc 6850612: Deprecate Class.newInstance since it violates the checked exception language contract
darcy
parents: 37672
diff changeset
  1343
                    @SuppressWarnings("deprecation")
ad8fe7507ecc 6850612: Deprecate Class.newInstance since it violates the checked exception language contract
darcy
parents: 37672
diff changeset
  1344
                    Object witness = clz.newInstance();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
            } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
                System.err.println("Logging configuration class \"" + cname + "\" failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
                System.err.println("" + ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
                // keep going and useful config file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
32984
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1354
        String fname = getConfigurationFileName();
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1355
        try (final InputStream in = new FileInputStream(fname)) {
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1356
            final BufferedInputStream bin = new BufferedInputStream(in);
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1357
            readConfiguration(bin);
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1358
        }
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1359
    }
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1360
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1361
    String getConfigurationFileName() throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
        String fname = System.getProperty("java.util.logging.config.file");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
        if (fname == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
            fname = System.getProperty("java.home");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
            if (fname == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
                throw new Error("Can't find java.home ??");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
            }
32984
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1368
            fname = Paths.get(fname, "conf", "logging.properties")
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1369
                    .toAbsolutePath().normalize().toString();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
        }
32984
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1371
        return fname;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
     * Reset the logging configuration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
     * For all named loggers, the reset operation removes and closes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
     * all Handlers and (except for the root logger) sets the level
32984
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1379
     * to {@code null}. The root logger's level is set to {@code Level.INFO}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
     *
32984
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1381
     * @apiNote Calling this method also clears the LogManager {@linkplain
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1382
     * #getProperty(java.lang.String) properties}. The {@link
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1383
     * #updateConfiguration(java.util.function.Function)
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1384
     * updateConfiguration(Function)} or
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1385
     * {@link #updateConfiguration(java.io.InputStream, java.util.function.Function)
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1386
     * updateConfiguration(InputStream, Function)} method can be used to
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1387
     * properly update to a new configuration.
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1388
     *
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1389
     * @throws  SecurityException  if a security manager exists and if
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
     *             the caller does not have LoggingPermission("control").
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
    public void reset() throws SecurityException {
14216
23714b376286 7169884: LogManager checks do not work correctly for sub-types
alanb
parents: 13578
diff changeset
  1394
        checkPermission();
30643
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1395
27754
ee2f7768e1c9 8060132: Handlers configured on abstract nodes in logging.properties are not always properly closed
dfuchs
parents: 27191
diff changeset
  1396
        List<CloseOnReset> persistent;
30643
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1397
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1398
        // We don't want reset() and readConfiguration()
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1399
        // to run in parallel
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1400
        configurationLock.lock();
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1401
        try {
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1402
            // install new empty properties
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
            props = new Properties();
27754
ee2f7768e1c9 8060132: Handlers configured on abstract nodes in logging.properties are not always properly closed
dfuchs
parents: 27191
diff changeset
  1404
            // 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
  1405
            // 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
  1406
            // 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
  1407
            // 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
  1408
            persistent = new ArrayList<>(closeOnResetLoggers);
ee2f7768e1c9 8060132: Handlers configured on abstract nodes in logging.properties are not always properly closed
dfuchs
parents: 27191
diff changeset
  1409
            closeOnResetLoggers.clear();
30643
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1410
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1411
            // if reset has been called from shutdown-hook (Cleaner),
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1412
            // or if reset has been called from readConfiguration() which
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1413
            // 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
  1414
            // then do not change state here...
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1415
            if (globalHandlersState != STATE_SHUTDOWN &&
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1416
                globalHandlersState != STATE_READING_CONFIG) {
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1417
                // ...else user called reset()...
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1418
                // 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
  1419
                // 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
  1420
                globalHandlersState = STATE_INITIALIZED;
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1421
            }
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1422
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1423
            for (LoggerContext cx : contexts()) {
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1424
                resetLoggerContext(cx);
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1425
            }
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1426
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1427
            persistent.clear();
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1428
        } finally {
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1429
            configurationLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
        }
30643
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1431
    }
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1432
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1433
    private void resetLoggerContext(LoggerContext cx) {
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1434
        Enumeration<String> enum_ = cx.getLoggerNames();
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1435
        while (enum_.hasMoreElements()) {
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1436
            String name = enum_.nextElement();
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1437
            Logger logger = cx.findLogger(name);
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1438
            if (logger != null) {
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1439
                resetLogger(logger);
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
  1440
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
30643
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1444
    private void closeHandlers(Logger logger) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
        Handler[] targets = logger.getHandlers();
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 22046
diff changeset
  1446
        for (Handler h : targets) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
            logger.removeHandler(h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
                h.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
            } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
                // Problems closing a handler?  Keep going...
41835
90450650fe74 8152515: (logging) LogManager.resetLogger should ignore LinkageError
dfuchs
parents: 39634
diff changeset
  1452
            } catch (Error e) {
90450650fe74 8152515: (logging) LogManager.resetLogger should ignore LinkageError
dfuchs
parents: 39634
diff changeset
  1453
                // ignore Errors while shutting down
90450650fe74 8152515: (logging) LogManager.resetLogger should ignore LinkageError
dfuchs
parents: 39634
diff changeset
  1454
                if (globalHandlersState != STATE_SHUTDOWN) {
90450650fe74 8152515: (logging) LogManager.resetLogger should ignore LinkageError
dfuchs
parents: 39634
diff changeset
  1455
                    throw e;
90450650fe74 8152515: (logging) LogManager.resetLogger should ignore LinkageError
dfuchs
parents: 39634
diff changeset
  1456
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
        }
30643
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1459
    }
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1460
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1461
    // Private method to reset an individual target logger.
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1462
    private void resetLogger(Logger logger) {
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1463
        // Close all the Logger handlers.
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1464
        closeHandlers(logger);
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1465
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1466
        // Reset Logger level
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
  1467
        String name = logger.getName();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
        if (name != null && name.equals("")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
            // This is the root logger.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
            logger.setLevel(defaultLevel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
            logger.setLevel(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
    // get a list of whitespace separated classnames from a property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
    private String[] parseClassNames(String propertyName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
        String hands = getProperty(propertyName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
        if (hands == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
            return new String[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
        hands = hands.trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
        int ix = 0;
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
  1484
        final List<String> result = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
        while (ix < hands.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
            int end = ix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
            while (end < hands.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
                if (Character.isWhitespace(hands.charAt(end))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
                if (hands.charAt(end) == ',') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
                end++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
            String word = hands.substring(ix, end);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
            ix = end+1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
            word = word.trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
            if (word.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
            result.add(word);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
        return result.toArray(new String[result.size()]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
    /**
32984
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1508
     * Reads and initializes the logging configuration from the given input stream.
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1509
     *
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1510
     * <p>
26859
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  1511
     * 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
  1512
     * listener} will be invoked after the properties are read.
33875
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents: 32984
diff changeset
  1513
     *
32984
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1514
     * @apiNote This {@code readConfiguration} method should only be used for
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1515
     * initializing the configuration during LogManager initialization or
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1516
     * used with the "java.util.logging.config.class" property.
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1517
     * When this method is called after loggers have been created, all
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1518
     * existing loggers will be {@linkplain #reset() reset}. Then any
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1519
     * existing loggers that have a level property specified in the
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1520
     * given input stream will be {@linkplain
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1521
     * Logger#setLevel(java.util.logging.Level) set} to the specified log level.
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1522
     * <p>
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1523
     * To properly update the logging configuration, use the
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1524
     * {@link #updateConfiguration(java.util.function.Function)} or
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1525
     * {@link #updateConfiguration(java.io.InputStream, java.util.function.Function)}
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1526
     * method instead.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
     *
32984
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1528
     * @param ins  stream to read properties from
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1529
     * @throws  SecurityException  if a security manager exists and if
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
     *             the caller does not have LoggingPermission("control").
32984
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1531
     * @throws  IOException if there are problems reading from the stream,
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1532
     *             or the given stream is not in the
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1533
     *             {@linkplain java.util.Properties properties file} format.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
    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
  1536
        checkPermission();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
30643
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1538
        // We don't want reset() and readConfiguration() to run
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1539
        // in parallel.
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1540
        configurationLock.lock();
29739
3966f5f6a6fd 8075810: LogManager.readConfiguration may throw undocumented IllegalArgumentException
dfuchs
parents: 27932
diff changeset
  1541
        try {
30643
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1542
            if (globalHandlersState == STATE_SHUTDOWN) {
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1543
                // already in terminal state: don't even bother
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1544
                // to read the configuration
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1545
                return;
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1546
            }
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1547
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1548
            // 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
  1549
            globalHandlersState = STATE_READING_CONFIG;
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1550
            try {
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1551
                // reset configuration which leaves globalHandlersState at STATE_READING_CONFIG
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1552
                // 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
  1553
                // 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
  1554
                reset();
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1555
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1556
                try {
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1557
                    // Load the properties
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1558
                    props.load(ins);
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1559
                } catch (IllegalArgumentException x) {
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1560
                    // props.load may throw an IllegalArgumentException if the stream
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1561
                    // contains malformed Unicode escape sequences.
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1562
                    // We wrap that in an IOException as readConfiguration is
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1563
                    // specified to throw IOException if there are problems reading
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1564
                    // from the stream.
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1565
                    // 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
  1566
                    // concise error message than new IOException(x);
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1567
                    throw new IOException(x.getMessage(), x);
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1568
                }
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1569
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1570
                // Instantiate new configuration objects.
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1571
                String names[] = parseClassNames("config");
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1572
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1573
                for (String word : names) {
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1574
                    try {
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1575
                        Class<?> clz = ClassLoader.getSystemClassLoader().loadClass(word);
37782
ad8fe7507ecc 6850612: Deprecate Class.newInstance since it violates the checked exception language contract
darcy
parents: 37672
diff changeset
  1576
                        @SuppressWarnings("deprecation")
ad8fe7507ecc 6850612: Deprecate Class.newInstance since it violates the checked exception language contract
darcy
parents: 37672
diff changeset
  1577
                        Object witness = clz.newInstance();
30643
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1578
                    } catch (Exception ex) {
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1579
                        System.err.println("Can't load config class \"" + word + "\"");
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1580
                        System.err.println("" + ex);
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1581
                        // ex.printStackTrace();
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1582
                    }
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1583
                }
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1584
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1585
                // 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
  1586
                setLevelsOnExistingLoggers();
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1587
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1588
                // Note that we need to reinitialize global handles when
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1589
                // they are first referenced.
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1590
                globalHandlersState = STATE_UNINITIALIZED;
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1591
            } catch (Throwable t) {
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1592
                // 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
  1593
                // 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
  1594
                // initialized configuration.
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1595
                globalHandlersState = STATE_INITIALIZED;
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1596
                // re-throw
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1597
                throw t;
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1598
            }
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1599
        } finally {
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1600
            configurationLock.unlock();
29739
3966f5f6a6fd 8075810: LogManager.readConfiguration may throw undocumented IllegalArgumentException
dfuchs
parents: 27932
diff changeset
  1601
        }
3966f5f6a6fd 8075810: LogManager.readConfiguration may throw undocumented IllegalArgumentException
dfuchs
parents: 27932
diff changeset
  1602
30643
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1603
        // 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
  1604
        // when user code is involved
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  1605
        invokeConfigurationListeners();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
32984
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1608
    // This enum enumerate the configuration properties that will be
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1609
    // updated on existing loggers when the configuration is updated
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1610
    // with LogManager.updateConfiguration().
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1611
    //
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1612
    // Note that this works properly only for the global LogManager - as
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1613
    // Handler and its subclasses get their configuration from
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1614
    // LogManager.getLogManager().
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1615
    //
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1616
    static enum ConfigProperty {
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1617
        LEVEL(".level"), HANDLERS(".handlers"), USEPARENT(".useParentHandlers");
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1618
        final String suffix;
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1619
        final int length;
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1620
        private ConfigProperty(String suffix) {
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1621
            this.suffix = Objects.requireNonNull(suffix);
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1622
            length = suffix.length();
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1623
        }
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1624
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1625
        public boolean handleKey(String key) {
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1626
            if (this == HANDLERS && suffix.substring(1).equals(key)) return true;
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1627
            if (this == HANDLERS && suffix.equals(key)) return false;
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1628
            return key.endsWith(suffix);
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1629
        }
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1630
        String key(String loggerName) {
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1631
            if (this == HANDLERS && (loggerName == null || loggerName.isEmpty())) {
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1632
                return suffix.substring(1);
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1633
            }
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1634
            return loggerName + suffix;
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1635
        }
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1636
        String loggerName(String key) {
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1637
            assert key.equals(suffix.substring(1)) && this == HANDLERS || key.endsWith(suffix);
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1638
            if (this == HANDLERS && suffix.substring(1).equals(key)) return "";
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1639
            return key.substring(0, key.length() - length);
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1640
        }
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1641
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1642
        /**
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1643
         * If the property is one that should be updated on existing loggers by
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1644
         * updateConfiguration, returns the name of the logger for which the
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1645
         * property is configured. Otherwise, returns null.
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1646
         * @param property a property key in 'props'
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1647
         * @return the name of the logger on which the property is to be set,
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1648
         *         if the property is one that should be updated on existing
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1649
         *         loggers, {@code null} otherwise.
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1650
         */
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1651
        static String getLoggerName(String property) {
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1652
            for (ConfigProperty p : ConfigProperty.ALL) {
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1653
                if (p.handleKey(property)) {
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1654
                    return p.loggerName(property);
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1655
                }
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1656
            }
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1657
            return null; // Not a property that should be updated.
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1658
        }
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1659
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1660
        /**
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1661
         * Find the ConfigProperty corresponding to the given
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1662
         * property key (may find none).
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1663
         * @param property a property key in 'props'
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1664
         * @return An optional containing a ConfigProperty object,
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1665
         *         if the property is one that should be updated on existing
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1666
         *         loggers, empty otherwise.
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1667
         */
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1668
        static Optional<ConfigProperty> find(String property) {
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1669
            return ConfigProperty.ALL.stream()
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1670
                    .filter(p -> p.handleKey(property))
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1671
                    .findFirst();
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1672
         }
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1673
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1674
        /**
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1675
         * Returns true if the given property is one that should be updated
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1676
         * on existing loggers.
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1677
         * Used to filter property name streams.
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1678
         * @param property a property key from the configuration.
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1679
         * @return true if this property is of interest for updateConfiguration.
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1680
         */
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1681
        static boolean matches(String property) {
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1682
            return find(property).isPresent();
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1683
        }
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1684
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1685
        /**
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1686
         * Returns true if the new property value is different from the old,
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1687
         * and therefore needs to be updated on existing loggers.
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1688
         * @param k a property key in the configuration
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1689
         * @param previous the old configuration
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1690
         * @param next the new configuration
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1691
         * @return true if the property is changing value between the two
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1692
         *         configurations.
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1693
         */
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1694
        static boolean needsUpdating(String k, Properties previous, Properties next) {
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1695
            final String p = trim(previous.getProperty(k, null));
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1696
            final String n = trim(next.getProperty(k, null));
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1697
            return ! Objects.equals(p,n);
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1698
        }
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1699
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1700
        /**
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1701
         * Applies the mapping function for the given key to the next
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1702
         * configuration.
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1703
         * If the mapping function is null then this method does nothing.
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1704
         * Otherwise, it calls the mapping function to compute the value
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1705
         * that should be associated with {@code key} in the resulting
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1706
         * configuration, and applies it to {@code next}.
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1707
         * If the mapping function returns {@code null} the key is removed
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1708
         * from {@code next}.
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1709
         *
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1710
         * @param k a property key in the configuration
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1711
         * @param previous the old configuration
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1712
         * @param next the new configuration (modified by this function)
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1713
         * @param remappingFunction the mapping function.
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1714
         */
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1715
        static void merge(String k, Properties previous, Properties next,
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1716
                          BiFunction<String, String, String> mappingFunction) {
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1717
            String p = trim(previous.getProperty(k, null));
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1718
            String n = trim(next.getProperty(k, null));
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1719
            String mapped = trim(mappingFunction.apply(p,n));
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1720
            if (!Objects.equals(n, mapped)) {
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1721
                if (mapped == null) {
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1722
                    next.remove(k);
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1723
                } else {
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1724
                    next.setProperty(k, mapped);
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1725
                }
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1726
            }
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1727
        }
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1728
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1729
        private static final EnumSet<ConfigProperty> ALL =
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1730
                EnumSet.allOf(ConfigProperty.class);
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1731
    }
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1732
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1733
    // trim the value if not null.
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1734
    private static String trim(String value) {
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1735
        return value == null ? null : value.trim();
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1736
    }
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1737
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1738
    /**
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1739
     * An object that keep track of loggers we have already visited.
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1740
     * Used when updating configuration, to avoid processing the same logger
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1741
     * twice.
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1742
     */
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1743
    static final class VisitedLoggers implements Predicate<Logger> {
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1744
        final IdentityHashMap<Logger,Boolean> visited;
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1745
        private VisitedLoggers(IdentityHashMap<Logger,Boolean> visited) {
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1746
            this.visited = visited;
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1747
        }
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1748
        VisitedLoggers() {
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1749
            this(new IdentityHashMap<>());
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1750
        }
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1751
        @Override
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1752
        public boolean test(Logger logger) {
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1753
            return visited != null && visited.put(logger, Boolean.TRUE) != null;
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1754
        }
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1755
        public void clear() {
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1756
            if (visited != null) visited.clear();
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1757
        }
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1758
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1759
        // An object that considers that no logger has ever been visited.
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1760
        // This is used when processParentHandlers is called from
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1761
        // LoggerContext.addLocalLogger
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1762
        static final VisitedLoggers NEVER = new VisitedLoggers(null);
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1763
    }
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1764
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1765
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1766
    /**
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1767
     * Type of the modification for a given property. One of SAME, ADDED, CHANGED,
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1768
     * or REMOVED.
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1769
     */
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1770
    static enum ModType {
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1771
        SAME,    // property had no value in the old and new conf, or had the
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1772
                 // same value in both.
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1773
        ADDED,   // property had no value in the old conf, but has one in the new.
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1774
        CHANGED, // property has a different value in the old conf and the new conf.
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1775
        REMOVED; // property has no value in the new conf, but had one in the old.
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1776
        static ModType of(String previous, String next) {
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1777
            if (previous == null && next != null) {
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1778
                return ADDED;
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1779
            }
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1780
            if (next == null && previous != null) {
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1781
                return REMOVED;
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1782
            }
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1783
            if (!Objects.equals(trim(previous), trim(next))) {
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1784
                return CHANGED;
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1785
            }
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1786
            return SAME;
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1787
        }
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1788
    }
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1789
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1790
    /**
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1791
     * Updates the logging configuration.
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1792
     * <p>
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1793
     * If the "java.util.logging.config.file" system property is set,
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1794
     * then the property value specifies the properties file to be read
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1795
     * as the new configuration. Otherwise, the LogManager default
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1796
     * configuration is used.
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1797
     * <br>The default configuration is typically loaded from the
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1798
     * properties file "{@code conf/logging.properties}" in the
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1799
     * Java installation directory.
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1800
     * <p>
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1801
     * This method reads the new configuration and calls the {@link
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1802
     * #updateConfiguration(java.io.InputStream, java.util.function.Function)
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1803
     * updateConfiguration(ins, mapper)} method to
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1804
     * update the configuration.
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1805
     *
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1806
     * @apiNote
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1807
     * This method updates the logging configuration from reading
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1808
     * a properties file and ignores the "java.util.logging.config.class"
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1809
     * system property.  The "java.util.logging.config.class" property is
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1810
     * only used by the {@link #readConfiguration()}  method to load a custom
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1811
     * configuration class as an initial configuration.
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1812
     *
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1813
     * @param mapper a functional interface that takes a configuration
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1814
     *   key <i>k</i> and returns a function <i>f(o,n)</i> whose returned
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1815
     *   value will be applied to the resulting configuration. The
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1816
     *   function <i>f</i> may return {@code null} to indicate that the property
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1817
     *   <i>k</i> will not be added to the resulting configuration.
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1818
     *   <br>
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1819
     *   If {@code mapper} is {@code null} then {@code (k) -> ((o, n) -> n)} is
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1820
     *   assumed.
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1821
     *   <br>
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1822
     *   For each <i>k</i>, the mapped function <i>f</i> will
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1823
     *   be invoked with the value associated with <i>k</i> in the old
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1824
     *   configuration (i.e <i>o</i>) and the value associated with
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1825
     *   <i>k</i> in the new configuration (i.e. <i>n</i>).
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1826
     *   <br>A {@code null} value for <i>o</i> or <i>n</i> indicates that no
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1827
     *   value was present for <i>k</i> in the corresponding configuration.
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1828
     *
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1829
     * @throws  SecurityException  if a security manager exists and if
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1830
     *          the caller does not have LoggingPermission("control"), or
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1831
     *          does not have the permissions required to set up the
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1832
     *          configuration (e.g. open file specified for FileHandlers
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1833
     *          etc...)
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1834
     *
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1835
     * @throws  NullPointerException  if {@code mapper} returns a {@code null}
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1836
     *         function when invoked.
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1837
     *
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1838
     * @throws  IOException if there are problems reading from the
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1839
     *          logging configuration file.
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1840
     *
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1841
     * @see #updateConfiguration(java.io.InputStream, java.util.function.Function)
44263
7a9297d467e7 8176563: @since value errors in apis of java.base/java.logging module
mli
parents: 41835
diff changeset
  1842
     * @since 9
32984
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1843
     */
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1844
    public void updateConfiguration(Function<String, BiFunction<String,String,String>> mapper)
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1845
            throws IOException {
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1846
        checkPermission();
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1847
        ensureLogManagerInitialized();
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1848
        drainLoggerRefQueueBounded();
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1849
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1850
        String fname = getConfigurationFileName();
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1851
        try (final InputStream in = new FileInputStream(fname)) {
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1852
            final BufferedInputStream bin = new BufferedInputStream(in);
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1853
            updateConfiguration(bin, mapper);
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1854
        }
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1855
    }
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1856
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1857
    /**
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1858
     * Updates the logging configuration.
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1859
     * <p>
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1860
     * For each configuration key in the {@linkplain
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1861
     * #getProperty(java.lang.String) existing configuration} and
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1862
     * the given input stream configuration, the given {@code mapper} function
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1863
     * is invoked to map from the configuration key to a function,
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1864
     * <i>f(o,n)</i>, that takes the old value and new value and returns
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1865
     * the resulting value to be applied in the resulting configuration,
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1866
     * as specified in the table below.
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1867
     * <p>Let <i>k</i> be a configuration key in the old or new configuration,
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1868
     * <i>o</i> be the old value (i.e. the value associated
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1869
     * with <i>k</i> in the old configuration), <i>n</i> be the
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1870
     * new value (i.e. the value associated with <i>k</i> in the new
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1871
     * configuration), and <i>f</i> be the function returned
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1872
     * by {@code mapper.apply(}<i>k</i>{@code )}: then <i>v = f(o,n)</i> is the
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1873
     * resulting value. If <i>v</i> is not {@code null}, then a property
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1874
     * <i>k</i> with value <i>v</i> will be added to the resulting configuration.
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1875
     * Otherwise, it will be omitted.
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1876
     * <br>A {@code null} value may be passed to function
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1877
     * <i>f</i> to indicate that the corresponding configuration has no
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1878
     * configuration key <i>k</i>.
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1879
     * The function <i>f</i> may return {@code null} to indicate that
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1880
     * there will be no value associated with <i>k</i> in the resulting
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1881
     * configuration.
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1882
     * <p>
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1883
     * If {@code mapper} is {@code null}, then <i>v</i> will be set to
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1884
     * <i>n</i>.
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1885
     * <p>
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1886
     * LogManager {@linkplain #getProperty(java.lang.String) properties} are
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1887
     * updated with the resulting value in the resulting configuration.
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1888
     * <p>
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1889
     * The registered {@linkplain #addConfigurationListener configuration
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1890
     * listeners} will be invoked after the configuration is successfully updated.
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1891
     * <br><br>
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1892
     * <table summary="Updating configuration properties">
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1893
     * <tr>
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1894
     * <th>Property</th>
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1895
     * <th>Resulting Behavior</th>
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1896
     * </tr>
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1897
     * <tr>
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1898
     * <td valign="top">{@code <logger>.level}</td>
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1899
     * <td>
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1900
     * <ul>
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1901
     *   <li>If the resulting configuration defines a level for a logger and
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1902
     *       if the resulting level is different than the level specified in the
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1903
     *       the old configuration, or not specified in
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1904
     *       the old configuration, then if the logger exists or if children for
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1905
     *       that logger exist, the level for that logger will be updated,
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1906
     *       and the change propagated to any existing logger children.
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1907
     *       This may cause the logger to be created, if necessary.
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1908
     *   </li>
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1909
     *   <li>If the old configuration defined a level for a logger, and the
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1910
     *       resulting configuration doesn't, then this change will not be
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1911
     *       propagated to existing loggers, if any.
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1912
     *       To completely replace a configuration - the caller should therefore
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1913
     *       call {@link #reset() reset} to empty the current configuration,
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1914
     *       before calling {@code updateConfiguration}.
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1915
     *   </li>
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1916
     * </ul>
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1917
     * </td>
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1918
     * <tr>
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1919
     * <td valign="top">{@code <logger>.useParentHandlers}</td>
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1920
     * <td>
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1921
     * <ul>
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1922
     *   <li>If either the resulting or the old value for the useParentHandlers
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1923
     *       property is not null, then if the logger exists or if children for
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1924
     *       that logger exist, that logger will be updated to the resulting
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1925
     *       value.
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1926
     *       The value of the useParentHandlers property is the value specified
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1927
     *       in the configuration; if not specified, the default is true.
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1928
     *   </li>
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1929
     * </ul>
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1930
     * </td>
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1931
     * </tr>
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1932
     * <tr>
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1933
     * <td valign="top">{@code <logger>.handlers}</td>
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1934
     * <td>
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1935
     * <ul>
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1936
     *   <li>If the resulting configuration defines a list of handlers for a
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1937
     *       logger, and if the resulting list is different than the list
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1938
     *       specified in the old configuration for that logger (that could be
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1939
     *       empty), then if the logger exists or its children exist, the
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1940
     *       handlers associated with that logger are closed and removed and
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1941
     *       the new handlers will be created per the resulting configuration
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1942
     *       and added to that logger, creating that logger if necessary.
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1943
     *   </li>
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1944
     *   <li>If the old configuration defined some handlers for a logger, and
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1945
     *       the resulting configuration doesn't, if that logger exists,
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1946
     *       its handlers will be removed and closed.
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1947
     *   </li>
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1948
     *   <li>Changing the list of handlers on an existing logger will cause all
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1949
     *       its previous handlers to be removed and closed, regardless of whether
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1950
     *       they had been created from the configuration or programmatically.
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1951
     *       The old handlers will be replaced by new handlers, if any.
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1952
     *   </li>
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1953
     * </ul>
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1954
     * </td>
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1955
     * </tr>
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1956
     * <tr>
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1957
     * <td valign="top">{@code <handler-name>.*}</td>
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1958
     * <td>
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1959
     * <ul>
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1960
     *   <li>Properties configured/changed on handler classes will only affect
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1961
     *       newly created handlers. If a node is configured with the same list
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1962
     *       of handlers in the old and the resulting configuration, then these
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1963
     *       handlers will remain unchanged.
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1964
     *   </li>
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1965
     * </ul>
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1966
     * </td>
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1967
     * </tr>
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1968
     * <tr>
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1969
     * <td valign="top">{@code config} and any other property</td>
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1970
     * <td>
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1971
     * <ul>
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1972
     *   <li>The resulting value for these property will be stored in the
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1973
     *   LogManager properties, but {@code updateConfiguration} will not parse
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1974
     *   or process their values.
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1975
     *   </li>
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1976
     * </ul>
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1977
     * </td>
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1978
     * </tr>
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1979
     * </table>
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1980
     * <p>
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1981
     * <em>Example mapper functions:</em>
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1982
     * <br><br>
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1983
     * <ul>
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1984
     * <li>Replace all logging properties with the new configuration:
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1985
     * <br><br>{@code     (k) -> ((o, n) -> n)}:
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1986
     * <br><br>this is equivalent to passing a null {@code mapper} parameter.
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1987
     * </li>
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1988
     * <li>Merge the new configuration and old configuration and use the
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1989
     * new value if <i>k</i> exists in the new configuration:
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1990
     * <br><br>{@code     (k) -> ((o, n) -> n == null ? o : n)}:
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1991
     * <br><br>as if merging two collections as follows:
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1992
     * {@code result.putAll(oldc); result.putAll(newc)}.<br></li>
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1993
     * <li>Merge the new configuration and old configuration and use the old
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1994
     * value if <i>k</i> exists in the old configuration:
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1995
     * <br><br>{@code     (k) -> ((o, n) -> o == null ? n : o)}:
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1996
     * <br><br>as if merging two collections as follows:
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1997
     * {@code result.putAll(newc); result.putAll(oldc)}.<br></li>
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1998
     * <li>Replace all properties with the new configuration except the handler
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  1999
     * property to configure Logger's handler that is not root logger:
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2000
     * <br>
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2001
     * <pre>{@code (k) -> k.endsWith(".handlers")}
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2002
     *      {@code     ? ((o, n) -> (o == null ? n : o))}
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2003
     *      {@code     : ((o, n) -> n)}</pre>
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2004
     * </li>
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2005
     * </ul>
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2006
     * <p>
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2007
     * To completely reinitialize a configuration, an application can first call
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2008
     * {@link #reset() reset} to fully remove the old configuration, followed by
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2009
     * {@code updateConfiguration} to initialize the new configuration.
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2010
     *
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2011
     * @param ins    a stream to read properties from
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2012
     * @param mapper a functional interface that takes a configuration
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2013
     *   key <i>k</i> and returns a function <i>f(o,n)</i> whose returned
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2014
     *   value will be applied to the resulting configuration. The
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2015
     *   function <i>f</i> may return {@code null} to indicate that the property
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2016
     *   <i>k</i> will not be added to the resulting configuration.
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2017
     *   <br>
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2018
     *   If {@code mapper} is {@code null} then {@code (k) -> ((o, n) -> n)} is
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2019
     *   assumed.
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2020
     *   <br>
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2021
     *   For each <i>k</i>, the mapped function <i>f</i> will
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2022
     *   be invoked with the value associated with <i>k</i> in the old
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2023
     *   configuration (i.e <i>o</i>) and the value associated with
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2024
     *   <i>k</i> in the new configuration (i.e. <i>n</i>).
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2025
     *   <br>A {@code null} value for <i>o</i> or <i>n</i> indicates that no
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2026
     *   value was present for <i>k</i> in the corresponding configuration.
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2027
     *
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2028
     * @throws  SecurityException if a security manager exists and if
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2029
     *          the caller does not have LoggingPermission("control"), or
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2030
     *          does not have the permissions required to set up the
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2031
     *          configuration (e.g. open files specified for FileHandlers)
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2032
     *
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2033
     * @throws  NullPointerException if {@code ins} is null or if
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2034
     *          {@code mapper} returns a null function when invoked.
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2035
     *
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2036
     * @throws  IOException if there are problems reading from the stream,
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2037
     *          or the given stream is not in the
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2038
     *          {@linkplain java.util.Properties properties file} format.
44263
7a9297d467e7 8176563: @since value errors in apis of java.base/java.logging module
mli
parents: 41835
diff changeset
  2039
     * @since 9
32984
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2040
     */
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2041
    public void updateConfiguration(InputStream ins,
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2042
            Function<String, BiFunction<String,String,String>> mapper)
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2043
            throws IOException {
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2044
        checkPermission();
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2045
        ensureLogManagerInitialized();
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2046
        drainLoggerRefQueueBounded();
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2047
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2048
        final Properties previous;
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2049
        final Set<String> updatePropertyNames;
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2050
        List<LoggerContext> cxs = Collections.emptyList();
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2051
        final VisitedLoggers visited = new VisitedLoggers();
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2052
        final Properties next = new Properties();
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2053
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2054
        try {
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2055
            // Load the properties
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2056
            next.load(ins);
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2057
        } catch (IllegalArgumentException x) {
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2058
            // props.load may throw an IllegalArgumentException if the stream
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2059
            // contains malformed Unicode escape sequences.
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2060
            // We wrap that in an IOException as updateConfiguration is
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2061
            // specified to throw IOException if there are problems reading
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2062
            // from the stream.
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2063
            // Note: new IOException(x.getMessage(), x) allow us to get a more
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2064
            // concise error message than new IOException(x);
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2065
            throw new IOException(x.getMessage(), x);
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2066
        }
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2067
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2068
        if (globalHandlersState == STATE_SHUTDOWN) return;
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2069
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2070
        // exclusive lock: readConfiguration/reset/updateConfiguration can't
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2071
        //           run concurrently.
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2072
        // configurationLock.writeLock().lock();
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2073
        configurationLock.lock();
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2074
        try {
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2075
            if (globalHandlersState == STATE_SHUTDOWN) return;
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2076
            previous = props;
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2077
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2078
            // Builds a TreeSet of all (old and new) property names.
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2079
            updatePropertyNames =
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2080
                    Stream.concat(previous.stringPropertyNames().stream(),
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2081
                                  next.stringPropertyNames().stream())
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2082
                        .collect(Collectors.toCollection(TreeSet::new));
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2083
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2084
            if (mapper != null) {
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2085
                // mapper will potentially modify the content of
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2086
                // 'next', so we need to call it before affecting props=next.
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2087
                // give a chance to the mapper to control all
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2088
                // properties - not just those we will reset.
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2089
                updatePropertyNames.stream()
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2090
                        .forEachOrdered(k -> ConfigProperty
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2091
                                .merge(k, previous, next,
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2092
                                       Objects.requireNonNull(mapper.apply(k))));
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2093
            }
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2094
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2095
            props = next;
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2096
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2097
            // allKeys will contain all keys:
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2098
            //    - which correspond to a configuration property we are interested in
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2099
            //      (first filter)
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2100
            //    - whose value needs to be updated (because it's new, removed, or
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2101
            //      different) in the resulting configuration (second filter)
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2102
            final Stream<String> allKeys = updatePropertyNames.stream()
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2103
                    .filter(ConfigProperty::matches)
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2104
                    .filter(k -> ConfigProperty.needsUpdating(k, previous, next));
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2105
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2106
            // Group configuration properties by logger name
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2107
            // We use a TreeMap so that parent loggers will be visited before
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2108
            // child loggers.
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2109
            final Map<String, TreeSet<String>> loggerConfigs =
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2110
                    allKeys.collect(Collectors.groupingBy(ConfigProperty::getLoggerName,
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2111
                                    TreeMap::new,
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2112
                                    Collectors.toCollection(TreeSet::new)));
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2113
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2114
            if (!loggerConfigs.isEmpty()) {
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2115
                cxs = contexts();
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2116
            }
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2117
            final List<Logger> loggers = cxs.isEmpty()
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2118
                    ? Collections.emptyList() : new ArrayList<>(cxs.size());
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2119
            for (Map.Entry<String, TreeSet<String>> e : loggerConfigs.entrySet()) {
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2120
                // This can be a logger name, or something else...
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2121
                // The only thing we know is that we found a property
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2122
                //    we are interested in.
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2123
                // For instance, if we found x.y.z.level, then x.y.z could be
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2124
                // a logger, but it could also be a handler class...
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2125
                // Anyway...
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2126
                final String name = e.getKey();
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2127
                final Set<String> properties = e.getValue();
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2128
                loggers.clear();
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2129
                for (LoggerContext cx : cxs) {
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2130
                    Logger l = cx.findLogger(name);
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2131
                    if (l != null && !visited.test(l)) {
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2132
                        loggers.add(l);
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2133
                    }
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2134
                }
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2135
                if (loggers.isEmpty()) continue;
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2136
                for (String pk : properties) {
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2137
                    ConfigProperty cp = ConfigProperty.find(pk).get();
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2138
                    String p = previous.getProperty(pk, null);
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2139
                    String n = next.getProperty(pk, null);
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2140
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2141
                    // Determines the type of modification.
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2142
                    ModType mod = ModType.of(p, n);
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2143
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2144
                    // mod == SAME means that the two values are equals, there
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2145
                    // is nothing to do. Usually, this should not happen as such
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2146
                    // properties should have been filtered above.
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2147
                    // It could happen however if the properties had
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2148
                    // trailing/leading whitespaces.
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2149
                    if (mod == ModType.SAME) continue;
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2150
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2151
                    switch (cp) {
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2152
                        case LEVEL:
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2153
                            if (mod == ModType.REMOVED) continue;
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2154
                            Level level = Level.findLevel(trim(n));
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2155
                            if (level != null) {
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2156
                                if (name.isEmpty()) {
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2157
                                    rootLogger.setLevel(level);
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2158
                                }
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2159
                                for (Logger l : loggers) {
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2160
                                    if (!name.isEmpty() || l != rootLogger) {
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2161
                                        l.setLevel(level);
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2162
                                    }
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2163
                                }
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2164
                            }
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2165
                            break;
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2166
                        case USEPARENT:
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2167
                            if (!name.isEmpty()) {
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2168
                                boolean useParent = getBooleanProperty(pk, true);
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2169
                                if (n != null || p != null) {
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2170
                                    // reset the flag only if the previous value
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2171
                                    // or the new value are not null.
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2172
                                    for (Logger l : loggers) {
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2173
                                        l.setUseParentHandlers(useParent);
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2174
                                    }
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2175
                                }
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2176
                            }
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2177
                            break;
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2178
                        case HANDLERS:
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2179
                            List<Handler> hdls = null;
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2180
                            if (name.isEmpty()) {
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2181
                                // special handling for the root logger.
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2182
                                globalHandlersState = STATE_READING_CONFIG;
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2183
                                try {
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2184
                                    closeHandlers(rootLogger);
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2185
                                    globalHandlersState = STATE_UNINITIALIZED;
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2186
                                } catch (Throwable t) {
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2187
                                    globalHandlersState = STATE_INITIALIZED;
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2188
                                    throw t;
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2189
                                }
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2190
                            }
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2191
                            for (Logger l : loggers) {
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2192
                                if (l == rootLogger) continue;
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2193
                                closeHandlers(l);
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2194
                                if (mod == ModType.REMOVED) {
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2195
                                    closeOnResetLoggers.removeIf(c -> c.logger == l);
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2196
                                    continue;
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2197
                                }
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2198
                                if (hdls == null) {
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2199
                                    hdls = name.isEmpty()
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2200
                                            ? Arrays.asList(rootLogger.getHandlers())
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2201
                                            : createLoggerHandlers(name, pk);
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2202
                                }
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2203
                                setLoggerHandlers(l, name, pk, hdls);
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2204
                            }
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2205
                            break;
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2206
                        default: break;
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2207
                    }
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2208
                }
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2209
            }
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2210
        } finally {
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2211
            configurationLock.unlock();
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2212
            visited.clear();
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2213
        }
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2214
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2215
        // Now ensure that if an existing logger has acquired a new parent
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2216
        // in the configuration, this new parent will be created - if needed,
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2217
        // and added to the context of the existing child.
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2218
        //
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2219
        drainLoggerRefQueueBounded();
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2220
        for (LoggerContext cx : cxs) {
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2221
            for (Enumeration<String> names = cx.getLoggerNames() ; names.hasMoreElements();) {
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2222
                String name = names.nextElement();
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2223
                if (name.isEmpty()) continue;  // don't need to process parents on root.
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2224
                Logger l = cx.findLogger(name);
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2225
                if (l != null && !visited.test(l)) {
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2226
                    // should pass visited here to cut the processing when
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2227
                    // reaching a logger already visited.
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2228
                    cx.processParentHandlers(l, name, visited);
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2229
                }
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2230
            }
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2231
        }
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2232
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2233
        // We changed the configuration: invoke configuration listeners
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2234
        invokeConfigurationListeners();
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2235
    }
4f0f4023875a 8033661: readConfiguration does not cleanly reinitialize the logging system
dfuchs
parents: 32834
diff changeset
  2236
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2237
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2238
     * Get the value of a logging property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2239
     * The method returns null if the property is not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2240
     * @param name      property name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2241
     * @return          property value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2242
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2243
    public String getProperty(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2244
        return props.getProperty(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2245
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2246
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2247
    // Package private method to get a String property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2248
    // If the property is not defined we return the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2249
    // default value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2250
    String getStringProperty(String name, String defaultValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2251
        String val = getProperty(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2252
        if (val == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2253
            return defaultValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2254
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2255
        return val.trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2256
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2257
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2258
    // Package private method to get an integer property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2259
    // If the property is not defined or cannot be parsed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2260
    // we return the given default value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2261
    int getIntProperty(String name, int defaultValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2262
        String val = getProperty(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2263
        if (val == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2264
            return defaultValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2265
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2266
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2267
            return Integer.parseInt(val.trim());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2268
        } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2269
            return defaultValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2270
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2271
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2272
27191
45a3002de56f 8059767: FileHandler should allow 'long' limits and handle overflow of MeteredStream.written.
dfuchs
parents: 26859
diff changeset
  2273
    // 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
  2274
    // 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
  2275
    // we return the given default value.
45a3002de56f 8059767: FileHandler should allow 'long' limits and handle overflow of MeteredStream.written.
dfuchs
parents: 26859
diff changeset
  2276
    long getLongProperty(String name, long defaultValue) {
45a3002de56f 8059767: FileHandler should allow 'long' limits and handle overflow of MeteredStream.written.
dfuchs
parents: 26859
diff changeset
  2277
        String val = getProperty(name);
45a3002de56f 8059767: FileHandler should allow 'long' limits and handle overflow of MeteredStream.written.
dfuchs
parents: 26859
diff changeset
  2278
        if (val == null) {
45a3002de56f 8059767: FileHandler should allow 'long' limits and handle overflow of MeteredStream.written.
dfuchs
parents: 26859
diff changeset
  2279
            return defaultValue;
45a3002de56f 8059767: FileHandler should allow 'long' limits and handle overflow of MeteredStream.written.
dfuchs
parents: 26859
diff changeset
  2280
        }
45a3002de56f 8059767: FileHandler should allow 'long' limits and handle overflow of MeteredStream.written.
dfuchs
parents: 26859
diff changeset
  2281
        try {
45a3002de56f 8059767: FileHandler should allow 'long' limits and handle overflow of MeteredStream.written.
dfuchs
parents: 26859
diff changeset
  2282
            return Long.parseLong(val.trim());
45a3002de56f 8059767: FileHandler should allow 'long' limits and handle overflow of MeteredStream.written.
dfuchs
parents: 26859
diff changeset
  2283
        } catch (Exception ex) {
45a3002de56f 8059767: FileHandler should allow 'long' limits and handle overflow of MeteredStream.written.
dfuchs
parents: 26859
diff changeset
  2284
            return defaultValue;
45a3002de56f 8059767: FileHandler should allow 'long' limits and handle overflow of MeteredStream.written.
dfuchs
parents: 26859
diff changeset
  2285
        }
45a3002de56f 8059767: FileHandler should allow 'long' limits and handle overflow of MeteredStream.written.
dfuchs
parents: 26859
diff changeset
  2286
    }
45a3002de56f 8059767: FileHandler should allow 'long' limits and handle overflow of MeteredStream.written.
dfuchs
parents: 26859
diff changeset
  2287
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2288
    // Package private method to get a boolean property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2289
    // If the property is not defined or cannot be parsed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2290
    // we return the given default value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2291
    boolean getBooleanProperty(String name, boolean defaultValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2292
        String val = getProperty(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2293
        if (val == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2294
            return defaultValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2295
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2296
        val = val.toLowerCase();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2297
        if (val.equals("true") || val.equals("1")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2298
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2299
        } else if (val.equals("false") || val.equals("0")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2300
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2301
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2302
        return defaultValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2303
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2304
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2305
    // Package private method to get a Level property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2306
    // If the property is not defined or cannot be parsed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2307
    // we return the given default value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2308
    Level getLevelProperty(String name, Level defaultValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2309
        String val = getProperty(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2310
        if (val == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2311
            return defaultValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2312
        }
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
  2313
        Level l = Level.findLevel(val.trim());
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
  2314
        return l != null ? l : defaultValue;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2315
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2316
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2317
    // Package private method to get a filter property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2318
    // We return an instance of the class named by the "name"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2319
    // property. If the property is not defined or has problems
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2320
    // we return the defaultValue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2321
    Filter getFilterProperty(String name, Filter defaultValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2322
        String val = getProperty(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2323
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2324
            if (val != null) {
37782
ad8fe7507ecc 6850612: Deprecate Class.newInstance since it violates the checked exception language contract
darcy
parents: 37672
diff changeset
  2325
                @SuppressWarnings("deprecation")
ad8fe7507ecc 6850612: Deprecate Class.newInstance since it violates the checked exception language contract
darcy
parents: 37672
diff changeset
  2326
                Object o = ClassLoader.getSystemClassLoader().loadClass(val).newInstance();
ad8fe7507ecc 6850612: Deprecate Class.newInstance since it violates the checked exception language contract
darcy
parents: 37672
diff changeset
  2327
                return (Filter) o;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2328
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2329
        } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2330
            // We got one of a variety of exceptions in creating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2331
            // class or creating an instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2332
            // Drop through.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2333
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2334
        // We got an exception.  Return the defaultValue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2335
        return defaultValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2336
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2337
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2338
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2339
    // Package private method to get a formatter property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2340
    // We return an instance of the class named by the "name"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2341
    // property. If the property is not defined or has problems
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2342
    // we return the defaultValue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2343
    Formatter getFormatterProperty(String name, Formatter defaultValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2344
        String val = getProperty(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2345
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2346
            if (val != null) {
37782
ad8fe7507ecc 6850612: Deprecate Class.newInstance since it violates the checked exception language contract
darcy
parents: 37672
diff changeset
  2347
                @SuppressWarnings("deprecation")
ad8fe7507ecc 6850612: Deprecate Class.newInstance since it violates the checked exception language contract
darcy
parents: 37672
diff changeset
  2348
                Object o = ClassLoader.getSystemClassLoader().loadClass(val).newInstance();
ad8fe7507ecc 6850612: Deprecate Class.newInstance since it violates the checked exception language contract
darcy
parents: 37672
diff changeset
  2349
                return (Formatter) o;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2350
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2351
        } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2352
            // We got one of a variety of exceptions in creating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2353
            // class or creating an instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2354
            // Drop through.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2355
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2356
        // We got an exception.  Return the defaultValue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2357
        return defaultValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2358
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2359
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2360
    // Private method to load the global handlers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2361
    // We do the real work lazily, when the global handlers
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2362
    // are first used.
30643
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  2363
    private void initializeGlobalHandlers() {
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  2364
        int state = globalHandlersState;
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  2365
        if (state == STATE_INITIALIZED ||
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  2366
            state == STATE_SHUTDOWN) {
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  2367
            // Nothing to do: return.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2368
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2369
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2370
30643
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  2371
        // 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
  2372
        // 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
  2373
        // globalHandlersState == STATE_UNINITIALIZED).
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  2374
        // 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
  2375
        // 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
  2376
        // globalHandlersState == STATE_INITIALIZING).
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  2377
        // 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
  2378
        // 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
  2379
        // is indicated by globalHandlersState == STATE_READING_CONFIG)
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  2380
        // 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
  2381
        configurationLock.lock();
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  2382
        try {
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  2383
            if (globalHandlersState != STATE_UNINITIALIZED) {
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  2384
                return; // recursive call or nothing to do
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  2385
            }
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  2386
            // set globalHandlersState to STATE_INITIALIZING first to avoid
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  2387
            // getting an infinite recursion when loadLoggerHandlers(...)
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  2388
            // is going to call addHandler(...)
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  2389
            globalHandlersState = STATE_INITIALIZING;
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  2390
            try {
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  2391
                loadLoggerHandlers(rootLogger, null, "handlers");
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  2392
            } finally {
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  2393
                globalHandlersState = STATE_INITIALIZED;
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  2394
            }
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  2395
        } finally {
1fcf87dbdc3c 8077846: improve locking strategy for readConfiguration(), reset(), and initializeGlobalHandlers()
plevart
parents: 29919
diff changeset
  2396
            configurationLock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2397
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2398
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2399
33875
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents: 32984
diff changeset
  2400
    static final Permission controlPermission =
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents: 32984
diff changeset
  2401
            new LoggingPermission("control", null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2402
14216
23714b376286 7169884: LogManager checks do not work correctly for sub-types
alanb
parents: 13578
diff changeset
  2403
    void checkPermission() {
23714b376286 7169884: LogManager checks do not work correctly for sub-types
alanb
parents: 13578
diff changeset
  2404
        SecurityManager sm = System.getSecurityManager();
23714b376286 7169884: LogManager checks do not work correctly for sub-types
alanb
parents: 13578
diff changeset
  2405
        if (sm != null)
23714b376286 7169884: LogManager checks do not work correctly for sub-types
alanb
parents: 13578
diff changeset
  2406
            sm.checkPermission(controlPermission);
23714b376286 7169884: LogManager checks do not work correctly for sub-types
alanb
parents: 13578
diff changeset
  2407
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2408
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2409
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2410
     * Check that the current context is trusted to modify the logging
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2411
     * configuration.  This requires LoggingPermission("control").
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2412
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2413
     * If the check fails we throw a SecurityException, otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2414
     * we return normally.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2415
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2416
     * @exception  SecurityException  if a security manager exists and if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2417
     *             the caller does not have LoggingPermission("control").
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2418
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2419
    public void checkAccess() throws SecurityException {
14216
23714b376286 7169884: LogManager checks do not work correctly for sub-types
alanb
parents: 13578
diff changeset
  2420
        checkPermission();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2421
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2422
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2423
    // Nested class to represent a node in our tree of named loggers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2424
    private static class LogNode {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2425
        HashMap<String,LogNode> children;
5964
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  2426
        LoggerWeakRef loggerRef;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2427
        LogNode parent;
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
  2428
        final LoggerContext context;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2429
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
  2430
        LogNode(LogNode parent, LoggerContext context) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2431
            this.parent = parent;
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
  2432
            this.context = context;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2433
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2434
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2435
        // Recursive method to walk the tree below a node and set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2436
        // a new parent logger.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2437
        void walkAndSetParent(Logger parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2438
            if (children == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2439
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2440
            }
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 22046
diff changeset
  2441
            for (LogNode node : children.values()) {
5964
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  2442
                LoggerWeakRef ref = node.loggerRef;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2443
                Logger logger = (ref == null) ? null : ref.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2444
                if (logger == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2445
                    node.walkAndSetParent(parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2446
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2447
                    doSetParent(logger, parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2448
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2449
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2450
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2451
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2452
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2453
    // We use a subclass of Logger for the root logger, so
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2454
    // that we only instantiate the global handlers when they
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2455
    // are first needed.
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
  2456
    private final class RootLogger extends Logger {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2457
        private RootLogger() {
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
  2458
            // 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
  2459
            // to avoid calling LogManager.getLogManager() from within the
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
  2460
            // RootLogger constructor.
23899
a926ecef7eb3 8029740: Enhance handling of loggers
dfuchs
parents: 23342
diff changeset
  2461
            super("", null, null, LogManager.this, true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2462
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2463
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
  2464
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2465
        public void log(LogRecord record) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2466
            // Make sure that the global handlers have been instantiated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2467
            initializeGlobalHandlers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2468
            super.log(record);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2469
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2470
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
  2471
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2472
        public void addHandler(Handler h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2473
            initializeGlobalHandlers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2474
            super.addHandler(h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2475
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2476
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
  2477
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2478
        public void removeHandler(Handler h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2479
            initializeGlobalHandlers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2480
            super.removeHandler(h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2481
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2482
19825
a7e79bc2e437 8023168: Cleanup LogManager class initialization and LogManager/LoggerContext relationship
dfuchs
parents: 19809
diff changeset
  2483
        @Override
23899
a926ecef7eb3 8029740: Enhance handling of loggers
dfuchs
parents: 23342
diff changeset
  2484
        Handler[] accessCheckedHandlers() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2485
            initializeGlobalHandlers();
23899
a926ecef7eb3 8029740: Enhance handling of loggers
dfuchs
parents: 23342
diff changeset
  2486
            return super.accessCheckedHandlers();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2487
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2488
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2489
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2490
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2491
    // Private method to be called when the configuration has
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2492
    // 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
  2493
    private void setLevelsOnExistingLoggers() {
11274
7e7196757acd 7117249: fix warnings in java.util.jar, .logging, .prefs, .zip
smarks
parents: 9700
diff changeset
  2494
        Enumeration<?> enum_ = props.propertyNames();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2495
        while (enum_.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2496
            String key = (String)enum_.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2497
            if (!key.endsWith(".level")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2498
                // Not a level definition.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2499
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2500
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2501
            int ix = key.length() - 6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2502
            String name = key.substring(0, ix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2503
            Level level = getLevelProperty(key, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2504
            if (level == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2505
                System.err.println("Bad level value for property: " + key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2506
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2507
            }
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
  2508
            for (LoggerContext cx : contexts()) {
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
  2509
                Logger l = cx.findLogger(name);
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
  2510
                if (l == null) {
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
  2511
                    continue;
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
  2512
                }
9001e536ab4e 6664509: Add logging context
mchung
parents: 14216
diff changeset
  2513
                l.setLevel(level);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2514
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2515
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2516
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2517
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2518
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2519
     * String representation of the
9013
eedac0b9f552 7024172: Move BufferPoolMXBean and PlatformLoggingMXBean java.lang.management
mchung
parents: 7803
diff changeset
  2520
     * {@link javax.management.ObjectName} for the management interface
eedac0b9f552 7024172: Move BufferPoolMXBean and PlatformLoggingMXBean java.lang.management
mchung
parents: 7803
diff changeset
  2521
     * for the logging facility.
eedac0b9f552 7024172: Move BufferPoolMXBean and PlatformLoggingMXBean java.lang.management
mchung
parents: 7803
diff changeset
  2522
     *
eedac0b9f552 7024172: Move BufferPoolMXBean and PlatformLoggingMXBean java.lang.management
mchung
parents: 7803
diff changeset
  2523
     * @see java.lang.management.PlatformLoggingMXBean
eedac0b9f552 7024172: Move BufferPoolMXBean and PlatformLoggingMXBean java.lang.management
mchung
parents: 7803
diff changeset
  2524
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2525
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2526
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2527
    public final static String LOGGING_MXBEAN_NAME
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2528
        = "java.util.logging:type=Logging";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2529
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2530
    /**
32037
ab4526f4ac10 8133115: docs: replace <tt> tags (obsolete in html5) for java.util.logging, java.util.prefs, java.util.zip, java.util.jar
avstepan
parents: 32035
diff changeset
  2531
     * Returns {@code LoggingMXBean} for managing loggers.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2532
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2533
     * @return a {@link LoggingMXBean} object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2534
     *
38370
61f46e1e7aee 8139982: Re-examine java.management dependency on java.util.logging.LoggingMXBean
dfuchs
parents: 37782
diff changeset
  2535
     * @deprecated {@code java.util.logging.LoggingMXBean} is deprecated and
61f46e1e7aee 8139982: Re-examine java.management dependency on java.util.logging.LoggingMXBean
dfuchs
parents: 37782
diff changeset
  2536
     *      replaced with {@code java.lang.management.PlatformLoggingMXBean}. Use
61f46e1e7aee 8139982: Re-examine java.management dependency on java.util.logging.LoggingMXBean
dfuchs
parents: 37782
diff changeset
  2537
     *      {@link java.lang.management.ManagementFactory#getPlatformMXBean(Class)
61f46e1e7aee 8139982: Re-examine java.management dependency on java.util.logging.LoggingMXBean
dfuchs
parents: 37782
diff changeset
  2538
     *      ManagementFactory.getPlatformMXBean}(PlatformLoggingMXBean.class)
61f46e1e7aee 8139982: Re-examine java.management dependency on java.util.logging.LoggingMXBean
dfuchs
parents: 37782
diff changeset
  2539
     *      instead.
61f46e1e7aee 8139982: Re-examine java.management dependency on java.util.logging.LoggingMXBean
dfuchs
parents: 37782
diff changeset
  2540
     *
9013
eedac0b9f552 7024172: Move BufferPoolMXBean and PlatformLoggingMXBean java.lang.management
mchung
parents: 7803
diff changeset
  2541
     * @see java.lang.management.PlatformLoggingMXBean
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2542
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2543
     */
38370
61f46e1e7aee 8139982: Re-examine java.management dependency on java.util.logging.LoggingMXBean
dfuchs
parents: 37782
diff changeset
  2544
    @Deprecated(since="9")
9013
eedac0b9f552 7024172: Move BufferPoolMXBean and PlatformLoggingMXBean java.lang.management
mchung
parents: 7803
diff changeset
  2545
    public static synchronized LoggingMXBean getLoggingMXBean() {
38370
61f46e1e7aee 8139982: Re-examine java.management dependency on java.util.logging.LoggingMXBean
dfuchs
parents: 37782
diff changeset
  2546
        return Logging.getInstance();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2547
    }
26859
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  2548
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  2549
    /**
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  2550
     * 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
  2551
     * configuration is read.
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  2552
     * 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
  2553
     * <p>
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  2554
     * 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
  2555
     * 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
  2556
     * 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
  2557
     * <p>
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  2558
     * 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
  2559
     *
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  2560
     * 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
  2561
     * 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
  2562
     * {@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
  2563
     * 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
  2564
     *
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  2565
     * @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
  2566
     * 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
  2567
     * 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
  2568
     * suppressed exceptions}.
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  2569
     *
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  2570
     * @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
  2571
     *        configuration changed.
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  2572
     * @return This LogManager.
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  2573
     * @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
  2574
     * 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
  2575
     * @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
  2576
     *
35302
e4d2275861c3 8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents: 33875
diff changeset
  2577
     * @since 9
26859
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  2578
     */
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  2579
    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
  2580
        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
  2581
        checkPermission();
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  2582
        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
  2583
        final AccessControlContext acc =
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  2584
                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
  2585
        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
  2586
                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
  2587
        final Runnable pr =
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  2588
                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
  2589
        // 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
  2590
        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
  2591
        return this;
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  2592
    }
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  2593
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  2594
    /**
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  2595
     * 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
  2596
     *
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  2597
     * 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
  2598
     *
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  2599
     * @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
  2600
     * @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
  2601
     * @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
  2602
     * 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
  2603
     *
35302
e4d2275861c3 8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents: 33875
diff changeset
  2604
     * @since 9
26859
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  2605
     */
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  2606
    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
  2607
        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
  2608
        checkPermission();
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  2609
        listeners.remove(key);
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  2610
    }
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  2611
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  2612
    private void invokeConfigurationListeners() {
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  2613
        Throwable t = null;
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  2614
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  2615
        // 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
  2616
        // keys using identity (==).
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  2617
        // 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
  2618
        // 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
  2619
        // 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
  2620
        // ConcurrentModificationException while looping.
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  2621
        //
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  2622
        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
  2623
            try {
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  2624
                c.run();
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  2625
            } catch (ThreadDeath death) {
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  2626
                throw death;
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  2627
            } 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
  2628
                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
  2629
                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
  2630
            }
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  2631
        }
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  2632
        // 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
  2633
        // 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
  2634
        // 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
  2635
        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
  2636
        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
  2637
    }
8df035e321b8 8043306: Provide a replacement for the API that allowed to listen for LogManager configuration changes
dfuchs
parents: 25859
diff changeset
  2638
33875
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents: 32984
diff changeset
  2639
    /**
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents: 32984
diff changeset
  2640
     * This class allows the {@link LoggingProviderImpl} to demand loggers on
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents: 32984
diff changeset
  2641
     * behalf of system and application classes.
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents: 32984
diff changeset
  2642
     */
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents: 32984
diff changeset
  2643
    private static final class LoggingProviderAccess
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents: 32984
diff changeset
  2644
        implements LoggingProviderImpl.LogManagerAccess,
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents: 32984
diff changeset
  2645
                   PrivilegedAction<Void> {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents: 32984
diff changeset
  2646
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents: 32984
diff changeset
  2647
        private LoggingProviderAccess() {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents: 32984
diff changeset
  2648
        }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents: 32984
diff changeset
  2649
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents: 32984
diff changeset
  2650
        /**
37672
03684934dc09 8148568: LoggerFinder.getLogger and LoggerFinder.getLocalizedLogger should take a Module argument instead of a Class.
dfuchs
parents: 37520
diff changeset
  2651
         * Demands a logger on behalf of the given {@code module}.
33875
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents: 32984
diff changeset
  2652
         * <p>
37672
03684934dc09 8148568: LoggerFinder.getLogger and LoggerFinder.getLocalizedLogger should take a Module argument instead of a Class.
dfuchs
parents: 37520
diff changeset
  2653
         * If a named logger suitable for the given module is found
33875
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents: 32984
diff changeset
  2654
         * returns it.
37672
03684934dc09 8148568: LoggerFinder.getLogger and LoggerFinder.getLocalizedLogger should take a Module argument instead of a Class.
dfuchs
parents: 37520
diff changeset
  2655
         * Otherwise, creates a new logger suitable for the given module.
33875
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents: 32984
diff changeset
  2656
         *
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents: 32984
diff changeset
  2657
         * @param name   The logger name.
37672
03684934dc09 8148568: LoggerFinder.getLogger and LoggerFinder.getLocalizedLogger should take a Module argument instead of a Class.
dfuchs
parents: 37520
diff changeset
  2658
         * @param module The module on which behalf the logger is created/retrieved.
03684934dc09 8148568: LoggerFinder.getLogger and LoggerFinder.getLocalizedLogger should take a Module argument instead of a Class.
dfuchs
parents: 37520
diff changeset
  2659
         * @return A logger for the given {@code module}.
33875
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents: 32984
diff changeset
  2660
         *
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents: 32984
diff changeset
  2661
         * @throws NullPointerException if {@code name} is {@code null}
37672
03684934dc09 8148568: LoggerFinder.getLogger and LoggerFinder.getLocalizedLogger should take a Module argument instead of a Class.
dfuchs
parents: 37520
diff changeset
  2662
         *         or {@code module} is {@code null}.
33875
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents: 32984
diff changeset
  2663
         * @throws IllegalArgumentException if {@code manager} is not the default
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents: 32984
diff changeset
  2664
         *         LogManager.
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents: 32984
diff changeset
  2665
         * @throws SecurityException if a security manager is present and the
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents: 32984
diff changeset
  2666
         *         calling code doesn't have the
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents: 32984
diff changeset
  2667
         *        {@link LoggingPermission LoggingPermission("demandLogger", null)}.
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents: 32984
diff changeset
  2668
         */
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents: 32984
diff changeset
  2669
        @Override
37672
03684934dc09 8148568: LoggerFinder.getLogger and LoggerFinder.getLocalizedLogger should take a Module argument instead of a Class.
dfuchs
parents: 37520
diff changeset
  2670
        public Logger demandLoggerFor(LogManager manager, String name, Module module) {
33875
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents: 32984
diff changeset
  2671
            if (manager != getLogManager()) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents: 32984
diff changeset
  2672
                // having LogManager as parameter just ensures that the
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents: 32984
diff changeset
  2673
                // caller will have initialized the LogManager before reaching
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents: 32984
diff changeset
  2674
                // here.
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents: 32984
diff changeset
  2675
                throw new IllegalArgumentException("manager");
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents: 32984
diff changeset
  2676
            }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents: 32984
diff changeset
  2677
            Objects.requireNonNull(name);
37672
03684934dc09 8148568: LoggerFinder.getLogger and LoggerFinder.getLocalizedLogger should take a Module argument instead of a Class.
dfuchs
parents: 37520
diff changeset
  2678
            Objects.requireNonNull(module);
33875
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents: 32984
diff changeset
  2679
            SecurityManager sm = System.getSecurityManager();
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents: 32984
diff changeset
  2680
            if (sm != null) {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents: 32984
diff changeset
  2681
                sm.checkPermission(controlPermission);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents: 32984
diff changeset
  2682
            }
37672
03684934dc09 8148568: LoggerFinder.getLogger and LoggerFinder.getLocalizedLogger should take a Module argument instead of a Class.
dfuchs
parents: 37520
diff changeset
  2683
            if (isSystem(module)) {
33875
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents: 32984
diff changeset
  2684
                return manager.demandSystemLogger(name,
37672
03684934dc09 8148568: LoggerFinder.getLogger and LoggerFinder.getLocalizedLogger should take a Module argument instead of a Class.
dfuchs
parents: 37520
diff changeset
  2685
                    Logger.SYSTEM_LOGGER_RB_NAME, module);
33875
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents: 32984
diff changeset
  2686
            } else {
37672
03684934dc09 8148568: LoggerFinder.getLogger and LoggerFinder.getLocalizedLogger should take a Module argument instead of a Class.
dfuchs
parents: 37520
diff changeset
  2687
                return manager.demandLogger(name, null, module);
33875
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents: 32984
diff changeset
  2688
            }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents: 32984
diff changeset
  2689
        }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents: 32984
diff changeset
  2690
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents: 32984
diff changeset
  2691
        @Override
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents: 32984
diff changeset
  2692
        public Void run() {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents: 32984
diff changeset
  2693
            LoggingProviderImpl.setLogManagerAccess(INSTANCE);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents: 32984
diff changeset
  2694
            return null;
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents: 32984
diff changeset
  2695
        }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents: 32984
diff changeset
  2696
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents: 32984
diff changeset
  2697
        static final LoggingProviderAccess INSTANCE = new LoggingProviderAccess();
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents: 32984
diff changeset
  2698
    }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents: 32984
diff changeset
  2699
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents: 32984
diff changeset
  2700
    static {
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents: 32984
diff changeset
  2701
        AccessController.doPrivileged(LoggingProviderAccess.INSTANCE, null,
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents: 32984
diff changeset
  2702
                                      controlPermission);
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents: 32984
diff changeset
  2703
    }
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents: 32984
diff changeset
  2704
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2705
}