INT-1909 added support for injecting JschSession with Properties configuration (e.g., session.setConfig(Properties) )

This commit is contained in:
Oleg Zhurakousky
2011-05-23 15:48:32 -04:00
parent 5bf590bbfb
commit d56471edba
3 changed files with 23 additions and 2 deletions

View File

@@ -16,6 +16,8 @@
package org.springframework.integration.sftp.session;
import java.util.Properties;
import org.springframework.core.io.Resource;
import org.springframework.integration.file.remote.session.Session;
import org.springframework.integration.file.remote.session.SessionFactory;
@@ -48,11 +50,13 @@ public class DefaultSftpSessionFactory implements SessionFactory {
private volatile Resource privateKey;
private volatile String privateKeyPassphrase;
private volatile Properties sessionConfig;
private final JSch jsch = new JSch();
public void setHost(String host) {
public void setHost(String host) {
this.host = host;
}
@@ -79,6 +83,10 @@ public class DefaultSftpSessionFactory implements SessionFactory {
public void setPrivateKeyPassphrase(String privateKeyPassphrase) {
this.privateKeyPassphrase = privateKeyPassphrase;
}
public void setSessionConfig(Properties sessionConfig) {
this.sessionConfig = sessionConfig;
}
public Session getSession() {
Assert.hasText(this.host, "host must not be empty");
@@ -118,6 +126,9 @@ public class DefaultSftpSessionFactory implements SessionFactory {
}
}
com.jcraft.jsch.Session jschSession = this.jsch.getSession(this.user, this.host, this.port);
if (this.sessionConfig != null){
jschSession.setConfig(this.sessionConfig);
}
if (StringUtils.hasText(this.password)) {
jschSession.setPassword(this.password);
}

View File

@@ -29,8 +29,12 @@
<beans:property name="password" value="hello"/>
<beans:property name="port" value="2222"/>
<beans:property name="user" value="oleg"/>
<beans:property name="sessionConfig" ref="sessionConfig"/>
</beans:bean>
<util:properties id="sessionConfig">
<beans:prop key="StrictHostKeyChecking">no</beans:prop>
</util:properties>
<sftp:inbound-channel-adapter id="defaultAdapter"
session-factory="sftpSessionFactory"
channel="requestChannel"

View File

@@ -16,8 +16,11 @@
package org.springframework.integration.sftp.config;
import static junit.framework.Assert.assertNotNull;
import static org.junit.Assert.assertEquals;
import java.util.Properties;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -40,12 +43,15 @@ public class InboundChannelAdapterParserCachingTests {
@Autowired private Object cachingAdapter;
@Autowired private Object nonCachingAdapter;
@Test
public void defaultAdapter() {
Object sessionFactory = TestUtils.getPropertyValue(defaultAdapter, "source.synchronizer.sessionFactory");
assertEquals(CachingSessionFactory.class, sessionFactory.getClass());
Properties sessionConfig = TestUtils.getPropertyValue(sessionFactory, "sessionFactory.sessionConfig", Properties.class);
assertNotNull(sessionConfig);
assertEquals("no", sessionConfig.getProperty("StrictHostKeyChecking"));
}
@Test