0N/A/*
2362N/A * Copyright (c) 2000, 2003, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A
0N/Apackage java.util.logging;
0N/A
0N/Aimport java.io.*;
0N/Aimport java.net.*;
0N/A
0N/A/**
0N/A * Simple network logging <tt>Handler</tt>.
0N/A * <p>
0N/A * <tt>LogRecords</tt> are published to a network stream connection. By default
0N/A * the <tt>XMLFormatter</tt> class is used for formatting.
0N/A * <p>
0N/A * <b>Configuration:</b>
0N/A * By default each <tt>SocketHandler</tt> is initialized using the following
0N/A * <tt>LogManager</tt> configuration properties. If properties are not defined
0N/A * (or have invalid values) then the specified default values are used.
0N/A * <ul>
0N/A * <li> java.util.logging.SocketHandler.level
0N/A * specifies the default level for the <tt>Handler</tt>
0N/A * (defaults to <tt>Level.ALL</tt>).
0N/A * <li> java.util.logging.SocketHandler.filter
0N/A * specifies the name of a <tt>Filter</tt> class to use
0N/A * (defaults to no <tt>Filter</tt>).
0N/A * <li> java.util.logging.SocketHandler.formatter
0N/A * specifies the name of a <tt>Formatter</tt> class to use
0N/A * (defaults to <tt>java.util.logging.XMLFormatter</tt>).
0N/A * <li> java.util.logging.SocketHandler.encoding
0N/A * the name of the character set encoding to use (defaults to
0N/A * the default platform encoding).
0N/A * <li> java.util.logging.SocketHandler.host
0N/A * specifies the target host name to connect to (no default).
0N/A * <li> java.util.logging.SocketHandler.port
0N/A * specifies the target TCP port to use (no default).
0N/A * </ul>
0N/A * <p>
0N/A * The output IO stream is buffered, but is flushed after each
0N/A * <tt>LogRecord</tt> is written.
0N/A *
0N/A * @since 1.4
0N/A */
0N/A
0N/Apublic class SocketHandler extends StreamHandler {
0N/A private Socket sock;
0N/A private String host;
0N/A private int port;
0N/A private String portProperty;
0N/A
0N/A // Private method to configure a SocketHandler from LogManager
0N/A // properties and/or default values as specified in the class
0N/A // javadoc.
0N/A private void configure() {
0N/A LogManager manager = LogManager.getLogManager();
0N/A String cname = getClass().getName();
0N/A
0N/A setLevel(manager.getLevelProperty(cname +".level", Level.ALL));
0N/A setFilter(manager.getFilterProperty(cname +".filter", null));
0N/A setFormatter(manager.getFormatterProperty(cname +".formatter", new XMLFormatter()));
0N/A try {
0N/A setEncoding(manager.getStringProperty(cname +".encoding", null));
0N/A } catch (Exception ex) {
0N/A try {
0N/A setEncoding(null);
0N/A } catch (Exception ex2) {
0N/A // doing a setEncoding with null should always work.
0N/A // assert false;
0N/A }
0N/A }
0N/A port = manager.getIntProperty(cname + ".port", 0);
0N/A host = manager.getStringProperty(cname + ".host", null);
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Create a <tt>SocketHandler</tt>, using only <tt>LogManager</tt> properties
0N/A * (or their defaults).
0N/A * @throws IllegalArgumentException if the host or port are invalid or
0N/A * are not specified as LogManager properties.
0N/A * @throws IOException if we are unable to connect to the target
0N/A * host and port.
0N/A */
0N/A public SocketHandler() throws IOException {
0N/A // We are going to use the logging defaults.
0N/A sealed = false;
0N/A configure();
0N/A
0N/A try {
0N/A connect();
0N/A } catch (IOException ix) {
0N/A System.err.println("SocketHandler: connect failed to " + host + ":" + port);
0N/A throw ix;
0N/A }
0N/A sealed = true;
0N/A }
0N/A
0N/A /**
0N/A * Construct a <tt>SocketHandler</tt> using a specified host and port.
0N/A *
0N/A * The <tt>SocketHandler</tt> is configured based on <tt>LogManager</tt>
0N/A * properties (or their default values) except that the given target host
0N/A * and port arguments are used. If the host argument is empty, but not
0N/A * null String then the localhost is used.
0N/A *
0N/A * @param host target host.
0N/A * @param port target port.
0N/A *
0N/A * @throws IllegalArgumentException if the host or port are invalid.
0N/A * @throws IOException if we are unable to connect to the target
0N/A * host and port.
0N/A */
0N/A public SocketHandler(String host, int port) throws IOException {
0N/A sealed = false;
0N/A configure();
0N/A sealed = true;
0N/A this.port = port;
0N/A this.host = host;
0N/A connect();
0N/A }
0N/A
0N/A private void connect() throws IOException {
0N/A // Check the arguments are valid.
0N/A if (port == 0) {
0N/A throw new IllegalArgumentException("Bad port: " + port);
0N/A }
0N/A if (host == null) {
0N/A throw new IllegalArgumentException("Null host name: " + host);
0N/A }
0N/A
0N/A // Try to open a new socket.
0N/A sock = new Socket(host, port);
0N/A OutputStream out = sock.getOutputStream();
0N/A BufferedOutputStream bout = new BufferedOutputStream(out);
0N/A setOutputStream(bout);
0N/A }
0N/A
0N/A /**
0N/A * Close this output stream.
0N/A *
0N/A * @exception SecurityException if a security manager exists and if
0N/A * the caller does not have <tt>LoggingPermission("control")</tt>.
0N/A */
0N/A public synchronized void close() throws SecurityException {
0N/A super.close();
0N/A if (sock != null) {
0N/A try {
0N/A sock.close();
0N/A } catch (IOException ix) {
0N/A // drop through.
0N/A }
0N/A }
0N/A sock = null;
0N/A }
0N/A
0N/A /**
0N/A * Format and publish a <tt>LogRecord</tt>.
0N/A *
0N/A * @param record description of the log event. A null record is
0N/A * silently ignored and is not published
0N/A */
0N/A public synchronized void publish(LogRecord record) {
0N/A if (!isLoggable(record)) {
0N/A return;
0N/A }
0N/A super.publish(record);
0N/A flush();
0N/A }
0N/A}