KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > util > logging > SocketHandler


1 /*
2  * @(#)SocketHandler.java 1.18 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8
9 package java.util.logging;
10
11 import java.io.*;
12 import java.net.*;
13
14 /**
15  * Simple network logging <tt>Handler</tt>.
16  * <p>
17  * <tt>LogRecords</tt> are published to a network stream connection. By default
18  * the <tt>XMLFormatter</tt> class is used for formatting.
19  * <p>
20  * <b>Configuration:</b>
21  * By default each <tt>SocketHandler</tt> is initialized using the following
22  * <tt>LogManager</tt> configuration properties. If properties are not defined
23  * (or have invalid values) then the specified default values are used.
24  * <ul>
25  * <li> java.util.logging.SocketHandler.level
26  * specifies the default level for the <tt>Handler</tt>
27  * (defaults to <tt>Level.ALL</tt>).
28  * <li> java.util.logging.SocketHandler.filter
29  * specifies the name of a <tt>Filter</tt> class to use
30  * (defaults to no <tt>Filter</tt>).
31  * <li> java.util.logging.SocketHandler.formatter
32  * specifies the name of a <tt>Formatter</tt> class to use
33  * (defaults to <tt>java.util.logging.XMLFormatter</tt>).
34  * <li> java.util.logging.SocketHandler.encoding
35  * the name of the character set encoding to use (defaults to
36  * the default platform encoding).
37  * <li> java.util.logging.SocketHandler.host
38  * specifies the target host name to connect to (no default).
39  * <li> java.util.logging.SocketHandler.port
40  * specifies the target TCP port to use (no default).
41  * </ul>
42  * <p>
43  * The output IO stream is buffered, but is flushed after each
44  * <tt>LogRecord</tt> is written.
45  *
46  * @version 1.18, 12/19/03
47  * @since 1.4
48  */

49
50 public class SocketHandler extends StreamHandler JavaDoc {
51     private Socket sock;
52     private String JavaDoc host;
53     private int port;
54     private String JavaDoc portProperty;
55
56     // Private method to configure a SocketHandler from LogManager
57
// properties and/or default values as specified in the class
58
// javadoc.
59
private void configure() {
60         LogManager JavaDoc manager = LogManager.getLogManager();
61     String JavaDoc cname = getClass().getName();
62
63     setLevel(manager.getLevelProperty(cname +".level", Level.ALL));
64     setFilter(manager.getFilterProperty(cname +".filter", null));
65     setFormatter(manager.getFormatterProperty(cname +".formatter", new XMLFormatter JavaDoc()));
66     try {
67         setEncoding(manager.getStringProperty(cname +".encoding", null));
68     } catch (Exception JavaDoc ex) {
69         try {
70             setEncoding(null);
71         } catch (Exception JavaDoc ex2) {
72         // doing a setEncoding with null should always work.
73
// assert false;
74
}
75     }
76     port = manager.getIntProperty(cname + ".port", 0);
77     host = manager.getStringProperty(cname + ".host", null);
78     }
79
80
81     /**
82      * Create a <tt>SocketHandler</tt>, using only <tt>LogManager</tt> properties
83      * (or their defaults).
84      * @throws IllegalArgumentException if the host or port are invalid or
85      * are not specified as LogManager properties.
86      * @throws IOException if we are unable to connect to the target
87      * host and port.
88      */

89     public SocketHandler() throws IOException {
90     // We are going to use the logging defaults.
91
sealed = false;
92     configure();
93
94     try {
95             connect();
96     } catch (IOException ix) {
97         System.err.println("SocketHandler: connect failed to " + host + ":" + port);
98         throw ix;
99     }
100         sealed = true;
101     }
102
103     /**
104      * Construct a <tt>SocketHandler</tt> using a specified host and port.
105      *
106      * The <tt>SocketHandler</tt> is configured based on <tt>LogManager</tt>
107      * properties (or their default values) except that the given target host
108      * and port arguments are used. If the host argument is empty, but not
109      * null String then the localhost is used.
110      *
111      * @param host target host.
112      * @param port target port.
113      *
114      * @throws IllegalArgumentException if the host or port are invalid.
115      * @throws IOException if we are unable to connect to the target
116      * host and port.
117      */

118     public SocketHandler(String JavaDoc host, int port) throws IOException {
119     sealed = false;
120     configure();
121     sealed = true;
122     this.port = port;
123     this.host = host;
124     connect();
125     }
126
127     private void connect() throws IOException {
128     // Check the arguments are valid.
129
if (port == 0) {
130         throw new IllegalArgumentException JavaDoc("Bad port: " + port);
131     }
132     if (host == null) {
133         throw new IllegalArgumentException JavaDoc("Null host name: " + host);
134     }
135
136     // Try to open a new socket.
137
sock = new Socket(host, port);
138     OutputStream out = sock.getOutputStream();
139     BufferedOutputStream bout = new BufferedOutputStream(out);
140     setOutputStream(bout);
141     }
142
143     /**
144      * Close this output stream.
145      *
146      * @exception SecurityException if a security manager exists and if
147      * the caller does not have <tt>LoggingPermission("control")</tt>.
148      */

149     public synchronized void close() throws SecurityException JavaDoc {
150     super.close();
151     if (sock != null) {
152         try {
153             sock.close();
154         } catch (IOException ix) {
155         // drop through.
156
}
157     }
158     sock = null;
159     }
160
161     /**
162      * Format and publish a <tt>LogRecord</tt>.
163      *
164      * @param record description of the log event. A null record is
165      * silently ignored and is not published
166      */

167     public synchronized void publish(LogRecord JavaDoc record) {
168     if (!isLoggable(record)) {
169         return;
170     }
171     super.publish(record);
172     flush();
173     }
174 }
175
Popular Tags