From 37b967cc14f1a4e19bd965c3cc8a5acfda152ed4 Mon Sep 17 00:00:00 2001 From: Oleg Zhurakousky Date: Thu, 18 Nov 2010 01:24:29 -0500 Subject: [PATCH] INT-1631 added remote-file-generator attribute to te outbound adapter, final polishing 76.4% coverage --- .../SftpInboundChannelAdapterParser.java | 11 ++-- .../SftpOutboundChannelAdapterParser.java | 26 +++++++++- .../outbound/SftpSendingMessageHandler.java | 50 +------------------ .../config/spring-integration-sftp-2.0.xsd | 1 + ...erParserTests-context-fail-fileFileGen.xml | 35 +++++++++++++ ...oundChannelAdapaterParserTests-context.xml | 4 +- .../OutboundChannelAdapaterParserTests.java | 14 +++++- .../SftpOutboundTransferSample-ignored.xml | 1 + .../src/test/resources/log4j.properties | 8 +++ 9 files changed, 92 insertions(+), 58 deletions(-) create mode 100644 spring-integration-sftp/src/test/java/org/springframework/integration/sftp/config/OutboundChannelAdapaterParserTests-context-fail-fileFileGen.xml create mode 100644 spring-integration-sftp/src/test/resources/log4j.properties diff --git a/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/config/SftpInboundChannelAdapterParser.java b/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/config/SftpInboundChannelAdapterParser.java index 4b0225cb60..ac01dab247 100644 --- a/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/config/SftpInboundChannelAdapterParser.java +++ b/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/config/SftpInboundChannelAdapterParser.java @@ -44,11 +44,14 @@ public class SftpInboundChannelAdapterParser extends AbstractPollingInboundChann String filter = element.getAttribute("filter"); boolean hasFileNamePattern = StringUtils.hasText(fileNamePattern); boolean hasFilter = StringUtils.hasText(filter); - if (!(hasFileNamePattern ^ hasFilter)) { - throw new BeanDefinitionStoreException("exactly one of 'filename-pattern' or 'filter' " + - "is allowed on SFTP inbound adapter"); - } + if (hasFileNamePattern | hasFilter){ + if (!(hasFileNamePattern ^ hasFilter)) { + throw new BeanDefinitionStoreException("exactly one of 'filename-pattern' or 'filter' " + + "is allowed on SFTP inbound adapter"); + } + } + BeanDefinitionBuilder sessionPollBuilder = BeanDefinitionBuilder.genericBeanDefinition("org.springframework.integration.sftp.session.QueuedSftpSessionPool"); sessionPollBuilder.addConstructorArgReference(sessionFactoryName); diff --git a/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/config/SftpOutboundChannelAdapterParser.java b/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/config/SftpOutboundChannelAdapterParser.java index 933fad358d..e942de53f2 100644 --- a/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/config/SftpOutboundChannelAdapterParser.java +++ b/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/config/SftpOutboundChannelAdapterParser.java @@ -21,6 +21,7 @@ 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.BeanDefinitionReaderUtils; import org.springframework.beans.factory.support.RootBeanDefinition; import org.springframework.beans.factory.xml.ParserContext; import org.springframework.integration.config.xml.AbstractOutboundChannelAdapterParser; @@ -39,10 +40,13 @@ public class SftpOutboundChannelAdapterParser extends AbstractOutboundChannelAda BeanDefinitionBuilder sessionPollBuilder = BeanDefinitionBuilder.genericBeanDefinition("org.springframework.integration.sftp.session.QueuedSftpSessionPool"); sessionPollBuilder.addConstructorArgReference(element.getAttribute("session-factory")); + String sessionPollName = + BeanDefinitionReaderUtils.registerWithGeneratedName(sessionPollBuilder.getBeanDefinition(), parserContext.getRegistry()); + BeanDefinitionBuilder handlerBuilder = BeanDefinitionBuilder.genericBeanDefinition("org.springframework.integration.sftp.outbound.SftpSendingMessageHandler"); - handlerBuilder.addConstructorArgValue(sessionPollBuilder.getBeanDefinition()); + handlerBuilder.addConstructorArgReference(sessionPollName); IntegrationNamespaceUtils.setValueIfAttributeDefined(handlerBuilder, element, "charset"); String remoteDirectory = element.getAttribute("remote-directory"); @@ -63,7 +67,25 @@ public class SftpOutboundChannelAdapterParser extends AbstractOutboundChannelAda expressionDef.getConstructorArgumentValues().addGenericArgumentValue(remoteDirectoryExpression); } handlerBuilder.addPropertyValue("remoteDirectoryExpression", expressionDef); - + + String remoteFileExpression = element.getAttribute("remote-file-expression"); + String fileNameGenerator = element.getAttribute("filename-generator"); + boolean hasRemoteFileExp = StringUtils.hasText(remoteFileExpression); + boolean hasFileNameGener = StringUtils.hasText(fileNameGenerator); + if (hasRemoteFileExp | hasFileNameGener){ + if (!(hasRemoteFileExp ^ hasFileNameGener)) { + throw new BeanDefinitionStoreException("exactly one of 'remote-file-expression' or 'filename-generator' " + + "is allowed on SFTP outbound adapter"); + } + if (StringUtils.hasText(remoteFileExpression)){ + BeanDefinitionBuilder fNameGenerBuilder = + BeanDefinitionBuilder.genericBeanDefinition("org.springframework.integration.file.DefaultFileNameGenerator"); + fNameGenerBuilder.addPropertyValue("expression", remoteFileExpression); + handlerBuilder.addPropertyValue("filenameGenerator", fNameGenerBuilder.getBeanDefinition()); + } + } + + IntegrationNamespaceUtils.setReferenceIfAttributeDefined(handlerBuilder, element, "filename-generator"); return handlerBuilder.getBeanDefinition(); } diff --git a/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/outbound/SftpSendingMessageHandler.java b/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/outbound/SftpSendingMessageHandler.java index 7865d22511..1bc93cc904 100644 --- a/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/outbound/SftpSendingMessageHandler.java +++ b/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/outbound/SftpSendingMessageHandler.java @@ -23,12 +23,10 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStreamWriter; import java.nio.charset.Charset; -import java.util.concurrent.locks.ReentrantLock; import org.apache.commons.io.IOUtils; import org.apache.commons.lang.SystemUtils; -import org.springframework.context.SmartLifecycle; import org.springframework.core.io.FileSystemResource; import org.springframework.core.io.Resource; import org.springframework.expression.Expression; @@ -55,11 +53,9 @@ import com.jcraft.jsch.ChannelSftp; * @author Oleg Zhurakousky * @since 2.0 */ -public class SftpSendingMessageHandler extends AbstractMessageHandler implements SmartLifecycle{ +public class SftpSendingMessageHandler extends AbstractMessageHandler{ private static final String TEMPORARY_FILE_SUFFIX = ".writing"; - - private final ReentrantLock lifecycleLock = new ReentrantLock(); private final SftpSessionPool sessionPool; @@ -75,17 +71,11 @@ public class SftpSendingMessageHandler extends AbstractMessageHandler implements private volatile String charset = Charset.defaultCharset().name(); - private volatile boolean started; - - private volatile boolean autoStartup = true; - - public SftpSendingMessageHandler(SftpSessionPool sessionPool) { Assert.notNull(sessionPool, "'sessionPool' must not be null"); this.sessionPool = sessionPool; } - public void setTemporaryBufferFolder(Resource temporaryBufferFolder) { this.temporaryBufferFolder = temporaryBufferFolder; } @@ -109,42 +99,6 @@ public class SftpSendingMessageHandler extends AbstractMessageHandler implements new ExpressionEvaluatingMessageProcessor(remoteDirectoryExpression, String.class); } } - -// Lifecycle - - public void start() { - sessionPool.start(); - started = true; - } - - public void stop() { - sessionPool.stop(); - started = false; - } - - public boolean isRunning() { - return started; - } - - public int getPhase() { - return 0; - } - - public boolean isAutoStartup() { - return autoStartup; - } - - public void stop(Runnable callback) { - this.lifecycleLock.lock(); - try { - this.stop(); - callback.run(); - } - finally { - this.lifecycleLock.unlock(); - } - } - @Override protected void handleMessageInternal(Message message) throws Exception { @@ -215,7 +169,7 @@ public class SftpSendingMessageHandler extends AbstractMessageHandler implements } InputStream fileInputStream = null; try { - //session.start(); + session.start(); ChannelSftp sftp = session.getChannel(); fileInputStream = new FileInputStream(file); String baseOfRemotePath = ""; diff --git a/spring-integration-sftp/src/main/resources/org/springframework/integration/sftp/config/spring-integration-sftp-2.0.xsd b/spring-integration-sftp/src/main/resources/org/springframework/integration/sftp/config/spring-integration-sftp-2.0.xsd index 856e372206..b59ca72f36 100644 --- a/spring-integration-sftp/src/main/resources/org/springframework/integration/sftp/config/spring-integration-sftp-2.0.xsd +++ b/spring-integration-sftp/src/main/resources/org/springframework/integration/sftp/config/spring-integration-sftp-2.0.xsd @@ -55,6 +55,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 new file mode 100644 index 0000000000..a2688f4387 --- /dev/null +++ b/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/config/OutboundChannelAdapaterParserTests-context-fail-fileFileGen.xml @@ -0,0 +1,35 @@ + + + + + + + + + + + + + + + + + + + + + + + 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 805cc18356..8116e56134 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 @@ -31,8 +31,8 @@ session-factory="sftpSessionFactory" channel="inputChannel" charset="UTF-8" - filename-generator="fileNameGenerator" - remote-directory-expression="'foo' + '/' + 'bar'"/> + remote-directory-expression="'foo' + '/' + 'bar'" + remote-file-expression="payload.getName() + '-foo'"/> diff --git a/spring-integration-sftp/src/test/resources/log4j.properties b/spring-integration-sftp/src/test/resources/log4j.properties new file mode 100644 index 0000000000..d57195708d --- /dev/null +++ b/spring-integration-sftp/src/test/resources/log4j.properties @@ -0,0 +1,8 @@ +log4j.rootCategory=WARN, stdout + +log4j.appender.stdout=org.apache.log4j.ConsoleAppender +log4j.appender.stdout.layout=org.apache.log4j.PatternLayout +log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %t %c{2}:%L - %m%n + +log4j.category.org.springframework=WARN +log4j.category.org.springframework.integration.sftp=TRACE