jdk/src/java.logging/share/classes/java/util/logging/Handler.java
author shade
Fri, 20 Feb 2015 18:32:10 +0300
changeset 29094 a4fd2b5e49f8
parent 25859 3317bb8137f4
child 32037 ab4526f4ac10
permissions -rw-r--r--
8073479: Replace obj.getClass hacks with Objects.requireNonNull Reviewed-by: dfuchs, plevart, vlivanov
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
18565
5025d43d3731 8019315: Fix doclint issues in java.util.logging
darcy
parents: 14342
diff changeset
     2
 * Copyright (c) 2000, 2013, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
package java.util.logging;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
29094
a4fd2b5e49f8 8073479: Replace obj.getClass hacks with Objects.requireNonNull
shade
parents: 25859
diff changeset
    29
import java.util.Objects;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.UnsupportedEncodingException;
22110
06e486bc20b6 8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents: 19808
diff changeset
    31
import java.security.AccessController;
06e486bc20b6 8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents: 19808
diff changeset
    32
import java.security.PrivilegedAction;
06e486bc20b6 8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents: 19808
diff changeset
    33
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * A <tt>Handler</tt> object takes log messages from a <tt>Logger</tt> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * exports them.  It might for example, write them to a console
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * or write them to a file, or send them to a network logging service,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * or forward them to an OS log, or whatever.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * A <tt>Handler</tt> can be disabled by doing a <tt>setLevel(Level.OFF)</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * and can  be re-enabled by doing a <tt>setLevel</tt> with an appropriate level.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <tt>Handler</tt> classes typically use <tt>LogManager</tt> properties to set
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * default values for the <tt>Handler</tt>'s <tt>Filter</tt>, <tt>Formatter</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * and <tt>Level</tt>.  See the specific documentation for each concrete
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <tt>Handler</tt> class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
public abstract class Handler {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private static final int offValue = Level.OFF.intValue();
19808
39cb79123ab2 6823527: java.util.logging.Handler has thread safety issues
dfuchs
parents: 18565
diff changeset
    54
    private final LogManager manager = LogManager.getLogManager();
39cb79123ab2 6823527: java.util.logging.Handler has thread safety issues
dfuchs
parents: 18565
diff changeset
    55
39cb79123ab2 6823527: java.util.logging.Handler has thread safety issues
dfuchs
parents: 18565
diff changeset
    56
    // We're using volatile here to avoid synchronizing getters, which
39cb79123ab2 6823527: java.util.logging.Handler has thread safety issues
dfuchs
parents: 18565
diff changeset
    57
    // would prevent other threads from calling isLoggable()
39cb79123ab2 6823527: java.util.logging.Handler has thread safety issues
dfuchs
parents: 18565
diff changeset
    58
    // while publish() is executing.
39cb79123ab2 6823527: java.util.logging.Handler has thread safety issues
dfuchs
parents: 18565
diff changeset
    59
    // On the other hand, setters will be synchronized to exclude concurrent
39cb79123ab2 6823527: java.util.logging.Handler has thread safety issues
dfuchs
parents: 18565
diff changeset
    60
    // execution with more complex methods, such as StreamHandler.publish().
39cb79123ab2 6823527: java.util.logging.Handler has thread safety issues
dfuchs
parents: 18565
diff changeset
    61
    // We wouldn't want 'level' to be changed by another thread in the middle
39cb79123ab2 6823527: java.util.logging.Handler has thread safety issues
dfuchs
parents: 18565
diff changeset
    62
    // of the execution of a 'publish' call.
39cb79123ab2 6823527: java.util.logging.Handler has thread safety issues
dfuchs
parents: 18565
diff changeset
    63
    private volatile Filter filter;
39cb79123ab2 6823527: java.util.logging.Handler has thread safety issues
dfuchs
parents: 18565
diff changeset
    64
    private volatile Formatter formatter;
39cb79123ab2 6823527: java.util.logging.Handler has thread safety issues
dfuchs
parents: 18565
diff changeset
    65
    private volatile Level logLevel = Level.ALL;
39cb79123ab2 6823527: java.util.logging.Handler has thread safety issues
dfuchs
parents: 18565
diff changeset
    66
    private volatile ErrorManager errorManager = new ErrorManager();
39cb79123ab2 6823527: java.util.logging.Handler has thread safety issues
dfuchs
parents: 18565
diff changeset
    67
    private volatile String encoding;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * Default constructor.  The resulting <tt>Handler</tt> has a log
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * level of <tt>Level.ALL</tt>, no <tt>Formatter</tt>, and no
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * <tt>Filter</tt>.  A default <tt>ErrorManager</tt> instance is installed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * as the <tt>ErrorManager</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    protected Handler() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /**
22110
06e486bc20b6 8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents: 19808
diff changeset
    79
     * Package-private constructor for chaining from subclass constructors
06e486bc20b6 8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents: 19808
diff changeset
    80
     * that wish to configure the handler with specific default and/or
06e486bc20b6 8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents: 19808
diff changeset
    81
     * specified values.
06e486bc20b6 8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents: 19808
diff changeset
    82
     *
06e486bc20b6 8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents: 19808
diff changeset
    83
     * @param defaultLevel       a default {@link Level} to configure if one is
06e486bc20b6 8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents: 19808
diff changeset
    84
     *                           not found in LogManager configuration properties
06e486bc20b6 8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents: 19808
diff changeset
    85
     * @param defaultFormatter   a default {@link Formatter} to configure if one is
06e486bc20b6 8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents: 19808
diff changeset
    86
     *                           not specified by {@code specifiedFormatter} parameter
06e486bc20b6 8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents: 19808
diff changeset
    87
     *                           nor found in LogManager configuration properties
06e486bc20b6 8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents: 19808
diff changeset
    88
     * @param specifiedFormatter if not null, this is the formatter to configure
06e486bc20b6 8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents: 19808
diff changeset
    89
     */
06e486bc20b6 8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents: 19808
diff changeset
    90
    Handler(Level defaultLevel, Formatter defaultFormatter,
06e486bc20b6 8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents: 19808
diff changeset
    91
            Formatter specifiedFormatter) {
06e486bc20b6 8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents: 19808
diff changeset
    92
06e486bc20b6 8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents: 19808
diff changeset
    93
        LogManager manager = LogManager.getLogManager();
06e486bc20b6 8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents: 19808
diff changeset
    94
        String cname = getClass().getName();
06e486bc20b6 8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents: 19808
diff changeset
    95
06e486bc20b6 8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents: 19808
diff changeset
    96
        final Level level = manager.getLevelProperty(cname + ".level", defaultLevel);
06e486bc20b6 8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents: 19808
diff changeset
    97
        final Filter filter = manager.getFilterProperty(cname + ".filter", null);
06e486bc20b6 8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents: 19808
diff changeset
    98
        final Formatter formatter = specifiedFormatter == null
06e486bc20b6 8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents: 19808
diff changeset
    99
                                    ? manager.getFormatterProperty(cname + ".formatter", defaultFormatter)
06e486bc20b6 8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents: 19808
diff changeset
   100
                                    : specifiedFormatter;
06e486bc20b6 8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents: 19808
diff changeset
   101
        final String encoding = manager.getStringProperty(cname + ".encoding", null);
06e486bc20b6 8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents: 19808
diff changeset
   102
06e486bc20b6 8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents: 19808
diff changeset
   103
        AccessController.doPrivileged(new PrivilegedAction<Void>() {
06e486bc20b6 8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents: 19808
diff changeset
   104
            @Override
06e486bc20b6 8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents: 19808
diff changeset
   105
            public Void run() {
06e486bc20b6 8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents: 19808
diff changeset
   106
                setLevel(level);
06e486bc20b6 8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents: 19808
diff changeset
   107
                setFilter(filter);
06e486bc20b6 8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents: 19808
diff changeset
   108
                setFormatter(formatter);
06e486bc20b6 8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents: 19808
diff changeset
   109
                try {
06e486bc20b6 8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents: 19808
diff changeset
   110
                    setEncoding(encoding);
06e486bc20b6 8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents: 19808
diff changeset
   111
                } catch (Exception ex) {
06e486bc20b6 8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents: 19808
diff changeset
   112
                    try {
06e486bc20b6 8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents: 19808
diff changeset
   113
                        setEncoding(null);
06e486bc20b6 8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents: 19808
diff changeset
   114
                    } catch (Exception ex2) {
06e486bc20b6 8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents: 19808
diff changeset
   115
                        // doing a setEncoding with null should always work.
06e486bc20b6 8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents: 19808
diff changeset
   116
                        // assert false;
06e486bc20b6 8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents: 19808
diff changeset
   117
                    }
06e486bc20b6 8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents: 19808
diff changeset
   118
                }
06e486bc20b6 8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents: 19808
diff changeset
   119
                return null;
06e486bc20b6 8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents: 19808
diff changeset
   120
            }
06e486bc20b6 8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents: 19808
diff changeset
   121
        }, null, LogManager.controlPermission);
06e486bc20b6 8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents: 19808
diff changeset
   122
    }
06e486bc20b6 8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents: 19808
diff changeset
   123
06e486bc20b6 8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents: 19808
diff changeset
   124
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * Publish a <tt>LogRecord</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * The logging request was made initially to a <tt>Logger</tt> object,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * which initialized the <tt>LogRecord</tt> and forwarded it here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * The <tt>Handler</tt>  is responsible for formatting the message, when and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * if necessary.  The formatting should include localization.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * @param  record  description of the log event. A null record is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     *                 silently ignored and is not published
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    public abstract void publish(LogRecord record);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * Flush any buffered output.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    public abstract void flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * Close the <tt>Handler</tt> and free all associated resources.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * The close method will perform a <tt>flush</tt> and then close the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * <tt>Handler</tt>.   After close has been called this <tt>Handler</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * should no longer be used.  Method calls may either be silently
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * ignored or may throw runtime exceptions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * @exception  SecurityException  if a security manager exists and if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     *             the caller does not have <tt>LoggingPermission("control")</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    public abstract void close() throws SecurityException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * Set a <tt>Formatter</tt>.  This <tt>Formatter</tt> will be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * to format <tt>LogRecords</tt> for this <tt>Handler</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * Some <tt>Handlers</tt> may not use <tt>Formatters</tt>, in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * which case the <tt>Formatter</tt> will be remembered, but not used.
24196
61c9885d76e2 8029451: Tidy warnings cleanup for java.util package; minor changes in java.nio, java.sql
yan
parents: 22110
diff changeset
   162
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * @param newFormatter the <tt>Formatter</tt> to use (may not be null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * @exception  SecurityException  if a security manager exists and if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     *             the caller does not have <tt>LoggingPermission("control")</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     */
19808
39cb79123ab2 6823527: java.util.logging.Handler has thread safety issues
dfuchs
parents: 18565
diff changeset
   167
    public synchronized void setFormatter(Formatter newFormatter) throws SecurityException {
14216
23714b376286 7169884: LogManager checks do not work correctly for sub-types
alanb
parents: 5506
diff changeset
   168
        checkPermission();
29094
a4fd2b5e49f8 8073479: Replace obj.getClass hacks with Objects.requireNonNull
shade
parents: 25859
diff changeset
   169
        formatter = Objects.requireNonNull(newFormatter);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * Return the <tt>Formatter</tt> for this <tt>Handler</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * @return the <tt>Formatter</tt> (may be null).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    public Formatter getFormatter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        return formatter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * Set the character encoding used by this <tt>Handler</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * The encoding should be set before any <tt>LogRecords</tt> are written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * to the <tt>Handler</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * @param encoding  The name of a supported character encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     *        May be null, to indicate the default platform encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * @exception  SecurityException  if a security manager exists and if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     *             the caller does not have <tt>LoggingPermission("control")</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * @exception  UnsupportedEncodingException if the named encoding is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     *          not supported.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     */
19808
39cb79123ab2 6823527: java.util.logging.Handler has thread safety issues
dfuchs
parents: 18565
diff changeset
   193
    public synchronized void setEncoding(String encoding)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                        throws SecurityException, java.io.UnsupportedEncodingException {
14216
23714b376286 7169884: LogManager checks do not work correctly for sub-types
alanb
parents: 5506
diff changeset
   195
        checkPermission();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        if (encoding != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                if(!java.nio.charset.Charset.isSupported(encoding)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                    throw new UnsupportedEncodingException(encoding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            } catch (java.nio.charset.IllegalCharsetNameException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                throw new UnsupportedEncodingException(encoding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        this.encoding = encoding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * Return the character encoding for this <tt>Handler</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * @return  The encoding name.  May be null, which indicates the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     *          default encoding should be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    public String getEncoding() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        return encoding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * Set a <tt>Filter</tt> to control output on this <tt>Handler</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * For each call of <tt>publish</tt> the <tt>Handler</tt> will call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * this <tt>Filter</tt> (if it is non-null) to check if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * <tt>LogRecord</tt> should be published or discarded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * @param   newFilter  a <tt>Filter</tt> object (may be null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * @exception  SecurityException  if a security manager exists and if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     *             the caller does not have <tt>LoggingPermission("control")</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     */
19808
39cb79123ab2 6823527: java.util.logging.Handler has thread safety issues
dfuchs
parents: 18565
diff changeset
   229
    public synchronized void setFilter(Filter newFilter) throws SecurityException {
14216
23714b376286 7169884: LogManager checks do not work correctly for sub-types
alanb
parents: 5506
diff changeset
   230
        checkPermission();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        filter = newFilter;
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
     * Get the current <tt>Filter</tt> for this <tt>Handler</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * @return  a <tt>Filter</tt> object (may be null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    public Filter getFilter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        return filter;
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
     * Define an ErrorManager for this Handler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * The ErrorManager's "error" method will be invoked if any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * errors occur while using this Handler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * @param em  the new ErrorManager
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * @exception  SecurityException  if a security manager exists and if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     *             the caller does not have <tt>LoggingPermission("control")</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     */
19808
39cb79123ab2 6823527: java.util.logging.Handler has thread safety issues
dfuchs
parents: 18565
diff changeset
   253
    public synchronized void setErrorManager(ErrorManager em) {
14216
23714b376286 7169884: LogManager checks do not work correctly for sub-types
alanb
parents: 5506
diff changeset
   254
        checkPermission();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        if (em == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
           throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        errorManager = em;
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
     * Retrieves the ErrorManager for this Handler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     *
18565
5025d43d3731 8019315: Fix doclint issues in java.util.logging
darcy
parents: 14342
diff changeset
   264
     * @return the ErrorManager for this Handler
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * @exception  SecurityException  if a security manager exists and if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     *             the caller does not have <tt>LoggingPermission("control")</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    public ErrorManager getErrorManager() {
14216
23714b376286 7169884: LogManager checks do not work correctly for sub-types
alanb
parents: 5506
diff changeset
   269
        checkPermission();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        return errorManager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * Protected convenience method to report an error to this Handler's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * ErrorManager.  Note that this method retrieves and uses the ErrorManager
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * without doing a security check.  It can therefore be used in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * environments where the caller may be non-privileged.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * @param msg    a descriptive string (may be null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * @param ex     an exception (may be null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * @param code   an error code defined in ErrorManager
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    protected void reportError(String msg, Exception ex, int code) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            errorManager.error(msg, ex, code);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        } catch (Exception ex2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            System.err.println("Handler.reportError caught:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            ex2.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * Set the log level specifying which message levels will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * logged by this <tt>Handler</tt>.  Message levels lower than this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * value will be discarded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * The intention is to allow developers to turn on voluminous
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * logging, but to limit the messages that are sent to certain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * <tt>Handlers</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * @param newLevel   the new value for the log level
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * @exception  SecurityException  if a security manager exists and if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     *             the caller does not have <tt>LoggingPermission("control")</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    public synchronized void setLevel(Level newLevel) throws SecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        if (newLevel == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        }
14216
23714b376286 7169884: LogManager checks do not work correctly for sub-types
alanb
parents: 5506
diff changeset
   309
        checkPermission();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        logLevel = newLevel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * Get the log level specifying which messages will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * logged by this <tt>Handler</tt>.  Message levels lower
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * than this level will be discarded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * @return  the level of messages being logged.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     */
19808
39cb79123ab2 6823527: java.util.logging.Handler has thread safety issues
dfuchs
parents: 18565
diff changeset
   319
    public Level getLevel() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        return logLevel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * Check if this <tt>Handler</tt> would actually log a given <tt>LogRecord</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * This method checks if the <tt>LogRecord</tt> has an appropriate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * <tt>Level</tt> and  whether it satisfies any <tt>Filter</tt>.  It also
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * may make other <tt>Handler</tt> specific checks that might prevent a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * handler from logging the <tt>LogRecord</tt>. It will return false if
3853
9d2382b74894 6882363: 4/4 typos in java.util.logging javadocs
dcubed
parents: 2
diff changeset
   330
     * the <tt>LogRecord</tt> is null.
24196
61c9885d76e2 8029451: Tidy warnings cleanup for java.util package; minor changes in java.nio, java.sql
yan
parents: 22110
diff changeset
   331
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * @param record  a <tt>LogRecord</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * @return true if the <tt>LogRecord</tt> would be logged.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    public boolean isLoggable(LogRecord record) {
19808
39cb79123ab2 6823527: java.util.logging.Handler has thread safety issues
dfuchs
parents: 18565
diff changeset
   337
        final int levelValue = getLevel().intValue();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        if (record.getLevel().intValue() < levelValue || levelValue == offValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        }
19808
39cb79123ab2 6823527: java.util.logging.Handler has thread safety issues
dfuchs
parents: 18565
diff changeset
   341
        final Filter filter = getFilter();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        if (filter == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        return filter.isLoggable(record);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    // Package-private support method for security checks.
22110
06e486bc20b6 8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents: 19808
diff changeset
   349
    // We check that the caller has appropriate security privileges
06e486bc20b6 8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents: 19808
diff changeset
   350
    // to update Handler state and if not throw a SecurityException.
14216
23714b376286 7169884: LogManager checks do not work correctly for sub-types
alanb
parents: 5506
diff changeset
   351
    void checkPermission() throws SecurityException {
22110
06e486bc20b6 8030801: SocketHandler(host, port) requires permission ("java.util.logging.LoggingPermission" "control")
plevart
parents: 19808
diff changeset
   352
        manager.checkPermission();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
}