jdk/src/share/classes/java/util/logging/LogManager.java
author alanb
Sat, 08 Sep 2012 20:31:42 +0100
changeset 14216 23714b376286
parent 13578 cb435d74ab6b
child 16098 9001e536ab4e
child 14229 40fbffe104bd
permissions -rw-r--r--
7169884: LogManager checks do not work correctly for sub-types Reviewed-by: mchung, ahgross
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.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    public void addPropertyChangeListener(PropertyChangeListener l) throws SecurityException {
13578
cb435d74ab6b 7192275: Minimize LogManager dependencies on java.beans
alanb
parents: 11274
diff changeset
   316
        PropertyChangeListener listener = Objects.requireNonNull(l);
14216
23714b376286 7169884: LogManager checks do not work correctly for sub-types
alanb
parents: 13578
diff changeset
   317
        checkPermission();
13578
cb435d74ab6b 7192275: Minimize LogManager dependencies on java.beans
alanb
parents: 11274
diff changeset
   318
        synchronized (listenerMap) {
cb435d74ab6b 7192275: Minimize LogManager dependencies on java.beans
alanb
parents: 11274
diff changeset
   319
            // increment the registration count if already registered
cb435d74ab6b 7192275: Minimize LogManager dependencies on java.beans
alanb
parents: 11274
diff changeset
   320
            Integer value = listenerMap.get(listener);
cb435d74ab6b 7192275: Minimize LogManager dependencies on java.beans
alanb
parents: 11274
diff changeset
   321
            value = (value == null) ? 1 : (value + 1);
cb435d74ab6b 7192275: Minimize LogManager dependencies on java.beans
alanb
parents: 11274
diff changeset
   322
            listenerMap.put(listener, value);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * Removes an event listener for property change events.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * If the same listener instance has been added to the listener table
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * through multiple invocations of <CODE>addPropertyChangeListener</CODE>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * then an equivalent number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * <CODE>removePropertyChangeListener</CODE> invocations are required to remove
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * all instances of that listener from the listener table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * Returns silently if the given listener is not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * @param l  event listener (can be null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * @exception  SecurityException  if a security manager exists and if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     *             the caller does not have LoggingPermission("control").
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    public void removePropertyChangeListener(PropertyChangeListener l) throws SecurityException {
14216
23714b376286 7169884: LogManager checks do not work correctly for sub-types
alanb
parents: 13578
diff changeset
   341
        checkPermission();
13578
cb435d74ab6b 7192275: Minimize LogManager dependencies on java.beans
alanb
parents: 11274
diff changeset
   342
        if (l != null) {
cb435d74ab6b 7192275: Minimize LogManager dependencies on java.beans
alanb
parents: 11274
diff changeset
   343
            PropertyChangeListener listener = l;
cb435d74ab6b 7192275: Minimize LogManager dependencies on java.beans
alanb
parents: 11274
diff changeset
   344
            synchronized (listenerMap) {
cb435d74ab6b 7192275: Minimize LogManager dependencies on java.beans
alanb
parents: 11274
diff changeset
   345
                Integer value = listenerMap.get(listener);
cb435d74ab6b 7192275: Minimize LogManager dependencies on java.beans
alanb
parents: 11274
diff changeset
   346
                if (value != null) {
cb435d74ab6b 7192275: Minimize LogManager dependencies on java.beans
alanb
parents: 11274
diff changeset
   347
                    // remove from map if registration count is 1, otherwise
cb435d74ab6b 7192275: Minimize LogManager dependencies on java.beans
alanb
parents: 11274
diff changeset
   348
                    // just decrement its count
cb435d74ab6b 7192275: Minimize LogManager dependencies on java.beans
alanb
parents: 11274
diff changeset
   349
                    int i = value.intValue();
cb435d74ab6b 7192275: Minimize LogManager dependencies on java.beans
alanb
parents: 11274
diff changeset
   350
                    if (i == 1) {
cb435d74ab6b 7192275: Minimize LogManager dependencies on java.beans
alanb
parents: 11274
diff changeset
   351
                        listenerMap.remove(listener);
cb435d74ab6b 7192275: Minimize LogManager dependencies on java.beans
alanb
parents: 11274
diff changeset
   352
                    } else {
cb435d74ab6b 7192275: Minimize LogManager dependencies on java.beans
alanb
parents: 11274
diff changeset
   353
                        assert i > 1;
cb435d74ab6b 7192275: Minimize LogManager dependencies on java.beans
alanb
parents: 11274
diff changeset
   354
                        listenerMap.put(listener, i - 1);
cb435d74ab6b 7192275: Minimize LogManager dependencies on java.beans
alanb
parents: 11274
diff changeset
   355
                    }
cb435d74ab6b 7192275: Minimize LogManager dependencies on java.beans
alanb
parents: 11274
diff changeset
   356
                }
cb435d74ab6b 7192275: Minimize LogManager dependencies on java.beans
alanb
parents: 11274
diff changeset
   357
            }
cb435d74ab6b 7192275: Minimize LogManager dependencies on java.beans
alanb
parents: 11274
diff changeset
   358
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    // Package-level method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    // Find or create a specified logger instance. If a logger has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    // already been created with the given name it is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    // Otherwise a new logger instance is created and registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    // 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
   366
fdbacf68f185 7016208: 4/3 null sometimes returned by java.util.logging.Logger.getLogger(String name) in -server -Xcomp
dcubed
parents: 9013
diff changeset
   367
    // 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
   368
    // 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
   369
    // 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
   370
    Logger demandLogger(String name) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        Logger result = getLogger(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        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
   373
            // 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
   374
            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
   375
            do {
fdbacf68f185 7016208: 4/3 null sometimes returned by java.util.logging.Logger.getLogger(String name) in -server -Xcomp
dcubed
parents: 9013
diff changeset
   376
                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
   377
                    // 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
   378
                    // 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
   379
                    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
   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
fdbacf68f185 7016208: 4/3 null sometimes returned by java.util.logging.Logger.getLogger(String name) in -server -Xcomp
dcubed
parents: 9013
diff changeset
   382
                // 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
   383
                // 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
   384
                // 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
   385
                // 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
   386
                // 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
   387
                // 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
   388
                // 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
   389
                // 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
   390
                // 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
   391
                // 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
   392
                // 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
   393
                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
   394
            } while (result == null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    // If logger.getUseParentHandlers() returns 'true' and any of the logger's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    // parents have levels or handlers defined, make sure they are instantiated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    private void processParentHandlers(Logger logger, String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        int ix = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            int ix2 = name.indexOf(".", ix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            if (ix2 < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            String pname = name.substring(0,ix2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            if (getProperty(pname+".level")    != null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                getProperty(pname+".handlers") != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                // This pname has a level/handlers definition.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                // Make sure it exists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                demandLogger(pname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            ix = ix2+1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    // Add new per logger handlers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    // We need to raise privilege here. All our decisions will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    // be made based on the logging configuration, which can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    // only be modified by trusted code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    private void loadLoggerHandlers(final Logger logger, final String name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                                    final String handlersPropertyName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        AccessController.doPrivileged(new PrivilegedAction<Object>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                if (logger != rootLogger) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                    boolean useParent = getBooleanProperty(name + ".useParentHandlers", true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                    if (!useParent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                        logger.setUseParentHandlers(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                String names[] = parseClassNames(handlersPropertyName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                for (int i = 0; i < names.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                    String word = names[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                    try {
11274
7e7196757acd 7117249: fix warnings in java.util.jar, .logging, .prefs, .zip
smarks
parents: 9700
diff changeset
   439
                        Class<?> clz = ClassLoader.getSystemClassLoader().loadClass(word);
7e7196757acd 7117249: fix warnings in java.util.jar, .logging, .prefs, .zip
smarks
parents: 9700
diff changeset
   440
                        Handler  hdl = (Handler) clz.newInstance();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                            // Check if there is a property defining the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                            // this handler's level.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                            String levs = getProperty(word + ".level");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                            if (levs != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                                hdl.setLevel(Level.parse(levs));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                        } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                            System.err.println("Can't set level for " + word);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                            // Probably a bad level. Drop through.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                        // Add this Handler to the logger
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                        logger.addHandler(hdl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                    } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                        System.err.println("Can't load log handler \"" + word + "\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                        System.err.println("" + ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                        ex.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            }});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
5964
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   464
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   465
    // loggerRefQueue holds LoggerWeakRef objects for Logger objects
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   466
    // that have been GC'ed.
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   467
    private final ReferenceQueue<Logger> loggerRefQueue
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 6675
diff changeset
   468
        = new ReferenceQueue<>();
5964
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   469
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   470
    // Package-level inner class.
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   471
    // 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
   472
    //
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   473
    // LogManager.namedLoggers
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   474
    //     - has weak references to all named Loggers
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   475
    //     - 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
   476
    //       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
   477
    //       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
   478
    // LogManager.LogNode.loggerRef
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   479
    //     - 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
   480
    //     - 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
   481
    //       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
   482
    // Logger.kids
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   483
    //     - 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
   484
    //       includes anonymous and named Loggers
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   485
    //     - 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
   486
    //       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
   487
    //       LoggerWeakRef objects for the anonymous Loggers around
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   488
    //       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
   489
    //
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   490
    final class LoggerWeakRef extends WeakReference<Logger> {
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   491
        private String                name;       // for namedLoggers cleanup
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   492
        private LogNode               node;       // for loggerRef cleanup
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   493
        private WeakReference<Logger> parentRef;  // for kids cleanup
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   494
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   495
        LoggerWeakRef(Logger logger) {
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   496
            super(logger, loggerRefQueue);
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   497
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   498
            name = logger.getName();  // save for namedLoggers cleanup
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   499
        }
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   500
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   501
        // dispose of this LoggerWeakRef object
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   502
        void dispose() {
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   503
            if (node != null) {
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   504
                // 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
   505
                // so clear namedLoggers weak ref to us
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   506
                manager.namedLoggers.remove(name);
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   507
                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
   508
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   509
                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
   510
                node = null;            // clear our ref to LogNode
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
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   513
            if (parentRef != null) {
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   514
                // 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
   515
                Logger parent = parentRef.get();
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   516
                if (parent != null) {
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   517
                    // 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
   518
                    // parent Logger's weak ref to us
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   519
                    parent.removeChildLogger(this);
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   520
                }
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   521
                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
   522
            }
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   523
        }
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   524
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   525
        // 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
   526
        void setNode(LogNode node) {
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   527
            this.node = node;
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   528
        }
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   529
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   530
        // 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
   531
        void setParentRef(WeakReference<Logger> parentRef) {
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   532
            this.parentRef = parentRef;
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   533
        }
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
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   536
    // Package-level method.
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   537
    // 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
   538
    //
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   539
    // drainLoggerRefQueueBounded() is called by addLogger() below
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   540
    // 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
   541
    // 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
   542
    //
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   543
    // 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
   544
    // 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
   545
    // 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
   546
    // 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
   547
    //   - test duration 1 minute
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   548
    //   - sample size of 125 sets of 400
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   549
    //   - average: 1.99 ms
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   550
    //   - minimum: 0.57 ms
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   551
    //   - maximum: 25.3 ms
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
    // 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
   554
    // 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
   555
    // 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
   556
    //   - test duration 2 minutes
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   557
    //   - sample size of 506 sets of 400
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   558
    //   - average: 0.57 ms
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   559
    //   - minimum: 0.02 ms
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   560
    //   - maximum: 10.9 ms
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   561
    //
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   562
    private final static int MAX_ITERATIONS = 400;
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   563
    final synchronized void drainLoggerRefQueueBounded() {
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   564
        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
   565
            if (loggerRefQueue == null) {
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   566
                // haven't finished loading LogManager yet
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   567
                break;
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   568
            }
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   569
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   570
            LoggerWeakRef ref = (LoggerWeakRef) loggerRefQueue.poll();
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   571
            if (ref == null) {
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   572
                break;
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   573
            }
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   574
            // 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
   575
            ref.dispose();
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   576
        }
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   577
    }
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   578
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * Add a named logger.  This does nothing and returns false if a logger
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * with the same name is already registered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * The Logger factory methods call this method to register each
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * newly created Logger.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * The application should retain its own reference to the Logger
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     * object to avoid it being garbage collected.  The LogManager
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     * may only retain a weak reference.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     * @param   logger the new logger.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     * @return  true if the argument logger was registered successfully,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     *          false if a logger of that name already exists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     * @exception NullPointerException if the logger name is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
    public synchronized boolean addLogger(Logger logger) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        final String name = logger.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        if (name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
5964
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   601
        // 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
   602
        drainLoggerRefQueueBounded();
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   603
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   604
        LoggerWeakRef ref = namedLoggers.get(name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        if (ref != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
            if (ref.get() == null) {
5964
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   607
                // 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
   608
                // drainLoggerRefQueueBounded() call above so allow
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   609
                // a new one to be registered.
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   610
                namedLoggers.remove(name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
                // We already have a registered logger with the given name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        // We're adding a new logger.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        // 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
   619
        ref = new LoggerWeakRef(logger);
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   620
        namedLoggers.put(name, ref);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        // Apply any initial level defined for the new logger.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        Level level = getLevelProperty(name+".level", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        if (level != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
            doSetLevel(logger, level);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        // Do we have a per logger handler too?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        // Note: this will add a 200ms penalty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        loadLoggerHandlers(logger, name, name+".handlers");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        processParentHandlers(logger, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
        // Find the new node and its parent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        LogNode node = findNode(name);
5964
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   635
        node.loggerRef = ref;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        Logger parent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        LogNode nodep = node.parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        while (nodep != null) {
5964
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   639
            LoggerWeakRef nodeRef = nodep.loggerRef;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
            if (nodeRef != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
                parent = nodeRef.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                if (parent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
            nodep = nodep.parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        if (parent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
            doSetParent(logger, parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        // Walk over the children and tell them we are their new parent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
        node.walkAndSetParent(logger);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
5964
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   655
        // 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
   656
        ref.setNode(node);
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   657
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
    // Private method to set a level on a logger.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
    // If necessary, we raise privilege before doing the call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
    private static void doSetLevel(final Logger logger, final Level level) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        if (sm == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
            // There is no security manager, so things are easy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
            logger.setLevel(level);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        // There is a security manager.  Raise privilege before
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        // calling setLevel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
        AccessController.doPrivileged(new PrivilegedAction<Object>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
            public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
                logger.setLevel(level);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
            }});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
    // Private method to set a parent on a logger.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
    // If necessary, we raise privilege before doing the setParent call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
    private static void doSetParent(final Logger logger, final Logger parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
        if (sm == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
            // There is no security manager, so things are easy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
            logger.setParent(parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        // There is a security manager.  Raise privilege before
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
        // calling setParent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        AccessController.doPrivileged(new PrivilegedAction<Object>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
            public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
                logger.setParent(parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
            }});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
    // Find a node in our tree of logger nodes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
    // If necessary, create it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
    private LogNode findNode(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        if (name == null || name.equals("")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
            return root;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        LogNode node = root;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        while (name.length() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
            int ix = name.indexOf(".");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
            String head;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
            if (ix > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
                head = name.substring(0,ix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
                name = name.substring(ix+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
                head = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
                name = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
            if (node.children == null) {
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 6675
diff changeset
   718
                node.children = new HashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
            LogNode child = node.children.get(head);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
            if (child == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
                child = new LogNode(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
                node.children.put(head, child);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
            node = child;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
        return node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
     * Method to find a named logger.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
     * Note that since untrusted code may create loggers with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
     * arbitrary names this method should not be relied on to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
     * 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
   736
     * 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
   737
     * 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
   738
     * 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
   739
     * 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
   740
     * the case where the Logger has been garbage collected.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
     * @param name name of the logger
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
     * @return  matching logger or null if none is found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
    public synchronized Logger getLogger(String name) {
5964
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   746
        LoggerWeakRef ref = namedLoggers.get(name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        if (ref == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
        Logger logger = ref.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
        if (logger == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
            // Hashtable holds stale weak reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
            // 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
   754
            namedLoggers.remove(name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        return logger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
     * Get an enumeration of known logger names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
     * Note:  Loggers may be added dynamically as new classes are loaded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
     * 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
   764
     * 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
   765
     * 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
   766
     * 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
   767
     * 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
   768
     * 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
   769
     * 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
   770
     * 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
   771
     * time since its name was returned by this method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
     * @return  enumeration of logger name strings
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
    public synchronized Enumeration<String> getLoggerNames() {
5964
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
   776
        return namedLoggers.keys();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
     * Reinitialize the logging properties and reread the logging configuration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
     * The same rules are used for locating the configuration properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
     * as are used at startup.  So normally the logging properties will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
     * be re-read from the same file that was used at startup.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
     * Any log level definitions in the new configuration file will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
     * applied using Logger.setLevel(), if the target Logger exists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
     * A PropertyChangeEvent will be fired after the properties are read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
     * @exception  SecurityException  if a security manager exists and if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
     *             the caller does not have LoggingPermission("control").
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
     * @exception  IOException if there are IO problems reading the configuration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
    public void readConfiguration() throws IOException, SecurityException {
14216
23714b376286 7169884: LogManager checks do not work correctly for sub-types
alanb
parents: 13578
diff changeset
   796
        checkPermission();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
        // if a configuration class is specified, load it and use it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
        String cname = System.getProperty("java.util.logging.config.class");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
        if (cname != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
                // Instantiate the named class.  It is its constructor's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
                // responsibility to initialize the logging configuration, by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
                // calling readConfiguration(InputStream) with a suitable stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
                try {
11274
7e7196757acd 7117249: fix warnings in java.util.jar, .logging, .prefs, .zip
smarks
parents: 9700
diff changeset
   806
                    Class<?> clz = ClassLoader.getSystemClassLoader().loadClass(cname);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
                    clz.newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
                } catch (ClassNotFoundException ex) {
11274
7e7196757acd 7117249: fix warnings in java.util.jar, .logging, .prefs, .zip
smarks
parents: 9700
diff changeset
   810
                    Class<?> clz = Thread.currentThread().getContextClassLoader().loadClass(cname);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
                    clz.newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
            } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
                System.err.println("Logging configuration class \"" + cname + "\" failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
                System.err.println("" + ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
                // keep going and useful config file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
        String fname = System.getProperty("java.util.logging.config.file");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
        if (fname == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
            fname = System.getProperty("java.home");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
            if (fname == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
                throw new Error("Can't find java.home ??");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
            File f = new File(fname, "lib");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
            f = new File(f, "logging.properties");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
            fname = f.getCanonicalPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
        InputStream in = new FileInputStream(fname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        BufferedInputStream bin = new BufferedInputStream(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
            readConfiguration(bin);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
            if (in != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
                in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
     * Reset the logging configuration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
     * For all named loggers, the reset operation removes and closes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
     * all Handlers and (except for the root logger) sets the level
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
     * to null.  The root logger's level is set to Level.INFO.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
     * @exception  SecurityException  if a security manager exists and if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
     *             the caller does not have LoggingPermission("control").
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
    public void reset() throws SecurityException {
14216
23714b376286 7169884: LogManager checks do not work correctly for sub-types
alanb
parents: 13578
diff changeset
   854
        checkPermission();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
            props = new Properties();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
            // Since we are doing a reset we no longer want to initialize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
            // the global handlers, if they haven't been initialized yet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
            initializedGlobalHandlers = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
        }
11274
7e7196757acd 7117249: fix warnings in java.util.jar, .logging, .prefs, .zip
smarks
parents: 9700
diff changeset
   861
        Enumeration<String> enum_ = getLoggerNames();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
        while (enum_.hasMoreElements()) {
11274
7e7196757acd 7117249: fix warnings in java.util.jar, .logging, .prefs, .zip
smarks
parents: 9700
diff changeset
   863
            String name = enum_.nextElement();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
            resetLogger(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
    // Private method to reset an individual target logger.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
    private void resetLogger(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
        Logger logger = getLogger(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
        if (logger == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
        // Close all the Logger's handlers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
        Handler[] targets = logger.getHandlers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
        for (int i = 0; i < targets.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
            Handler h = targets[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
            logger.removeHandler(h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
                h.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
            } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
                // Problems closing a handler?  Keep going...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
        if (name != null && name.equals("")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
            // This is the root logger.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
            logger.setLevel(defaultLevel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
            logger.setLevel(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
    // get a list of whitespace separated classnames from a property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
    private String[] parseClassNames(String propertyName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
        String hands = getProperty(propertyName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
        if (hands == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
            return new String[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
        hands = hands.trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
        int ix = 0;
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 6675
diff changeset
   902
        Vector<String> result = new Vector<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
        while (ix < hands.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
            int end = ix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
            while (end < hands.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
                if (Character.isWhitespace(hands.charAt(end))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
                if (hands.charAt(end) == ',') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
                end++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
            String word = hands.substring(ix, end);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
            ix = end+1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
            word = word.trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
            if (word.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
            result.add(word);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
        return result.toArray(new String[result.size()]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
     * Reinitialize the logging properties and reread the logging configuration
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
     * from the given stream, which should be in java.util.Properties format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
     * A PropertyChangeEvent will be fired after the properties are read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
     * Any log level definitions in the new configuration file will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
     * applied using Logger.setLevel(), if the target Logger exists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
     * @param ins       stream to read properties from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
     * @exception  SecurityException  if a security manager exists and if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
     *             the caller does not have LoggingPermission("control").
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
     * @exception  IOException if there are problems reading from the stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
    public void readConfiguration(InputStream ins) throws IOException, SecurityException {
14216
23714b376286 7169884: LogManager checks do not work correctly for sub-types
alanb
parents: 13578
diff changeset
   939
        checkPermission();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
        reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
        // Load the properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
        props.load(ins);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
        // Instantiate new configuration objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
        String names[] = parseClassNames("config");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
        for (int i = 0; i < names.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
            String word = names[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
            try {
11274
7e7196757acd 7117249: fix warnings in java.util.jar, .logging, .prefs, .zip
smarks
parents: 9700
diff changeset
   950
                Class<?> clz = ClassLoader.getSystemClassLoader().loadClass(word);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
                clz.newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
            } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
                System.err.println("Can't load config class \"" + word + "\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
                System.err.println("" + ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
                // ex.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
        // Set levels on any pre-existing loggers, based on the new properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
        setLevelsOnExistingLoggers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
        // Notify any interested parties that our properties have changed.
13578
cb435d74ab6b 7192275: Minimize LogManager dependencies on java.beans
alanb
parents: 11274
diff changeset
   963
        // 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
   964
        // locks when calling the listeners.
cb435d74ab6b 7192275: Minimize LogManager dependencies on java.beans
alanb
parents: 11274
diff changeset
   965
        Map<PropertyChangeListener,Integer> listeners = null;
cb435d74ab6b 7192275: Minimize LogManager dependencies on java.beans
alanb
parents: 11274
diff changeset
   966
        synchronized (listenerMap) {
cb435d74ab6b 7192275: Minimize LogManager dependencies on java.beans
alanb
parents: 11274
diff changeset
   967
            if (!listenerMap.isEmpty())
cb435d74ab6b 7192275: Minimize LogManager dependencies on java.beans
alanb
parents: 11274
diff changeset
   968
                listeners = new HashMap<>(listenerMap);
cb435d74ab6b 7192275: Minimize LogManager dependencies on java.beans
alanb
parents: 11274
diff changeset
   969
        }
cb435d74ab6b 7192275: Minimize LogManager dependencies on java.beans
alanb
parents: 11274
diff changeset
   970
        if (listeners != null) {
cb435d74ab6b 7192275: Minimize LogManager dependencies on java.beans
alanb
parents: 11274
diff changeset
   971
            PropertyChangeEvent ev = new PropertyChangeEvent(LogManager.class, null, null, null);
cb435d74ab6b 7192275: Minimize LogManager dependencies on java.beans
alanb
parents: 11274
diff changeset
   972
            for (Map.Entry<PropertyChangeListener,Integer> entry : listeners.entrySet()) {
cb435d74ab6b 7192275: Minimize LogManager dependencies on java.beans
alanb
parents: 11274
diff changeset
   973
                PropertyChangeListener listener = entry.getKey();
cb435d74ab6b 7192275: Minimize LogManager dependencies on java.beans
alanb
parents: 11274
diff changeset
   974
                int count = entry.getValue().intValue();
cb435d74ab6b 7192275: Minimize LogManager dependencies on java.beans
alanb
parents: 11274
diff changeset
   975
                for (int i = 0; i < count; i++) {
cb435d74ab6b 7192275: Minimize LogManager dependencies on java.beans
alanb
parents: 11274
diff changeset
   976
                    listener.propertyChange(ev);
cb435d74ab6b 7192275: Minimize LogManager dependencies on java.beans
alanb
parents: 11274
diff changeset
   977
                }
cb435d74ab6b 7192275: Minimize LogManager dependencies on java.beans
alanb
parents: 11274
diff changeset
   978
            }
cb435d74ab6b 7192275: Minimize LogManager dependencies on java.beans
alanb
parents: 11274
diff changeset
   979
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
        // Note that we need to reinitialize global handles when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
        // they are first referenced.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
            initializedGlobalHandlers = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
     * Get the value of a logging property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
     * The method returns null if the property is not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
     * @param name      property name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
     * @return          property value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
    public String getProperty(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
        return props.getProperty(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
    // Package private method to get a String property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
    // If the property is not defined we return the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
    // default value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
    String getStringProperty(String name, String defaultValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
        String val = getProperty(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
        if (val == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
            return defaultValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
        return val.trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
    // Package private method to get an integer property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
    // If the property is not defined or cannot be parsed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
    // we return the given default value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
    int getIntProperty(String name, int defaultValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
        String val = getProperty(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
        if (val == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
            return defaultValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
            return Integer.parseInt(val.trim());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
        } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
            return defaultValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
    // Package private method to get a boolean property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
    // If the property is not defined or cannot be parsed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
    // we return the given default value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
    boolean getBooleanProperty(String name, boolean defaultValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
        String val = getProperty(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
        if (val == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
            return defaultValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
        val = val.toLowerCase();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
        if (val.equals("true") || val.equals("1")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
        } else if (val.equals("false") || val.equals("0")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
        return defaultValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
    // Package private method to get a Level property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
    // If the property is not defined or cannot be parsed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
    // we return the given default value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
    Level getLevelProperty(String name, Level defaultValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
        String val = getProperty(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
        if (val == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
            return defaultValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
            return Level.parse(val.trim());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
        } catch (Exception ex) {
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
    // Package private method to get a filter property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
    // We return an instance of the class named by the "name"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
    // property. If the property is not defined or has problems
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
    // we return the defaultValue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
    Filter getFilterProperty(String name, Filter defaultValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
        String val = getProperty(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
            if (val != null) {
11274
7e7196757acd 7117249: fix warnings in java.util.jar, .logging, .prefs, .zip
smarks
parents: 9700
diff changeset
  1064
                Class<?> clz = ClassLoader.getSystemClassLoader().loadClass(val);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
                return (Filter) clz.newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
        } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
            // We got one of a variety of exceptions in creating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
            // class or creating an instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
            // Drop through.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
        // We got an exception.  Return the defaultValue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
        return defaultValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
    // Package private method to get a formatter property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
    // We return an instance of the class named by the "name"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
    // property. If the property is not defined or has problems
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
    // we return the defaultValue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
    Formatter getFormatterProperty(String name, Formatter defaultValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
        String val = getProperty(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
            if (val != null) {
11274
7e7196757acd 7117249: fix warnings in java.util.jar, .logging, .prefs, .zip
smarks
parents: 9700
diff changeset
  1085
                Class<?> clz = ClassLoader.getSystemClassLoader().loadClass(val);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
                return (Formatter) clz.newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
        } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
            // We got one of a variety of exceptions in creating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
            // class or creating an instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
            // Drop through.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
        // We got an exception.  Return the defaultValue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
        return defaultValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
    // Private method to load the global handlers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
    // We do the real work lazily, when the global handlers
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
    // are first used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
    private synchronized void initializeGlobalHandlers() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
        if (initializedGlobalHandlers) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
        initializedGlobalHandlers = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
        if (deathImminent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
            // Aaargh...
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
            // The VM is shutting down and our exit hook has been called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
            // Avoid allocating global handlers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
        loadLoggerHandlers(rootLogger, null, "handlers");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
14216
23714b376286 7169884: LogManager checks do not work correctly for sub-types
alanb
parents: 13578
diff changeset
  1116
    private final Permission controlPermission = new LoggingPermission("control", null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
14216
23714b376286 7169884: LogManager checks do not work correctly for sub-types
alanb
parents: 13578
diff changeset
  1118
    void checkPermission() {
23714b376286 7169884: LogManager checks do not work correctly for sub-types
alanb
parents: 13578
diff changeset
  1119
        SecurityManager sm = System.getSecurityManager();
23714b376286 7169884: LogManager checks do not work correctly for sub-types
alanb
parents: 13578
diff changeset
  1120
        if (sm != null)
23714b376286 7169884: LogManager checks do not work correctly for sub-types
alanb
parents: 13578
diff changeset
  1121
            sm.checkPermission(controlPermission);
23714b376286 7169884: LogManager checks do not work correctly for sub-types
alanb
parents: 13578
diff changeset
  1122
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
     * Check that the current context is trusted to modify the logging
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
     * configuration.  This requires LoggingPermission("control").
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
     * If the check fails we throw a SecurityException, otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
     * we return normally.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
     * @exception  SecurityException  if a security manager exists and if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
     *             the caller does not have LoggingPermission("control").
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
    public void checkAccess() throws SecurityException {
14216
23714b376286 7169884: LogManager checks do not work correctly for sub-types
alanb
parents: 13578
diff changeset
  1135
        checkPermission();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
    // Nested class to represent a node in our tree of named loggers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
    private static class LogNode {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
        HashMap<String,LogNode> children;
5964
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1141
        LoggerWeakRef loggerRef;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
        LogNode parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
        LogNode(LogNode parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
            this.parent = parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
        // Recursive method to walk the tree below a node and set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
        // a new parent logger.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
        void walkAndSetParent(Logger parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
            if (children == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
            Iterator<LogNode> values = children.values().iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
            while (values.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
                LogNode node = values.next();
5964
0496aa46ae9f 6942989: 2/2 Memory leak of java.lang.ref.WeakReference objects
dcubed
parents: 5506
diff changeset
  1157
                LoggerWeakRef ref = node.loggerRef;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
                Logger logger = (ref == null) ? null : ref.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
                if (logger == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
                    node.walkAndSetParent(parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
                    doSetParent(logger, parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
    // We use a subclass of Logger for the root logger, so
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
    // that we only instantiate the global handlers when they
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
    // are first needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
    private class RootLogger extends Logger {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
        private RootLogger() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
            super("", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
            setLevel(defaultLevel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
        public void log(LogRecord record) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
            // Make sure that the global handlers have been instantiated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
            initializeGlobalHandlers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
            super.log(record);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
        public void addHandler(Handler h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
            initializeGlobalHandlers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
            super.addHandler(h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
        public void removeHandler(Handler h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
            initializeGlobalHandlers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
            super.removeHandler(h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
        public Handler[] getHandlers() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
            initializeGlobalHandlers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
            return super.getHandlers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
    // Private method to be called when the configuration has
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
    // changed to apply any level settings to any pre-existing loggers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
    synchronized private void setLevelsOnExistingLoggers() {
11274
7e7196757acd 7117249: fix warnings in java.util.jar, .logging, .prefs, .zip
smarks
parents: 9700
diff changeset
  1204
        Enumeration<?> enum_ = props.propertyNames();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
        while (enum_.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
            String key = (String)enum_.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
            if (!key.endsWith(".level")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
                // Not a level definition.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
            int ix = key.length() - 6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
            String name = key.substring(0, ix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
            Level level = getLevelProperty(key, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
            if (level == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
                System.err.println("Bad level value for property: " + key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
            Logger l = getLogger(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
            if (l == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
            l.setLevel(level);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
    // Management Support
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
    private static LoggingMXBean loggingMXBean = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
     * String representation of the
9013
eedac0b9f552 7024172: Move BufferPoolMXBean and PlatformLoggingMXBean java.lang.management
mchung
parents: 7803
diff changeset
  1230
     * {@link javax.management.ObjectName} for the management interface
eedac0b9f552 7024172: Move BufferPoolMXBean and PlatformLoggingMXBean java.lang.management
mchung
parents: 7803
diff changeset
  1231
     * for the logging facility.
eedac0b9f552 7024172: Move BufferPoolMXBean and PlatformLoggingMXBean java.lang.management
mchung
parents: 7803
diff changeset
  1232
     *
eedac0b9f552 7024172: Move BufferPoolMXBean and PlatformLoggingMXBean java.lang.management
mchung
parents: 7803
diff changeset
  1233
     * @see java.lang.management.PlatformLoggingMXBean
eedac0b9f552 7024172: Move BufferPoolMXBean and PlatformLoggingMXBean java.lang.management
mchung
parents: 7803
diff changeset
  1234
     * @see java.util.logging.LoggingMXBean
eedac0b9f552 7024172: Move BufferPoolMXBean and PlatformLoggingMXBean java.lang.management
mchung
parents: 7803
diff changeset
  1235
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
    public final static String LOGGING_MXBEAN_NAME
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
        = "java.util.logging:type=Logging";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
     * Returns <tt>LoggingMXBean</tt> for managing loggers.
9013
eedac0b9f552 7024172: Move BufferPoolMXBean and PlatformLoggingMXBean java.lang.management
mchung
parents: 7803
diff changeset
  1243
     * An alternative way to manage loggers is through the
eedac0b9f552 7024172: Move BufferPoolMXBean and PlatformLoggingMXBean java.lang.management
mchung
parents: 7803
diff changeset
  1244
     * {@link java.lang.management.PlatformLoggingMXBean} interface
eedac0b9f552 7024172: Move BufferPoolMXBean and PlatformLoggingMXBean java.lang.management
mchung
parents: 7803
diff changeset
  1245
     * that can be obtained by calling:
4172
bceea7bc12b7 6876135: Add PlatformLoggingMXBean to eliminate the dependency on JMX from logging
mchung
parents: 3861
diff changeset
  1246
     * <pre>
9013
eedac0b9f552 7024172: Move BufferPoolMXBean and PlatformLoggingMXBean java.lang.management
mchung
parents: 7803
diff changeset
  1247
     *     PlatformLoggingMXBean logging = {@link java.lang.management.ManagementFactory#getPlatformMXBean(Class)
eedac0b9f552 7024172: Move BufferPoolMXBean and PlatformLoggingMXBean java.lang.management
mchung
parents: 7803
diff changeset
  1248
     *         ManagementFactory.getPlatformMXBean}(PlatformLoggingMXBean.class);
4172
bceea7bc12b7 6876135: Add PlatformLoggingMXBean to eliminate the dependency on JMX from logging
mchung
parents: 3861
diff changeset
  1249
     * </pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
     * @return a {@link LoggingMXBean} object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
     *
9013
eedac0b9f552 7024172: Move BufferPoolMXBean and PlatformLoggingMXBean java.lang.management
mchung
parents: 7803
diff changeset
  1253
     * @see java.lang.management.PlatformLoggingMXBean
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
     */
9013
eedac0b9f552 7024172: Move BufferPoolMXBean and PlatformLoggingMXBean java.lang.management
mchung
parents: 7803
diff changeset
  1256
    public static synchronized LoggingMXBean getLoggingMXBean() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
        if (loggingMXBean == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
            loggingMXBean =  new Logging();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
        return loggingMXBean;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
}