jdk/src/share/classes/java/util/logging/LogManager.java
author alanb
Wed, 10 Oct 2012 20:47:12 +0100
changeset 14175 b8ae22efba75
parent 13578 cb435d74ab6b
child 14229 40fbffe104bd
permissions -rw-r--r--
7192274: Deprecate LogManager addPropertyChangeListener and removePropertyChangeLister methods Reviewed-by: mchung, lancea, chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9700
fdbacf68f185 7016208: 4/3 null sometimes returned by java.util.logging.Logger.getLogger(String name) in -server -Xcomp
dcubed
parents: 9013
diff changeset
     2
 * Copyright (c) 2000, 2011, 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;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.beans.PropertyChangeListener;
13578
cb435d74ab6b 7192275: Minimize LogManager dependencies on java.beans
alanb
parents: 11274
diff changeset
    35
import java.beans.PropertyChangeEvent;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.net.URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import sun.security.action.GetPropertyAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * There is a single global LogManager object that is used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * maintain a set of shared state about Loggers and log services.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * This LogManager object:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <li> Manages a hierarchical namespace of Logger objects.  All
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *      named Loggers are stored in this namespace.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * <li> Manages a set of logging control properties.  These are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *      simple key-value pairs that can be used by Handlers and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *      other logging objects to configure themselves.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * The global LogManager object can be retrieved using LogManager.getLogManager().
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * The LogManager object is created during class initialization and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * cannot subsequently be changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * At startup the LogManager class is located using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * java.util.logging.manager system property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * By default, the LogManager reads its initial configuration from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * a properties file "lib/logging.properties" in the JRE directory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * If you edit that property file you can change the default logging
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * configuration for all uses of that JRE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * In addition, the LogManager uses two optional system properties that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * allow more control over reading the initial configuration:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * <li>"java.util.logging.config.class"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * <li>"java.util.logging.config.file"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * These two properties may be set via the Preferences API, or as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * command line property definitions to the "java" command, or as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * system property definitions passed to JNI_CreateJavaVM.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * If the "java.util.logging.config.class" property is set, then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * property value is treated as a class name.  The given class will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * loaded, an object will be instantiated, and that object's constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * is responsible for reading in the initial configuration.  (That object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * may use other system properties to control its configuration.)  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * alternate configuration class can use <tt>readConfiguration(InputStream)</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * to define properties in the LogManager.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * If "java.util.logging.config.class" property is <b>not</b> set,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * then the "java.util.logging.config.file" system property can be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * to specify a properties file (in java.util.Properties format). The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * initial logging configuration will be read from this file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * If neither of these properties is defined then, as described
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * above, the LogManager will read its initial configuration from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * a properties file "lib/logging.properties" in the JRE directory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * The properties for loggers and Handlers will have names starting
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * with the dot-separated name for the handler or logger.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * The global logging properties may include:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * <li>A property "handlers".  This defines a whitespace or comma separated
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * list of class names for handler classes to load and register as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * handlers on the root Logger (the Logger named "").  Each class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * name must be for a Handler class which has a default constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * Note that these Handlers may be created lazily, when they are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * first used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * <li>A property "&lt;logger&gt;.handlers". This defines a whitespace or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * comma separated list of class names for handlers classes to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * load and register as handlers to the specified logger. Each class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * name must be for a Handler class which has a default constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * Note that these Handlers may be created lazily, when they are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * first used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * <li>A property "&lt;logger&gt;.useParentHandlers". This defines a boolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * value. By default every logger calls its parent in addition to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * handling the logging message itself, this often result in messages
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 * being handled by the root logger as well. When setting this property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 * to false a Handler needs to be configured for this logger otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * no logging messages are delivered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 * <li>A property "config".  This property is intended to allow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 * arbitrary configuration code to be run.  The property defines a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 * whitespace or comma separated list of class names.  A new instance will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 * created for each named class.  The default constructor of each class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 * may execute arbitrary code to update the logging configuration, such as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 * setting logger levels, adding handlers, adding filters, etc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 * Note that all classes loaded during LogManager configuration are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 * first searched on the system class path before any user class path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 * That includes the LogManager class, any config classes, and any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 * handler classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 * Loggers are organized into a naming hierarchy based on their
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 * dot separated names.  Thus "a.b.c" is a child of "a.b", but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 * "a.b1" and a.b2" are peers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 * All properties whose names end with ".level" are assumed to define
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 * log levels for Loggers.  Thus "foo.level" defines a log level for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 * the logger called "foo" and (recursively) for any of its children
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 * in the naming hierarchy.  Log Levels are applied in the order they
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
 * are defined in the properties file.  Thus level settings for child
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
 * nodes in the tree should come after settings for their parents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
 * The property name ".level" can be used to set the level for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
 * root of the tree.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
 * All methods on the LogManager object are multi-thread safe.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
