Skip to content

Commit c68c7b3

Browse files
garyrussellartembilan
authored andcommitted
INT-3763: (S)FTP - Inbound Remote Dir. Expression
JIRA: https://jira.spring.io/browse/INT-3763 Fix `AbstractRemoteFileSynchronizerTests` according to this change.
1 parent 5a9b898 commit c68c7b3

File tree

14 files changed

+113
-35
lines changed

14 files changed

+113
-35
lines changed

spring-integration-file/src/main/java/org/springframework/integration/file/config/AbstractRemoteFileInboundChannelAdapterParser.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.w3c.dom.Element;
2020

2121
import org.springframework.beans.BeanMetadataElement;
22+
import org.springframework.beans.factory.config.BeanDefinition;
2223
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
2324
import org.springframework.beans.factory.xml.ParserContext;
2425
import org.springframework.integration.config.ExpressionFactoryBean;
@@ -44,7 +45,9 @@ protected final BeanMetadataElement parseSource(Element element, ParserContext p
4445
synchronizerBuilder.addConstructorArgReference(element.getAttribute("session-factory"));
4546

4647
// configure the InboundFileSynchronizer properties
47-
IntegrationNamespaceUtils.setValueIfAttributeDefined(synchronizerBuilder, element, "remote-directory");
48+
BeanDefinition expressionDef = IntegrationNamespaceUtils.createExpressionDefinitionFromValueOrExpression(
49+
"remote-directory", "remote-directory-expression", parserContext, element, true);
50+
synchronizerBuilder.addPropertyValue("remoteDirectoryExpression", expressionDef);
4851
IntegrationNamespaceUtils.setValueIfAttributeDefined(synchronizerBuilder, element, "delete-remote-files");
4952
IntegrationNamespaceUtils.setValueIfAttributeDefined(synchronizerBuilder, element, "preserve-timestamp");
5053

spring-integration-file/src/main/java/org/springframework/integration/file/remote/synchronizer/AbstractInboundFileSynchronizer.java

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,16 @@
2525
import java.util.Arrays;
2626
import java.util.List;
2727

28+
import org.apache.commons.logging.Log;
29+
import org.apache.commons.logging.LogFactory;
30+
2831
import org.springframework.beans.BeansException;
2932
import org.springframework.beans.factory.BeanFactory;
3033
import org.springframework.beans.factory.BeanFactoryAware;
3134
import org.springframework.beans.factory.InitializingBean;
3235
import org.springframework.expression.EvaluationContext;
3336
import org.springframework.expression.Expression;
37+
import org.springframework.expression.common.LiteralExpression;
3438
import org.springframework.integration.expression.ExpressionUtils;
3539
import org.springframework.integration.file.filters.FileListFilter;
3640
import org.springframework.integration.file.filters.ReversibleFileListFilter;
@@ -42,9 +46,6 @@
4246
import org.springframework.util.Assert;
4347
import org.springframework.util.ObjectUtils;
4448

45-
import org.apache.commons.logging.Log;
46-
import org.apache.commons.logging.LogFactory;
47-
4849
/**
4950
* Base class charged with knowing how to connect to a remote file system,
5051
* scan it for new files and then download the files.
@@ -81,7 +82,7 @@ public abstract class AbstractInboundFileSynchronizer<F>
8182
/**
8283
* the path on the remote mount as a String.
8384
*/
84-
private volatile String remoteDirectory;
85+
private volatile Expression remoteDirectoryExpression;
8586

8687
/**
8788
* An {@link FileListFilter} that runs against the <em>remote</em> file system view.
@@ -145,7 +146,18 @@ public void setTemporaryFileSuffix(String temporaryFileSuffix) {
145146
* @param remoteDirectory The remote directory.
146147
*/
147148
public void setRemoteDirectory(String remoteDirectory) {
148-
this.remoteDirectory = remoteDirectory;
149+
this.remoteDirectoryExpression = new LiteralExpression(remoteDirectory);
150+
}
151+
152+
/**
153+
* Specify an expression that evaluates to the full path to the remote directory.
154+
*
155+
* @param remoteDirectoryExpression The remote directory expression.
156+
* @since 4.2
157+
*/
158+
public void setRemoteDirectoryExpression(Expression remoteDirectoryExpression) {
159+
Assert.notNull(remoteDirectoryExpression, "'remoteDirectoryExpression' must not be null");
160+
this.remoteDirectoryExpression = remoteDirectoryExpression;
149161
}
150162

151163
/**
@@ -184,7 +196,7 @@ public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
184196

185197
@Override
186198
public final void afterPropertiesSet() {
187-
Assert.notNull(this.remoteDirectory, "remoteDirectory must not be null");
199+
Assert.state(this.remoteDirectoryExpression != null, "'remoteDirectoryExpression' must not be null");
188200
if (this.evaluationContext == null) {
189201
this.evaluationContext = ExpressionUtils.createStandardEvaluationContext(this.beanFactory);
190202
}
@@ -212,14 +224,15 @@ public void synchronizeToLocalDirectory(final File localDirectory) {
212224

213225
@Override
214226
public Integer doInSession(Session<F> session) throws IOException {
215-
F[] files = session.list(AbstractInboundFileSynchronizer.this.remoteDirectory);
227+
String remoteDirectory = remoteDirectoryExpression.getValue(evaluationContext, String.class);
228+
F[] files = session.list(remoteDirectory);
216229
if (!ObjectUtils.isEmpty(files)) {
217-
List<F> filteredFiles = AbstractInboundFileSynchronizer.this.filterFiles(files);
230+
List<F> filteredFiles = filterFiles(files);
218231
for (F file : filteredFiles) {
219232
try {
220233
if (file != null) {
221-
AbstractInboundFileSynchronizer.this.copyFileToLocalDirectory(
222-
AbstractInboundFileSynchronizer.this.remoteDirectory, file, localDirectory,
234+
copyFileToLocalDirectory(
235+
remoteDirectory, file, localDirectory,
223236
session);
224237
}
225238
}

spring-integration-file/src/main/resources/org/springframework/integration/file/config/spring-integration-file-4.2.xsd

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -731,9 +731,9 @@ Only files matching this regular expression will be picked up by this adapter.
731731
type="xsd:string">
732732
<xsd:annotation>
733733
<xsd:documentation>
734-
Allows you to provide a SpEL expression which
735-
will compute the directory
736-
path where the files will be transferred to
734+
Specify a SpEL expression which
735+
will be used to evaluate the directory
736+
path to where the files will be transferred
737737
(e.g., "headers.['remote_dir'] +
738738
'/myTransfers'");
739739
</xsd:documentation>
@@ -743,9 +743,9 @@ Only files matching this regular expression will be picked up by this adapter.
743743
type="xsd:string">
744744
<xsd:annotation>
745745
<xsd:documentation>
746-
Allows you to provide a SpEL expression which
747-
will compute the temporary directory
748-
path where files will be transferred to before they are moved to the remote-directory
746+
Specify a SpEL expression which
747+
will be used to evaluate the temporary directory
748+
path to where files will be transferred before they are moved to the remote-directory
749749
(e.g., "headers.['remote_dir'] +
750750
'/temp/myTransfers'");
751751
</xsd:documentation>

spring-integration-file/src/test/java/org/springframework/integration/file/remote/synchronizer/AbstractRemoteFileSynchronizerTests.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014 the original author or authors.
2+
* Copyright 2014-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -38,6 +38,7 @@
3838

3939
/**
4040
* @author Gary Russell
41+
* @author Artem Bilan
4142
* @since 4.0.4
4243
*
4344
*/
@@ -76,6 +77,7 @@ protected void copyFileToLocalDirectory(String remoteDirectoryPath, String remot
7677

7778
};
7879
sync.setFilter(new AcceptOnceFileListFilter<String>());
80+
sync.setRemoteDirectory("foo");
7981

8082
try {
8183
sync.synchronizeToLocalDirectory(mock(File.class));

spring-integration-ftp/src/main/resources/org/springframework/integration/ftp/config/spring-integration-ftp-4.2.xsd

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,18 @@
215215
</xsd:documentation>
216216
</xsd:annotation>
217217
</xsd:attribute>
218+
<xsd:attribute name="remote-directory-expression"
219+
type="xsd:string">
220+
<xsd:annotation>
221+
<xsd:documentation>
222+
Specify a SpEL expression which
223+
will be used to evaluate the directory
224+
path from where the files will be transferred
225+
(e.g., "@someBean.fetchDirectory");
226+
Mutually exclusive with 'remote-directory'.
227+
</xsd:documentation>
228+
</xsd:annotation>
229+
</xsd:attribute>
218230
</xsd:extension>
219231
</xsd:complexContent>
220232
</xsd:complexType>
@@ -500,11 +512,11 @@
500512
<xsd:complexType name="base-ftp-adapter-type">
501513
<xsd:complexContent>
502514
<xsd:extension base="base-adapter-type">
503-
<xsd:attribute name="remote-directory" type="xsd:string"
504-
use="optional">
515+
<xsd:attribute name="remote-directory" type="xsd:string" use="optional">
505516
<xsd:annotation>
506517
<xsd:documentation>
507518
Identifies the remote directory path (e.g., "/remote/mytransfers")
519+
Mutually exclusive with 'remote-directory-expression'.
508520
</xsd:documentation>
509521
</xsd:annotation>
510522
</xsd:attribute>

spring-integration-ftp/src/test/java/org/springframework/integration/ftp/config/FtpInboundChannelAdapterParserTests-context.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
comparator="comparator"
3030
temporary-file-suffix=".foo"
3131
local-filter="acceptAllFilter"
32-
remote-directory="foo/bar">
32+
remote-directory-expression="'foo/bar'">
3333
<int:poller fixed-rate="1000">
3434
<int:transactional synchronization-factory="syncFactory"/>
3535
</int:poller>

spring-integration-ftp/src/test/java/org/springframework/integration/ftp/config/FtpInboundChannelAdapterParserTests.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import org.springframework.beans.factory.annotation.Autowired;
3838
import org.springframework.beans.factory.annotation.Qualifier;
3939
import org.springframework.context.ApplicationContext;
40+
import org.springframework.expression.Expression;
4041
import org.springframework.integration.endpoint.SourcePollingChannelAdapter;
4142
import org.springframework.integration.file.filters.FileListFilter;
4243
import org.springframework.integration.file.remote.session.CachingSessionFactory;
@@ -96,6 +97,8 @@ public void testFtpInboundChannelAdapterComplete() throws Exception{
9697

9798
FtpInboundFileSynchronizer fisync =
9899
(FtpInboundFileSynchronizer) TestUtils.getPropertyValue(inbound, "synchronizer");
100+
assertEquals("'foo/bar'", TestUtils.getPropertyValue(fisync, "remoteDirectoryExpression", Expression.class)
101+
.getExpressionString());
99102
assertNotNull(TestUtils.getPropertyValue(fisync, "localFilenameGeneratorExpression"));
100103
assertTrue(TestUtils.getPropertyValue(fisync, "preserveTimestamp", Boolean.class));
101104
assertEquals(".foo", TestUtils.getPropertyValue(fisync, "temporaryFileSuffix", String.class));
@@ -131,6 +134,8 @@ public void cachingSessionFactory() throws Exception{
131134
String remoteFileSeparator = (String) TestUtils.getPropertyValue(fisync, "remoteFileSeparator");
132135
assertNotNull(remoteFileSeparator);
133136
assertEquals("/", remoteFileSeparator);
137+
assertEquals("foo/bar", TestUtils.getPropertyValue(fisync, "remoteDirectoryExpression", Expression.class)
138+
.getExpressionString());
134139
}
135140

136141
@Test

spring-integration-jpa/src/test/java/org/springframework/integration/jpa/config/xml/JpaInboundChannelAdapterParserTests.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
entity-manager-factory="entityManagerFactory"
1818
entity-class="org.springframework.integration.jpa.test.entity.StudentDomain"
1919
expect-single-result="true"
20+
parameter-source=""
2021
channel="out">
2122
<int:poller fixed-rate="5000"/>
2223
</int-jpa:inbound-channel-adapter>

spring-integration-sftp/src/main/resources/org/springframework/integration/sftp/config/spring-integration-sftp-4.2.xsd

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,18 @@
219219
</xsd:documentation>
220220
</xsd:annotation>
221221
</xsd:attribute>
222+
<xsd:attribute name="remote-directory-expression"
223+
type="xsd:string">
224+
<xsd:annotation>
225+
<xsd:documentation>
226+
Specify a SpEL expression which
227+
will be used to evaluate the directory
228+
path from where the files will be transferred
229+
(e.g., "@someBean.fetchDirectory").
230+
Mutually exclusive with 'remote-directory'.
231+
</xsd:documentation>
232+
</xsd:annotation>
233+
</xsd:attribute>
222234
</xsd:extension>
223235
</xsd:complexContent>
224236
</xsd:complexType>
@@ -501,12 +513,12 @@
501513
<xsd:complexType name="base-sftp-adapter-type">
502514
<xsd:complexContent>
503515
<xsd:extension base="base-adapter-type">
504-
<xsd:attribute name="remote-directory" type="xsd:string"
505-
use="optional">
516+
<xsd:attribute name="remote-directory" type="xsd:string" use="optional">
506517
<xsd:annotation>
507518
<xsd:documentation>
508519
Identifies the directory path (e.g.,
509520
"/temp/mytransfers")
521+
Mutually exclusive with 'remote-directory-expression'.
510522
</xsd:documentation>
511523
</xsd:annotation>
512524
</xsd:attribute>

spring-integration-sftp/src/test/java/org/springframework/integration/sftp/config/InboundChannelAdapterParserTests-context.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
session-factory="sftpSessionFactory"
4242
channel="requestChannel"
4343
filename-regex="f[o]+\.txt"
44-
remote-directory="/foo"
44+
remote-directory-expression="'/foo'"
4545
local-directory="file:local-test-dir"
4646
auto-create-local-directory="false"
4747
remote-file-separator="."

0 commit comments

Comments
 (0)