/** * Sets the address to connect to by doing a lookup on the hostname. * <p> * Note that is resets the {@linkplain #setClientSocketFactory(ClientSocketFactory) client socket factory}. * </p> * * @param hostname the host name used to resolve the address * * @throws UnknownHostException if an error occurs resolving the address */ public void setHostname(final String hostname) throws UnknownHostException { checkAccess(this); setAddress(InetAddress.getByName(hostname)); }
private void safeClose(Closeable c) { try { if (c != null) c.close(); } catch (Exception e) { reportError("Error closing resource", e, ErrorManager.CLOSE_FAILURE); } catch (Throwable ignored) { } }
try { if (current != null) { writeTail(current); safeFlush(current); safeClose(current); final OutputStream out = createOutputStream(); if (out == null) { return; final String encoding = getEncoding(); final UninterruptibleOutputStream outputStream = new UninterruptibleOutputStream(out); if (encoding == null) { writer = new OutputStreamWriter(outputStream, encoding); writeHead(writer); okay = true; } catch (UnsupportedEncodingException e) { reportError("Error opening", e, ErrorManager.OPEN_FAILURE); } finally { safeClose(current); if (!okay) { safeClose(writer);
@Override protected void doPublish(final ExtLogRecord record) { final String formatted; final Formatter formatter = getFormatter(); try { formatted = formatter.format(record); } catch (Exception e) { reportError("Could not format message", e, ErrorManager.FORMAT_FAILURE); return; } if (formatted.isEmpty()) { // nothing to write; move along return; } try { synchronized (outputLock) { if (initialize) { initialize(); initialize = false; } if (writer == null) { return; } writer.write(formatted); super.doPublish(record); } } catch (Exception e) { reportError("Error writing log message", e, ErrorManager.WRITE_FAILURE); } }
private void writeTail(final Writer writer) { try { final Formatter formatter = getFormatter(); if (formatter != null) writer.write(formatter.getTail(this)); } catch (Exception ex) { reportError("Error writing section tail", ex, ErrorManager.WRITE_FAILURE); } }
@Override public void close() throws SecurityException { checkAccess(this); synchronized (outputLock) { safeClose(writer); writer = null; initialize = true; } super.close(); }
private OutputStream createOutputStream() { if (address != null || port >= 0) { try { final ClientSocketFactory socketFactory = getClientSocketFactory(); if (protocol == Protocol.UDP) { return new UdpOutputStream(socketFactory); } return new TcpOutputStream(socketFactory, blockOnReconnect); } catch (IOException e) { reportError("Failed to create socket output stream", e, ErrorManager.OPEN_FAILURE); } } return null; }
/** * Sets the client socket factory used to create sockets. If {@code null} the * {@linkplain #setAddress(InetAddress) address} and {@linkplain #setPort(int) port} are required to be set. * * @param clientSocketFactory the client socket factory to use */ public void setClientSocketFactory(final ClientSocketFactory clientSocketFactory) { checkAccess(this); synchronized (outputLock) { this.clientSocketFactory = clientSocketFactory; initialize = true; } }
@Override public void flush() { synchronized (outputLock) { safeFlush(writer); } super.flush(); }
private void writeHead(final Writer writer) { try { final Formatter formatter = getFormatter(); if (formatter != null) writer.write(formatter.getHead(this)); } catch (Exception e) { reportError("Error writing section header", e, ErrorManager.WRITE_FAILURE); } }
/** * Enables or disables blocking when attempting to reconnect the socket when using a {@linkplain Protocol#TCP TCP} * or {@linkplain Protocol#SSL_TCP SSL TCP} connections. * <p/> * If set to {@code true} the {@code write} methods will block when attempting to reconnect. This is only advisable * to be set to {@code true} if using an asynchronous handler. * * @param blockOnReconnect {@code true} to block when reconnecting or {@code false} to reconnect asynchronously * discarding any new messages coming in */ public void setBlockOnReconnect(final boolean blockOnReconnect) { checkAccess(this); synchronized (outputLock) { this.blockOnReconnect = blockOnReconnect; initialize = true; } }
/** * Sets the port to connect to. * <p> * Note that is resets the {@linkplain #setClientSocketFactory(ClientSocketFactory) client socket factory}. * </p> * * @param port the port */ public void setPort(final int port) { checkAccess(this); synchronized (outputLock) { if (this.port != port) { initialize = true; clientSocketFactory = null; } this.port = port; } }
private void safeFlush(Flushable f) { try { if (f != null) f.flush(); } catch (Exception e) { reportError("Error on flush", e, ErrorManager.FLUSH_FAILURE); } catch (Throwable ignored) { } } }
/** * Sets the protocol to use. If the value is {@code null} the protocol will be set to * {@linkplain Protocol#TCP TCP}. * <p> * Note that is resets the {@linkplain #setSocketFactory(SocketFactory) socket factory} if it was previously set. * </p> * * @param protocol the protocol to use */ public void setProtocol(final Protocol protocol) { checkAccess(this); synchronized (outputLock) { if (protocol == null) { this.protocol = Protocol.TCP; } if (this.protocol != protocol) { socketFactory = null; initialize = true; } this.protocol = protocol; } }
/** * Sets the socket factory to use for creating {@linkplain Protocol#TCP TCP} or {@linkplain Protocol#SSL_TCP SSL} * connections. * <p> * Note that if the {@linkplain #setProtocol(Protocol) protocol} is set the socket factory will be set to * {@code null} and reset. Setting a value here also resets the * {@linkplain #setClientSocketFactory(ClientSocketFactory) client socket factory}. * </p> * * @param socketFactory the socket factory * * @see #setClientSocketFactory(ClientSocketFactory) */ public void setSocketFactory(final SocketFactory socketFactory) { checkAccess(this); synchronized (outputLock) { this.socketFactory = socketFactory; this.clientSocketFactory = null; initialize = true; } }
/** * Sets the address to connect to. * <p> * Note that is resets the {@linkplain #setClientSocketFactory(ClientSocketFactory) client socket factory}. * </p> * * @param address the address */ public void setAddress(final InetAddress address) { checkAccess(this); synchronized (outputLock) { if (!this.address.equals(address)) { initialize = true; clientSocketFactory = null; } this.address = address; } }