Skip to content

Commit b7d4a46

Browse files
authored
Fix Files DSL for auto-create input directory (#10525)
Related to: #7971 The `FileReadingMessageSource` has always had a feature to create a directory to scan on startup. After introducing an expression variant, such a logic is limited only for the `ValueExpression` as only this one comes with a static value. The Java DSL has been changed to support a `Supplier` variant for an input directory. The `Supplier` does not always give us the same value; therefore, we cannot assume directory auto-creation on startup. * Fix `FileInboundChannelAdapterSpec` for the `directory(Expression directoryExpression)` instead of `Supplier` * Fix `Files` to use a `ValueExpression` for statically provided `File directory`
1 parent d4ff307 commit b7d4a46

File tree

2 files changed

+20
-18
lines changed

2 files changed

+20
-18
lines changed

spring-integration-file/src/main/java/org/springframework/integration/file/dsl/FileInboundChannelAdapterSpec.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import java.util.Map;
2424
import java.util.function.Function;
2525
import java.util.function.Predicate;
26-
import java.util.function.Supplier;
2726

2827
import org.jspecify.annotations.Nullable;
2928

@@ -32,7 +31,6 @@
3231
import org.springframework.integration.dsl.ComponentsRegistration;
3332
import org.springframework.integration.dsl.MessageSourceSpec;
3433
import org.springframework.integration.expression.FunctionExpression;
35-
import org.springframework.integration.expression.SupplierExpression;
3634
import org.springframework.integration.file.DirectoryScanner;
3735
import org.springframework.integration.file.FileLocker;
3836
import org.springframework.integration.file.RecursiveDirectoryScanner;
@@ -54,7 +52,7 @@ public class FileInboundChannelAdapterSpec
5452
extends MessageSourceSpec<FileInboundChannelAdapterSpec, FileReadingMessageSource>
5553
implements ComponentsRegistration {
5654

57-
protected final FileListFilterFactoryBean fileListFilterFactoryBean = new FileListFilterFactoryBean(); // NOSONAR
55+
protected final FileListFilterFactoryBean fileListFilterFactoryBean = new FileListFilterFactoryBean();
5856

5957
private @Nullable FileLocker locker;
6058

@@ -73,18 +71,18 @@ protected FileInboundChannelAdapterSpec(@Nullable Comparator<File> receptionOrde
7371
}
7472

7573
/**
76-
* Specify the Supplier for input directory.
77-
* @param directorySupplier the Supplier for directory to poll.
74+
* Specify the SpEL expression for the input directory.
75+
* @param directoryExpression the SpEL expression for directory to poll.
7876
* @return the spec.
7977
* @see FileReadingMessageSource#setDirectoryExpression(Expression)
8078
*/
81-
FileInboundChannelAdapterSpec directory(Supplier<File> directorySupplier) {
82-
this.target.setDirectoryExpression(new SupplierExpression<>(directorySupplier));
79+
FileInboundChannelAdapterSpec directory(Expression directoryExpression) {
80+
this.target.setDirectoryExpression(directoryExpression);
8381
return _this();
8482
}
8583

8684
/**
87-
* A convenient flag to determine if target message source should use a
85+
* A convenient flag to determine if a target message source should use a
8886
* {@link RecursiveDirectoryScanner} or stay with a default one.
8987
* @param recursive to set or not a {@link RecursiveDirectoryScanner}.
9088
* @return the spec.

spring-integration-file/src/main/java/org/springframework/integration/file/dsl/Files.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import org.jspecify.annotations.Nullable;
2525

2626
import org.springframework.expression.Expression;
27+
import org.springframework.integration.expression.SupplierExpression;
28+
import org.springframework.integration.expression.ValueExpression;
2729
import org.springframework.integration.file.transformer.FileToByteArrayTransformer;
2830
import org.springframework.integration.file.transformer.FileToStringTransformer;
2931
import org.springframework.messaging.Message;
@@ -43,7 +45,7 @@ public abstract class Files {
4345
* @return the {@link FileInboundChannelAdapterSpec} instance.
4446
*/
4547
public static FileInboundChannelAdapterSpec inboundAdapter(File directory) {
46-
return inboundAdapter(() -> directory);
48+
return inboundAdapter(directory, null);
4749
}
4850

4951
/**
@@ -65,7 +67,8 @@ public static FileInboundChannelAdapterSpec inboundAdapter(Supplier<File> direct
6567
public static FileInboundChannelAdapterSpec inboundAdapter(File directory,
6668
@Nullable Comparator<File> receptionOrderComparator) {
6769

68-
return inboundAdapter(() -> directory, null);
70+
return new FileInboundChannelAdapterSpec(receptionOrderComparator)
71+
.directory(new ValueExpression<>(directory));
6972
}
7073

7174
/**
@@ -78,7 +81,8 @@ public static FileInboundChannelAdapterSpec inboundAdapter(File directory,
7881
public static FileInboundChannelAdapterSpec inboundAdapter(Supplier<File> directorySupplier,
7982
@Nullable Comparator<File> receptionOrderComparator) {
8083

81-
return new FileInboundChannelAdapterSpec(receptionOrderComparator).directory(directorySupplier);
84+
return new FileInboundChannelAdapterSpec(receptionOrderComparator)
85+
.directory(new SupplierExpression<>(directorySupplier));
8286
}
8387

8488
/**
@@ -92,7 +96,7 @@ public static FileWritingMessageHandlerSpec outboundAdapter(File destinationDire
9296

9397
/**
9498
* Create a {@link FileWritingMessageHandlerSpec} builder for the one-way {@code FileWritingMessageHandler}.
95-
* @param directoryExpression the SpEL expression to evaluate target directory for writing files.
99+
* @param directoryExpression the SpEL expression to evaluate the target directory for writing files.
96100
* @return the {@link FileWritingMessageHandlerSpec} instance.
97101
*/
98102
public static FileWritingMessageHandlerSpec outboundAdapter(String directoryExpression) {
@@ -129,7 +133,7 @@ public static FileWritingMessageHandlerSpec outboundGateway(File destinationDire
129133

130134
/**
131135
* Create a {@link FileWritingMessageHandlerSpec} builder for the gateway {@code FileWritingMessageHandler}.
132-
* @param directoryExpression the SpEL expression to evaluate target directory for writing files.
136+
* @param directoryExpression the SpEL expression to evaluate the target directory for writing files.
133137
* @return the {@link FileWritingMessageHandlerSpec} instance.
134138
*/
135139
public static FileWritingMessageHandlerSpec outboundGateway(String directoryExpression) {
@@ -193,15 +197,15 @@ public static FileSplitterSpec splitter(boolean iterator, boolean markers) {
193197
}
194198

195199
/**
196-
* Create a {@link FileToStringTransformer} instance with default {@code charset} and no delete files afterwards.
200+
* Create a {@link FileToStringTransformer} instance with default {@code charset} and no delete files afterward.
197201
* @return the {@link FileToStringTransformer}.
198202
*/
199203
public static FileToStringTransformer toStringTransformer() {
200204
return toStringTransformer(false);
201205
}
202206

203207
/**
204-
* Create a {@link FileToStringTransformer} instance with default {@code charset} and with delete files flag.
208+
* Create a {@link FileToStringTransformer} instance with default {@code charset} and the flag to delete files.
205209
* @param deleteFiles true to delete the file.
206210
* @return the {@link FileToStringTransformer}.
207211
*/
@@ -210,7 +214,7 @@ public static FileToStringTransformer toStringTransformer(boolean deleteFiles) {
210214
}
211215

212216
/**
213-
* Create a {@link FileToStringTransformer} instance with provided {@code charset} and no delete files afterwards.
217+
* Create a {@link FileToStringTransformer} instance with provided {@code charset} and no delete files afterward.
214218
* @param charset The charset.
215219
* @return the {@link FileToStringTransformer}.
216220
*/
@@ -219,7 +223,7 @@ public static FileToStringTransformer toStringTransformer(String charset) {
219223
}
220224

221225
/**
222-
* Create a {@link FileToStringTransformer} instance with provided {@code charset} and delete files flag.
226+
* Create a {@link FileToStringTransformer} instance with provided {@code charset} and the flag to delete files.
223227
* @param charset The charset.
224228
* @param deleteFiles true to delete the file.
225229
* @return the {@link FileToStringTransformer}.
@@ -244,7 +248,7 @@ public static FileToByteArrayTransformer toByteArrayTransformer() {
244248
/**
245249
* Create a {@link FileToByteArrayTransformer} instance.
246250
* @param deleteFiles specify whether to delete the File after transformation.
247-
* Default is <em>false</em>.
251+
* Default is {@code false}.
248252
* @return the {@link FileToByteArrayTransformer}.
249253
*/
250254
public static FileToByteArrayTransformer toByteArrayTransformer(boolean deleteFiles) {

0 commit comments

Comments
 (0)