Skip to content

Commit

Permalink
close transport and streams to properly close ssh connection (#1205)
Browse files Browse the repository at this point in the history
* close transport and streams
  • Loading branch information
csc-gip authored Oct 24, 2024
1 parent d21341d commit 8ae41dc
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 24 deletions.
2 changes: 1 addition & 1 deletion modules/xact/connection/ssh/application.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* limitations under the License.
* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-->
<Application applicationName="SSH" comment="" factoryVersion="" versionName="2.0.27" xmlVersion="1.1">
<Application applicationName="SSH" comment="" factoryVersion="" versionName="2.0.28" xmlVersion="1.1">
<ApplicationInfo>
<Description Lang="DE">SSH Connections (NETCONF, Shell), Management, SFTP, SharedLibs, SFTPTrigger</Description>
<Description Lang="EN">SSH connections (NETCONF, Shell), management, SFTP, sharedLibs, SFTPTrigger</Description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ public void connect() {

try {
transientConnectionData.setSession(createSession(getSSHConnectionParameter()));
transientConnectionData.setTransport(client.getTransport());
} catch (xact.connection.SSHException sshE) {
logger.trace("Error (SSHException) in Connect",sshE);
throw new RuntimeException(sshE);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
* Copyright 2022 Xyna GmbH, Germany
* Copyright 2024 Xyna GmbH, Germany
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,84 +17,103 @@
*/
package xact.ssh.impl;



import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import net.schmizz.sshj.connection.ConnectionException;
import net.schmizz.sshj.connection.channel.Channel;
import net.schmizz.sshj.connection.channel.direct.Session;
import net.schmizz.sshj.transport.Transport;
import net.schmizz.sshj.transport.TransportException;


import org.apache.log4j.Logger;
import com.gip.xyna.CentralFactoryLogging;

public class TransientConnectionData {

private static final Logger logger = CentralFactoryLogging.getLogger(TransientConnectionData.class);

private Transport transport;
private Session session;
private Channel channel;
private InputStream inputStream;
private OutputStream outputStream;


public void setSession(Session session) {
this.session = session;
}

public void setTransport(Transport transport) {
this.transport = transport;
}

public void setChannelAndStreams(Channel channel) throws IOException {
this.channel = channel;
this.outputStream = channel.getOutputStream();
this.inputStream = channel.getInputStream();
}


public Session getSession() {
return session;
}


public Channel getChannel() {
return channel;
}


public OutputStream getOutputStream() {
return outputStream;
}


public InputStream getInputStream() {
return inputStream;
}


public void disconnect() throws TransportException, ConnectionException {
try {
if (channel != null) {
channel.close();
channel = null;
if (logger.isDebugEnabled())
logger.debug("closed channel");
}
} catch (TransportException e) {
throw e;
} catch (ConnectionException e) {
throw e;
} finally {
if (session != null) {
try {
channel = null;
try {
if (session != null) {
session.close();
} catch (TransportException e) {
throw e;
} catch (ConnectionException e) {
throw e;
if (logger.isDebugEnabled())
logger.debug("closed session");
}
} finally {
session = null;
try {
if (transport != null) {
transport.disconnect();
if (logger.isDebugEnabled())
logger.debug("closed transport");
}
} finally {
try {
if (outputStream != null) {
outputStream.close();
if (logger.isDebugEnabled())
logger.debug("closed outputStream");
}
if (inputStream != null) {
inputStream.close();
if (logger.isDebugEnabled())
logger.debug("closed inputStream");
}
} catch (IOException e) {
if (logger.isDebugEnabled())
logger.debug("error closing streams.", e);
}
}
}
}
}


public boolean isChannelNullOrClosed() {
return channel == null || !channel.isOpen();
}
Expand Down
2 changes: 1 addition & 1 deletion modules/xact/ssh/file/application.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<RuntimeContextRequirements>
<RuntimeContextRequirement>
<ApplicationName>SSH</ApplicationName>
<VersionName>2.0.27</VersionName>
<VersionName>2.0.28</VersionName>
</RuntimeContextRequirement>
<RuntimeContextRequirement>
<ApplicationName>FileMgmt</ApplicationName>
Expand Down

0 comments on commit 8ae41dc

Please sign in to comment.