From 69e35d6c4a77c1c693587dd84b9032118fa7b7bd Mon Sep 17 00:00:00 2001 From: Timothy Bish Date: Wed, 4 Dec 2013 11:09:28 -0500 Subject: [PATCH] https://issues.apache.org/jira/browse/AMQ-4889 Improve the stream close logic in the init method to ensure we don't leak and resources. --- .../activemq/transport/nio/NIOSSLTransport.java | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/activemq-client/src/main/java/org/apache/activemq/transport/nio/NIOSSLTransport.java b/activemq-client/src/main/java/org/apache/activemq/transport/nio/NIOSSLTransport.java index 52c3e97498..02789f39b9 100644 --- a/activemq-client/src/main/java/org/apache/activemq/transport/nio/NIOSSLTransport.java +++ b/activemq-client/src/main/java/org/apache/activemq/transport/nio/NIOSSLTransport.java @@ -34,7 +34,6 @@ import javax.net.ssl.SSLEngineResult; import javax.net.ssl.SSLPeerUnverifiedException; import javax.net.ssl.SSLSession; -import org.apache.activemq.command.Command; import org.apache.activemq.command.ConnectionInfo; import org.apache.activemq.openwire.OpenWireFormat; import org.apache.activemq.thread.TaskRunnerFactory; @@ -75,6 +74,7 @@ public class NIOSSLTransport extends NIOTransport { @Override protected void initializeStreams() throws IOException { + NIOOutputStream outputStream = null; try { channel = socket.getChannel(); channel.configureBlocking(false); @@ -119,7 +119,7 @@ public class NIOSSLTransport extends NIOTransport { inputBuffer = ByteBuffer.allocate(sslSession.getPacketBufferSize()); inputBuffer.clear(); - NIOOutputStream outputStream = new NIOOutputStream(channel); + outputStream = new NIOOutputStream(channel); outputStream.setEngine(sslEngine); this.dataOut = new DataOutputStream(outputStream); this.buffOut = outputStream; @@ -127,6 +127,12 @@ public class NIOSSLTransport extends NIOTransport { handshakeStatus = sslEngine.getHandshakeStatus(); doHandshake(); } catch (Exception e) { + try { + if(outputStream != null) { + outputStream.close(); + } + super.closeStreams(); + } catch (Exception ex) {} throw new IOException(e); } } @@ -143,10 +149,12 @@ public class NIOSSLTransport extends NIOTransport { // listen for events telling us when the socket is readable. selection = SelectorManager.getInstance().register(channel, new SelectorManager.Listener() { + @Override public void onSelect(SelectorSelection selection) { serviceRead(); } + @Override public void onError(SelectorSelection selection, Throwable error) { if (error instanceof IOException) { onException((IOException) error); @@ -158,6 +166,7 @@ public class NIOSSLTransport extends NIOTransport { } } + @Override protected void serviceRead() { try { if (handshakeInProgress) { @@ -272,7 +281,7 @@ public class NIOSSLTransport extends NIOTransport { } else { currentBuffer.flip(); Object command = wireFormat.unmarshal(new DataInputStream(new NIOInputStream(currentBuffer))); - doConsume((Command) command); + doConsume(command); nextFrameSize = -1; currentBuffer = null; }