INT-724, INT-725 Added an 'auto-create-directory' attribute to all File adapters with a default value of TRUE. Now, if the inbound or outbound directory does not yet exist, it may be created upon initialization.
This commit is contained in:
@@ -28,6 +28,7 @@ import java.util.concurrent.PriorityBlockingQueue;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.integration.aggregator.Resequencer;
|
||||
import org.springframework.integration.core.Message;
|
||||
@@ -61,7 +62,7 @@ import org.springframework.util.Assert;
|
||||
* @author Iwein Fuld
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
public class FileReadingMessageSource implements MessageSource<File> {
|
||||
public class FileReadingMessageSource implements MessageSource<File>, InitializingBean {
|
||||
|
||||
private static final int INTERNAL_QUEUE_CAPACITY = 5;
|
||||
|
||||
@@ -69,6 +70,8 @@ public class FileReadingMessageSource implements MessageSource<File> {
|
||||
|
||||
private volatile File inputDirectory;
|
||||
|
||||
private volatile boolean autoCreateDirectory = true;
|
||||
|
||||
/**
|
||||
* {@link PriorityBlockingQueue#iterator()} throws
|
||||
* {@link java.util.ConcurrentModificationException} in Java 5.
|
||||
@@ -99,16 +102,29 @@ public class FileReadingMessageSource implements MessageSource<File> {
|
||||
toBeReceived = new PriorityBlockingQueue<File>(INTERNAL_QUEUE_CAPACITY, receptionOrderComparator);
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify the input directory.
|
||||
*/
|
||||
public void setInputDirectory(Resource inputDirectory) {
|
||||
Assert.notNull(inputDirectory, "inputDirectory cannot be null");
|
||||
Assert.isTrue(inputDirectory.exists(), inputDirectory + " doesn't exist.");
|
||||
try {
|
||||
this.inputDirectory = inputDirectory.getFile();
|
||||
}
|
||||
catch (IOException e) {
|
||||
throw new IllegalArgumentException("Unexpected IOException when looking for " + inputDirectory, e);
|
||||
throw new IllegalArgumentException(
|
||||
"Unexpected IOException when looking for source directory: " + inputDirectory, e);
|
||||
}
|
||||
Assert.isTrue(this.inputDirectory.canRead(), "No read permissions on " + this.inputDirectory);
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify whether to create the source directory automatically if it does
|
||||
* not yet exist upon initialization. By default, this value is
|
||||
* <emphasis>true</emphasis>. If set to <emphasis>false</emphasis> and the
|
||||
* source directory does not exist, an Exception will be thrown upon
|
||||
* initialization.
|
||||
*/
|
||||
public void setAutoCreateDirectory(boolean autoCreateDirectory) {
|
||||
this.autoCreateDirectory = autoCreateDirectory;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -136,6 +152,18 @@ public class FileReadingMessageSource implements MessageSource<File> {
|
||||
this.scanEachPoll = scanEachPoll;
|
||||
}
|
||||
|
||||
public final void afterPropertiesSet() {
|
||||
if (!this.inputDirectory.exists() && this.autoCreateDirectory) {
|
||||
this.inputDirectory.mkdirs();
|
||||
}
|
||||
Assert.isTrue(this.inputDirectory.exists(),
|
||||
"Source directory [" + inputDirectory + "] does not exist.");
|
||||
Assert.isTrue(this.inputDirectory.isDirectory(),
|
||||
"Source path [" + this.inputDirectory + "] does not point to a directory.");
|
||||
Assert.isTrue(this.inputDirectory.canRead(),
|
||||
"Source directory [" + this.inputDirectory + "] is not readable.");
|
||||
}
|
||||
|
||||
public Message<File> receive() throws MessagingException {
|
||||
Message<File> message = null;
|
||||
// rescan only if needed or explicitly configured
|
||||
|
||||
@@ -25,6 +25,7 @@ import java.nio.charset.Charset;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.integration.core.Message;
|
||||
import org.springframework.integration.handler.AbstractReplyProducingMessageHandler;
|
||||
@@ -57,7 +58,7 @@ import org.springframework.util.FileCopyUtils;
|
||||
* @author Iwein Fuld
|
||||
* @author Alex Peters
|
||||
*/
|
||||
public class FileWritingMessageHandler extends AbstractReplyProducingMessageHandler {
|
||||
public class FileWritingMessageHandler extends AbstractReplyProducingMessageHandler implements InitializingBean {
|
||||
|
||||
private static final String TEMPORARY_FILE_SUFFIX =".writing";
|
||||
|
||||
@@ -68,6 +69,8 @@ public class FileWritingMessageHandler extends AbstractReplyProducingMessageHand
|
||||
|
||||
private final File destinationDirectory;
|
||||
|
||||
private volatile boolean autoCreateDirectory = true;
|
||||
|
||||
private volatile boolean deleteSourceFiles;
|
||||
|
||||
private volatile Charset charset = Charset.defaultCharset();
|
||||
@@ -75,20 +78,26 @@ public class FileWritingMessageHandler extends AbstractReplyProducingMessageHand
|
||||
|
||||
public FileWritingMessageHandler(Resource destinationDirectory) {
|
||||
try {
|
||||
Assert.isTrue(destinationDirectory.exists(),
|
||||
"Output directory [" + destinationDirectory + "] does not exist");
|
||||
this.destinationDirectory = destinationDirectory.getFile();
|
||||
Assert.isTrue(this.destinationDirectory.isDirectory(),
|
||||
"[" + this.destinationDirectory + "] is not a directory");
|
||||
Assert.isTrue(this.destinationDirectory.canWrite(),
|
||||
"[" + this.destinationDirectory + "] is not writable");
|
||||
}
|
||||
catch (IOException e) {
|
||||
throw new IllegalArgumentException("Inaccessible output directory", e);
|
||||
throw new IllegalArgumentException(
|
||||
"Unexpected IOException when looking for destination directory: " + destinationDirectory, e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Specify whether to create the destination directory automatically if it
|
||||
* does not yet exist upon initialization. By default, this value is
|
||||
* <emphasis>true</emphasis>. If set to <emphasis>false</emphasis> and the
|
||||
* destination directory does not exist, an Exception will be thrown upon
|
||||
* initialization.
|
||||
*/
|
||||
public void setAutoCreateDirectory(boolean autoCreateDirectory) {
|
||||
this.autoCreateDirectory = autoCreateDirectory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Provide the {@link FileNameGenerator} strategy to use when generating
|
||||
* the destination file's name.
|
||||
@@ -119,6 +128,18 @@ public class FileWritingMessageHandler extends AbstractReplyProducingMessageHand
|
||||
this.charset = Charset.forName(charset);
|
||||
}
|
||||
|
||||
public void afterPropertiesSet() {
|
||||
if (!this.destinationDirectory.exists() && this.autoCreateDirectory) {
|
||||
this.destinationDirectory.mkdirs();
|
||||
}
|
||||
Assert.isTrue(destinationDirectory.exists(),
|
||||
"Destination directory [" + destinationDirectory + "] does not exist.");
|
||||
Assert.isTrue(this.destinationDirectory.isDirectory(),
|
||||
"Destination path [" + this.destinationDirectory + "] does not point to a directory.");
|
||||
Assert.isTrue(this.destinationDirectory.canWrite(),
|
||||
"Destination directory [" + this.destinationDirectory + "] is not writable.");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void handleRequestMessage(Message<?> requestMessage, ReplyMessageHolder replyMessageHolder) {
|
||||
Assert.notNull(requestMessage, "message must not be null");
|
||||
|
||||
@@ -23,6 +23,7 @@ import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionReaderUtils;
|
||||
import org.springframework.beans.factory.xml.ParserContext;
|
||||
import org.springframework.integration.config.xml.AbstractPollingInboundChannelAdapterParser;
|
||||
import org.springframework.integration.config.xml.IntegrationNamespaceUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
@@ -51,6 +52,7 @@ public class FileInboundChannelAdapterParser extends AbstractPollingInboundChann
|
||||
}
|
||||
builder.addPropertyValue("inputDirectory", directory);
|
||||
}
|
||||
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "auto-create-directory");
|
||||
String filterBeanName = this.registerFileListFilter(element, parserContext);
|
||||
builder.addPropertyReference("filter", filterBeanName);
|
||||
return BeanDefinitionReaderUtils.registerWithGeneratedName(builder.getBeanDefinition(), parserContext.getRegistry());
|
||||
|
||||
@@ -51,6 +51,7 @@ abstract class FileWritingMessageHandlerBeanDefinitionBuilder {
|
||||
if (StringUtils.hasText(outputChannelBeanName)) {
|
||||
builder.addPropertyReference("outputChannel", outputChannelBeanName);
|
||||
}
|
||||
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "auto-create-directory");
|
||||
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "delete-source-files");
|
||||
String fileNameGenerator = element.getAttribute("filename-generator");
|
||||
if (StringUtils.hasText(fileNameGenerator)) {
|
||||
|
||||
@@ -72,6 +72,15 @@
|
||||
</xsd:annotation>
|
||||
</xsd:attribute>
|
||||
<xsd:attribute name="auto-startup" type="xsd:string" default="true"/>
|
||||
<xsd:attribute name="auto-create-directory" type="xsd:string" default="true">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation>
|
||||
Specify whether to automatically create the source directory if it does not yet exist when this
|
||||
adapter is being initialized. The default value is 'true'. If set to 'false' and the directory
|
||||
does not exist upon initialization, an Exception will be thrown.
|
||||
</xsd:documentation>
|
||||
</xsd:annotation>
|
||||
</xsd:attribute>
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
|
||||
@@ -164,6 +173,15 @@
|
||||
]]></xsd:documentation>
|
||||
</xsd:annotation>
|
||||
</xsd:attribute>
|
||||
<xsd:attribute name="auto-create-directory" type="xsd:string" default="true">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation>
|
||||
Specify whether to automatically create the destination directory if it does not yet exist
|
||||
when this adapter is being initialized. The default value is 'true'. If set to 'false' and
|
||||
the directory does not exist upon initialization, an Exception will be thrown.
|
||||
</xsd:documentation>
|
||||
</xsd:annotation>
|
||||
</xsd:attribute>
|
||||
<xsd:attribute name="auto-startup" type="xsd:string"/>
|
||||
</xsd:complexType>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user