This is an automated email from the ASF dual-hosted git repository. pgil pushed a commit to branch release22.01 in repository https://gitbox.apache.org/repos/asf/ofbiz-framework.git
The following commit(s) were added to refs/heads/release22.01 by this push: new f5d7bd9b9e Improved: Update sshd dependency and implementation (OFBIZ-12612) f5d7bd9b9e is described below commit f5d7bd9b9e5a28cebc29098254a31734c1566f52 Author: Gil Portenseigne <p...@apache.org> AuthorDate: Fri May 6 16:23:09 2022 +0200 Improved: Update sshd dependency and implementation (OFBIZ-12612) This commit fix an issue that, under high usage, redondant thread are created. Update sshd-core to 2.8.0, adding sshd-ftp since it has been separated in its own library. Add SshClientHelper to hold the unique instance of SshClient. Update SshFtpClient class to use the unique SshClient, manage the password/identity into the session and clean close the session. --- .../apache/ofbiz/content/ftp/SshClientHelper.java | 32 ++++++++++++++++++++++ .../org/apache/ofbiz/content/ftp/SshFtpClient.java | 24 ++++++++-------- build.gradle | 3 +- 3 files changed, 45 insertions(+), 14 deletions(-) diff --git a/applications/content/src/main/java/org/apache/ofbiz/content/ftp/SshClientHelper.java b/applications/content/src/main/java/org/apache/ofbiz/content/ftp/SshClientHelper.java new file mode 100644 index 0000000000..1b93116192 --- /dev/null +++ b/applications/content/src/main/java/org/apache/ofbiz/content/ftp/SshClientHelper.java @@ -0,0 +1,32 @@ +package org.apache.ofbiz.content.ftp; + +import org.apache.sshd.client.ClientBuilder; +import org.apache.sshd.client.SshClient; +import org.apache.sshd.client.config.keys.ClientIdentityLoader; +import org.apache.sshd.common.NamedFactory; +import org.apache.sshd.common.kex.BuiltinDHFactories; +import org.apache.sshd.common.signature.BuiltinSignatures; + +import java.util.ArrayList; + +public abstract class SshClientHelper { + + private static SshClient client = null; + + public static SshClient getSshClient() { + if (client == null) { + client = SshClient.setUpDefaultClient(); + client.setClientIdentityLoader(ClientIdentityLoader.DEFAULT); + client.setKeyExchangeFactories(NamedFactory.setUpTransformedFactories( + false, + BuiltinDHFactories.VALUES, + ClientBuilder.DH2KEX)); + client.setSignatureFactories(new ArrayList<>(BuiltinSignatures.VALUES)); + } + if (!client.isStarted()) { + client.start(); + } + return client; + } + +} diff --git a/applications/content/src/main/java/org/apache/ofbiz/content/ftp/SshFtpClient.java b/applications/content/src/main/java/org/apache/ofbiz/content/ftp/SshFtpClient.java index a089f3828c..9a051c3230 100644 --- a/applications/content/src/main/java/org/apache/ofbiz/content/ftp/SshFtpClient.java +++ b/applications/content/src/main/java/org/apache/ofbiz/content/ftp/SshFtpClient.java @@ -28,7 +28,8 @@ import org.apache.commons.io.IOUtils; import org.apache.ofbiz.base.util.UtilValidate; import org.apache.sshd.client.SshClient; import org.apache.sshd.client.session.ClientSession; -import org.apache.sshd.client.subsystem.sftp.SftpClient; +import org.apache.sshd.sftp.client.SftpClient; +import org.apache.sshd.sftp.client.SftpClientFactory; /** * Basic client to copy files to an ssh ftp server @@ -37,12 +38,12 @@ public class SshFtpClient implements FtpClientInterface { private static final String MODULE = SshFtpClient.class.getName(); - private SshClient client; + private final SshClient client; private SftpClient sftp; + private ClientSession session; public SshFtpClient() { - client = SshClient.setUpDefaultClient(); - client.start(); + client = SshClientHelper.getSshClient(); } @Override @@ -50,11 +51,10 @@ public class SshFtpClient implements FtpClientInterface { if (port == null) port = 22L; if (timeout == null) timeout = 10000L; - if (sftp != null) return; - ClientSession session = client.connect(username, hostname, port.intValue()).verify(timeout.intValue()).getSession(); + session = client.connect(username, hostname, port.intValue()).verify(timeout.intValue()).getSession(); session.addPasswordIdentity(password); - session.auth().verify(timeout.intValue()); - sftp = session.createSftpClient(); + session.auth().verify(); + sftp = SftpClientFactory.instance().createSftpClient(session).singleSessionInstance(); } @Override @@ -83,10 +83,8 @@ public class SshFtpClient implements FtpClientInterface { } @Override - public void closeConnection() { - if (sftp != null) { - client.stop(); - sftp = null; - } + public void closeConnection() throws IOException { + if (sftp != null) sftp.close(); + if (session != null) session.close(); } } diff --git a/build.gradle b/build.gradle index 62aeff801f..f6adcdb09b 100644 --- a/build.gradle +++ b/build.gradle @@ -223,7 +223,8 @@ dependencies { implementation 'org.apache.poi:poi:4.1.2' // poi-ooxml-schemas-5.0.0.pom'. Received status code 401 from server implementation 'org.apache.pdfbox:pdfbox:2.0.24' implementation 'org.apache.shiro:shiro-core:1.8.0' - implementation 'org.apache.sshd:sshd-core:1.7.0' // So far we did not update from 1.7.0 because of a compile issue. You may try w/ a newer version than 2.4.0 + implementation 'org.apache.sshd:sshd-core:2.8.0' + implementation 'org.apache.sshd:sshd-sftp:2.8.0' implementation 'org.apache.tika:tika-core:1.28.1' // 2.1.0 does not work implementation 'org.apache.tika:tika-parsers:1.28.1' // 2.1.0 does not work implementation 'org.apache.tomcat:tomcat-catalina-ha:9.0.60' // Remember to change the version number (9 now) in javadoc block if needed.