author | jgish |
Thu, 25 Oct 2012 15:04:09 -0700 | |
changeset 14321 | 7f9f265ac11e |
parent 5506 | 202f599c92aa |
child 19808 | 39cb79123ab2 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
14321
7f9f265ac11e
7159567: inconsistent configuration of MemoryHandler
jgish
parents:
5506
diff
changeset
|
2 |
* Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
||
27 |
package java.util.logging; |
|
28 |
||
29 |
import java.io.*; |
|
30 |
import 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 |
|
14321
7f9f265ac11e
7159567: inconsistent configuration of MemoryHandler
jgish
parents:
5506
diff
changeset
|
40 |
* <tt>LogManager</tt> configuration properties where <tt><handler-name></tt> |
7f9f265ac11e
7159567: inconsistent configuration of MemoryHandler
jgish
parents:
5506
diff
changeset
|
41 |
* refers to the fully-qualified class name of the handler. |
7f9f265ac11e
7159567: inconsistent configuration of MemoryHandler
jgish
parents:
5506
diff
changeset
|
42 |
* If properties are not defined |
2 | 43 |
* (or have invalid values) then the specified default values are used. |
44 |
* <ul> |
|
14321
7f9f265ac11e
7159567: inconsistent configuration of MemoryHandler
jgish
parents:
5506
diff
changeset
|
45 |
* <li> <handler-name>.level |
2 | 46 |
* specifies the default level for the <tt>Handler</tt> |
14321
7f9f265ac11e
7159567: inconsistent configuration of MemoryHandler
jgish
parents:
5506
diff
changeset
|
47 |
* (defaults to <tt>Level.ALL</tt>). </li> |
7f9f265ac11e
7159567: inconsistent configuration of MemoryHandler
jgish
parents:
5506
diff
changeset
|
48 |
* <li> <handler-name>.filter |
2 | 49 |
* specifies the name of a <tt>Filter</tt> class to use |
14321
7f9f265ac11e
7159567: inconsistent configuration of MemoryHandler
jgish
parents:
5506
diff
changeset
|
50 |
* (defaults to no <tt>Filter</tt>). </li> |
7f9f265ac11e
7159567: inconsistent configuration of MemoryHandler
jgish
parents:
5506
diff
changeset
|
51 |
* <li> <handler-name>.formatter |
2 | 52 |
* specifies the name of a <tt>Formatter</tt> class to use |
14321
7f9f265ac11e
7159567: inconsistent configuration of MemoryHandler
jgish
parents:
5506
diff
changeset
|
53 |
* (defaults to <tt>java.util.logging.XMLFormatter</tt>). </li> |
7f9f265ac11e
7159567: inconsistent configuration of MemoryHandler
jgish
parents:
5506
diff
changeset
|
54 |
* <li> <handler-name>.encoding |
2 | 55 |
* the name of the character set encoding to use (defaults to |
14321
7f9f265ac11e
7159567: inconsistent configuration of MemoryHandler
jgish
parents:
5506
diff
changeset
|
56 |
* the default platform encoding). </li> |
7f9f265ac11e
7159567: inconsistent configuration of MemoryHandler
jgish
parents:
5506
diff
changeset
|
57 |
* <li> <handler-name>.host |
7f9f265ac11e
7159567: inconsistent configuration of MemoryHandler
jgish
parents:
5506
diff
changeset
|
58 |
* specifies the target host name to connect to (no default). </li> |
7f9f265ac11e
7159567: inconsistent configuration of MemoryHandler
jgish
parents:
5506
diff
changeset
|
59 |
* <li> <handler-name>.port |
7f9f265ac11e
7159567: inconsistent configuration of MemoryHandler
jgish
parents:
5506
diff
changeset
|
60 |
* specifies the target TCP port to use (no default). </li> |
7f9f265ac11e
7159567: inconsistent configuration of MemoryHandler
jgish
parents:
5506
diff
changeset
|
61 |
* </ul> |
7f9f265ac11e
7159567: inconsistent configuration of MemoryHandler
jgish
parents:
5506
diff
changeset
|
62 |
* <p> |
7f9f265ac11e
7159567: inconsistent configuration of MemoryHandler
jgish
parents:
5506
diff
changeset
|
63 |
* For example, the properties for {@code SocketHandler} would be: |
7f9f265ac11e
7159567: inconsistent configuration of MemoryHandler
jgish
parents:
5506
diff
changeset
|
64 |
* <ul> |
7f9f265ac11e
7159567: inconsistent configuration of MemoryHandler
jgish
parents:
5506
diff
changeset
|
65 |
* <li> java.util.logging.SocketHandler.level=INFO </li> |
7f9f265ac11e
7159567: inconsistent configuration of MemoryHandler
jgish
parents:
5506
diff
changeset
|
66 |
* <li> java.util.logging.SocketHandler.formatter=java.util.logging.SimpleFormatter </li> |
7f9f265ac11e
7159567: inconsistent configuration of MemoryHandler
jgish
parents:
5506
diff
changeset
|
67 |
* </ul> |
7f9f265ac11e
7159567: inconsistent configuration of MemoryHandler
jgish
parents:
5506
diff
changeset
|
68 |
* <p> |
7f9f265ac11e
7159567: inconsistent configuration of MemoryHandler
jgish
parents:
5506
diff
changeset
|
69 |
* For a custom handler, e.g. com.foo.MyHandler, the properties would be: |
7f9f265ac11e
7159567: inconsistent configuration of MemoryHandler
jgish
parents:
5506
diff
changeset
|
70 |
* <ul> |
7f9f265ac11e
7159567: inconsistent configuration of MemoryHandler
jgish
parents:
5506
diff
changeset
|
71 |
* <li> com.foo.MyHandler.level=INFO </li> |
7f9f265ac11e
7159567: inconsistent configuration of MemoryHandler
jgish
parents:
5506
diff
changeset
|
72 |
* <li> com.foo.MyHandler.formatter=java.util.logging.SimpleFormatter </li> |
2 | 73 |
* </ul> |
74 |
* <p> |
|
75 |
* The output IO stream is buffered, but is flushed after each |
|
76 |
* <tt>LogRecord</tt> is written. |
|
77 |
* |
|
78 |
* @since 1.4 |
|
79 |
*/ |
|
80 |
||
81 |
public class SocketHandler extends StreamHandler { |
|
82 |
private Socket sock; |
|
83 |
private String host; |
|
84 |
private int port; |
|
85 |
private String portProperty; |
|
86 |
||
87 |
// Private method to configure a SocketHandler from LogManager |
|
88 |
// properties and/or default values as specified in the class |
|
89 |
// javadoc. |
|
90 |
private void configure() { |
|
91 |
LogManager manager = LogManager.getLogManager(); |
|
92 |
String cname = getClass().getName(); |
|
93 |
||
94 |
setLevel(manager.getLevelProperty(cname +".level", Level.ALL)); |
|
95 |
setFilter(manager.getFilterProperty(cname +".filter", null)); |
|
96 |
setFormatter(manager.getFormatterProperty(cname +".formatter", new XMLFormatter())); |
|
97 |
try { |
|
98 |
setEncoding(manager.getStringProperty(cname +".encoding", null)); |
|
99 |
} catch (Exception ex) { |
|
100 |
try { |
|
101 |
setEncoding(null); |
|
102 |
} catch (Exception ex2) { |
|
103 |
// doing a setEncoding with null should always work. |
|
104 |
// assert false; |
|
105 |
} |
|
106 |
} |
|
107 |
port = manager.getIntProperty(cname + ".port", 0); |
|
108 |
host = manager.getStringProperty(cname + ".host", null); |
|
109 |
} |
|
110 |
||
111 |
||
112 |
/** |
|
113 |
* Create a <tt>SocketHandler</tt>, using only <tt>LogManager</tt> properties |
|
114 |
* (or their defaults). |
|
115 |
* @throws IllegalArgumentException if the host or port are invalid or |
|
116 |
* are not specified as LogManager properties. |
|
117 |
* @throws IOException if we are unable to connect to the target |
|
118 |
* host and port. |
|
119 |
*/ |
|
120 |
public SocketHandler() throws IOException { |
|
121 |
// We are going to use the logging defaults. |
|
122 |
sealed = false; |
|
123 |
configure(); |
|
124 |
||
125 |
try { |
|
126 |
connect(); |
|
127 |
} catch (IOException ix) { |
|
128 |
System.err.println("SocketHandler: connect failed to " + host + ":" + port); |
|
129 |
throw ix; |
|
130 |
} |
|
131 |
sealed = true; |
|
132 |
} |
|
133 |
||
134 |
/** |
|
135 |
* Construct a <tt>SocketHandler</tt> using a specified host and port. |
|
136 |
* |
|
137 |
* The <tt>SocketHandler</tt> is configured based on <tt>LogManager</tt> |
|
138 |
* properties (or their default values) except that the given target host |
|
139 |
* and port arguments are used. If the host argument is empty, but not |
|
140 |
* null String then the localhost is used. |
|
141 |
* |
|
142 |
* @param host target host. |
|
143 |
* @param port target port. |
|
144 |
* |
|
145 |
* @throws IllegalArgumentException if the host or port are invalid. |
|
146 |
* @throws IOException if we are unable to connect to the target |
|
147 |
* host and port. |
|
148 |
*/ |
|
149 |
public SocketHandler(String host, int port) throws IOException { |
|
150 |
sealed = false; |
|
151 |
configure(); |
|
152 |
sealed = true; |
|
153 |
this.port = port; |
|
154 |
this.host = host; |
|
155 |
connect(); |
|
156 |
} |
|
157 |
||
158 |
private void connect() throws IOException { |
|
159 |
// Check the arguments are valid. |
|
160 |
if (port == 0) { |
|
161 |
throw new IllegalArgumentException("Bad port: " + port); |
|
162 |
} |
|
163 |
if (host == null) { |
|
164 |
throw new IllegalArgumentException("Null host name: " + host); |
|
165 |
} |
|
166 |
||
167 |
// Try to open a new socket. |
|
168 |
sock = new Socket(host, port); |
|
169 |
OutputStream out = sock.getOutputStream(); |
|
170 |
BufferedOutputStream bout = new BufferedOutputStream(out); |
|
171 |
setOutputStream(bout); |
|
172 |
} |
|
173 |
||
174 |
/** |
|
175 |
* Close this output stream. |
|
176 |
* |
|
177 |
* @exception SecurityException if a security manager exists and if |
|
178 |
* the caller does not have <tt>LoggingPermission("control")</tt>. |
|
179 |
*/ |
|
180 |
public synchronized void close() throws SecurityException { |
|
181 |
super.close(); |
|
182 |
if (sock != null) { |
|
183 |
try { |
|
184 |
sock.close(); |
|
185 |
} catch (IOException ix) { |
|
186 |
// drop through. |
|
187 |
} |
|
188 |
} |
|
189 |
sock = null; |
|
190 |
} |
|
191 |
||
192 |
/** |
|
193 |
* Format and publish a <tt>LogRecord</tt>. |
|
194 |
* |
|
195 |
* @param record description of the log event. A null record is |
|
196 |
* silently ignored and is not published |
|
197 |
*/ |
|
198 |
public synchronized void publish(LogRecord record) { |
|
199 |
if (!isLoggable(record)) { |
|
200 |
return; |
|
201 |
} |
|
202 |
super.publish(record); |
|
203 |
flush(); |
|
204 |
} |
|
205 |
} |