From 66e64979ef40ad752a1cc5d6906f6fd4b79b7734 Mon Sep 17 00:00:00 2001 From: Mark Fisher Date: Fri, 19 Nov 2010 18:03:46 -0500 Subject: [PATCH] INT-1614 refactoring FTP for Session and SessionFactory --- .../remote/session/CachingSessionFactory.java | 7 ++--- ...bstractFtpInboundChannelAdapterParser.java | 2 +- ...stractFtpOutboundChannelAdapterParser.java | 26 ++++++++++++++++--- .../outbound/FtpSendingMessageHandler.java | 22 +++++++++++++--- .../ftp/config/spring-integration-ftp-2.0.xsd | 4 ++- .../ftp/FtpParserInboundTests-context.xml | 4 +-- .../FtpParserInboundTests-fail-context.xml | 2 +- .../ftp/FtpParserOutboundTests-context.xml | 3 ++- ...boundChannelAdapterParserTests-context.xml | 4 +-- ...boundChannelAdapterParserTests-context.xml | 3 ++- ...boundChannelAdapterParserTests-context.xml | 4 +-- ...boundChannelAdapterParserTests-context.xml | 3 ++- .../ftp/ftp-message-history-context.xml | 2 +- 13 files changed, 64 insertions(+), 22 deletions(-) diff --git a/spring-integration-file/src/main/java/org/springframework/integration/file/remote/session/CachingSessionFactory.java b/spring-integration-file/src/main/java/org/springframework/integration/file/remote/session/CachingSessionFactory.java index 7b66151a19..01efd3f221 100644 --- a/spring-integration-file/src/main/java/org/springframework/integration/file/remote/session/CachingSessionFactory.java +++ b/spring-integration-file/src/main/java/org/springframework/integration/file/remote/session/CachingSessionFactory.java @@ -33,6 +33,7 @@ import org.springframework.util.Assert; * * @author Josh Long * @author Oleg Zhurakousky + * @author Mark Fisher * @since 2.0 */ public class CachingSessionFactory implements SessionFactory, DisposableBean { @@ -70,7 +71,7 @@ public class CachingSessionFactory implements SessionFactory, DisposableBean { if (null == session) { session = sessionFactory.getSession(); } - return (session != null) ? new PooledSftpSession(session) : null; + return (session != null) ? new CachedSession(session) : null; } finally { this.lock.unlock(); @@ -99,11 +100,11 @@ public class CachingSessionFactory implements SessionFactory, DisposableBean { } - private class PooledSftpSession implements Session { + private class CachedSession implements Session { private final Session targetSession; - private PooledSftpSession(Session targetSession) { + private CachedSession(Session targetSession) { this.targetSession = targetSession; } diff --git a/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/config/AbstractFtpInboundChannelAdapterParser.java b/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/config/AbstractFtpInboundChannelAdapterParser.java index 168b813a98..c1d3af385e 100644 --- a/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/config/AbstractFtpInboundChannelAdapterParser.java +++ b/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/config/AbstractFtpInboundChannelAdapterParser.java @@ -40,7 +40,7 @@ public abstract class AbstractFtpInboundChannelAdapterParser extends AbstractPol BeanDefinitionBuilder poolBuilder = BeanDefinitionBuilder.genericBeanDefinition("org.springframework.integration.file.remote.session.CachingSessionFactory"); - poolBuilder.addConstructorArgReference(element.getAttribute("client-factory")); + poolBuilder.addConstructorArgReference(element.getAttribute("session-factory")); BeanDefinitionBuilder synchronizerBuilder = BeanDefinitionBuilder.genericBeanDefinition("org.springframework.integration.ftp.inbound.FtpInboundFileSynchronizer"); diff --git a/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/config/AbstractFtpOutboundChannelAdapterParser.java b/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/config/AbstractFtpOutboundChannelAdapterParser.java index b6e4d3d061..a914c5a655 100644 --- a/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/config/AbstractFtpOutboundChannelAdapterParser.java +++ b/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/config/AbstractFtpOutboundChannelAdapterParser.java @@ -17,11 +17,15 @@ package org.springframework.integration.ftp.config; import org.w3c.dom.Element; +import org.springframework.beans.factory.BeanDefinitionStoreException; +import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.support.AbstractBeanDefinition; import org.springframework.beans.factory.support.BeanDefinitionBuilder; +import org.springframework.beans.factory.support.RootBeanDefinition; import org.springframework.beans.factory.xml.ParserContext; import org.springframework.integration.config.xml.AbstractOutboundChannelAdapterParser; import org.springframework.integration.config.xml.IntegrationNamespaceUtils; +import org.springframework.util.StringUtils; /** * @author Oleg Zhurakousky @@ -35,10 +39,26 @@ public abstract class AbstractFtpOutboundChannelAdapterParser extends AbstractOu BeanDefinitionBuilder poolBuilder = BeanDefinitionBuilder.genericBeanDefinition("org.springframework.integration.file.remote.session.CachingSessionFactory"); - poolBuilder.addConstructorArgReference(element.getAttribute("client-factory")); - + poolBuilder.addConstructorArgReference(element.getAttribute("session-factory")); handlerBuilder.addConstructorArgValue(poolBuilder.getBeanDefinition()); - + String remoteDirectory = element.getAttribute("remote-directory"); + String remoteDirectoryExpression = element.getAttribute("remote-directory-expression"); + boolean hasDirectory = StringUtils.hasText(remoteDirectory); + boolean hasDirectoryExpression = StringUtils.hasText(remoteDirectoryExpression); + if (!(hasDirectory ^ hasDirectoryExpression)) { + throw new BeanDefinitionStoreException("exactly one of 'remote-directory' or 'remote-directory-expression' " + + "is required on the SFTP outbound adapter"); + } + BeanDefinition expressionDef = null; + if (hasDirectory) { + expressionDef = new RootBeanDefinition("org.springframework.expression.common.LiteralExpression"); + expressionDef.getConstructorArgumentValues().addGenericArgumentValue(remoteDirectory); + } + else if (hasDirectoryExpression) { + expressionDef = new RootBeanDefinition("org.springframework.integration.config.ExpressionFactoryBean"); + expressionDef.getConstructorArgumentValues().addGenericArgumentValue(remoteDirectoryExpression); + } + handlerBuilder.addPropertyValue("remoteDirectoryExpression", expressionDef); IntegrationNamespaceUtils.setValueIfAttributeDefined(handlerBuilder, element, "charset"); IntegrationNamespaceUtils.setReferenceIfAttributeDefined(handlerBuilder, element,"filename-generator", "fileNameGenerator"); return handlerBuilder.getBeanDefinition(); diff --git a/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/outbound/FtpSendingMessageHandler.java b/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/outbound/FtpSendingMessageHandler.java index 55e3467e1a..6f8106a2d3 100644 --- a/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/outbound/FtpSendingMessageHandler.java +++ b/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/outbound/FtpSendingMessageHandler.java @@ -28,6 +28,7 @@ import org.apache.commons.lang.SystemUtils; import org.springframework.core.io.FileSystemResource; import org.springframework.core.io.Resource; +import org.springframework.expression.Expression; import org.springframework.integration.Message; import org.springframework.integration.MessageDeliveryException; import org.springframework.integration.file.DefaultFileNameGenerator; @@ -35,6 +36,7 @@ import org.springframework.integration.file.FileNameGenerator; import org.springframework.integration.file.remote.session.Session; import org.springframework.integration.file.remote.session.SessionFactory; import org.springframework.integration.handler.AbstractMessageHandler; +import org.springframework.integration.handler.ExpressionEvaluatingMessageProcessor; import org.springframework.util.Assert; import org.springframework.util.FileCopyUtils; @@ -50,6 +52,10 @@ public class FtpSendingMessageHandler extends AbstractMessageHandler{ private static final String TEMPORARY_FILE_SUFFIX = ".writing"; + private volatile ExpressionEvaluatingMessageProcessor directoryExpressionProcesor; + + private volatile Expression remoteDirectoryExpression; + private volatile SessionFactory sessionFactory; private volatile FileNameGenerator fileNameGenerator = new DefaultFileNameGenerator(); @@ -73,6 +79,10 @@ public class FtpSendingMessageHandler extends AbstractMessageHandler{ this.sessionFactory = sessionFactory; } + public void setRemoteDirectoryExpression(Expression remoteDirectoryExpression) { + this.remoteDirectoryExpression = remoteDirectoryExpression; + } + public void setTemporaryBufferFolder(Resource temporaryBufferFolder) { this.temporaryBufferFolder = temporaryBufferFolder; } @@ -90,6 +100,10 @@ public class FtpSendingMessageHandler extends AbstractMessageHandler{ Assert.notNull(this.temporaryBufferFolder, "'temporaryBufferFolder' must not be null"); this.temporaryBufferFolderFile = this.temporaryBufferFolder.getFile(); + if (this.remoteDirectoryExpression != null) { + this.directoryExpressionProcesor = + new ExpressionEvaluatingMessageProcessor(this.remoteDirectoryExpression, String.class); + } } private File handleFileMessage(File sourceFile, File tempFile, File resultFile) throws IOException { @@ -147,7 +161,8 @@ public class FtpSendingMessageHandler extends AbstractMessageHandler{ Session session = this.sessionFactory.getSession(); boolean sentSuccesfully; try { - sentSuccesfully = sendFile(file, session); + String targetDirectory = this.directoryExpressionProcesor.processMessage(message); + sentSuccesfully = sendFile(file, targetDirectory, session); } catch (FileNotFoundException e) { throw new MessageDeliveryException(message, @@ -180,9 +195,10 @@ public class FtpSendingMessageHandler extends AbstractMessageHandler{ } } - private boolean sendFile(File file, Session session) throws FileNotFoundException, IOException { + private boolean sendFile(File file, String targetDirectory, Session session) throws FileNotFoundException, IOException { FileInputStream fileInputStream = new FileInputStream(file); - session.put(fileInputStream, file.getName()); + String remoteFilePath = targetDirectory + File.separatorChar + file.getName(); + session.put(fileInputStream, remoteFilePath); fileInputStream.close(); return true; } diff --git a/spring-integration-ftp/src/main/resources/org/springframework/integration/ftp/config/spring-integration-ftp-2.0.xsd b/spring-integration-ftp/src/main/resources/org/springframework/integration/ftp/config/spring-integration-ftp-2.0.xsd index 9d9642285e..acd54f6aaa 100644 --- a/spring-integration-ftp/src/main/resources/org/springframework/integration/ftp/config/spring-integration-ftp-2.0.xsd +++ b/spring-integration-ftp/src/main/resources/org/springframework/integration/ftp/config/spring-integration-ftp-2.0.xsd @@ -22,6 +22,8 @@ + + @@ -73,7 +75,7 @@ - + diff --git a/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/FtpParserInboundTests-context.xml b/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/FtpParserInboundTests-context.xml index 386536d4f2..ec5e488284 100644 --- a/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/FtpParserInboundTests-context.xml +++ b/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/FtpParserInboundTests-context.xml @@ -21,7 +21,7 @@ diff --git a/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/config/FtpInboundChannelAdapterParserTests-context.xml b/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/config/FtpInboundChannelAdapterParserTests-context.xml index 1cf60380d0..f2cc20ef3e 100644 --- a/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/config/FtpInboundChannelAdapterParserTests-context.xml +++ b/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/config/FtpInboundChannelAdapterParserTests-context.xml @@ -13,7 +13,7 @@ diff --git a/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/config/FtpsInboundChannelAdapterParserTests-context.xml b/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/config/FtpsInboundChannelAdapterParserTests-context.xml index 1e83029497..0fb8aa122f 100644 --- a/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/config/FtpsInboundChannelAdapterParserTests-context.xml +++ b/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/config/FtpsInboundChannelAdapterParserTests-context.xml @@ -19,7 +19,7 @@ diff --git a/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/ftp-message-history-context.xml b/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/ftp-message-history-context.xml index 0cb3be861d..4bc1ed965d 100644 --- a/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/ftp-message-history-context.xml +++ b/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/ftp-message-history-context.xml @@ -22,7 +22,7 @@