minor refactor to make it easier for derivations to expose exceptions on initialization

git-svn-id: https://svn.apache.org/repos/asf/incubator/activemq/trunk@467078 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
James Strachan 2006-10-23 18:48:18 +00:00
parent 7a25bcf203
commit e73f5aabf6
1 changed files with 103 additions and 95 deletions

View File

@ -17,6 +17,16 @@
*/
package org.apache.activemq.transport.tcp;
import org.apache.activemq.Service;
import org.apache.activemq.transport.Transport;
import org.apache.activemq.transport.TransportThreadSupport;
import org.apache.activemq.util.IntrospectionSupport;
import org.apache.activemq.util.ServiceStopper;
import org.apache.activemq.wireformat.WireFormat;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import javax.net.SocketFactory;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
@ -31,17 +41,6 @@ import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;
import javax.net.SocketFactory;
import org.apache.activemq.Service;
import org.apache.activemq.transport.Transport;
import org.apache.activemq.transport.TransportThreadSupport;
import org.apache.activemq.util.IntrospectionSupport;
import org.apache.activemq.util.ServiceStopper;
import org.apache.activemq.wireformat.WireFormat;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* An implementation of the {@link Transport} interface using raw tcp/ip
*
@ -85,7 +84,8 @@ public class TcpTransport extends TransportThreadSupport implements Transport, S
this.socketFactory = socketFactory;
try {
this.socket = socketFactory.createSocket();
} catch (SocketException e) {
}
catch (SocketException e) {
this.socket = null;
}
this.remoteLocation = remoteLocation;
@ -122,7 +122,7 @@ public class TcpTransport extends TransportThreadSupport implements Transport, S
* @return pretty print of 'this'
*/
public String toString() {
return "tcp://"+socket.getInetAddress()+":"+socket.getPort();
return "tcp://" + socket.getInetAddress() + ":" + socket.getPort();
}
/**
@ -132,7 +132,7 @@ public class TcpTransport extends TransportThreadSupport implements Transport, S
log.trace("TCP consumer thread starting");
while (!isStopped()) {
try {
Object command = wireFormat.unmarshal(dataIn);
Object command = readCommand();
doConsume(command);
}
catch (SocketTimeoutException e) {
@ -151,6 +151,10 @@ public class TcpTransport extends TransportThreadSupport implements Transport, S
}
}
protected Object readCommand() throws IOException {
return wireFormat.unmarshal(dataIn);
}
// Properties
// -------------------------------------------------------------------------
@ -258,7 +262,7 @@ public class TcpTransport extends TransportThreadSupport implements Transport, S
* @throws SocketException
*/
protected void initialiseSocket(Socket sock) throws SocketException {
if( socketOptions != null ) {
if (socketOptions != null) {
IntrospectionSupport.setProperties(socket, socketOptions);
}
@ -285,45 +289,49 @@ public class TcpTransport extends TransportThreadSupport implements Transport, S
super.doStart();
}
protected void connect() throws SocketException, IOException {
protected void connect() throws Exception {
if( socket == null && socketFactory == null ) {
if (socket == null && socketFactory == null) {
throw new IllegalStateException("Cannot connect if the socket or socketFactory have not been set");
}
InetSocketAddress localAddress=null;
InetSocketAddress remoteAddress=null;
InetSocketAddress localAddress = null;
InetSocketAddress remoteAddress = null;
if( localLocation!=null ) {
if (localLocation != null) {
localAddress = new InetSocketAddress(InetAddress.getByName(localLocation.getHost()), localLocation.getPort());
}
if( remoteLocation!=null ) {
if (remoteLocation != null) {
String host = resolveHostName(remoteLocation.getHost());
remoteAddress = new InetSocketAddress(host, remoteLocation.getPort());
}
if( socket!=null ) {
if (socket != null) {
if( localAddress!=null )
if (localAddress != null) {
socket.bind(localAddress);
}
// If it's a server accepted socket.. we don't need to connect it
// to a remote address.
if ( remoteAddress!=null ) {
if (remoteAddress != null) {
if (connectionTimeout >= 0) {
socket.connect(remoteAddress, connectionTimeout);
} else {
}
else {
socket.connect(remoteAddress);
}
}
} else {
}
else {
// For SSL sockets.. you can't create an unconnected socket :(
// This means the timout option are not supported either.
if( localAddress!=null ) {
if (localAddress != null) {
socket = socketFactory.createSocket(remoteAddress.getAddress(), remoteAddress.getPort(), localAddress.getAddress(), localAddress.getPort());
} else {
}
else {
socket = socketFactory.createSocket(remoteAddress.getAddress(), remoteAddress.getPort());
}
}
@ -341,10 +349,10 @@ public class TcpTransport extends TransportThreadSupport implements Transport, S
}
}
protected void initializeStreams() throws IOException {
TcpBufferedInputStream buffIn = new TcpBufferedInputStream(socket.getInputStream(), 8*1024);
protected void initializeStreams() throws Exception {
TcpBufferedInputStream buffIn = new TcpBufferedInputStream(socket.getInputStream(), 8 * 1024);
this.dataIn = new DataInputStream(buffIn);
TcpBufferedOutputStream buffOut = new TcpBufferedOutputStream(socket.getOutputStream(), 16*1024);
TcpBufferedOutputStream buffOut = new TcpBufferedOutputStream(socket.getOutputStream(), 16 * 1024);
this.dataOut = new DataOutputStream(buffOut);
}
@ -362,7 +370,7 @@ public class TcpTransport extends TransportThreadSupport implements Transport, S
}
public String getRemoteAddress() {
if(socket != null){
if (socket != null) {
return "" + socket.getRemoteSocketAddress();
}
return null;