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