--- old/src/share/classes/java/util/logging/SocketHandler.java 2013-12-14 18:14:18.737816374 +0100
+++ new/src/share/classes/java/util/logging/SocketHandler.java 2013-12-14 18:14:18.615818527 +0100
@@ -28,6 +28,8 @@
import java.io.*;
import java.net.*;
+import java.security.AccessController;
+import java.security.PrivilegedAction;
/**
* Simple network logging Handler.
@@ -83,30 +85,76 @@
private String host;
private int port;
- // Private method to configure a SocketHandler from LogManager
- // properties and/or default values as specified in the class
+ // Private PrivilegedAction to configure a SocketHandler from constructor parameters,
+ // LogManager properties and/or default values as specified in the class
// javadoc.
- private void configure() {
- LogManager manager = LogManager.getLogManager();
- String cname = getClass().getName();
-
- setLevel(manager.getLevelProperty(cname +".level", Level.ALL));
- setFilter(manager.getFilterProperty(cname +".filter", null));
- setFormatter(manager.getFormatterProperty(cname +".formatter", new XMLFormatter()));
- try {
- setEncoding(manager.getStringProperty(cname +".encoding", null));
- } catch (Exception ex) {
+ private class ConfigureAction implements PrivilegedAction {
+ private final String host;
+ private final int port;
+ private final boolean hostAndPortSet;
+
+ ConfigureAction() {
+ this.host = null;
+ this.port = 0;
+ this.hostAndPortSet = false;
+ }
+
+ ConfigureAction(String host, int port) {
+ this.host = host;
+ this.port = port;
+ this.hostAndPortSet = true;
+ }
+
+ @Override
+ public Void run() {
+ LogManager manager = LogManager.getLogManager();
+ String cname = SocketHandler.this.getClass().getName();
+
+ setLevel(manager.getLevelProperty(cname +".level", Level.ALL));
+ setFilter(manager.getFilterProperty(cname +".filter", null));
+ setFormatter(manager.getFormatterProperty(cname +".formatter", new XMLFormatter()));
try {
- setEncoding(null);
- } catch (Exception ex2) {
- // doing a setEncoding with null should always work.
- // assert false;
+ setEncoding(manager.getStringProperty(cname +".encoding", null));
+ } catch (Exception ex) {
+ try {
+ setEncoding(null);
+ } catch (Exception ex2) {
+ // doing a setEncoding with null should always work.
+ // assert false;
+ }
}
+ if (hostAndPortSet) {
+ SocketHandler.this.port = port;
+ SocketHandler.this.host = host;
+ } else {
+ SocketHandler.this.port = manager.getIntProperty(cname + ".port", 0);
+ SocketHandler.this.host = manager.getStringProperty(cname + ".host", null);
+ }
+ // Check host and port are valid.
+ if (port == 0) {
+ throw new IllegalArgumentException("Bad port: " + port);
+ }
+ if (host == null) {
+ throw new IllegalArgumentException("Null host name: " + host);
+ }
+ return null;
}
- port = manager.getIntProperty(cname + ".port", 0);
- host = manager.getStringProperty(cname + ".host", null);
}
+ // Private PrivilegedAction to set outputStream from given constructor parameter.
+ private class SetOutputStreamAction implements PrivilegedAction {
+ private final OutputStream outputStream;
+
+ SetOutputStreamAction(OutputStream outputStream) {
+ this.outputStream = outputStream;
+ }
+
+ @Override
+ public Void run() {
+ setOutputStream(outputStream);
+ return null;
+ }
+ }
/**
* Create a SocketHandler, using only LogManager properties
@@ -118,16 +166,17 @@
*/
public SocketHandler() throws IOException {
// We are going to use the logging defaults.
- sealed = false;
- configure();
-
+ AccessController.doPrivileged(new ConfigureAction(),
+ null, LogManager.controlPermission);
+ final OutputStream outputStream;
try {
- connect();
- } catch (IOException ix) {
+ outputStream = connect();
+ } catch (IOException e) {
System.err.println("SocketHandler: connect failed to " + host + ":" + port);
- throw ix;
+ throw e;
}
- sealed = true;
+ AccessController.doPrivileged(new SetOutputStreamAction(outputStream),
+ null, LogManager.controlPermission);
}
/**
@@ -146,28 +195,16 @@
* host and port.
*/
public SocketHandler(String host, int port) throws IOException {
- sealed = false;
- configure();
- sealed = true;
- this.port = port;
- this.host = host;
- connect();
+ AccessController.doPrivileged(new ConfigureAction(host, port),
+ null, LogManager.controlPermission);
+ setOutputStream(connect());
}
- private void connect() throws IOException {
- // Check the arguments are valid.
- if (port == 0) {
- throw new IllegalArgumentException("Bad port: " + port);
- }
- if (host == null) {
- throw new IllegalArgumentException("Null host name: " + host);
- }
-
+ private OutputStream connect() throws IOException {
// Try to open a new socket.
sock = new Socket(host, port);
OutputStream out = sock.getOutputStream();
- BufferedOutputStream bout = new BufferedOutputStream(out);
- setOutputStream(bout);
+ return new BufferedOutputStream(out);
}
/**