public class LogManager {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    // The global LogManager object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    private static LogManager manager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    private final static Handler[] emptyHandlers = { };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    private Properties props = new Properties();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    private final static Level defaultLevel = Level.INFO;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
13578
cb435d74ab6b 7192275: Minimize LogManager dependencies on java.beans
alanb
parents: 11274
diff changeset
   156
    // 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
   157
    // count to allow for cases where the same listener is registered many times.
cb435d74ab6b 7192275: Minimize LogManager dependencies on java.beans
alanb
parents: 11274
diff changeset
   158
    private final Map<PropertyChangeListener,Integer> listenerMap = new HashMap<>();
cb435d74ab6b 7192275: Minimize LogManager dependencies on java.beans
alanb
parents: 11274
diff changeset
   159
5964
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   160
    // Table of named Loggers that maps names to Loggers.
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 6675
diff changeset
   161
    private Hashtable<String,LoggerWeakRef> namedLoggers = new Hashtable<>();
5964
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   162
    // Tree of named Loggers
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    private LogNode root = new LogNode(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    private Logger rootLogger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    // Have we done the primordial reading of the configuration file?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    // (Must be done after a suitable amount of java.lang.System
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    // initialization has been done)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    private volatile boolean readPrimordialConfiguration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    // Have we initialized global (root) handlers yet?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    // This gets set to false in readConfiguration
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    private boolean initializedGlobalHandlers = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    // True if JVM death is imminent and the exit hook has been called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    private boolean deathImminent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        AccessController.doPrivileged(new PrivilegedAction<Object>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                    String cname = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                        cname = System.getProperty("java.util.logging.manager");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                        if (cname != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                            try {
11274
7e7196757acd 7117249: fix warnings in java.util.jar, .logging, .prefs, .zip
smarks
parents: 9700
diff changeset
   184
                                Class<?> clz = ClassLoader.getSystemClassLoader().loadClass(cname);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                                manager = (LogManager) clz.newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                            } catch (ClassNotFoundException ex) {
11274
7e7196757acd 7117249: fix warnings in java.util.jar, .logging, .prefs, .zip
smarks
parents: 9700
diff changeset
   187
                                Class<?> clz = Thread.currentThread().getContextClassLoader().loadClass(cname);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                                manager = (LogManager) clz.newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                    } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                        System.err.println("Could not load Logmanager \"" + cname + "\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                        ex.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                    if (manager == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                        manager = new LogManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                    // Create and retain Logger for the root of the namespace.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                    manager.rootLogger = manager.new RootLogger();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                    manager.addLogger(manager.rootLogger);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                    // Adding the global Logger. Doing so in the Logger.<clinit>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                    // would deadlock with the LogManager.<clinit>.
11274
7e7196757acd 7117249: fix warnings in java.util.jar, .logging, .prefs, .zip
smarks
parents: 9700
diff changeset
   205
                    Logger.getGlobal().setLogManager(manager);
7e7196757acd 7117249: fix warnings in java.util.jar, .logging, .prefs, .zip
smarks
parents: 9700
diff changeset
   206
                    manager.addLogger(Logger.getGlobal());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                    // We don't call readConfiguration() here, as we may be running
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                    // very early in the JVM startup sequence.  Instead readConfiguration
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                    // will be called lazily in getLogManager().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    // This private class is used as a shutdown hook.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    // It does a "reset" to close all open handlers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    private class Cleaner extends Thread {
2384
b3ba5fb77f89 6799583: LogManager shutdown hook may cause a memory leak.
bae
parents: 2
diff changeset
   220
b3ba5fb77f89 6799583: LogManager shutdown hook may cause a memory leak.
bae
parents: 2
diff changeset
   221
        private Cleaner() {
b3ba5fb77f89 6799583: LogManager shutdown hook may cause a memory leak.
bae
parents: 2
diff changeset
   222
            /* 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
   223
             * keeping a strong reference to an application classloader.
b3ba5fb77f89 6799583: LogManager shutdown hook may cause a memory leak.
bae
parents: 2
diff changeset
   224
             */
b3ba5fb77f89 6799583: LogManager shutdown hook may cause a memory leak.
bae
parents: 2
diff changeset
   225
            this.setContextClassLoader(null);
b3ba5fb77f89 6799583: LogManager shutdown hook may cause a memory leak.
bae
parents: 2
diff changeset
   226
        }
b3ba5fb77f89 6799583: LogManager shutdown hook may cause a memory leak.
bae
parents: 2
diff changeset
   227
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            // This is to ensure the LogManager.<clinit> is completed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            // before synchronized block. Otherwise deadlocks are possible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            LogManager mgr = manager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            // If the global handlers haven't been initialized yet, we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            // don't want to initialize them just so we can close them!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            synchronized (LogManager.this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                // Note that death is imminent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                deathImminent = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                initializedGlobalHandlers = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            // Do a reset to close all active handlers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * Protected constructor.  This is protected so that container applications
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * (such as J2EE containers) can subclass the object.  It is non-public as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * it is intended that there only be one LogManager object, whose value is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * retrieved by calling Logmanager.getLogManager.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    protected LogManager() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        // Add a shutdown hook to close the global handlers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            Runtime.getRuntime().addShutdownHook(new Cleaner());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        } catch (IllegalStateException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            // If the VM is already shutting down,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            // We do not need to register shutdownHook.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * Return the global LogManager object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    public static LogManager getLogManager() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        if (manager != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            manager.readPrimordialConfiguration();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        return manager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    private void readPrimordialConfiguration() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        if (!readPrimordialConfiguration) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                if (!readPrimordialConfiguration) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                    // If System.in/out/err are null, it's a good
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                    // indication that we're still in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                    // bootstrapping phase
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                    if (System.out == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                    readPrimordialConfiguration = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                        AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                                public Object run() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                                    readConfiguration();
3861
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents: 3319
diff changeset
   288
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents: 3319
diff changeset
   289
                                    // 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
   290
                                    sun.util.logging.PlatformLogger.redirectPlatformLoggers();
a98a057ec335 6882376: Add internal support for JRE implementation to eliminate the dependency on logging
mchung
parents: 3319
diff changeset
   291
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                    } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                        // System.err.println("Can't read logging configuration:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                        // ex.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * Adds an event listener to be invoked when the logging
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * properties are re-read. Adding multiple instances of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * the same event Listener results in multiple entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * in the property event listener table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * @param l  event listener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * @exception  SecurityException  if a security manager exists and if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     *             the caller does not have LoggingPermission("control").
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * @exception NullPointerException if the PropertyChangeListener is null.
14175
b8ae22efba75 7192274: Deprecate LogManager addPropertyChangeListener and removePropertyChangeLister methods
alanb
parents: 13578
diff changeset
   314
     * @deprecated The dependency on {@code PropertyChangeListener} creates a
b8ae22efba75 7192274: Deprecate LogManager addPropertyChangeListener and removePropertyChangeLister methods
alanb
parents: 13578
diff changeset
   315
     *             significant impediment to future modularization of the Java
b8ae22efba75 7192274: Deprecate LogManager addPropertyChangeListener and removePropertyChangeLister methods
alanb
parents: 13578
diff changeset
   316
     *             platform. This method will be removed in a future release.
b8ae22efba75 7192274: Deprecate LogManager addPropertyChangeListener and removePropertyChangeLister methods
alanb
parents: 13578
diff changeset
   317
     *             The global {@code LogManager} can detect changes to the
b8ae22efba75 7192274: Deprecate LogManager addPropertyChangeListener and removePropertyChangeLister methods
alanb
parents: 13578
diff changeset
   318
     *             logging configuration by overridding the {@link
b8ae22efba75 7192274: Deprecate LogManager addPropertyChangeListener and removePropertyChangeLister methods
alanb
parents: 13578
diff changeset
   319
     *             #readConfiguration readConfiguration} method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     */
14175
b8ae22efba75 7192274: Deprecate LogManager addPropertyChangeListener and removePropertyChangeLister methods
alanb
parents: 13578
diff changeset
   321
    @Deprecated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    public void addPropertyChangeListener(PropertyChangeListener l) throws SecurityException {
13578
cb435d74ab6b 7192275: Minimize LogManager dependencies on java.beans
alanb
parents: 11274
diff changeset
   323
        PropertyChangeListener listener = Objects.requireNonNull(l);
cb435d74ab6b 7192275: Minimize LogManager dependencies on java.beans
alanb
parents: 11274
diff changeset
   324
        checkAccess();
cb435d74ab6b 7192275: Minimize LogManager dependencies on java.beans
alanb
parents: 11274
diff changeset
   325
        synchronized (listenerMap) {
cb435d74ab6b 7192275: Minimize LogManager dependencies on java.beans
alanb
parents: 11274
diff changeset
   326
            // increment the registration count if already registered
cb435d74ab6b 7192275: Minimize LogManager dependencies on java.beans
alanb
parents: 11274
diff changeset
   327
            Integer value = listenerMap.get(listener);
cb435d74ab6b 7192275: Minimize LogManager dependencies on java.beans
alanb
parents: 11274
diff changeset
   328
            value = (value == null) ? 1 : (value + 1);
cb435d74ab6b 7192275: Minimize LogManager dependencies on java.beans
alanb
parents: 11274
diff changeset
   329
            listenerMap.put(listener, value);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * Removes an event listener for property change events.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * If the same listener instance has been added to the listener table
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * through multiple invocations of <CODE>addPropertyChangeListener</CODE>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * then an equivalent number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * <CODE>removePropertyChangeListener</CODE> invocations are required to remove
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * all instances of that listener from the listener table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * Returns silently if the given listener is not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * @param l  event listener (can be null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * @exception  SecurityException  if a security manager exists and if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     *             the caller does not have LoggingPermission("control").
14175
b8ae22efba75 7192274: Deprecate LogManager addPropertyChangeListener and removePropertyChangeLister methods
alanb
parents: 13578
diff changeset
   346
     * @deprecated The dependency on {@code PropertyChangeListener} creates a
b8ae22efba75 7192274: Deprecate LogManager addPropertyChangeListener and removePropertyChangeLister methods
alanb
parents: 13578
diff changeset
   347
     *             significant impediment to future modularization of the Java
b8ae22efba75 7192274: Deprecate LogManager addPropertyChangeListener and removePropertyChangeLister methods
alanb
parents: 13578
diff changeset
   348
     *             platform. This method will be removed in a future release.
b8ae22efba75 7192274: Deprecate LogManager addPropertyChangeListener and removePropertyChangeLister methods
alanb
parents: 13578
diff changeset
   349
     *             The global {@code LogManager} can detect changes to the
b8ae22efba75 7192274: Deprecate LogManager addPropertyChangeListener and removePropertyChangeLister methods
alanb
parents: 13578
diff changeset
   350
     *             logging configuration by overridding the {@link
b8ae22efba75 7192274: Deprecate LogManager addPropertyChangeListener and removePropertyChangeLister methods
alanb
parents: 13578
diff changeset
   351
     *             #readConfiguration readConfiguration} method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     */
14175
b8ae22efba75 7192274: Deprecate LogManager addPropertyChangeListener and removePropertyChangeLister methods
alanb
parents: 13578
diff changeset
   353
    @Deprecated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    public void removePropertyChangeListener(PropertyChangeListener l) throws SecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        checkAccess();
13578
cb435d74ab6b 7192275: Minimize LogManager dependencies on java.beans
alanb
parents: 11274
diff changeset
   356
        if (l != null) {
cb435d74ab6b 7192275: Minimize LogManager dependencies on java.beans
alanb
parents: 11274
diff changeset
   357
            PropertyChangeListener listener = l;
cb435d74ab6b 7192275: Minimize LogManager dependencies on java.beans
alanb
parents: 11274
diff changeset
   358
            synchronized (listenerMap) {
cb435d74ab6b 7192275: Minimize LogManager dependencies on java.beans
alanb
parents: 11274
diff changeset
   359
                Integer value = listenerMap.get(listener);
cb435d74ab6b 7192275: Minimize LogManager dependencies on java.beans
alanb
parents: 11274
diff changeset
   360
                if (value != null) {
cb435d74ab6b 7192275: Minimize LogManager dependencies on java.beans
alanb
parents: 11274
diff changeset
   361
                    // remove from map if registration count is 1, otherwise
cb435d74ab6b 7192275: Minimize LogManager dependencies on java.beans
alanb
parents: 11274
diff changeset
   362
                    // just decrement its count
cb435d74ab6b 7192275: Minimize LogManager dependencies on java.beans
alanb
parents: 11274
diff changeset
   363
                    int i = value.intValue();
cb435d74ab6b 7192275: Minimize LogManager dependencies on java.beans
alanb
parents: 11274
diff changeset
   364
                    if (i == 1) {
cb435d74ab6b 7192275: Minimize LogManager dependencies on java.beans
alanb
parents: 11274
diff changeset
   365
                        listenerMap.remove(listener);
cb435d74ab6b 7192275: Minimize LogManager dependencies on java.beans
alanb
parents: 11274
diff changeset
   366
                    } else {
cb435d74ab6b 7192275: Minimize LogManager dependencies on java.beans
alanb
parents: 11274
diff changeset
   367
                        assert i > 1;
cb435d74ab6b 7192275: Minimize LogManager dependencies on java.beans
alanb
parents: 11274
diff changeset
   368
                        listenerMap.put(listener, i - 1);
cb435d74ab6b 7192275: Minimize LogManager dependencies on java.beans
alanb
parents: 11274
diff changeset
   369
                    }
cb435d74ab6b 7192275: Minimize LogManager dependencies on java.beans
alanb
parents: 11274
diff changeset
   370
                }
cb435d74ab6b 7192275: Minimize LogManager dependencies on java.beans
alanb
parents: 11274
diff changeset
   371
            }
cb435d74ab6b 7192275: Minimize LogManager dependencies on java.beans
alanb
parents: 11274
diff changeset
   372
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    // Package-level method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    // Find or create a specified logger instance. If a logger has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    // already been created with the given name it is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    // Otherwise a new logger instance is created and registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    // in the LogManager global namespace.
9700
fdbacf68f185 7016208: 4/3 null sometimes returned by java.util.logging.Logger.getLogger(String name) in -server -Xcomp
dcubed
parents: 9013
diff changeset
   380
fdbacf68f185 7016208: 4/3 null sometimes returned by java.util.logging.Logger.getLogger(String name) in -server -Xcomp
dcubed
parents: 9013
diff changeset
   381
    // This method will always return a non-null Logger object.
fdbacf68f185 7016208: 4/3 null sometimes returned by java.util.logging.Logger.getLogger(String name) in -server -Xcomp
dcubed
parents: 9013
diff changeset
   382
    // Synchronization is not required here. All synchronization for
fdbacf68f185 7016208: 4/3 null sometimes returned by java.util.logging.Logger.getLogger(String name) in -server -Xcomp
dcubed
parents: 9013
diff changeset
   383
    // adding a new Logger object is handled by addLogger().
3319
53a6d815c92f 6814140: deadlock due to synchronized demandLogger() code that locks ServerLogManager
poonam
parents: 2384
diff changeset
   384
    Logger demandLogger(String name) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        Logger result = getLogger(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        if (result == 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
   387
            // only allocate the new logger once
fdbacf68f185 7016208: 4/3 null sometimes returned by java.util.logging.Logger.getLogger(String name) in -server -Xcomp
dcubed
parents: 9013
diff changeset
   388
            Logger newLogger = new Logger(name, null);
fdbacf68f185 7016208: 4/3 null sometimes returned by java.util.logging.Logger.getLogger(String name) in -server -Xcomp
dcubed
parents: 9013
diff changeset
   389
            do {
fdbacf68f185 7016208: 4/3 null sometimes returned by java.util.logging.Logger.getLogger(String name) in -server -Xcomp
dcubed
parents: 9013
diff changeset
   390
                if (addLogger(newLogger)) {
fdbacf68f185 7016208: 4/3 null sometimes returned by java.util.logging.Logger.getLogger(String name) in -server -Xcomp
dcubed
parents: 9013
diff changeset
   391
                    // We successfully added the new Logger that we
fdbacf68f185 7016208: 4/3 null sometimes returned by java.util.logging.Logger.getLogger(String name) in -server -Xcomp
dcubed
parents: 9013
diff changeset
   392
                    // created above so return it without refetching.
fdbacf68f185 7016208: 4/3 null sometimes returned by java.util.logging.Logger.getLogger(String name) in -server -Xcomp
dcubed
parents: 9013
diff changeset
   393
                    return newLogger;
fdbacf68f185 7016208: 4/3 null sometimes returned by java.util.logging.Logger.getLogger(String name) in -server -Xcomp
dcubed
parents: 9013
diff changeset
   394
                }
fdbacf68f185 7016208: 4/3 null sometimes returned by java.util.logging.Logger.getLogger(String name) in -server -Xcomp
dcubed
parents: 9013
diff changeset
   395
fdbacf68f185 7016208: 4/3 null sometimes returned by java.util.logging.Logger.getLogger(String name) in -server -Xcomp
dcubed
parents: 9013
diff changeset
   396
                // We didn't add the new Logger that we created above
fdbacf68f185 7016208: 4/3 null sometimes returned by java.util.logging.Logger.getLogger(String name) in -server -Xcomp
dcubed
parents: 9013
diff changeset
   397
                // because another thread added a Logger with the same
fdbacf68f185 7016208: 4/3 null sometimes returned by java.util.logging.Logger.getLogger(String name) in -server -Xcomp
dcubed
parents: 9013
diff changeset
   398
                // name after our null check above and before our call
fdbacf68f185 7016208: 4/3 null sometimes returned by java.util.logging.Logger.getLogger(String name) in -server -Xcomp
dcubed
parents: 9013
diff changeset
   399
                // to addLogger(). We have to refetch the Logger because
fdbacf68f185 7016208: 4/3 null sometimes returned by java.util.logging.Logger.getLogger(String name) in -server -Xcomp
dcubed
parents: 9013
diff changeset
   400
                // addLogger() returns a boolean instead of the Logger
fdbacf68f185 7016208: 4/3 null sometimes returned by java.util.logging.Logger.getLogger(String name) in -server -Xcomp
dcubed
parents: 9013
diff changeset
   401
                // reference itself. However, if the thread that created
fdbacf68f185 7016208: 4/3 null sometimes returned by java.util.logging.Logger.getLogger(String name) in -server -Xcomp
dcubed
parents: 9013
diff changeset
   402
                // the other Logger is not holding a strong reference to
fdbacf68f185 7016208: 4/3 null sometimes returned by java.util.logging.Logger.getLogger(String name) in -server -Xcomp
dcubed
parents: 9013
diff changeset
   403
                // the other Logger, then it is possible for the other
fdbacf68f185 7016208: 4/3 null sometimes returned by java.util.logging.Logger.getLogger(String name) in -server -Xcomp
dcubed
parents: 9013
diff changeset
   404
                // Logger to be GC'ed after we saw it in addLogger() and
fdbacf68f185 7016208: 4/3 null sometimes returned by java.util.logging.Logger.getLogger(String name) in -server -Xcomp
dcubed
parents: 9013
diff changeset
   405
                // before we can refetch it. If it has been GC'ed then
fdbacf68f185 7016208: 4/3 null sometimes returned by java.util.logging.Logger.getLogger(String name) in -server -Xcomp
dcubed
parents: 9013
diff changeset
   406
                // we'll just loop around and try again.
fdbacf68f185 7016208: 4/3 null sometimes returned by java.util.logging.Logger.getLogger(String name) in -server -Xcomp
dcubed
parents: 9013
diff changeset
   407
                result = getLogger(name);
fdbacf68f185 7016208: 4/3 null sometimes returned by java.util.logging.Logger.getLogger(String name) in -server -Xcomp
dcubed
parents: 9013
diff changeset
   408
            } while (result == null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    // If logger.getUseParentHandlers() returns 'true' and any of the logger's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    // parents have levels or handlers defined, make sure they are instantiated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    private void processParentHandlers(Logger logger, String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        int ix = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            int ix2 = name.indexOf(".", ix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            if (ix2 < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            String pname = name.substring(0,ix2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            if (getProperty(pname+".level")    != null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                getProperty(pname+".handlers") != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                // This pname has a level/handlers definition.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                // Make sure it exists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                demandLogger(pname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            ix = ix2+1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    // Add new per logger handlers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    // We need to raise privilege here. All our decisions will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
    // be made based on the logging configuration, which can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    // only be modified by trusted code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    private void loadLoggerHandlers(final Logger logger, final String name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                                    final String handlersPropertyName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        AccessController.doPrivileged(new PrivilegedAction<Object>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                if (logger != rootLogger) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                    boolean useParent = getBooleanProperty(name + ".useParentHandlers", true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                    if (!useParent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                        logger.setUseParentHandlers(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                String names[] = parseClassNames(handlersPropertyName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                for (int i = 0; i < names.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                    String word = names[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                    try {
11274
7e7196757acd 7117249: fix warnings in java.util.jar, .logging, .prefs, .zip
smarks
parents: 9700
diff changeset
   453
                        Class<?> clz = ClassLoader.getSystemClassLoader().loadClass(word);
7e7196757acd 7117249: fix warnings in java.util.jar, .logging, .prefs, .zip
smarks
parents: 9700
diff changeset
   454
                        Handler  hdl = (Handler) clz.newInstance();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                            // Check if there is a property defining the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                            // this handler's level.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                            String levs = getProperty(word + ".level");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                            if (levs != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                                hdl.setLevel(Level.parse(levs));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                        } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                            System.err.println("Can't set level for " + word);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                            // Probably a bad level. Drop through.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                        // Add this Handler to the logger
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                        logger.addHandler(hdl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                    } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                        System.err.println("Can't load log handler \"" + word + "\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                        System.err.println("" + ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                        ex.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            }});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
5964
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   478
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   479
    // loggerRefQueue holds LoggerWeakRef objects for Logger objects
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   480
    // that have been GC'ed.
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   481
    private final ReferenceQueue<Logger> loggerRefQueue
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 6675
diff changeset
   482
        = new ReferenceQueue<>();
5964
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   483
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   484
    // Package-level inner class.
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   485
    // 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
   486
    //
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   487
    // LogManager.namedLoggers
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   488
    //     - has weak references to all named Loggers
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   489
    //     - 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
   490
    //       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
   491
    //       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
   492
    // LogManager.LogNode.loggerRef
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   493
    //     - 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
   494
    //     - 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
   495
    //       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
   496
    // Logger.kids
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   497
    //     - 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
   498
    //       includes anonymous and named Loggers
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   499
    //     - 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
   500
    //       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
   501
    //       LoggerWeakRef objects for the anonymous Loggers around
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   502
    //       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
   503
    //
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   504
    final class LoggerWeakRef extends WeakReference<Logger> {
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   505
        private String                name;       // for namedLoggers cleanup
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   506
        private LogNode               node;       // for loggerRef cleanup
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   507
        private WeakReference<Logger> parentRef;  // for kids cleanup
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   508
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   509
        LoggerWeakRef(Logger logger) {
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   510
            super(logger, loggerRefQueue);
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   511
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   512
            name = logger.getName();  // save for namedLoggers cleanup
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   513
        }
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   514
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   515
        // dispose of this LoggerWeakRef object
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   516
        void dispose() {
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   517
            if (node != null) {
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   518
                // 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
   519
                // so clear namedLoggers weak ref to us
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   520
                manager.namedLoggers.remove(name);
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   521
                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
   522
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   523
                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
   524
                node = null;            // clear our ref to LogNode
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   525
            }
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   526
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   527
            if (parentRef != null) {
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   528
                // 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
   529
                Logger parent = parentRef.get();
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   530
                if (parent != null) {
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   531
                    // 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
   532
                    // parent Logger's weak ref to us
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   533
                    parent.removeChildLogger(this);
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   534
                }
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   535
                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
   536
            }
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   537
        }
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   538
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   539
        // 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
   540
        void setNode(LogNode node) {
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   541
            this.node = node;
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   542
        }
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   543
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   544
        // 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
   545
        void setParentRef(WeakReference<Logger> parentRef) {
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   546
            this.parentRef = parentRef;
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   547
        }
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   548
    }
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   549
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   550
    // Package-level method.
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   551
    // 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
   552
    //
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   553
    // drainLoggerRefQueueBounded() is called by addLogger() below
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   554
    // 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
   555
    // 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
   556
    //
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   557
    // 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
   558
    // 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
   559
    // 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
   560
    // 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
   561
    //   - test duration 1 minute
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   562
    //   - sample size of 125 sets of 400
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   563
    //   - average: 1.99 ms
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   564
    //   - minimum: 0.57 ms
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   565
    //   - maximum: 25.3 ms
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   566
    //
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   567
    // 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
   568
    // 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
   569
    // 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
   570
    //   - test duration 2 minutes
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   571
    //   - sample size of 506 sets of 400
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   572
    //   - average: 0.57 ms
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   573
    //   - minimum: 0.02 ms
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   574
    //   - maximum: 10.9 ms
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   575
    //
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   576
    private final static int MAX_ITERATIONS = 400;
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   577
    final synchronized void drainLoggerRefQueueBounded() {
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   578
        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
   579
            if (loggerRefQueue == null) {
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   580
                // haven't finished loading LogManager yet
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   581
                break;
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   582
            }
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   583
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   584
            LoggerWeakRef ref = (LoggerWeakRef) loggerRefQueue.poll();
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   585
            if (ref == null) {
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   586
                break;
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   587
            }
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   588
            // 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
   589
            ref.dispose();
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   590
        }
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   591
    }
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   592
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     * Add a named logger.  This does nothing and returns false if a logger
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     * with the same name is already registered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     * The Logger factory methods call this method to register each
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     * newly created Logger.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     * The application should retain its own reference to the Logger
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * object to avoid it being garbage collected.  The LogManager
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     * may only retain a weak reference.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     * @param   logger the new logger.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     * @return  true if the argument logger was registered successfully,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     *          false if a logger of that name already exists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     * @exception NullPointerException if the logger name is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
    public synchronized boolean addLogger(Logger logger) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        final String name = logger.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        if (name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
5964
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   615
        // cleanup some Loggers that have been GC'ed
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   616
        drainLoggerRefQueueBounded();
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   617
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   618
        LoggerWeakRef ref = namedLoggers.get(name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        if (ref != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
            if (ref.get() == null) {
5964
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   621
                // It's possible that the Logger was GC'ed after the
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   622
                // drainLoggerRefQueueBounded() call above so allow
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   623
                // a new one to be registered.
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   624
                namedLoggers.remove(name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
                // We already have a registered logger with the given name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        // We're adding a new logger.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        // Note that we are creating a weak reference here.
5964
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   633
        ref = new LoggerWeakRef(logger);
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   634
        namedLoggers.put(name, ref);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        // Apply any initial level defined for the new logger.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        Level level = getLevelProperty(name+".level", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        if (level != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
            doSetLevel(logger, level);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        // Do we have a per logger handler too?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        // Note: this will add a 200ms penalty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        loadLoggerHandlers(logger, name, name+".handlers");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        processParentHandlers(logger, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        // Find the new node and its parent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
        LogNode node = findNode(name);
5964
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   649
        node.loggerRef = ref;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        Logger parent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        LogNode nodep = node.parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        while (nodep != null) {
5964
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   653
            LoggerWeakRef nodeRef = nodep.loggerRef;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
            if (nodeRef != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                parent = nodeRef.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
                if (parent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
            nodep = nodep.parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        if (parent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
            doSetParent(logger, parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        // Walk over the children and tell them we are their new parent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        node.walkAndSetParent(logger);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
5964
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   669
        // new LogNode is ready so tell the LoggerWeakRef about it
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   670
        ref.setNode(node);
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   671
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
    // Private method to set a level on a logger.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
    // If necessary, we raise privilege before doing the call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
    private static void doSetLevel(final Logger logger, final Level level) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        if (sm == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
            // There is no security manager, so things are easy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
            logger.setLevel(level);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        // There is a security manager.  Raise privilege before
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
        // calling setLevel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
        AccessController.doPrivileged(new PrivilegedAction<Object>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
            public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                logger.setLevel(level);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
            }});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
    // Private method to set a parent on a logger.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
    // If necessary, we raise privilege before doing the setParent call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
    private static void doSetParent(final Logger logger, final Logger parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        if (sm == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
            // There is no security manager, so things are easy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
            logger.setParent(parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        // There is a security manager.  Raise privilege before
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        // calling setParent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        AccessController.doPrivileged(new PrivilegedAction<Object>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
            public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
                logger.setParent(parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
            }});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
    // Find a node in our tree of logger nodes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
    // If necessary, create it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
    private LogNode findNode(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
        if (name == null || name.equals("")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
            return root;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
        LogNode node = root;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
        while (name.length() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
            int ix = name.indexOf(".");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
            String head;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
            if (ix > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
                head = name.substring(0,ix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
                name = name.substring(ix+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
                head = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
                name = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
            if (node.children == null) {
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 6675
diff changeset
   732
                node.children = new HashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
            LogNode child = node.children.get(head);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
            if (child == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
                child = new LogNode(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
                node.children.put(head, child);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
            node = child;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
        return node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
     * Method to find a named logger.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
     * Note that since untrusted code may create loggers with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
     * arbitrary names this method should not be relied on to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
     * 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
   750
     * 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
   751
     * 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
   752
     * 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
   753
     * 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
   754
     * the case where the Logger has been garbage collected.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     * @param name name of the logger
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
     * @return  matching logger or null if none is found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
    public synchronized Logger getLogger(String name) {
5964
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   760
        LoggerWeakRef ref = namedLoggers.get(name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
        if (ref == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
        Logger logger = ref.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
        if (logger == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
            // Hashtable holds stale weak reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
            // to a logger which has been GC-ed.
5964
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   768
            namedLoggers.remove(name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
        return logger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
     * Get an enumeration of known logger names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
     * Note:  Loggers may be added dynamically as new classes are loaded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
     * 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
   778
     * 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
   779
     * 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
   780
     * 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
   781
     * 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
   782
     * 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
   783
     * 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
   784
     * 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
   785
     * time since its name was returned by this method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
     * @return  enumeration of logger name strings
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
    public synchronized Enumeration<String> getLoggerNames() {
5964
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   790
        return namedLoggers.keys();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
     * Reinitialize the logging properties and reread the logging configuration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
     * The same rules are used for locating the configuration properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
     * as are used at startup.  So normally the logging properties will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
     * be re-read from the same file that was used at startup.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
     * Any log level definitions in the new configuration file will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
     * applied using Logger.setLevel(), if the target Logger exists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
     * A PropertyChangeEvent will be fired after the properties are read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     * @exception  SecurityException  if a security manager exists and if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
     *             the caller does not have LoggingPermission("control").
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
     * @exception  IOException if there are IO problems reading the configuration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
    public void readConfiguration() throws IOException, SecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
        checkAccess();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
        // if a configuration class is specified, load it and use it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
        String cname = System.getProperty("java.util.logging.config.class");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
        if (cname != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
                // Instantiate the named class.  It is its constructor's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
                // responsibility to initialize the logging configuration, by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
                // calling readConfiguration(InputStream) with a suitable stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
                try {
11274
7e7196757acd 7117249: fix warnings in java.util.jar, .logging, .prefs, .zip
smarks
parents: 9700
diff changeset
   820
                    Class<?> clz = ClassLoader.getSystemClassLoader().loadClass(cname);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
                    clz.newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
                } catch (ClassNotFoundException ex) {
11274
7e7196757acd 7117249: fix warnings in java.util.jar, .logging, .prefs, .zip
smarks
parents: 9700
diff changeset
   824
                    Class<?> clz = Thread.currentThread().getContextClassLoader().loadClass(cname);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
                    clz.newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
            } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
                System.err.println("Logging configuration class \"" + cname + "\" failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
                System.err.println("" + ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
                // keep going and useful config file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
        String fname = System.getProperty("java.util.logging.config.file");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
        if (fname == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
            fname = System.getProperty("java.home");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
            if (fname == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
                throw new Error("Can't find java.home ??");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
            File f = new File(fname, "lib");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
            f = new File(f, "logging.properties");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
            fname = f.getCanonicalPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
        InputStream in = new FileInputStream(fname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
        BufferedInputStream bin = new BufferedInputStream(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
            readConfiguration(bin);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
            if (in != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
                in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
     * Reset the logging configuration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
     * For all named loggers, the reset operation removes and closes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
     * all Handlers and (except for the root logger) sets the level
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
     * to null.  The root logger's level is set to Level.INFO.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
     * @exception  SecurityException  if a security manager exists and if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
     *             the caller does not have LoggingPermission("control").
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
    public void reset() throws SecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
        checkAccess();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
            props = new Properties();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
            // Since we are doing a reset we no longer want to initialize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
            // the global handlers, if they haven't been initialized yet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
            initializedGlobalHandlers = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
        }
11274
7e7196757acd 7117249: fix warnings in java.util.jar, .logging, .prefs, .zip
smarks
parents: 9700
diff changeset
   875
        Enumeration<String> enum_ = getLoggerNames();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
        while (enum_.hasMoreElements()) {
11274
7e7196757acd 7117249: fix warnings in java.util.jar, .logging, .prefs, .zip
smarks
parents: 9700
diff changeset
   877
            String name = enum_.nextElement();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
            resetLogger(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
    // Private method to reset an individual target logger.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
    private void resetLogger(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
        Logger logger = getLogger(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
        if (logger == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
        // Close all the Logger's handlers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
        Handler[] targets = logger.getHandlers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
        for (int i = 0; i < targets.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
            Handler h = targets[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
            logger.removeHandler(h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
                h.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
            } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
                // Problems closing a handler?  Keep going...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
        if (name != null && name.equals("")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
            // This is the root logger.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
            logger.setLevel(defaultLevel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
            logger.setLevel(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
    // get a list of whitespace separated classnames from a property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
    private String[] parseClassNames(String propertyName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
        String hands = getProperty(propertyName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
        if (hands == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
            return new String[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
        hands = hands.trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
        int ix = 0;
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 6675
diff changeset
   916
        Vector<String> result = new Vector<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
        while (ix < hands.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
            int end = ix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
            while (end < hands.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
                if (Character.isWhitespace(hands.charAt(end))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
                if (hands.charAt(end) == ',') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
                end++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
            String word = hands.substring(ix, end);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
            ix = end+1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
            word = word.trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
            if (word.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
            result.add(word);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
        return result.toArray(new String[result.size()]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
     * Reinitialize the logging properties and reread the logging configuration
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
     * from the given stream, which should be in java.util.Properties format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
     * A PropertyChangeEvent will be fired after the properties are read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
     * Any log level definitions in the new configuration file will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
     * applied using Logger.setLevel(), if the target Logger exists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
     * @param ins       stream to read properties from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
     * @exception  SecurityException  if a security manager exists and if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
     *             the caller does not have LoggingPermission("control").
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
     * @exception  IOException if there are problems reading from the stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
    public void readConfiguration(InputStream ins) throws IOException, SecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
        checkAccess();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
        reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
        // Load the properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
        props.load(ins);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
        // Instantiate new configuration objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
        String names[] = parseClassNames("config");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
        for (int i = 0; i < names.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
            String word = names[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
            try {
11274
7e7196757acd 7117249: fix warnings in java.util.jar, .logging, .prefs, .zip
smarks
parents: 9700
diff changeset
   964
                Class<?> clz = ClassLoader.getSystemClassLoader().loadClass(word);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
                clz.newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
            } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
                System.err.println("Can't load config class \"" + word + "\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
                System.err.println("" + ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
                // ex.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
        // Set levels on any pre-existing loggers, based on the new properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
        setLevelsOnExistingLoggers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
        // Notify any interested parties that our properties have changed.
13578
cb435d74ab6b 7192275: Minimize LogManager dependencies on java.beans
alanb
parents: 11274
diff changeset
   977
        // 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
   978
        // locks when calling the listeners.
cb435d74ab6b 7192275: Minimize LogManager dependencies on java.beans
alanb
parents: 11274
diff changeset
   979
        Map<PropertyChangeListener,Integer> listeners = null;
cb435d74ab6b 7192275: Minimize LogManager dependencies on java.beans
alanb
parents: 11274
diff changeset
   980
        synchronized (listenerMap) {
cb435d74ab6b 7192275: Minimize LogManager dependencies on java.beans
alanb
parents: 11274
diff changeset
   981
            if (!listenerMap.isEmpty())
cb435d74ab6b 7192275: Minimize LogManager dependencies on java.beans
alanb
parents: 11274
diff changeset
   982
                listeners = new HashMap<>(listenerMap);
cb435d74ab6b 7192275: Minimize LogManager dependencies on java.beans
alanb
parents: 11274
diff changeset
   983
        }
cb435d74ab6b 7192275: Minimize LogManager dependencies on java.beans
alanb
parents: 11274
diff changeset
   984
        if (listeners != null) {
cb435d74ab6b 7192275: Minimize LogManager dependencies on java.beans
alanb
parents: 11274
diff changeset
   985
            PropertyChangeEvent ev = new PropertyChangeEvent(LogManager.class, null, null, null);
cb435d74ab6b 7192275: Minimize LogManager dependencies on java.beans
alanb
parents: 11274
diff changeset
   986
            for (Map.Entry<PropertyChangeListener,Integer> entry : listeners.entrySet()) {
cb435d74ab6b 7192275: Minimize LogManager dependencies on java.beans
alanb
parents: 11274
diff changeset
   987
                PropertyChangeListener listener = entry.getKey();
cb435d74ab6b 7192275: Minimize LogManager dependencies on java.beans
alanb
parents: 11274
diff changeset
   988
                int count = entry.getValue().intValue();
cb435d74ab6b 7192275: Minimize LogManager dependencies on java.beans
alanb
parents: 11274
diff changeset
   989
                for (int i = 0; i < count; i++) {
cb435d74ab6b 7192275: Minimize LogManager dependencies on java.beans
alanb
parents: 11274
diff changeset
   990
                    listener.propertyChange(ev);
cb435d74ab6b 7192275: Minimize LogManager dependencies on java.beans
alanb
parents: 11274
diff changeset
   991
                }
cb435d74ab6b 7192275: Minimize LogManager dependencies on java.beans
alanb
parents: 11274
diff changeset
   992
            }
cb435d74ab6b 7192275: Minimize LogManager dependencies on java.beans
alanb
parents: 11274
diff changeset
   993
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
        // Note that we need to reinitialize global handles when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
        // they are first referenced.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
            initializedGlobalHandlers = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
     * Get the value of a logging property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
     * The method returns null if the property is not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
     * @param name      property name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
     * @return          property value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
    public String getProperty(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
        return props.getProperty(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
    // Package private method to get a String property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
    // If the property is not defined we return the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
    // default value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
    String getStringProperty(String name, String defaultValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
        String val = getProperty(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
        if (val == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
            return defaultValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
        return val.trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
    // Package private method to get an integer property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
    // If the property is not defined or cannot be parsed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
    // we return the given default value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
    int getIntProperty(String name, int defaultValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
        String val = getProperty(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
        if (val == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
            return defaultValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
            return Integer.parseInt(val.trim());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
        } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
            return defaultValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
    // Package private method to get a boolean property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
    // If the property is not defined or cannot be parsed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
    // we return the given default value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
    boolean getBooleanProperty(String name, boolean defaultValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
        String val = getProperty(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
        if (val == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
            return defaultValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
        val = val.toLowerCase();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
        if (val.equals("true") || val.equals("1")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
        } else if (val.equals("false") || val.equals("0")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
        return defaultValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
    // Package private method to get a Level property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
    // If the property is not defined or cannot be parsed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
    // we return the given default value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
    Level getLevelProperty(String name, Level defaultValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
        String val = getProperty(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
        if (val == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
            return defaultValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
            return Level.parse(val.trim());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
        } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
            return defaultValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
    // Package private method to get a filter property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
    // We return an instance of the class named by the "name"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
    // property. If the property is not defined or has problems
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
    // we return the defaultValue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
    Filter getFilterProperty(String name, Filter defaultValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
        String val = getProperty(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
            if (val != null) {
11274
7e7196757acd 7117249: fix warnings in java.util.jar, .logging, .prefs, .zip
smarks
parents: 9700
diff changeset
  1078
                Class<?> clz = ClassLoader.getSystemClassLoader().loadClass(val);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
                return (Filter) clz.newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
        } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
            // We got one of a variety of exceptions in creating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
            // class or creating an instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
            // Drop through.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
        // We got an exception.  Return the defaultValue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
        return defaultValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
    // Package private method to get a formatter property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
    // We return an instance of the class named by the "name"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
    // property. If the property is not defined or has problems
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
    // we return the defaultValue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
    Formatter getFormatterProperty(String name, Formatter defaultValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
        String val = getProperty(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
            if (val != null) {
11274
7e7196757acd 7117249: fix warnings in java.util.jar, .logging, .prefs, .zip
smarks
parents: 9700
diff changeset
  1099
                Class<?> clz = ClassLoader.getSystemClassLoader().loadClass(val);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
                return (Formatter) clz.newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
        } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
            // We got one of a variety of exceptions in creating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
            // class or creating an instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
            // Drop through.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
        // We got an exception.  Return the defaultValue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
        return defaultValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
    // Private method to load the global handlers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
    // We do the real work lazily, when the global handlers
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
    // are first used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
    private synchronized void initializeGlobalHandlers() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
        if (initializedGlobalHandlers) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
        initializedGlobalHandlers = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
        if (deathImminent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
            // Aaargh...
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
            // The VM is shutting down and our exit hook has been called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
            // Avoid allocating global handlers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
        loadLoggerHandlers(rootLogger, null, "handlers");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
    private Permission ourPermission = new LoggingPermission("control", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
     * Check that the current context is trusted to modify the logging
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
     * configuration.  This requires LoggingPermission("control").
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
     * If the check fails we throw a SecurityException, otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
     * we return normally.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
     * @exception  SecurityException  if a security manager exists and if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
     *             the caller does not have LoggingPermission("control").
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
    public void checkAccess() throws SecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
        if (sm == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
        sm.checkPermission(ourPermission);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
    // Nested class to represent a node in our tree of named loggers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
    private static class LogNode {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
        HashMap<String,LogNode> children;
5964
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1154
        LoggerWeakRef loggerRef;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
        LogNode parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
        LogNode(LogNode parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
            this.parent = parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
        // Recursive method to walk the tree below a node and set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
        // a new parent logger.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
        void walkAndSetParent(Logger parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
            if (children == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
            Iterator<LogNode> values = children.values().iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
            while (values.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
                LogNode node = values.next();
5964
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1170
                LoggerWeakRef ref = node.loggerRef;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
                Logger logger = (ref == null) ? null : ref.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
                if (logger == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
                    node.walkAndSetParent(parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
                    doSetParent(logger, parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
    // We use a subclass of Logger for the root logger, so
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
    // that we only instantiate the global handlers when they
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
    // are first needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
    private class RootLogger extends Logger {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
        private RootLogger() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
            super("", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
            setLevel(defaultLevel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
        public void log(LogRecord record) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
            // Make sure that the global handlers have been instantiated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
            initializeGlobalHandlers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
            super.log(record);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
        public void addHandler(Handler h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
            initializeGlobalHandlers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
            super.addHandler(h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
        public void removeHandler(Handler h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
            initializeGlobalHandlers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
            super.removeHandler(h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
        public Handler[] getHandlers() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
            initializeGlobalHandlers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
            return super.getHandlers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
    // Private method to be called when the configuration has
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
    // changed to apply any level settings to any pre-existing loggers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
    synchronized private void setLevelsOnExistingLoggers() {
11274
7e7196757acd 7117249: fix warnings in java.util.jar, .logging, .prefs, .zip
smarks
parents: 9700
diff changeset
  1217
        Enumeration<?> enum_ = props.propertyNames();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
        while (enum_.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
            String key = (String)enum_.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
            if (!key.endsWith(".level")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
                // Not a level definition.
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
            int ix = key.length() - 6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
            String name = key.substring(0, ix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
            Level level = getLevelProperty(key, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
            if (level == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
                System.err.println("Bad level value for property: " + key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
            Logger l = getLogger(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
            if (l == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
            l.setLevel(level);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
    // Management Support
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
    private static LoggingMXBean loggingMXBean = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
     * String representation of the
9013
eedac0b9f552 7024172: Move BufferPoolMXBean and PlatformLoggingMXBean java.lang.management
mchung
parents: 7803
diff changeset
  1243
     * {@link javax.management.ObjectName} for the management interface
eedac0b9f552 7024172: Move BufferPoolMXBean and PlatformLoggingMXBean java.lang.management
mchung
parents: 7803
diff changeset
  1244
     * for the logging facility.
eedac0b9f552 7024172: Move BufferPoolMXBean and PlatformLoggingMXBean java.lang.management
mchung
parents: 7803
diff changeset
  1245
     *
eedac0b9f552 7024172: Move BufferPoolMXBean and PlatformLoggingMXBean java.lang.management
mchung
parents: 7803
diff changeset
  1246
     * @see java.lang.management.PlatformLoggingMXBean
eedac0b9f552 7024172: Move BufferPoolMXBean and PlatformLoggingMXBean java.lang.management
mchung
parents: 7803
diff changeset
  1247
     * @see java.util.logging.LoggingMXBean
eedac0b9f552 7024172: Move BufferPoolMXBean and PlatformLoggingMXBean java.lang.management
mchung
parents: 7803
diff changeset
  1248
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
    public final static String LOGGING_MXBEAN_NAME
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
        = "java.util.logging:type=Logging";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
     * Returns <tt>LoggingMXBean</tt> for managing loggers.
9013
eedac0b9f552 7024172: Move BufferPoolMXBean and PlatformLoggingMXBean java.lang.management
mchung
parents: 7803
diff changeset
  1256
     * An alternative way to manage loggers is through the
eedac0b9f552 7024172: Move BufferPoolMXBean and PlatformLoggingMXBean java.lang.management
mchung
parents: 7803
diff changeset
  1257
     * {@link java.lang.management.PlatformLoggingMXBean} interface
eedac0b9f552 7024172: Move BufferPoolMXBean and PlatformLoggingMXBean java.lang.management
mchung
parents: 7803
diff changeset
  1258
     * that can be obtained by calling:
4172
bceea7bc12b7 6876135: Add PlatformLoggingMXBean to eliminate the dependency on JMX from logging
mchung
parents: 3861
diff changeset
  1259
     * <pre>
9013
eedac0b9f552 7024172: Move BufferPoolMXBean and PlatformLoggingMXBean java.lang.management
mchung
parents: 7803
diff changeset
  1260
     *     PlatformLoggingMXBean logging = {@link java.lang.management.ManagementFactory#getPlatformMXBean(Class)
eedac0b9f552 7024172: Move BufferPoolMXBean and PlatformLoggingMXBean java.lang.management
mchung
parents: 7803
diff changeset
  1261
     *         ManagementFactory.getPlatformMXBean}(PlatformLoggingMXBean.class);
4172
bceea7bc12b7 6876135: Add PlatformLoggingMXBean to eliminate the dependency on JMX from logging
mchung
parents: 3861
diff changeset
  1262
     * </pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
     * @return a {@link LoggingMXBean} object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
     *
9013
eedac0b9f552 7024172: Move BufferPoolMXBean and PlatformLoggingMXBean java.lang.management
mchung
parents: 7803
diff changeset
  1266
     * @see java.lang.management.PlatformLoggingMXBean
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
     */
9013
eedac0b9f552 7024172: Move BufferPoolMXBean and PlatformLoggingMXBean java.lang.management
mchung
parents: 7803
diff changeset
  1269
    public static synchronized LoggingMXBean getLoggingMXBean() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
        if (loggingMXBean == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
            loggingMXBean =  new Logging();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
        return loggingMXBean;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
}