101 } catch (Exception ex2) { 102 // doing a setEncoding with null should always work. 103 // assert false; 104 } 105 } 106 port = manager.getIntProperty(cname + ".port", 0); 107 host = manager.getStringProperty(cname + ".host", null); 108 } 109 110 111 /** 112 * Create a <tt>SocketHandler</tt>, using only <tt>LogManager</tt> properties 113 * (or their defaults). 114 * @throws IllegalArgumentException if the host or port are invalid or 115 * are not specified as LogManager properties. 116 * @throws IOException if we are unable to connect to the target 117 * host and port. 118 */ 119 public SocketHandler() throws IOException { 120 // We are going to use the logging defaults. 121 sealed = false; 122 configure(); 123 124 try { 125 connect(); 126 } catch (IOException ix) { 127 System.err.println("SocketHandler: connect failed to " + host + ":" + port); 128 throw ix; 129 } 130 sealed = true; 131 } 132 133 /** 134 * Construct a <tt>SocketHandler</tt> using a specified host and port. 135 * 136 * The <tt>SocketHandler</tt> is configured based on <tt>LogManager</tt> 137 * properties (or their default values) except that the given target host 138 * and port arguments are used. If the host argument is empty, but not 139 * null String then the localhost is used. 140 * 141 * @param host target host. 142 * @param port target port. 143 * 144 * @throws IllegalArgumentException if the host or port are invalid. 145 * @throws IOException if we are unable to connect to the target 146 * host and port. 147 */ 148 public SocketHandler(String host, int port) throws IOException { 149 sealed = false; 150 configure(); 151 sealed = true; 152 this.port = port; 153 this.host = host; 154 connect(); 155 } 156 157 private void connect() throws IOException { 158 // Check the arguments are valid. 159 if (port == 0) { 160 throw new IllegalArgumentException("Bad port: " + port); 161 } 162 if (host == null) { 163 throw new IllegalArgumentException("Null host name: " + host); 164 } 165 166 // Try to open a new socket. 167 sock = new Socket(host, port); 168 OutputStream out = sock.getOutputStream(); 169 BufferedOutputStream bout = new BufferedOutputStream(out); 170 setOutputStream(bout); 171 } | 101 } catch (Exception ex2) { 102 // doing a setEncoding with null should always work. 103 // assert false; 104 } 105 } 106 port = manager.getIntProperty(cname + ".port", 0); 107 host = manager.getStringProperty(cname + ".host", null); 108 } 109 110 111 /** 112 * Create a <tt>SocketHandler</tt>, using only <tt>LogManager</tt> properties 113 * (or their defaults). 114 * @throws IllegalArgumentException if the host or port are invalid or 115 * are not specified as LogManager properties. 116 * @throws IOException if we are unable to connect to the target 117 * host and port. 118 */ 119 public SocketHandler() throws IOException { 120 // We are going to use the logging defaults. 121 try { 122 doWithControlPermission(() -> { 123 configure(); 124 try { 125 connect(); 126 } catch (IOException ioe) { 127 throw new UncheckedIOException(ioe); 128 } 129 }); 130 } catch (UncheckedIOException uioe) { 131 System.err.println("SocketHandler: connect failed to " + host + ":" + port); 132 throw uioe.getCause(); 133 } 134 } 135 136 /** 137 * Construct a <tt>SocketHandler</tt> using a specified host and port. 138 * 139 * The <tt>SocketHandler</tt> is configured based on <tt>LogManager</tt> 140 * properties (or their default values) except that the given target host 141 * and port arguments are used. If the host argument is empty, but not 142 * null String then the localhost is used. 143 * 144 * @param host target host. 145 * @param port target port. 146 * 147 * @throws IllegalArgumentException if the host or port are invalid. 148 * @throws IOException if we are unable to connect to the target 149 * host and port. 150 */ 151 public SocketHandler(String host, int port) throws IOException { 152 doWithControlPermission(this::configure); 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 } |