INT-3763: (S)FTP - Inbound Remote Dir. Expression

JIRA: https://jira.spring.io/browse/INT-3763

Fix `AbstractRemoteFileSynchronizerTests` according to this change.
This commit is contained in:
Gary Russell
2015-07-08 14:21:53 -04:00
committed by Artem Bilan
parent 5a9b898635
commit c68c7b3412
14 changed files with 113 additions and 35 deletions

View File

@@ -19,6 +19,7 @@ package org.springframework.integration.file.config;
import org.w3c.dom.Element;
import org.springframework.beans.BeanMetadataElement;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.integration.config.ExpressionFactoryBean;
@@ -44,7 +45,9 @@ public abstract class AbstractRemoteFileInboundChannelAdapterParser extends Abst
synchronizerBuilder.addConstructorArgReference(element.getAttribute("session-factory"));
// configure the InboundFileSynchronizer properties
IntegrationNamespaceUtils.setValueIfAttributeDefined(synchronizerBuilder, element, "remote-directory");
BeanDefinition expressionDef = IntegrationNamespaceUtils.createExpressionDefinitionFromValueOrExpression(
"remote-directory", "remote-directory-expression", parserContext, element, true);
synchronizerBuilder.addPropertyValue("remoteDirectoryExpression", expressionDef);
IntegrationNamespaceUtils.setValueIfAttributeDefined(synchronizerBuilder, element, "delete-remote-files");
IntegrationNamespaceUtils.setValueIfAttributeDefined(synchronizerBuilder, element, "preserve-timestamp");

View File

@@ -25,12 +25,16 @@ import java.io.OutputStream;
import java.util.Arrays;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression;
import org.springframework.expression.common.LiteralExpression;
import org.springframework.integration.expression.ExpressionUtils;
import org.springframework.integration.file.filters.FileListFilter;
import org.springframework.integration.file.filters.ReversibleFileListFilter;
@@ -42,9 +46,6 @@ import org.springframework.messaging.MessagingException;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* Base class charged with knowing how to connect to a remote file system,
* scan it for new files and then download the files.
@@ -81,7 +82,7 @@ public abstract class AbstractInboundFileSynchronizer<F>
/**
* the path on the remote mount as a String.
*/
private volatile String remoteDirectory;
private volatile Expression remoteDirectoryExpression;
/**
* An {@link FileListFilter} that runs against the <em>remote</em> file system view.
@@ -145,7 +146,18 @@ public abstract class AbstractInboundFileSynchronizer<F>
* @param remoteDirectory The remote directory.
*/
public void setRemoteDirectory(String remoteDirectory) {
this.remoteDirectory = remoteDirectory;
this.remoteDirectoryExpression = new LiteralExpression(remoteDirectory);
}
/**
* Specify an expression that evaluates to the full path to the remote directory.
*
* @param remoteDirectoryExpression The remote directory expression.
* @since 4.2
*/
public void setRemoteDirectoryExpression(Expression remoteDirectoryExpression) {
Assert.notNull(remoteDirectoryExpression, "'remoteDirectoryExpression' must not be null");
this.remoteDirectoryExpression = remoteDirectoryExpression;
}
/**
@@ -184,7 +196,7 @@ public abstract class AbstractInboundFileSynchronizer<F>
@Override
public final void afterPropertiesSet() {
Assert.notNull(this.remoteDirectory, "remoteDirectory must not be null");
Assert.state(this.remoteDirectoryExpression != null, "'remoteDirectoryExpression' must not be null");
if (this.evaluationContext == null) {
this.evaluationContext = ExpressionUtils.createStandardEvaluationContext(this.beanFactory);
}
@@ -212,14 +224,15 @@ public abstract class AbstractInboundFileSynchronizer<F>
@Override
public Integer doInSession(Session<F> session) throws IOException {
F[] files = session.list(AbstractInboundFileSynchronizer.this.remoteDirectory);
String remoteDirectory = remoteDirectoryExpression.getValue(evaluationContext, String.class);
F[] files = session.list(remoteDirectory);
if (!ObjectUtils.isEmpty(files)) {
List<F> filteredFiles = AbstractInboundFileSynchronizer.this.filterFiles(files);
List<F> filteredFiles = filterFiles(files);
for (F file : filteredFiles) {
try {
if (file != null) {
AbstractInboundFileSynchronizer.this.copyFileToLocalDirectory(
AbstractInboundFileSynchronizer.this.remoteDirectory, file, localDirectory,
copyFileToLocalDirectory(
remoteDirectory, file, localDirectory,
session);
}
}

View File

@@ -731,9 +731,9 @@ Only files matching this regular expression will be picked up by this adapter.
type="xsd:string">
<xsd:annotation>
<xsd:documentation>
Allows you to provide a SpEL expression which
will compute the directory
path where the files will be transferred to
Specify a SpEL expression which
will be used to evaluate the directory
path to where the files will be transferred
(e.g., "headers.['remote_dir'] +
'/myTransfers'");
</xsd:documentation>
@@ -743,9 +743,9 @@ Only files matching this regular expression will be picked up by this adapter.
type="xsd:string">
<xsd:annotation>
<xsd:documentation>
Allows you to provide a SpEL expression which
will compute the temporary directory
path where files will be transferred to before they are moved to the remote-directory
Specify a SpEL expression which
will be used to evaluate the temporary directory
path to where files will be transferred before they are moved to the remote-directory
(e.g., "headers.['remote_dir'] +
'/temp/myTransfers'");
</xsd:documentation>

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014 the original author or authors.
* Copyright 2014-2015 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.
@@ -38,6 +38,7 @@ import org.springframework.messaging.MessagingException;
/**
* @author Gary Russell
* @author Artem Bilan
* @since 4.0.4
*
*/
@@ -76,6 +77,7 @@ public class AbstractRemoteFileSynchronizerTests {
};
sync.setFilter(new AcceptOnceFileListFilter<String>());
sync.setRemoteDirectory("foo");
try {
sync.synchronizeToLocalDirectory(mock(File.class));