jdk/src/share/classes/java/util/logging/Level.java
author mchung
Thu, 07 Feb 2013 09:41:47 -0800
changeset 16112 fc9eb3e70734
parent 16098 9001e536ab4e
child 19795 6c628e165476
permissions -rw-r--r--
8007611: logging behavior in applet changed Reviewed-by: alanb, jgish
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 7803
diff changeset
     2
 * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3853
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3853
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3853
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3853
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3853
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.util.logging;
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
    27
import java.util.ArrayList;
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
    28
import java.util.HashMap;
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
    29
import java.util.List;
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
    30
import java.util.Map;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.ResourceBundle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * The Level class defines a set of standard logging levels that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * can be used to control logging output.  The logging Level objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * are ordered and are specified by ordered integers.  Enabling logging
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * at a given level also enables logging at all higher levels.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * Clients should normally use the predefined Level constants such
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * as Level.SEVERE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * The levels in descending order are:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * <li>SEVERE (highest value)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <li>WARNING
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <li>INFO
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * <li>CONFIG
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * <li>FINE
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * <li>FINER
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * <li>FINEST  (lowest value)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * In addition there is a level OFF that can be used to turn
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * off logging, and a level ALL that can be used to enable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * logging of all messages.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * It is possible for third parties to define additional logging
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * levels by subclassing Level.  In such cases subclasses should
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * take care to chose unique integer level values and to ensure that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * they maintain the Object uniqueness property across serialization
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * by defining a suitable readResolve method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
public class Level implements java.io.Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private static String defaultBundle = "sun.util.logging.resources.logging";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * @serial  The non-localized name of the level.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private final String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * @serial  The integer value of the level.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    private final int value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * @serial The resource bundle name to be used in localizing the level name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    private final String resourceBundleName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
    83
    // localized level name
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
    84
    private String localizedLevelName;
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
    85
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * OFF is a special level that can be used to turn off logging.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * This level is initialized to <CODE>Integer.MAX_VALUE</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    public static final Level OFF = new Level("OFF",Integer.MAX_VALUE, defaultBundle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * SEVERE is a message level indicating a serious failure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * In general SEVERE messages should describe events that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * of considerable importance and which will prevent normal
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * program execution.   They should be reasonably intelligible
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * to end users and to system administrators.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * This level is initialized to <CODE>1000</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    public static final Level SEVERE = new Level("SEVERE",1000, defaultBundle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * WARNING is a message level indicating a potential problem.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * In general WARNING messages should describe events that will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * be of interest to end users or system managers, or which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * indicate potential problems.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * This level is initialized to <CODE>900</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    public static final Level WARNING = new Level("WARNING", 900, defaultBundle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * INFO is a message level for informational messages.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * Typically INFO messages will be written to the console
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * or its equivalent.  So the INFO level should only be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * used for reasonably significant messages that will
3853
9d2382b74894 6882363: 4/4 typos in java.util.logging javadocs
dcubed
parents: 2
diff changeset
   119
     * make sense to end users and system administrators.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * This level is initialized to <CODE>800</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    public static final Level INFO = new Level("INFO", 800, defaultBundle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * CONFIG is a message level for static configuration messages.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * CONFIG messages are intended to provide a variety of static
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * configuration information, to assist in debugging problems
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * that may be associated with particular configurations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * For example, CONFIG message might include the CPU type,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * the graphics depth, the GUI look-and-feel, etc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * This level is initialized to <CODE>700</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    public static final Level CONFIG = new Level("CONFIG", 700, defaultBundle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * FINE is a message level providing tracing information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * All of FINE, FINER, and FINEST are intended for relatively
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * detailed tracing.  The exact meaning of the three levels will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * vary between subsystems, but in general, FINEST should be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * for the most voluminous detailed output, FINER for somewhat
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * less detailed output, and FINE for the  lowest volume (and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * most important) messages.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * In general the FINE level should be used for information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * that will be broadly interesting to developers who do not have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * a specialized interest in the specific subsystem.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * FINE messages might include things like minor (recoverable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * failures.  Issues indicating potential performance problems
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * are also worth logging as FINE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * This level is initialized to <CODE>500</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    public static final Level FINE = new Level("FINE", 500, defaultBundle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * FINER indicates a fairly detailed tracing message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * By default logging calls for entering, returning, or throwing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * an exception are traced at this level.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * This level is initialized to <CODE>400</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    public static final Level FINER = new Level("FINER", 400, defaultBundle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * FINEST indicates a highly detailed tracing message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * This level is initialized to <CODE>300</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    public static final Level FINEST = new Level("FINEST", 300, defaultBundle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * ALL indicates that all messages should be logged.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * This level is initialized to <CODE>Integer.MIN_VALUE</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    public static final Level ALL = new Level("ALL", Integer.MIN_VALUE, defaultBundle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * Create a named Level with a given integer value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * Note that this constructor is "protected" to allow subclassing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * In general clients of logging should use one of the constant Level
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * objects such as SEVERE or FINEST.  However, if clients need to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * add new logging levels, they may subclass Level and define new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * constants.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * @param name  the name of the Level, for example "SEVERE".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * @param value an integer value for the level.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * @throws NullPointerException if the name is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    protected Level(String name, int value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        this(name, value, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * Create a named Level with a given integer value and a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * given localization resource name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * @param name  the name of the Level, for example "SEVERE".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * @param value an integer value for the level.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * @param resourceBundleName name of a resource bundle to use in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     *    localizing the given name. If the resourceBundleName is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     *    or an empty string, it is ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * @throws NullPointerException if the name is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    protected Level(String name, int value, String resourceBundleName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        if (name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        this.value = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        this.resourceBundleName = resourceBundleName;
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   211
        this.localizedLevelName = resourceBundleName == null ? name : null;
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   212
        KnownLevel.add(this);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * Return the level's localization resource bundle name, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * null if no localization bundle is defined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * @return localization resource bundle name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    public String getResourceBundleName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        return resourceBundleName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * Return the non-localized string name of the Level.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * @return non-localized name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    public String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * Return the localized string name of the Level, for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * the current default locale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * If no localization information is available, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * non-localized name is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * @return localized name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    public String getLocalizedName() {
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   244
        return getLocalizedLevelName();
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   245
    }
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   246
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   247
    // package-private getLevelName() is used by the implementation
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   248
    // instead of getName() to avoid calling the subclass's version
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   249
    final String getLevelName() {
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   250
        return this.name;
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   251
    }
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   252
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   253
    final synchronized String getLocalizedLevelName() {
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   254
        if (localizedLevelName != null) {
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   255
            return localizedLevelName;
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   256
        }
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   257
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            ResourceBundle rb = ResourceBundle.getBundle(resourceBundleName);
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   260
            localizedLevelName = rb.getString(name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        } catch (Exception ex) {
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   262
            localizedLevelName = name;
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   263
        }
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   264
        return localizedLevelName;
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   265
    }
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   266
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   267
    // Returns a mirrored Level object that matches the given name as
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   268
    // specified in the Level.parse method.  Returns null if not found.
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   269
    //
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   270
    // It returns the same Level object as the one returned by Level.parse
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   271
    // method if the given name is a non-localized name or integer.
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   272
    //
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   273
    // If the name is a localized name, findLevel and parse method may
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   274
    // return a different level value if there is a custom Level subclass
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   275
    // that overrides Level.getLocalizedName() to return a different string
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   276
    // than what's returned by the default implementation.
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   277
    //
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   278
    static Level findLevel(String name) {
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   279
        if (name == null) {
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   280
            throw new NullPointerException();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        }
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   282
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   283
        KnownLevel level;
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   284
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   285
        // Look for a known Level with the given non-localized name.
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   286
        level = KnownLevel.findByName(name);
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   287
        if (level != null) {
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   288
            return level.mirroredLevel;
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   289
        }
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   290
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   291
        // Now, check if the given name is an integer.  If so,
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   292
        // first look for a Level with the given value and then
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   293
        // if necessary create one.
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   294
        try {
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   295
            int x = Integer.parseInt(name);
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   296
            level = KnownLevel.findByValue(x);
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   297
            if (level == null) {
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   298
                // add new Level
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   299
                Level levelObject = new Level(name, x);
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   300
                level = KnownLevel.findByValue(x);
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   301
            }
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   302
            return level.mirroredLevel;
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   303
        } catch (NumberFormatException ex) {
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   304
            // Not an integer.
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   305
            // Drop through.
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   306
        }
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   307
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   308
        level = KnownLevel.findByLocalizedLevelName(name);
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   309
        if (level != null) {
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   310
            return level.mirroredLevel;
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   311
        }
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   312
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   313
        return null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    /**
3853
9d2382b74894 6882363: 4/4 typos in java.util.logging javadocs
dcubed
parents: 2
diff changeset
   317
     * Returns a string representation of this Level.
9d2382b74894 6882363: 4/4 typos in java.util.logging javadocs
dcubed
parents: 2
diff changeset
   318
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * @return the non-localized name of the Level, for example "INFO".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    public final String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * Get the integer value for this level.  This integer value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * can be used for efficient ordering comparisons between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * Level objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * @return the integer value for this level.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    public final int intValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    private static final long serialVersionUID = -8176160795706313070L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    // Serialization magic to prevent "doppelgangers".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    // This is a performance optimization.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    private Object readResolve() {
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   340
        KnownLevel o = KnownLevel.matches(this);
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   341
        if (o != null) {
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   342
            return o.levelObject;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        }
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   344
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   345
        // Woops.  Whoever sent us this object knows
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   346
        // about a new log level.  Add it to our list.
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   347
        Level level = new Level(this.name, this.value, this.resourceBundleName);
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   348
        return level;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * Parse a level name string into a Level.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * The argument string may consist of either a level name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * or an integer value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * For example:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * <li>     "SEVERE"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * <li>     "1000"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * </ul>
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   362
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * @param  name   string to be parsed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * @throws NullPointerException if the name is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * @throws IllegalArgumentException if the value is not valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * Valid values are integers between <CODE>Integer.MIN_VALUE</CODE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * and <CODE>Integer.MAX_VALUE</CODE>, and all known level names.
3853
9d2382b74894 6882363: 4/4 typos in java.util.logging javadocs
dcubed
parents: 2
diff changeset
   368
     * Known names are the levels defined by this class (e.g., <CODE>FINE</CODE>,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * <CODE>FINER</CODE>, <CODE>FINEST</CODE>), or created by this class with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * appropriate package access, or new levels defined or created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * by subclasses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * @return The parsed value. Passing an integer that corresponds to a known name
3853
9d2382b74894 6882363: 4/4 typos in java.util.logging javadocs
dcubed
parents: 2
diff changeset
   374
     * (e.g., 700) will return the associated name (e.g., <CODE>CONFIG</CODE>).
9d2382b74894 6882363: 4/4 typos in java.util.logging javadocs
dcubed
parents: 2
diff changeset
   375
     * Passing an integer that does not (e.g., 1) will return a new level name
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * initialized to that value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    public static synchronized Level parse(String name) throws IllegalArgumentException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        // Check that name is not null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        name.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   382
        KnownLevel level;
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   383
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        // Look for a known Level with the given non-localized name.
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   385
        level = KnownLevel.findByName(name);
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   386
        if (level != null) {
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   387
            return level.levelObject;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        // Now, check if the given name is an integer.  If so,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        // first look for a Level with the given value and then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        // if necessary create one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            int x = Integer.parseInt(name);
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   395
            level = KnownLevel.findByValue(x);
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   396
            if (level == null) {
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   397
                // add new Level
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   398
                Level levelObject = new Level(name, x);
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   399
                level = KnownLevel.findByValue(x);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            }
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   401
            return level.levelObject;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        } catch (NumberFormatException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            // Not an integer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            // Drop through.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        // Finally, look for a known level with the given localized name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        // in the current default locale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        // This is relatively expensive, but not excessively so.
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   410
        level = KnownLevel.findByLocalizedName(name);
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   411
        if (level != null) {
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   412
            return level.levelObject;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        // OK, we've tried everything and failed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        throw new IllegalArgumentException("Bad level \"" + name + "\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * Compare two objects for value equality.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * @return true if and only if the two objects have the same level value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    public boolean equals(Object ox) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            Level lx = (Level)ox;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            return (lx.value == this.value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * Generate a hashcode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * @return a hashcode based on the level value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        return this.value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    }
16098
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   439
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   440
    // KnownLevel class maintains the global list of all known levels.
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   441
    // The API allows multiple custom Level instances of the same name/value
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   442
    // be created. This class provides convenient methods to find a level
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   443
    // by a given name, by a given value, or by a given localized name.
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   444
    //
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   445
    // KnownLevel wraps the following Level objects:
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   446
    // 1. levelObject:   standard Level object or custom Level object
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   447
    // 2. mirroredLevel: Level object representing the level specified in the
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   448
    //                   logging configuration.
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   449
    //
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   450
    // Level.getName, Level.getLocalizedName, Level.getResourceBundleName methods
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   451
    // are non-final but the name and resource bundle name are parameters to
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   452
    // the Level constructor.  Use the mirroredLevel object instead of the
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   453
    // levelObject to prevent the logging framework to execute foreign code
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   454
    // implemented by untrusted Level subclass.
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   455
    //
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   456
    // Implementation Notes:
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   457
    // If Level.getName, Level.getLocalizedName, Level.getResourceBundleName methods
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   458
    // were final, the following KnownLevel implementation can be removed.
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   459
    // Future API change should take this into consideration.
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   460
    static final class KnownLevel {
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   461
        private static Map<String, List<KnownLevel>> nameToLevels = new HashMap<>();
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   462
        private static Map<Integer, List<KnownLevel>> intToLevels = new HashMap<>();
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   463
        final Level levelObject;     // instance of Level class or Level subclass
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   464
        final Level mirroredLevel;   // instance of Level class
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   465
        KnownLevel(Level l) {
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   466
            this.levelObject = l;
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   467
            if (l.getClass() == Level.class) {
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   468
                this.mirroredLevel = l;
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   469
            } else {
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   470
                this.mirroredLevel = new Level(l.name, l.value, l.resourceBundleName);
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   471
            }
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   472
        }
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   473
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   474
        static synchronized void add(Level l) {
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   475
            // the mirroredLevel object is always added to the list
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   476
            // before the custom Level instance
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   477
            KnownLevel o = new KnownLevel(l);
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   478
            List<KnownLevel> list = nameToLevels.get(l.name);
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   479
            if (list == null) {
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   480
                list = new ArrayList<>();
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   481
                nameToLevels.put(l.name, list);
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   482
            }
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   483
            list.add(o);
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   484
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   485
            list = intToLevels.get(l.value);
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   486
            if (list == null) {
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   487
                list = new ArrayList<>();
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   488
                intToLevels.put(l.value, list);
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   489
            }
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   490
            list.add(o);
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   491
        }
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   492
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   493
        // Returns a KnownLevel with the given non-localized name.
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   494
        static synchronized KnownLevel findByName(String name) {
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   495
            List<KnownLevel> list = nameToLevels.get(name);
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   496
            if (list != null) {
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   497
                return list.get(0);
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   498
            }
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   499
            return null;
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   500
        }
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   501
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   502
        // Returns a KnownLevel with the given value.
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   503
        static synchronized KnownLevel findByValue(int value) {
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   504
            List<KnownLevel> list = intToLevels.get(value);
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   505
            if (list != null) {
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   506
                return list.get(0);
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   507
            }
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   508
            return null;
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   509
        }
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   510
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   511
        // Returns a KnownLevel with the given localized name matching
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   512
        // by calling the Level.getLocalizedLevelName() method (i.e. found
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   513
        // from the resourceBundle associated with the Level object).
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   514
        // This method does not call Level.getLocalizedName() that may
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   515
        // be overridden in a subclass implementation
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   516
        static synchronized KnownLevel findByLocalizedLevelName(String name) {
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   517
            for (List<KnownLevel> levels : nameToLevels.values()) {
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   518
                for (KnownLevel l : levels) {
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   519
                    String lname = l.levelObject.getLocalizedLevelName();
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   520
                    if (name.equals(lname)) {
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   521
                        return l;
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   522
                    }
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   523
                }
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   524
            }
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   525
            return null;
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   526
        }
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   527
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   528
        // Returns a KnownLevel with the given localized name matching
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   529
        // by calling the Level.getLocalizedName() method
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   530
        static synchronized KnownLevel findByLocalizedName(String name) {
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   531
            for (List<KnownLevel> levels : nameToLevels.values()) {
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   532
                for (KnownLevel l : levels) {
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   533
                    String lname = l.levelObject.getLocalizedName();
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   534
                    if (name.equals(lname)) {
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   535
                        return l;
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   536
                    }
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   537
                }
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   538
            }
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   539
            return null;
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   540
        }
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   541
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   542
        static synchronized KnownLevel matches(Level l) {
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   543
            List<KnownLevel> list = nameToLevels.get(l.name);
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   544
            if (list != null) {
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   545
                for (KnownLevel level : list) {
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   546
                    Level other = level.mirroredLevel;
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   547
                    if (l.value == other.value &&
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   548
                           (l.resourceBundleName == other.resourceBundleName ||
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   549
                               (l.resourceBundleName != null &&
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   550
                                l.resourceBundleName.equals(other.resourceBundleName)))) {
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   551
                        return level;
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   552
                    }
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   553
                }
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   554
            }
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   555
            return null;
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   556
        }
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   557
    }
9001e536ab4e 6664509: Add logging context
mchung
parents: 9035
diff changeset
   558
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
}