Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.logging/share/classes/java/util/logging/SocketHandler.java
41159 views
1
/*
2
* Copyright (c) 2000, 2020, Oracle and/or its affiliates. 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. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle 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 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.
24
*/
25
26
27
package java.util.logging;
28
29
import java.io.*;
30
import java.net.*;
31
32
/**
33
* Simple network logging {@code Handler}.
34
* <p>
35
* {@code LogRecords} are published to a network stream connection. By default
36
* the {@code XMLFormatter} class is used for formatting.
37
* <p>
38
* <b>Configuration:</b>
39
* By default each {@code SocketHandler} is initialized using the following
40
* {@code LogManager} configuration properties where {@code <handler-name>}
41
* refers to the fully-qualified class name of the handler.
42
* If properties are not defined
43
* (or have invalid values) then the specified default values are used.
44
* <ul>
45
* <li> &lt;handler-name&gt;.level
46
* specifies the default level for the {@code Handler}
47
* (defaults to {@code Level.ALL}). </li>
48
* <li> &lt;handler-name&gt;.filter
49
* specifies the name of a {@code Filter} class to use
50
* (defaults to no {@code Filter}). </li>
51
* <li> &lt;handler-name&gt;.formatter
52
* specifies the name of a {@code Formatter} class to use
53
* (defaults to {@code java.util.logging.XMLFormatter}). </li>
54
* <li> &lt;handler-name&gt;.encoding
55
* the name of the character set encoding to use (defaults to
56
* the default platform encoding). </li>
57
* <li> &lt;handler-name&gt;.host
58
* specifies the target host name to connect to (no default). </li>
59
* <li> &lt;handler-name&gt;.port
60
* specifies the target TCP port to use (no default). </li>
61
* </ul>
62
* <p>
63
* For example, the properties for {@code SocketHandler} would be:
64
* <ul>
65
* <li> java.util.logging.SocketHandler.level=INFO </li>
66
* <li> java.util.logging.SocketHandler.formatter=java.util.logging.SimpleFormatter </li>
67
* </ul>
68
* <p>
69
* For a custom handler, e.g. com.foo.MyHandler, the properties would be:
70
* <ul>
71
* <li> com.foo.MyHandler.level=INFO </li>
72
* <li> com.foo.MyHandler.formatter=java.util.logging.SimpleFormatter </li>
73
* </ul>
74
* <p>
75
* The output IO stream is buffered, but is flushed after each
76
* {@code LogRecord} 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
86
/**
87
* Create a {@code SocketHandler}, using only {@code LogManager} properties
88
* (or their defaults).
89
* @throws IllegalArgumentException if the host or port are invalid or
90
* are not specified as LogManager properties.
91
* @throws IOException if we are unable to connect to the target
92
* host and port.
93
*/
94
public SocketHandler() throws IOException {
95
// configure with specific defaults for SocketHandler
96
super(Level.ALL, new XMLFormatter(), null);
97
98
LogManager manager = LogManager.getLogManager();
99
String cname = getClass().getName();
100
port = manager.getIntProperty(cname + ".port", 0);
101
host = manager.getStringProperty(cname + ".host", null);
102
103
try {
104
connect();
105
} catch (IOException ix) {
106
System.err.println("SocketHandler: connect failed to " + host + ":" + port);
107
throw ix;
108
}
109
}
110
111
/**
112
* Construct a {@code SocketHandler} using a specified host and port.
113
*
114
* The {@code SocketHandler} is configured based on {@code LogManager}
115
* properties (or their default values) except that the given target host
116
* and port arguments are used. If the host argument is empty, but not
117
* null String then the localhost is used.
118
*
119
* @param host target host.
120
* @param port target port.
121
*
122
* @throws IllegalArgumentException if the host or port are invalid.
123
* @throws IOException if we are unable to connect to the target
124
* host and port.
125
*/
126
public SocketHandler(String host, int port) throws IOException {
127
// configure with specific defaults for SocketHandler
128
super(Level.ALL, new XMLFormatter(), null);
129
130
this.port = port;
131
this.host = host;
132
133
connect();
134
}
135
136
private void connect() throws IOException {
137
// Check the arguments are valid.
138
if (port == 0) {
139
throw new IllegalArgumentException("Bad port: " + port);
140
}
141
if (host == null) {
142
throw new IllegalArgumentException("Null host name: " + host);
143
}
144
145
// Try to open a new socket.
146
sock = new Socket(host, port);
147
OutputStream out = sock.getOutputStream();
148
BufferedOutputStream bout = new BufferedOutputStream(out);
149
setOutputStreamPrivileged(bout);
150
}
151
152
/**
153
* Close this output stream.
154
*
155
* @throws SecurityException if a security manager exists and if
156
* the caller does not have {@code LoggingPermission("control")}.
157
*/
158
@Override
159
public synchronized void close() throws SecurityException {
160
super.close();
161
if (sock != null) {
162
try {
163
sock.close();
164
} catch (IOException ix) {
165
// drop through.
166
}
167
}
168
sock = null;
169
}
170
171
/**
172
* Format and publish a {@code LogRecord}.
173
*
174
* @param record description of the log event. A null record is
175
* silently ignored and is not published
176
*/
177
@Override
178
public synchronized void publish(LogRecord record) {
179
if (!isLoggable(record)) {
180
return;
181
}
182
super.publish(record);
183
flush();
184
}
185
}
186
187