001/* SocketHandler.java -- a class for publishing log messages to network sockets 002 Copyright (C) 2002 Free Software Foundation, Inc. 003 004This file is part of GNU Classpath. 005 006GNU Classpath is free software; you can redistribute it and/or modify 007it under the terms of the GNU General Public License as published by 008the Free Software Foundation; either version 2, or (at your option) 009any later version. 010 011GNU Classpath is distributed in the hope that it will be useful, but 012WITHOUT ANY WARRANTY; without even the implied warranty of 013MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014General Public License for more details. 015 016You should have received a copy of the GNU General Public License 017along with GNU Classpath; see the file COPYING. If not, write to the 018Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 01902110-1301 USA. 020 021Linking this library statically or dynamically with other modules is 022making a combined work based on this library. Thus, the terms and 023conditions of the GNU General Public License cover the whole 024combination. 025 026As a special exception, the copyright holders of this library give you 027permission to link this library with independent modules to produce an 028executable, regardless of the license terms of these independent 029modules, and to copy and distribute the resulting executable under 030terms of your choice, provided that you also meet, for each linked 031independent module, the terms and conditions of the license of that 032module. An independent module is a module which is not derived from 033or based on this library. If you modify this library, you may extend 034this exception to your version of the library, but you are not 035obligated to do so. If you do not wish to do so, delete this 036exception statement from your version. */ 037 038 039package java.util.logging; 040 041 042/** 043 * A <code>SocketHandler</code> publishes log records to 044 * a TCP/IP socket. 045 * 046 * <p><strong>Configuration:</strong> Values of the subsequent 047 * <code>LogManager</code> properties are taken into consideration 048 * when a <code>SocketHandler</code> is initialized. 049 * If a property is not defined, or if it has an invalid 050 * value, a default is taken without an exception being thrown. 051 * 052 * <ul> 053 * 054 * <li><code>java.util.SocketHandler.level</code> - specifies 055 * the initial severity level threshold. Default value: 056 * <code>Level.ALL</code>.</li> 057 * 058 * <li><code>java.util.SocketHandler.filter</code> - specifies 059 * the name of a Filter class. Default value: No Filter.</li> 060 * 061 * <li><code>java.util.SocketHandler.formatter</code> - specifies 062 * the name of a Formatter class. Default value: 063 * <code>java.util.logging.XMLFormatter</code>.</li> 064 * 065 * <li><code>java.util.SocketHandler.encoding</code> - specifies 066 * the name of the character encoding. Default value: 067 * the default platform encoding.</li> 068 * 069 * <li><code>java.util.SocketHandler.host</code> - specifies 070 * the name of the host to which records are published. 071 * There is no default value for this property; if it is 072 * not set, the SocketHandler constructor will throw 073 * an exception.</li> 074 * 075 * <li><code>java.util.SocketHandler.port</code> - specifies 076 * the TCP/IP port to which records are published. 077 * There is no default value for this property; if it is 078 * not set, the SocketHandler constructor will throw 079 * an exception.</li> 080 * 081 * </ul> 082 * 083 * @author Sascha Brawer (brawer@acm.org) 084 */ 085public class SocketHandler 086 extends StreamHandler 087{ 088 /** 089 * Constructs a <code>SocketHandler</code> that publishes log 090 * records to a TCP/IP socket. Tthe initial configuration is 091 * determined by the <code>LogManager</code> properties described 092 * above. 093 * 094 * @throws java.io.IOException if the connection to the specified 095 * network host and port cannot be established. 096 * 097 * @throws java.lang.IllegalArgumentException if either the 098 * <code>java.util.logging.SocketHandler.host</code> 099 * or <code>java.util.logging.SocketHandler.port</code> 100 * LogManager properties is not defined, or specifies 101 * an invalid value. 102 */ 103 public SocketHandler() 104 throws java.io.IOException 105 { 106 this(LogManager.getLogManager().getProperty("java.util.logging.SocketHandler.host"), 107 getPortNumber()); 108 } 109 110 111 /** 112 * Constructs a <code>SocketHandler</code> that publishes log 113 * records to a TCP/IP socket. With the exception of the internet 114 * host and port, the initial configuration is determined by the 115 * <code>LogManager</code> properties described above. 116 * 117 * @param host the Internet host to which log records will be 118 * forwarded. 119 * 120 * @param port the port at the host which will accept a request 121 * for a TCP/IP connection. 122 * 123 * @throws java.io.IOException if the connection to the specified 124 * network host and port cannot be established. 125 * 126 * @throws java.lang.IllegalArgumentException if either 127 * <code>host</code> or <code>port</code> specify 128 * an invalid value. 129 */ 130 public SocketHandler(String host, int port) 131 throws java.io.IOException 132 { 133 super(createSocket(host, port), 134 "java.util.logging.SocketHandler", 135 /* default level */ Level.ALL, 136 /* formatter */ null, 137 /* default formatter */ XMLFormatter.class); 138 } 139 140 141 /** 142 * Retrieves the port number from the java.util.logging.SocketHandler.port 143 * LogManager property. 144 * 145 * @throws IllegalArgumentException if the property is not defined or 146 * does not specify an integer value. 147 */ 148 private static int getPortNumber() 149 { 150 try { 151 return Integer.parseInt(LogManager.getLogManager().getProperty("java.util.logging.SocketHandler.port")); 152 } catch (Exception ex) { 153 throw new IllegalArgumentException(); 154 } 155 } 156 157 158 /** 159 * Creates an OutputStream for publishing log records to an Internet 160 * host and port. This private method is a helper for use by the 161 * constructor of SocketHandler. 162 * 163 * @param host the Internet host to which log records will be 164 * forwarded. 165 * 166 * @param port the port at the host which will accept a request 167 * for a TCP/IP connection. 168 * 169 * @throws java.io.IOException if the connection to the specified 170 * network host and port cannot be established. 171 * 172 * @throws java.lang.IllegalArgumentException if either 173 * <code>host</code> or <code>port</code> specify 174 * an invalid value. 175 */ 176 private static java.io.OutputStream createSocket(String host, int port) 177 throws java.io.IOException, java.lang.IllegalArgumentException 178 { 179 java.net.Socket socket; 180 181 if ((host == null) || (port < 1)) 182 throw new IllegalArgumentException(); 183 184 socket = new java.net.Socket(host, port); 185 186 socket.shutdownInput(); 187 188 /* The architecture of the logging framework provides replaceable 189 * formatters. Because these formatters perform their task by 190 * returning one single String for each LogRecord to be formatted, 191 * there is no need to buffer. 192 */ 193 socket.setTcpNoDelay(true); 194 195 return socket.getOutputStream(); 196 } 197 198 199 /** 200 * Publishes a <code>LogRecord</code> to the network socket, 201 * provided the record passes all tests for being loggable. 202 * In addition, all data that may have been buffered will 203 * be forced to the network stream. 204 * 205 * <p>Most applications do not need to call this method directly. 206 * Instead, they will use a {@link Logger} instance, which will 207 * create LogRecords and distribute them to registered handlers. 208 * 209 * <p>In case of an I/O failure, the <code>ErrorManager</code> 210 * of this <code>SocketHandler</code> will be informed, but the caller 211 * of this method will not receive an exception. 212 * 213 * @param record the log event to be published. 214 */ 215 public void publish(LogRecord record) 216 { 217 super.publish(record); 218 flush(); 219 } 220} 221