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