[Minor] fixed logger in ClientSocketEndpoint

This commit is contained in:
Robert von Burg 2018-02-28 17:52:49 +01:00
parent 1830d9fe86
commit 5ecfb9d722
1 changed files with 43 additions and 37 deletions

View File

@ -1,12 +1,12 @@
/* /*
* Copyright 2014 Robert von Burg <eitch@eitchnet.ch> * Copyright 2014 Robert von Burg <eitch@eitchnet.ch>
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@ -24,6 +24,7 @@ import java.net.UnknownHostException;
import java.text.MessageFormat; import java.text.MessageFormat;
import java.util.Map; import java.util.Map;
import li.strolch.utils.helper.ExceptionHelper;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -47,7 +48,7 @@ import li.strolch.utils.helper.StringHelper;
* This endpoint is maintained as a client connection. This means that this endpoint opens the {@link Socket} to the * This endpoint is maintained as a client connection. This means that this endpoint opens the {@link Socket} to the
* remote server * remote server
* </p> * </p>
* *
* @author Robert von Burg &lt;eitch@eitchnet.ch&gt; * @author Robert von Burg &lt;eitch@eitchnet.ch&gt;
*/ */
public class ClientSocketEndpoint implements CommunicationEndpoint { public class ClientSocketEndpoint implements CommunicationEndpoint {
@ -95,15 +96,13 @@ public class ClientSocketEndpoint implements CommunicationEndpoint {
/** /**
* Checks the state of the connection and returns true if {@link Socket} is connected and ready for transmission, * Checks the state of the connection and returns true if {@link Socket} is connected and ready for transmission,
* false otherwise * false otherwise
* *
* @return true if {@link Socket} is connected and ready for transmission, false otherwise * @return true if {@link Socket} is connected and ready for transmission, false otherwise
*/ */
protected boolean checkConnection() { protected boolean checkConnection() {
return !this.closed return !this.closed && this.connected && (this.socket != null && !this.socket.isClosed() && this.socket
&& this.connected .isBound() && this.socket.isConnected() && !this.socket.isInputShutdown() && !this.socket
&& (this.socket != null && !this.socket.isClosed() && this.socket.isBound() .isOutputShutdown());
&& this.socket.isConnected() && !this.socket.isInputShutdown() && !this.socket
.isOutputShutdown());
} }
/** /**
@ -175,8 +174,9 @@ public class ClientSocketEndpoint implements CommunicationEndpoint {
// configure the socket // configure the socket
if (logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {
String msg = "BufferSize (send/read): {0} / {1} SoLinger: {2} TcpNoDelay: {3}"; //$NON-NLS-1$ String msg = "BufferSize (send/read): {0} / {1} SoLinger: {2} TcpNoDelay: {3}"; //$NON-NLS-1$
logger.info(MessageFormat.format(msg, this.socket.getSendBufferSize(), logger.info(MessageFormat
this.socket.getReceiveBufferSize(), this.socket.getSoLinger(), this.socket.getTcpNoDelay())); .format(msg, this.socket.getSendBufferSize(), this.socket.getReceiveBufferSize(),
this.socket.getSoLinger(), this.socket.getTcpNoDelay()));
} }
//outputSocket.setSendBufferSize(1); //outputSocket.setSendBufferSize(1);
//outputSocket.setSoLinger(true, 0); //outputSocket.setSoLinger(true, 0);
@ -213,9 +213,8 @@ public class ClientSocketEndpoint implements CommunicationEndpoint {
this.connection.notifyStateChange(ConnectionState.DISCONNECTED, null); this.connection.notifyStateChange(ConnectionState.DISCONNECTED, null);
} catch (Exception e) { } catch (Exception e) {
String msg = "Error while connecting to {0}:{1}: {2}"; //$NON-NLS-1$ String msg = "Error while connecting to {0}:{1}: {2}"; //$NON-NLS-1$
logger.error( logger.error(MessageFormat.format(msg, this.remoteInputAddressS, Integer.toString(this.remoteInputPort),
MessageFormat.format(msg, this.remoteInputAddressS, Integer.toString(this.remoteInputPort)), ExceptionHelper.formatExceptionMessage(e)));
e.getMessage());
this.connection.notifyStateChange(ConnectionState.BROKEN, e.getLocalizedMessage()); this.connection.notifyStateChange(ConnectionState.BROKEN, e.getLocalizedMessage());
} }
} }
@ -234,7 +233,8 @@ public class ClientSocketEndpoint implements CommunicationEndpoint {
try { try {
this.outputStream.close(); this.outputStream.close();
} catch (IOException e) { } catch (IOException e) {
logger.error(MessageFormat.format("Error closing OutputStream: {0}", e.getLocalizedMessage())); //$NON-NLS-1$ logger.error(
MessageFormat.format("Error closing OutputStream: {0}", e.getLocalizedMessage())); //$NON-NLS-1$
} finally { } finally {
this.outputStream = null; this.outputStream = null;
} }
@ -244,7 +244,8 @@ public class ClientSocketEndpoint implements CommunicationEndpoint {
try { try {
this.inputStream.close(); this.inputStream.close();
} catch (IOException e) { } catch (IOException e) {
logger.error(MessageFormat.format("Error closing InputStream: {0}", e.getLocalizedMessage())); //$NON-NLS-1$ logger.error(
MessageFormat.format("Error closing InputStream: {0}", e.getLocalizedMessage())); //$NON-NLS-1$
} finally { } finally {
this.inputStream = null; this.inputStream = null;
} }
@ -254,7 +255,8 @@ public class ClientSocketEndpoint implements CommunicationEndpoint {
try { try {
this.socket.close(); this.socket.close();
} catch (IOException e) { } catch (IOException e) {
logger.error(MessageFormat.format("Error closing OutputSocket: {0}", e.getLocalizedMessage())); //$NON-NLS-1$ logger.error(
MessageFormat.format("Error closing OutputSocket: {0}", e.getLocalizedMessage())); //$NON-NLS-1$
} finally { } finally {
this.socket = null; this.socket = null;
} }
@ -287,7 +289,7 @@ public class ClientSocketEndpoint implements CommunicationEndpoint {
* <li>connectOnStart - if true, then when the connection is started, the connection to the remote address is * <li>connectOnStart - if true, then when the connection is started, the connection to the remote address is
* attempted. default is {@link SocketEndpointConstants#CONNECT_ON_START} * attempted. default is {@link SocketEndpointConstants#CONNECT_ON_START}
* </ul> * </ul>
* *
* @see CommunicationEndpoint#configure(CommunicationConnection, IoMessageVisitor) * @see CommunicationEndpoint#configure(CommunicationConnection, IoMessageVisitor)
*/ */
@Override @Override
@ -361,8 +363,9 @@ public class ClientSocketEndpoint implements CommunicationEndpoint {
try { try {
this.retry = Long.parseLong(retryS); this.retry = Long.parseLong(retryS);
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
throw ConnectionMessages.throwInvalidParameter(ClientSocketEndpoint.class, throw ConnectionMessages
SocketEndpointConstants.PARAMETER_RETRY, retryS); .throwInvalidParameter(ClientSocketEndpoint.class, SocketEndpointConstants.PARAMETER_RETRY,
retryS);
} }
} }
@ -371,9 +374,9 @@ public class ClientSocketEndpoint implements CommunicationEndpoint {
if (StringHelper.isNotEmpty(connectOnStartS)) { if (StringHelper.isNotEmpty(connectOnStartS)) {
this.connectOnStart = StringHelper.parseBoolean(connectOnStartS); this.connectOnStart = StringHelper.parseBoolean(connectOnStartS);
} else { } else {
ConnectionMessages.warnUnsetParameter(ClientSocketEndpoint.class, ConnectionMessages
SocketEndpointConstants.PARAMETER_CONNECT_ON_START, .warnUnsetParameter(ClientSocketEndpoint.class, SocketEndpointConstants.PARAMETER_CONNECT_ON_START,
String.valueOf(SocketEndpointConstants.CONNECT_ON_START)); String.valueOf(SocketEndpointConstants.CONNECT_ON_START));
this.connectOnStart = SocketEndpointConstants.CONNECT_ON_START; this.connectOnStart = SocketEndpointConstants.CONNECT_ON_START;
} }
@ -382,17 +385,18 @@ public class ClientSocketEndpoint implements CommunicationEndpoint {
if (StringHelper.isNotEmpty(closeAfterSendS)) { if (StringHelper.isNotEmpty(closeAfterSendS)) {
this.closeAfterSend = StringHelper.parseBoolean(closeAfterSendS); this.closeAfterSend = StringHelper.parseBoolean(closeAfterSendS);
} else { } else {
ConnectionMessages.warnUnsetParameter(ClientSocketEndpoint.class, ConnectionMessages
SocketEndpointConstants.PARAMETER_CLOSE_AFTER_SEND, .warnUnsetParameter(ClientSocketEndpoint.class, SocketEndpointConstants.PARAMETER_CLOSE_AFTER_SEND,
String.valueOf(SocketEndpointConstants.CLOSE_AFTER_SEND)); String.valueOf(SocketEndpointConstants.CLOSE_AFTER_SEND));
this.closeAfterSend = SocketEndpointConstants.CLOSE_AFTER_SEND; this.closeAfterSend = SocketEndpointConstants.CLOSE_AFTER_SEND;
} }
// configure if timeout on connection should be activated // configure if timeout on connection should be activated
String useTimeoutS = parameters.get(SocketEndpointConstants.PARAMETER_USE_TIMEOUT); String useTimeoutS = parameters.get(SocketEndpointConstants.PARAMETER_USE_TIMEOUT);
if (useTimeoutS == null || useTimeoutS.length() == 0) { if (useTimeoutS == null || useTimeoutS.length() == 0) {
ConnectionMessages.warnUnsetParameter(ClientSocketEndpoint.class, ConnectionMessages
SocketEndpointConstants.PARAMETER_USE_TIMEOUT, String.valueOf(SocketEndpointConstants.USE_TIMEOUT)); .warnUnsetParameter(ClientSocketEndpoint.class, SocketEndpointConstants.PARAMETER_USE_TIMEOUT,
String.valueOf(SocketEndpointConstants.USE_TIMEOUT));
this.useTimeout = SocketEndpointConstants.USE_TIMEOUT; this.useTimeout = SocketEndpointConstants.USE_TIMEOUT;
} else { } else {
this.useTimeout = Boolean.parseBoolean(useTimeoutS); this.useTimeout = Boolean.parseBoolean(useTimeoutS);
@ -402,8 +406,9 @@ public class ClientSocketEndpoint implements CommunicationEndpoint {
// configure timeout on connection // configure timeout on connection
String timeoutS = parameters.get(SocketEndpointConstants.PARAMETER_TIMEOUT); String timeoutS = parameters.get(SocketEndpointConstants.PARAMETER_TIMEOUT);
if (timeoutS == null || timeoutS.length() == 0) { if (timeoutS == null || timeoutS.length() == 0) {
ConnectionMessages.warnUnsetParameter(ClientSocketEndpoint.class, ConnectionMessages
SocketEndpointConstants.PARAMETER_TIMEOUT, String.valueOf(SocketEndpointConstants.TIMEOUT)); .warnUnsetParameter(ClientSocketEndpoint.class, SocketEndpointConstants.PARAMETER_TIMEOUT,
String.valueOf(SocketEndpointConstants.TIMEOUT));
this.timeout = SocketEndpointConstants.TIMEOUT; this.timeout = SocketEndpointConstants.TIMEOUT;
} else { } else {
try { try {
@ -418,9 +423,9 @@ public class ClientSocketEndpoint implements CommunicationEndpoint {
// configure if the connection should be cleared on connect // configure if the connection should be cleared on connect
String clearOnConnectS = parameters.get(SocketEndpointConstants.PARAMETER_CLEAR_ON_CONNECT); String clearOnConnectS = parameters.get(SocketEndpointConstants.PARAMETER_CLEAR_ON_CONNECT);
if (clearOnConnectS == null || clearOnConnectS.length() == 0) { if (clearOnConnectS == null || clearOnConnectS.length() == 0) {
ConnectionMessages.warnUnsetParameter(ClientSocketEndpoint.class, ConnectionMessages
SocketEndpointConstants.PARAMETER_CLEAR_ON_CONNECT, .warnUnsetParameter(ClientSocketEndpoint.class, SocketEndpointConstants.PARAMETER_CLEAR_ON_CONNECT,
String.valueOf(SocketEndpointConstants.CLEAR_ON_CONNECT)); String.valueOf(SocketEndpointConstants.CLEAR_ON_CONNECT));
this.clearOnConnect = SocketEndpointConstants.CLEAR_ON_CONNECT; this.clearOnConnect = SocketEndpointConstants.CLEAR_ON_CONNECT;
} else { } else {
this.clearOnConnect = Boolean.parseBoolean(clearOnConnectS); this.clearOnConnect = Boolean.parseBoolean(clearOnConnectS);
@ -459,13 +464,14 @@ public class ClientSocketEndpoint implements CommunicationEndpoint {
/** /**
* Allows this end point to connect and then opens the connection to the defined remote server * Allows this end point to connect and then opens the connection to the defined remote server
* *
* @see CommunicationEndpoint#start() * @see CommunicationEndpoint#start()
*/ */
@Override @Override
public void start() { public void start() {
if (!this.closed) { if (!this.closed) {
logger.warn(MessageFormat.format("CommunicationConnection {0} already started.", this.connection.getId())); //$NON-NLS-1$ logger.warn(MessageFormat
.format("CommunicationConnection {0} already started.", this.connection.getId())); //$NON-NLS-1$
} else { } else {
// logger.info(MessageFormat.format("Enabling connection {0}...", this.connection.getId())); //$NON-NLS-1$ // logger.info(MessageFormat.format("Enabling connection {0}...", this.connection.getId())); //$NON-NLS-1$
this.closed = false; this.closed = false;
@ -478,7 +484,7 @@ public class ClientSocketEndpoint implements CommunicationEndpoint {
/** /**
* Closes this connection and disallows this end point to reconnect * Closes this connection and disallows this end point to reconnect
* *
* @see CommunicationEndpoint#stop() * @see CommunicationEndpoint#stop()
*/ */
@Override @Override