INT-1614 SftpSessionFactory is now an interface

This commit is contained in:
Mark Fisher
2010-11-19 09:57:22 -05:00
parent 2c2d2fd38a
commit 9f410d5068
12 changed files with 113 additions and 81 deletions

View File

@@ -45,7 +45,7 @@ public class QueuedSftpSessionPool implements SftpSessionPool, SmartLifecycle {
private volatile Queue<SftpSession> queue;
private final SftpSessionFactory sftpSessionFactory;
private final SimpleSftpSessionFactory sftpSessionFactory;
private final int maxPoolSize;
@@ -58,11 +58,11 @@ public class QueuedSftpSessionPool implements SftpSessionPool, SmartLifecycle {
private final ReentrantLock lock = new ReentrantLock();
public QueuedSftpSessionPool(SftpSessionFactory factory) {
public QueuedSftpSessionPool(SimpleSftpSessionFactory factory) {
this(DEFAULT_POOL_SIZE, factory);
}
public QueuedSftpSessionPool(int maxPoolSize, SftpSessionFactory sessionFactory) {
public QueuedSftpSessionPool(int maxPoolSize, SimpleSftpSessionFactory sessionFactory) {
this.sftpSessionFactory = sessionFactory;
this.maxPoolSize = maxPoolSize;
}
@@ -76,7 +76,7 @@ public class QueuedSftpSessionPool implements SftpSessionPool, SmartLifecycle {
this.phase = phase;
}
public SftpSession getSession() throws Exception {
public SftpSession getSession() {
Assert.notNull(this.queue, "SftpSession is unavailable since the pool component is not started");
this.lock.lock();
try {

View File

@@ -16,74 +16,12 @@
package org.springframework.integration.sftp.session;
import org.springframework.core.io.Resource;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/**
* Factory for creating {@link SftpSession} instances. There are lots of ways to construct a
* {@link SftpSession} instance, and not all of them are obvious. This factory should help.
*
* @author Josh Long
* @author Mario Gray
* @author Mark Fisher
* @since 2.0
*/
public class SftpSessionFactory {
public interface SftpSessionFactory {
private volatile String host;
private volatile int port = 22; // the default
private volatile String user;
private volatile String password;
private volatile String knownHosts;
private volatile Resource privateKey;
private volatile String privateKeyPassphrase;
public void setHost(String host) {
this.host = host;
}
public void setPort(int port) {
this.port = port;
}
public void setUser(String user) {
this.user = user;
}
public void setPassword(String password) {
this.password = password;
}
public void setKnownHosts(String knownHosts) {
this.knownHosts = knownHosts;
}
public void setPrivateKey(Resource privateKey) {
this.privateKey = privateKey;
}
public void setPrivateKeyPassphrase(String privateKeyPassphrase) {
this.privateKeyPassphrase = privateKeyPassphrase;
}
protected SftpSession getSession() throws Exception {
Assert.hasText(this.host, "host must not be empty");
Assert.hasText(this.user, "user must not be empty");
Assert.isTrue(this.port >= 0, "port must be a positive number");
Assert.isTrue(StringUtils.hasText(this.password) || privateKey != null || StringUtils.hasText(this.privateKeyPassphrase),
"either a password or a private key and/or a private key passphrase is required");
String privateKeyToPass = null;
if (privateKey != null){
privateKeyToPass = privateKey.getFile().getAbsolutePath();
}
return new SftpSession(this.user, this.host, this.password, this.port, this.knownHosts, null, privateKeyToPass, this.privateKeyPassphrase);
}
SftpSession getSession();
}

View File

@@ -26,7 +26,7 @@ import org.springframework.context.Lifecycle;
* @author Oleg Zhurakousky
* @since 2.0
*/
public interface SftpSessionPool extends Lifecycle{
public interface SftpSessionPool extends SftpSessionFactory, Lifecycle {
/**
* Returns a session that can be used to connect to an sftp instance and perform operations
@@ -34,7 +34,7 @@ public interface SftpSessionPool extends Lifecycle{
* @return the session from the pool ready to be connected to.
* @throws Exception if any fault occurs when trying to connect to the remote server
*/
SftpSession getSession() throws Exception;
SftpSession getSession();
/**
* Releases the session.
@@ -42,5 +42,5 @@ public interface SftpSessionPool extends Lifecycle{
* @param session the session to relinquish / renew
*/
void release(SftpSession session);
}

View File

@@ -0,0 +1,94 @@
/*
* Copyright 2002-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.sftp.session;
import org.springframework.core.io.Resource;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/**
* Factory for creating {@link SftpSession} instances. There are lots of ways to construct a
* {@link SftpSession} instance, and not all of them are obvious. This factory should help.
*
* @author Josh Long
* @author Mario Gray
* @since 2.0
*/
public class SimpleSftpSessionFactory implements SftpSessionFactory {
private volatile String host;
private volatile int port = 22; // the default
private volatile String user;
private volatile String password;
private volatile String knownHosts;
private volatile Resource privateKey;
private volatile String privateKeyPassphrase;
public void setHost(String host) {
this.host = host;
}
public void setPort(int port) {
this.port = port;
}
public void setUser(String user) {
this.user = user;
}
public void setPassword(String password) {
this.password = password;
}
public void setKnownHosts(String knownHosts) {
this.knownHosts = knownHosts;
}
public void setPrivateKey(Resource privateKey) {
this.privateKey = privateKey;
}
public void setPrivateKeyPassphrase(String privateKeyPassphrase) {
this.privateKeyPassphrase = privateKeyPassphrase;
}
public SftpSession getSession() {
Assert.hasText(this.host, "host must not be empty");
Assert.hasText(this.user, "user must not be empty");
Assert.isTrue(this.port >= 0, "port must be a positive number");
Assert.isTrue(StringUtils.hasText(this.password) || privateKey != null || StringUtils.hasText(this.privateKeyPassphrase),
"either a password or a private key and/or a private key passphrase is required");
String privateKeyToPass = null;
try {
if (privateKey != null){
privateKeyToPass = privateKey.getFile().getAbsolutePath();
}
return new SftpSession(this.user, this.host, this.password, this.port, this.knownHosts, null, privateKeyToPass, this.privateKeyPassphrase);
}
catch (Exception e) {
throw new IllegalStateException("failed to create SFTP Session", e);
}
}
}

View File

@@ -20,7 +20,7 @@
<channel id="requestChannel"/>
<beans:bean id="sftpSessionFactory" class="org.springframework.integration.sftp.session.SftpSessionFactory">
<beans:bean id="sftpSessionFactory" class="org.springframework.integration.sftp.session.SimpleSftpSessionFactory">
<beans:property name="host" value="loclahost"/>
<beans:property name="knownHosts" value="local, foo.com, bar.foo"/>
<beans:property name="privateKey" value="classpath:org/springframework/integration/sftp/config/sftpTest"/>

View File

@@ -20,7 +20,7 @@
<channel id="requestChannel"/>
<beans:bean id="sftpSessionFactory" class="org.springframework.integration.sftp.session.SftpSessionFactory">
<beans:bean id="sftpSessionFactory" class="org.springframework.integration.sftp.session.SimpleSftpSessionFactory">
<beans:property name="host" value="loclahost"/>
<beans:property name="knownHosts" value="local, foo.com, bar.foo"/>
<beans:property name="privateKey" value="classpath:org/springframework/integration/sftp/config/sftpTest"/>

View File

@@ -26,7 +26,7 @@
<beans:constructor-arg value="."/>
</beans:bean>
<beans:bean id="sftpSessionFactory" class="org.springframework.integration.sftp.session.SftpSessionFactory">
<beans:bean id="sftpSessionFactory" class="org.springframework.integration.sftp.session.SimpleSftpSessionFactory">
<beans:property name="host" value="loclahost"/>
<beans:property name="knownHosts" value="local, foo.com, bar.foo"/>
<beans:property name="privateKey" value="classpath:org/springframework/integration/sftp/config/sftpTest"/>

View File

@@ -20,7 +20,7 @@
<channel id="inboundFilesChannel"/>
<beans:bean id="sftpSessionFactory" class="org.springframework.integration.sftp.session.SftpSessionFactory">
<beans:bean id="sftpSessionFactory" class="org.springframework.integration.sftp.session.SimpleSftpSessionFactory">
<beans:property name="host" value="loclahost"/>
<beans:property name="knownHosts" value="local, foo.com, bar.foo"/>
<beans:property name="privateKey" value="classpath:org/springframework/integration/sftp/config/sftpTest"/>

View File

@@ -8,7 +8,7 @@
http://www.springframework.org/schema/integration/sftp http://www.springframework.org/schema/integration/sftp/spring-integration-sftp-2.0.xsd">
<bean id="sftpSessionFactory" class="org.springframework.integration.sftp.session.SftpSessionFactory">
<bean id="sftpSessionFactory" class="org.springframework.integration.sftp.session.SimpleSftpSessionFactory">
<property name="host" value="localhost"/>
<property name="knownHosts" value="local, foo.com, bar.foo"/>
<property name="privateKey" value="classpath:org/springframework/integration/sftp/config/sftpTest"/>

View File

@@ -8,7 +8,7 @@
http://www.springframework.org/schema/integration/sftp http://www.springframework.org/schema/integration/sftp/spring-integration-sftp-2.0.xsd">
<bean id="sftpSessionFactory" class="org.springframework.integration.sftp.session.SftpSessionFactory">
<bean id="sftpSessionFactory" class="org.springframework.integration.sftp.session.SimpleSftpSessionFactory">
<property name="host" value="localhost"/>
<property name="knownHosts" value="local, foo.com, bar.foo"/>
<property name="privateKey" value="classpath:org/springframework/integration/sftp/config/sftpTest"/>

View File

@@ -8,7 +8,7 @@
http://www.springframework.org/schema/integration/sftp http://www.springframework.org/schema/integration/sftp/spring-integration-sftp-2.0.xsd">
<bean id="sftpSessionFactory" class="org.springframework.integration.sftp.session.SftpSessionFactory">
<bean id="sftpSessionFactory" class="org.springframework.integration.sftp.session.SimpleSftpSessionFactory">
<property name="host" value="localhost"/>
<property name="knownHosts" value="local, foo.com, bar.foo"/>
<property name="privateKey" value="classpath:org/springframework/integration/sftp/config/sftpTest"/>

View File

@@ -31,7 +31,7 @@ import org.springframework.integration.endpoint.EventDrivenConsumer;
import org.springframework.integration.file.FileNameGenerator;
import org.springframework.integration.sftp.outbound.SftpSendingMessageHandler;
import org.springframework.integration.sftp.session.QueuedSftpSessionPool;
import org.springframework.integration.sftp.session.SftpSessionFactory;
import org.springframework.integration.sftp.session.SimpleSftpSessionFactory;
import org.springframework.integration.test.util.TestUtils;
/**
@@ -57,7 +57,7 @@ public class OutboundChannelAdapaterParserTests {
assertNotNull(TestUtils.getPropertyValue(handler, "temporaryBufferFolder"));
assertNotNull(TestUtils.getPropertyValue(handler, "temporaryBufferFolderFile"));
QueuedSftpSessionPool clientPoll = (QueuedSftpSessionPool) TestUtils.getPropertyValue(handler, "sessionPool");
SftpSessionFactory clientFactory = (SftpSessionFactory) TestUtils.getPropertyValue(clientPoll, "sftpSessionFactory");
SimpleSftpSessionFactory clientFactory = (SimpleSftpSessionFactory) TestUtils.getPropertyValue(clientPoll, "sftpSessionFactory");
assertEquals("localhost", TestUtils.getPropertyValue(clientFactory, "host"));
assertEquals(2222, TestUtils.getPropertyValue(clientFactory, "port"));
}