diff --git a/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/session/QueuedSftpSessionPool.java b/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/session/QueuedSftpSessionPool.java index 5019c35036..f97c077577 100644 --- a/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/session/QueuedSftpSessionPool.java +++ b/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/session/QueuedSftpSessionPool.java @@ -45,7 +45,7 @@ public class QueuedSftpSessionPool implements SftpSessionPool, SmartLifecycle { private volatile Queue 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 { diff --git a/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/session/SftpSessionFactory.java b/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/session/SftpSessionFactory.java index 9e5705d717..bd7b92ad1d 100644 --- a/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/session/SftpSessionFactory.java +++ b/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/session/SftpSessionFactory.java @@ -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(); } diff --git a/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/session/SftpSessionPool.java b/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/session/SftpSessionPool.java index 1a9a8bf3a8..d6742f47f1 100644 --- a/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/session/SftpSessionPool.java +++ b/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/session/SftpSessionPool.java @@ -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); - + } diff --git a/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/session/SimpleSftpSessionFactory.java b/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/session/SimpleSftpSessionFactory.java new file mode 100644 index 0000000000..e90b2d53e4 --- /dev/null +++ b/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/session/SimpleSftpSessionFactory.java @@ -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); + } + } + +} diff --git a/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/config/InboundChannelAdapaterParserTests-context-fail-autocreate.xml b/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/config/InboundChannelAdapaterParserTests-context-fail-autocreate.xml index 4a3b551b7f..e14c633e85 100644 --- a/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/config/InboundChannelAdapaterParserTests-context-fail-autocreate.xml +++ b/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/config/InboundChannelAdapaterParserTests-context-fail-autocreate.xml @@ -20,7 +20,7 @@ - + diff --git a/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/config/InboundChannelAdapaterParserTests-context-fail.xml b/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/config/InboundChannelAdapaterParserTests-context-fail.xml index 37248ef4ff..1d6027c522 100644 --- a/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/config/InboundChannelAdapaterParserTests-context-fail.xml +++ b/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/config/InboundChannelAdapaterParserTests-context-fail.xml @@ -20,7 +20,7 @@ - + diff --git a/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/config/InboundChannelAdapaterParserTests-context.xml b/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/config/InboundChannelAdapaterParserTests-context.xml index 86fdac73a1..7cdaaefb69 100644 --- a/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/config/InboundChannelAdapaterParserTests-context.xml +++ b/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/config/InboundChannelAdapaterParserTests-context.xml @@ -26,7 +26,7 @@ - + diff --git a/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/config/MessageHistory-context.xml b/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/config/MessageHistory-context.xml index 1832b634c4..aed5c71f18 100644 --- a/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/config/MessageHistory-context.xml +++ b/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/config/MessageHistory-context.xml @@ -20,7 +20,7 @@ - + diff --git a/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/config/OutboundChannelAdapaterParserTests-context-fail-fileFileGen.xml b/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/config/OutboundChannelAdapaterParserTests-context-fail-fileFileGen.xml index 57e394ce85..015ffb1d04 100644 --- a/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/config/OutboundChannelAdapaterParserTests-context-fail-fileFileGen.xml +++ b/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/config/OutboundChannelAdapaterParserTests-context-fail-fileFileGen.xml @@ -8,7 +8,7 @@ http://www.springframework.org/schema/integration/sftp http://www.springframework.org/schema/integration/sftp/spring-integration-sftp-2.0.xsd"> - + diff --git a/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/config/OutboundChannelAdapaterParserTests-context-fail.xml b/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/config/OutboundChannelAdapaterParserTests-context-fail.xml index adce321caa..38bb2dd125 100644 --- a/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/config/OutboundChannelAdapaterParserTests-context-fail.xml +++ b/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/config/OutboundChannelAdapaterParserTests-context-fail.xml @@ -8,7 +8,7 @@ http://www.springframework.org/schema/integration/sftp http://www.springframework.org/schema/integration/sftp/spring-integration-sftp-2.0.xsd"> - + diff --git a/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/config/OutboundChannelAdapaterParserTests-context.xml b/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/config/OutboundChannelAdapaterParserTests-context.xml index cbe4dbce34..99cd0c8850 100644 --- a/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/config/OutboundChannelAdapaterParserTests-context.xml +++ b/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/config/OutboundChannelAdapaterParserTests-context.xml @@ -8,7 +8,7 @@ http://www.springframework.org/schema/integration/sftp http://www.springframework.org/schema/integration/sftp/spring-integration-sftp-2.0.xsd"> - + diff --git a/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/config/OutboundChannelAdapaterParserTests.java b/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/config/OutboundChannelAdapaterParserTests.java index 8e0fb9974a..a75a2ea971 100644 --- a/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/config/OutboundChannelAdapaterParserTests.java +++ b/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/config/OutboundChannelAdapaterParserTests.java @@ -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")); }