jdk/src/share/classes/java/util/logging/LogManager.java
author bae
Wed, 04 Feb 2009 14:06:18 +0300
changeset 2384 b3ba5fb77f89
parent 2 90ce3da70b43
child 3319 53a6d815c92f
permissions -rw-r--r--
6799583: LogManager shutdown hook may cause a memory leak. Reviewed-by: igor, swamyv
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2000-2007 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                    } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                        // System.err.println("Can't read logging configuration:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                        // ex.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        }
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
     * Adds an event listener to be invoked when the logging
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * properties are re-read. Adding multiple instances of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * the same event Listener results in multiple entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * in the property event listener table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * @param l  event listener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * @exception  SecurityException  if a security manager exists and if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     *             the caller does not have LoggingPermission("control").
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * @exception NullPointerException if the PropertyChangeListener is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    public void addPropertyChangeListener(PropertyChangeListener l) throws SecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        if (l == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        checkAccess();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        changes.addPropertyChangeListener(l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * Removes an event listener for property change events.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * If the same listener instance has been added to the listener table
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * through multiple invocations of <CODE>addPropertyChangeListener</CODE>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * then an equivalent number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * <CODE>removePropertyChangeListener</CODE> invocations are required to remove
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * all instances of that listener from the listener table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * Returns silently if the given listener is not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * @param l  event listener (can be null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * @exception  SecurityException  if a security manager exists and if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     *             the caller does not have LoggingPermission("control").
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    public void removePropertyChangeListener(PropertyChangeListener l) throws SecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        checkAccess();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        changes.removePropertyChangeListener(l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    // Package-level method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    // Find or create a specified logger instance. If a logger has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    // already been created with the given name it is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    // Otherwise a new logger instance is created and registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    // in the LogManager global namespace.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    synchronized Logger demandLogger(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        Logger result = getLogger(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        if (result == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            result = new Logger(name, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            addLogger(result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            result = getLogger(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    // If logger.getUseParentHandlers() returns 'true' and any of the logger's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    // parents have levels or handlers defined, make sure they are instantiated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    private void processParentHandlers(Logger logger, String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        int ix = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            int ix2 = name.indexOf(".", ix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            if (ix2 < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            String pname = name.substring(0,ix2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            if (getProperty(pname+".level")    != null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                getProperty(pname+".handlers") != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                // This pname has a level/handlers definition.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                // Make sure it exists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                demandLogger(pname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            ix = ix2+1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    // Add new per logger handlers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    // We need to raise privilege here. All our decisions will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    // be made based on the logging configuration, which can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    // only be modified by trusted code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    private void loadLoggerHandlers(final Logger logger, final String name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                                    final String handlersPropertyName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        AccessController.doPrivileged(new PrivilegedAction<Object>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                if (logger != rootLogger) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                    boolean useParent = getBooleanProperty(name + ".useParentHandlers", true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                    if (!useParent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                        logger.setUseParentHandlers(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                String names[] = parseClassNames(handlersPropertyName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                for (int i = 0; i < names.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                    String word = names[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                        Class   clz = ClassLoader.getSystemClassLoader().loadClass(word);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                        Handler hdl = (Handler) clz.newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                            // Check if there is a property defining the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                            // this handler's level.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                            String levs = getProperty(word + ".level");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                            if (levs != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                                hdl.setLevel(Level.parse(levs));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                        } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                            System.err.println("Can't set level for " + word);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                            // Probably a bad level. Drop through.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                        // Add this Handler to the logger
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                        logger.addHandler(hdl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                    } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                        System.err.println("Can't load log handler \"" + word + "\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                        System.err.println("" + ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                        ex.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            }});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * Add a named logger.  This does nothing and returns false if a logger
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * with the same name is already registered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * The Logger factory methods call this method to register each
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * newly created Logger.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * The application should retain its own reference to the Logger
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * object to avoid it being garbage collected.  The LogManager
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * may only retain a weak reference.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * @param   logger the new logger.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * @return  true if the argument logger was registered successfully,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     *          false if a logger of that name already exists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * @exception NullPointerException if the logger name is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    public synchronized boolean addLogger(Logger logger) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        final String name = logger.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        if (name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        WeakReference<Logger> ref = loggers.get(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        if (ref != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            if (ref.get() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                // Hashtable holds stale weak reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                // to a logger which has been GC-ed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                // Allow to register new one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                loggers.remove(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                // We already have a registered logger with the given name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        // We're adding a new logger.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        // Note that we are creating a weak reference here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        loggers.put(name, new WeakReference<Logger>(logger));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        // Apply any initial level defined for the new logger.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        Level level = getLevelProperty(name+".level", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        if (level != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            doSetLevel(logger, level);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        // Do we have a per logger handler too?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        // Note: this will add a 200ms penalty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        loadLoggerHandlers(logger, name, name+".handlers");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        processParentHandlers(logger, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        // Find the new node and its parent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        LogNode node = findNode(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        node.loggerRef = new WeakReference<Logger>(logger);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        Logger parent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        LogNode nodep = node.parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        while (nodep != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            WeakReference<Logger> nodeRef = nodep.loggerRef;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            if (nodeRef != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                parent = nodeRef.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                if (parent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            nodep = nodep.parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        if (parent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            doSetParent(logger, parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        // Walk over the children and tell them we are their new parent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        node.walkAndSetParent(logger);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    // Private method to set a level on a logger.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    // If necessary, we raise privilege before doing the call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
    private static void doSetLevel(final Logger logger, final Level level) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        if (sm == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            // There is no security manager, so things are easy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            logger.setLevel(level);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        // There is a security manager.  Raise privilege before
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        // calling setLevel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        AccessController.doPrivileged(new PrivilegedAction<Object>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                logger.setLevel(level);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            }});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
    // Private method to set a parent on a logger.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    // If necessary, we raise privilege before doing the setParent call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
    private static void doSetParent(final Logger logger, final Logger parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        if (sm == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
            // There is no security manager, so things are easy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
            logger.setParent(parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        // There is a security manager.  Raise privilege before
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        // calling setParent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        AccessController.doPrivileged(new PrivilegedAction<Object>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                logger.setParent(parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
            }});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    // Find a node in our tree of logger nodes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
    // If necessary, create it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
    private LogNode findNode(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        if (name == null || name.equals("")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
            return root;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        LogNode node = root;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        while (name.length() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            int ix = name.indexOf(".");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            String head;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            if (ix > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                head = name.substring(0,ix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                name = name.substring(ix+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                head = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                name = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            if (node.children == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                node.children = new HashMap<String,LogNode>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            LogNode child = node.children.get(head);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
            if (child == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                child = new LogNode(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                node.children.put(head, child);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            node = child;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        return node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     * Method to find a named logger.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     * Note that since untrusted code may create loggers with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     * arbitrary names this method should not be relied on to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     * find Loggers for security sensitive logging.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     * @param name name of the logger
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     * @return  matching logger or null if none is found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
    public synchronized Logger getLogger(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        WeakReference<Logger> ref = loggers.get(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        if (ref == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        Logger logger = ref.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        if (logger == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
            // Hashtable holds stale weak reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
            // to a logger which has been GC-ed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
            loggers.remove(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        return logger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     * Get an enumeration of known logger names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     * Note:  Loggers may be added dynamically as new classes are loaded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     * This method only reports on the loggers that are currently registered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     * @return  enumeration of logger name strings
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
    public synchronized Enumeration<String> getLoggerNames() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        return loggers.keys();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     * Reinitialize the logging properties and reread the logging configuration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * The same rules are used for locating the configuration properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     * as are used at startup.  So normally the logging properties will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * be re-read from the same file that was used at startup.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     * Any log level definitions in the new configuration file will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     * applied using Logger.setLevel(), if the target Logger exists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     * A PropertyChangeEvent will be fired after the properties are read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     * @exception  SecurityException  if a security manager exists and if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     *             the caller does not have LoggingPermission("control").
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     * @exception  IOException if there are IO problems reading the configuration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
    public void readConfiguration() throws IOException, SecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        checkAccess();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        // if a configuration class is specified, load it and use it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        String cname = System.getProperty("java.util.logging.config.class");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        if (cname != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                // Instantiate the named class.  It is its constructor's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                // responsibility to initialize the logging configuration, by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                // calling readConfiguration(InputStream) with a suitable stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                    Class clz = ClassLoader.getSystemClassLoader().loadClass(cname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                    clz.newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
                } catch (ClassNotFoundException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
                    Class clz = Thread.currentThread().getContextClassLoader().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
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
            } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                System.err.println("Logging configuration class \"" + cname + "\" failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
                System.err.println("" + ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
                // keep going and useful config file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        String fname = System.getProperty("java.util.logging.config.file");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        if (fname == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
            fname = System.getProperty("java.home");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
            if (fname == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                throw new Error("Can't find java.home ??");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
            File f = new File(fname, "lib");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
            f = new File(f, "logging.properties");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
            fname = f.getCanonicalPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
        InputStream in = new FileInputStream(fname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        BufferedInputStream bin = new BufferedInputStream(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
            readConfiguration(bin);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
            if (in != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     * Reset the logging configuration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     * For all named loggers, the reset operation removes and closes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     * all Handlers and (except for the root logger) sets the level
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     * to null.  The root logger's level is set to Level.INFO.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     * @exception  SecurityException  if a security manager exists and if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     *             the caller does not have LoggingPermission("control").
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
    public void reset() throws SecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        checkAccess();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
            props = new Properties();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
            // Since we are doing a reset we no longer want to initialize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
            // the global handlers, if they haven't been initialized yet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
            initializedGlobalHandlers = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        Enumeration enum_ = getLoggerNames();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
        while (enum_.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
            String name = (String)enum_.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
            resetLogger(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
    // Private method to reset an individual target logger.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
    private void resetLogger(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
        Logger logger = getLogger(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
        if (logger == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
        // Close all the Logger's handlers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        Handler[] targets = logger.getHandlers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
        for (int i = 0; i < targets.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
            Handler h = targets[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
            logger.removeHandler(h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
                h.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
            } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
                // Problems closing a handler?  Keep going...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        if (name != null && name.equals("")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
            // This is the root logger.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
            logger.setLevel(defaultLevel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
            logger.setLevel(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
    // get a list of whitespace separated classnames from a property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
    private String[] parseClassNames(String propertyName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        String hands = getProperty(propertyName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
        if (hands == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
            return new String[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
        hands = hands.trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
        int ix = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
        Vector<String> result = new Vector<String>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
        while (ix < hands.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
            int end = ix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
            while (end < hands.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
                if (Character.isWhitespace(hands.charAt(end))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
                if (hands.charAt(end) == ',') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
                end++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
            String word = hands.substring(ix, end);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
            ix = end+1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
            word = word.trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
            if (word.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
            result.add(word);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
        return result.toArray(new String[result.size()]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
     * Reinitialize the logging properties and reread the logging configuration
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
     * from the given stream, which should be in java.util.Properties format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
     * A PropertyChangeEvent will be fired after the properties are read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
     * Any log level definitions in the new configuration file will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
     * applied using Logger.setLevel(), if the target Logger exists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
     * @param ins       stream to read properties from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
     * @exception  SecurityException  if a security manager exists and if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
     *             the caller does not have LoggingPermission("control").
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
     * @exception  IOException if there are problems reading from the stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
    public void readConfiguration(InputStream ins) throws IOException, SecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        checkAccess();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        // Load the properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
        props.load(ins);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
        // Instantiate new configuration objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
        String names[] = parseClassNames("config");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
        for (int i = 0; i < names.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
            String word = names[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
                Class clz = ClassLoader.getSystemClassLoader().loadClass(word);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
                clz.newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
            } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
                System.err.println("Can't load config class \"" + word + "\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
                System.err.println("" + ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
                // ex.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
        // Set levels on any pre-existing loggers, based on the new properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        setLevelsOnExistingLoggers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
        // Notify any interested parties that our properties have changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
        changes.firePropertyChange(null, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
        // Note that we need to reinitialize global handles when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
        // they are first referenced.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
            initializedGlobalHandlers = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
     * Get the value of a logging property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
     * The method returns null if the property is not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
     * @param name      property name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
     * @return          property value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
    public String getProperty(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
        return props.getProperty(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
    // Package private method to get a String property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
    // If the property is not defined we return the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
    // default value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
    String getStringProperty(String name, String defaultValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
        String val = getProperty(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
        if (val == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
            return defaultValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
        return val.trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
    // Package private method to get an integer property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
    // If the property is not defined or cannot be parsed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
    // we return the given default value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
    int getIntProperty(String name, int defaultValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
        String val = getProperty(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
        if (val == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
            return defaultValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
            return Integer.parseInt(val.trim());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
        } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
            return defaultValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
    // Package private method to get a boolean property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
    // If the property is not defined or cannot be parsed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
    // we return the given default value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
    boolean getBooleanProperty(String name, boolean defaultValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
        String val = getProperty(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
        if (val == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
            return defaultValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
        val = val.toLowerCase();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
        if (val.equals("true") || val.equals("1")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
        } else if (val.equals("false") || val.equals("0")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
        return defaultValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
    // Package private method to get a Level property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
    // If the property is not defined or cannot be parsed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
    // we return the given default value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
    Level getLevelProperty(String name, Level defaultValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
        String val = getProperty(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
        if (val == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
            return defaultValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
            return Level.parse(val.trim());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
        } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
            return defaultValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
    // Package private method to get a filter property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
    // We return an instance of the class named by the "name"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
    // property. If the property is not defined or has problems
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
    // we return the defaultValue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
    Filter getFilterProperty(String name, Filter defaultValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
        String val = getProperty(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
            if (val != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
                Class clz = ClassLoader.getSystemClassLoader().loadClass(val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
                return (Filter) clz.newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
        } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
            // We got one of a variety of exceptions in creating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
            // class or creating an instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
            // Drop through.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
        // We got an exception.  Return the defaultValue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
        return defaultValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
    // Package private method to get a formatter property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
    // We return an instance of the class named by the "name"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
    // property. If the property is not defined or has problems
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
    // we return the defaultValue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
    Formatter getFormatterProperty(String name, Formatter defaultValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
        String val = getProperty(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
            if (val != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
                Class clz = ClassLoader.getSystemClassLoader().loadClass(val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
                return (Formatter) clz.newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
        } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
            // We got one of a variety of exceptions in creating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
            // class or creating an instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
            // Drop through.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
        // We got an exception.  Return the defaultValue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
        return defaultValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
    // Private method to load the global handlers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
    // We do the real work lazily, when the global handlers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
    // are first used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
    private synchronized void initializeGlobalHandlers() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
        if (initializedGlobalHandlers) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
        initializedGlobalHandlers = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
        if (deathImminent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
            // Aaargh...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
            // The VM is shutting down and our exit hook has been called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
            // Avoid allocating global handlers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
        loadLoggerHandlers(rootLogger, null, "handlers");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
    private Permission ourPermission = new LoggingPermission("control", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
     * Check that the current context is trusted to modify the logging
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
     * configuration.  This requires LoggingPermission("control").
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
     * If the check fails we throw a SecurityException, otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
     * we return normally.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
     * @exception  SecurityException  if a security manager exists and if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
     *             the caller does not have LoggingPermission("control").
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
    public void checkAccess() throws SecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
        if (sm == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
        sm.checkPermission(ourPermission);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
    // Nested class to represent a node in our tree of named loggers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
    private static class LogNode {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
        HashMap<String,LogNode> children;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
        WeakReference<Logger> loggerRef;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
        LogNode parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
        LogNode(LogNode parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
            this.parent = parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
        // Recursive method to walk the tree below a node and set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
        // a new parent logger.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
        void walkAndSetParent(Logger parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
            if (children == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
            Iterator<LogNode> values = children.values().iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
            while (values.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
                LogNode node = values.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
                WeakReference<Logger> ref = node.loggerRef;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
                Logger logger = (ref == null) ? null : ref.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
                if (logger == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
                    node.walkAndSetParent(parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
                    doSetParent(logger, parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
    // We use a subclass of Logger for the root logger, so
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
    // that we only instantiate the global handlers when they
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
    // are first needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
    private class RootLogger extends Logger {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
        private RootLogger() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
            super("", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
            setLevel(defaultLevel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
        public void log(LogRecord record) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
            // Make sure that the global handlers have been instantiated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
            initializeGlobalHandlers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
            super.log(record);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
        public void addHandler(Handler h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
            initializeGlobalHandlers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
            super.addHandler(h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
        public void removeHandler(Handler h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
            initializeGlobalHandlers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
            super.removeHandler(h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
        public Handler[] getHandlers() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
            initializeGlobalHandlers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
            return super.getHandlers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
    // Private method to be called when the configuration has
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
    // changed to apply any level settings to any pre-existing loggers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
    synchronized private void setLevelsOnExistingLoggers() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
        Enumeration enum_ = props.propertyNames();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
        while (enum_.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
            String key = (String)enum_.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
            if (!key.endsWith(".level")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
                // Not a level definition.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
            int ix = key.length() - 6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
            String name = key.substring(0, ix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
            Level level = getLevelProperty(key, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
            if (level == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
                System.err.println("Bad level value for property: " + key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
            Logger l = getLogger(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
            if (l == null) {
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
            l.setLevel(level);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
    // Management Support
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
    private static LoggingMXBean loggingMXBean = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
     * String representation of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
     * {@link javax.management.ObjectName} for {@link LoggingMXBean}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
    public final static String LOGGING_MXBEAN_NAME
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
        = "java.util.logging:type=Logging";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
     * Returns <tt>LoggingMXBean</tt> for managing loggers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
     * The <tt>LoggingMXBean</tt> can also obtained from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
     * {@link java.lang.management.ManagementFactory#getPlatformMBeanServer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
     * platform <tt>MBeanServer</tt>} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
     * @return a {@link LoggingMXBean} object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
     * @see java.lang.management.ManagementFactory
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
    public static synchronized LoggingMXBean  getLoggingMXBean() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
        if (loggingMXBean == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
            loggingMXBean =  new Logging();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
        return loggingMXBean;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
}