jdk/src/share/classes/java/util/logging/SocketHandler.java
author mchung
Fri, 15 Apr 2011 23:42:12 -0700
changeset 9271 2d7e19ff03d1
parent 5506 202f599c92aa
child 14321 7f9f265ac11e
permissions -rw-r--r--
7032589: FileHandler leaking file descriptor of the file lock Reviewed-by: forax, dcubed
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2000, 2003, 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: 2
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: 2
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: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
package java.util.logging;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * Simple network logging <tt>Handler</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * <tt>LogRecords</tt> are published to a network stream connection.  By default
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * the <tt>XMLFormatter</tt> class is used for formatting.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * <b>Configuration:</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * By default each <tt>SocketHandler</tt> is initialized using the following
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * <tt>LogManager</tt> configuration properties.  If properties are not defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * (or have invalid values) then the specified default values are used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <li>   java.util.logging.SocketHandler.level
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *        specifies the default level for the <tt>Handler</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *        (defaults to <tt>Level.ALL</tt>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <li>   java.util.logging.SocketHandler.filter
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *        specifies the name of a <tt>Filter</tt> class to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *        (defaults to no <tt>Filter</tt>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * <li>   java.util.logging.SocketHandler.formatter
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *        specifies the name of a <tt>Formatter</tt> class to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *        (defaults to <tt>java.util.logging.XMLFormatter</tt>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * <li>   java.util.logging.SocketHandler.encoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *        the name of the character set encoding to use (defaults to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *        the default platform encoding).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * <li>   java.util.logging.SocketHandler.host
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *        specifies the target host name to connect to (no default).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * <li>   java.util.logging.SocketHandler.port
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *        specifies the target TCP port to use (no default).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * The output IO stream is buffered, but is flushed after each
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * <tt>LogRecord</tt> is written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
public class SocketHandler extends StreamHandler {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private Socket sock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private String host;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private int port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private String portProperty;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    // Private method to configure a SocketHandler from LogManager
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    // properties and/or default values as specified in the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    // javadoc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    private void configure() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        LogManager manager = LogManager.getLogManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        String cname = getClass().getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        setLevel(manager.getLevelProperty(cname +".level", Level.ALL));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        setFilter(manager.getFilterProperty(cname +".filter", null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        setFormatter(manager.getFormatterProperty(cname +".formatter", new XMLFormatter()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            setEncoding(manager.getStringProperty(cname +".encoding", null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                setEncoding(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            } catch (Exception ex2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                // doing a setEncoding with null should always work.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                // assert false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        port = manager.getIntProperty(cname + ".port", 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        host = manager.getStringProperty(cname + ".host", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * Create a <tt>SocketHandler</tt>, using only <tt>LogManager</tt> properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * (or their defaults).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * @throws IllegalArgumentException if the host or port are invalid or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     *          are not specified as LogManager properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * @throws IOException if we are unable to connect to the target
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     *         host and port.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    public SocketHandler() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        // We are going to use the logging defaults.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        sealed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        configure();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            connect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        } catch (IOException ix) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            System.err.println("SocketHandler: connect failed to " + host + ":" + port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            throw ix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        sealed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * Construct a <tt>SocketHandler</tt> using a specified host and port.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * The <tt>SocketHandler</tt> is configured based on <tt>LogManager</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * properties (or their default values) except that the given target host
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * and port arguments are used. If the host argument is empty, but not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * null String then the localhost is used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * @param host target host.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * @param port target port.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * @throws IllegalArgumentException if the host or port are invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * @throws IOException if we are unable to connect to the target
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     *         host and port.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    public SocketHandler(String host, int port) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        sealed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        configure();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        sealed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        this.port = port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        this.host = host;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        connect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    private void connect() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        // Check the arguments are valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        if (port == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            throw new IllegalArgumentException("Bad port: " + port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        if (host == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            throw new IllegalArgumentException("Null host name: " + host);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        // Try to open a new socket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        sock = new Socket(host, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        OutputStream out = sock.getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        BufferedOutputStream bout = new BufferedOutputStream(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        setOutputStream(bout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * Close this output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * @exception  SecurityException  if a security manager exists and if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     *             the caller does not have <tt>LoggingPermission("control")</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    public synchronized void close() throws SecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        super.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        if (sock != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                sock.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            } catch (IOException ix) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                // drop through.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        sock = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * Format and publish a <tt>LogRecord</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * @param  record  description of the log event. A null record is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     *                 silently ignored and is not published
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    public synchronized void publish(LogRecord record) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        if (!isLoggable(record)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        super.publish(record);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
}