blob: 520da790b557598d3c4c0548e1f53f199eedb358 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2000-2003 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26
27package java.util.logging;
28
29import java.io.*;
30import java.net.*;
31
32/**
33 * Simple network logging <tt>Handler</tt>.
34 * <p>
35 * <tt>LogRecords</tt> are published to a network stream connection. By default
36 * the <tt>XMLFormatter</tt> class is used for formatting.
37 * <p>
38 * <b>Configuration:</b>
39 * By default each <tt>SocketHandler</tt> is initialized using the following
40 * <tt>LogManager</tt> configuration properties. If properties are not defined
41 * (or have invalid values) then the specified default values are used.
42 * <ul>
43 * <li> java.util.logging.SocketHandler.level
44 * specifies the default level for the <tt>Handler</tt>
45 * (defaults to <tt>Level.ALL</tt>).
46 * <li> java.util.logging.SocketHandler.filter
47 * specifies the name of a <tt>Filter</tt> class to use
48 * (defaults to no <tt>Filter</tt>).
49 * <li> java.util.logging.SocketHandler.formatter
50 * specifies the name of a <tt>Formatter</tt> class to use
51 * (defaults to <tt>java.util.logging.XMLFormatter</tt>).
52 * <li> java.util.logging.SocketHandler.encoding
53 * the name of the character set encoding to use (defaults to
54 * the default platform encoding).
55 * <li> java.util.logging.SocketHandler.host
56 * specifies the target host name to connect to (no default).
57 * <li> java.util.logging.SocketHandler.port
58 * specifies the target TCP port to use (no default).
59 * </ul>
60 * <p>
61 * The output IO stream is buffered, but is flushed after each
62 * <tt>LogRecord</tt> is written.
63 *
64 * @since 1.4
65 */
66
67public class SocketHandler extends StreamHandler {
68 private Socket sock;
69 private String host;
70 private int port;
71 private String portProperty;
72
73 // Private method to configure a SocketHandler from LogManager
74 // properties and/or default values as specified in the class
75 // javadoc.
76 private void configure() {
77 LogManager manager = LogManager.getLogManager();
78 String cname = getClass().getName();
79
80 setLevel(manager.getLevelProperty(cname +".level", Level.ALL));
81 setFilter(manager.getFilterProperty(cname +".filter", null));
82 setFormatter(manager.getFormatterProperty(cname +".formatter", new XMLFormatter()));
83 try {
84 setEncoding(manager.getStringProperty(cname +".encoding", null));
85 } catch (Exception ex) {
86 try {
87 setEncoding(null);
88 } catch (Exception ex2) {
89 // doing a setEncoding with null should always work.
90 // assert false;
91 }
92 }
93 port = manager.getIntProperty(cname + ".port", 0);
94 host = manager.getStringProperty(cname + ".host", null);
95 }
96
97
98 /**
99 * Create a <tt>SocketHandler</tt>, using only <tt>LogManager</tt> properties
100 * (or their defaults).
101 * @throws IllegalArgumentException if the host or port are invalid or
102 * are not specified as LogManager properties.
103 * @throws IOException if we are unable to connect to the target
104 * host and port.
105 */
106 public SocketHandler() throws IOException {
107 // We are going to use the logging defaults.
108 sealed = false;
109 configure();
110
111 try {
112 connect();
113 } catch (IOException ix) {
114 System.err.println("SocketHandler: connect failed to " + host + ":" + port);
115 throw ix;
116 }
117 sealed = true;
118 }
119
120 /**
121 * Construct a <tt>SocketHandler</tt> using a specified host and port.
122 *
123 * The <tt>SocketHandler</tt> is configured based on <tt>LogManager</tt>
124 * properties (or their default values) except that the given target host
125 * and port arguments are used. If the host argument is empty, but not
126 * null String then the localhost is used.
127 *
128 * @param host target host.
129 * @param port target port.
130 *
131 * @throws IllegalArgumentException if the host or port are invalid.
132 * @throws IOException if we are unable to connect to the target
133 * host and port.
134 */
135 public SocketHandler(String host, int port) throws IOException {
136 sealed = false;
137 configure();
138 sealed = true;
139 this.port = port;
140 this.host = host;
141 connect();
142 }
143
144 private void connect() throws IOException {
145 // Check the arguments are valid.
146 if (port == 0) {
147 throw new IllegalArgumentException("Bad port: " + port);
148 }
149 if (host == null) {
150 throw new IllegalArgumentException("Null host name: " + host);
151 }
152
153 // Try to open a new socket.
154 sock = new Socket(host, port);
155 OutputStream out = sock.getOutputStream();
156 BufferedOutputStream bout = new BufferedOutputStream(out);
157 setOutputStream(bout);
158 }
159
160 /**
161 * Close this output stream.
162 *
163 * @exception SecurityException if a security manager exists and if
164 * the caller does not have <tt>LoggingPermission("control")</tt>.
165 */
166 public synchronized void close() throws SecurityException {
167 super.close();
168 if (sock != null) {
169 try {
170 sock.close();
171 } catch (IOException ix) {
172 // drop through.
173 }
174 }
175 sock = null;
176 }
177
178 /**
179 * Format and publish a <tt>LogRecord</tt>.
180 *
181 * @param record description of the log event. A null record is
182 * silently ignored and is not published
183 */
184 public synchronized void publish(LogRecord record) {
185 if (!isLoggable(record)) {
186 return;
187 }
188 super.publish(record);
189 flush();
190 }
191}