FileSource now uses a Resource, which must point to a directory. For namespace support, in order to simplify configuration, if the value provided in the 'directory' attribute of <file-source/> not a URL nor a 'classpath:' value, the parser will force the 'file:' prefix by default.
This commit is contained in:
@@ -23,6 +23,8 @@ import java.io.IOException;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.integration.ConfigurationException;
|
||||
import org.springframework.integration.message.MessageCreator;
|
||||
import org.springframework.integration.message.MessageDeliveryAware;
|
||||
import org.springframework.integration.message.MessagingException;
|
||||
@@ -46,14 +48,23 @@ public class FileSource extends AbstractDirectorySource implements Source<Object
|
||||
private volatile FilenameFilter filenameFilter;
|
||||
|
||||
|
||||
public FileSource(File directory) {
|
||||
public FileSource(Resource directory) {
|
||||
this(directory, new FileMessageCreator());
|
||||
}
|
||||
|
||||
public FileSource(File directory, MessageCreator<File, ?> messageCreator) {
|
||||
public FileSource(Resource directory, MessageCreator<File, ?> messageCreator) {
|
||||
super(messageCreator);
|
||||
Assert.notNull(directory, "The directory must not be null");
|
||||
this.directory = directory;
|
||||
try {
|
||||
this.directory = directory.getFile();
|
||||
if (!this.directory.isDirectory()) {
|
||||
throw new ConfigurationException("The FileSource can't be instantiated because "
|
||||
+ this.directory.getAbsolutePath() + " is not a directory.");
|
||||
}
|
||||
}
|
||||
catch (IOException e) {
|
||||
throw new ConfigurationException("The FileSource can't be instantiated", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -87,7 +98,7 @@ public class FileSource extends AbstractDirectorySource implements Source<Object
|
||||
|
||||
@Override
|
||||
protected void populateSnapshot(Map<String, FileInfo> snapshot) throws IOException {
|
||||
File[] files = null;
|
||||
File[] files;
|
||||
if (this.fileFilter != null) {
|
||||
files = this.directory.listFiles(this.fileFilter);
|
||||
}
|
||||
@@ -100,10 +111,10 @@ public class FileSource extends AbstractDirectorySource implements Source<Object
|
||||
if (files == null) {
|
||||
throw new MessagingException("Problem occurred while polling for files. " +
|
||||
"Is '" + directory.getAbsolutePath() + "' a directory?");
|
||||
}
|
||||
for (int i = 0; i < files.length; i++) {
|
||||
FileInfo fileInfo = new FileInfo(files[i].getName(), files[i].lastModified(), files[i].length());
|
||||
snapshot.put(files[i].getName(), fileInfo);
|
||||
}
|
||||
for (File file : files) {
|
||||
FileInfo fileInfo = new FileInfo(file.getName(), file.lastModified(), file.length());
|
||||
snapshot.put(file.getName(), fileInfo);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -16,12 +16,15 @@
|
||||
|
||||
package org.springframework.integration.adapter.file.config;
|
||||
|
||||
import org.w3c.dom.Element;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
|
||||
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
||||
import org.springframework.core.io.ResourceLoader;
|
||||
import org.springframework.integration.ConfigurationException;
|
||||
import org.springframework.integration.adapter.file.FileSource;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* Parser for the <file-source/> element.
|
||||
@@ -58,7 +61,12 @@ public class FileSourceParser extends AbstractDirectorySourceParser {
|
||||
|
||||
@Override
|
||||
protected void postProcess(BeanDefinitionBuilder beanDefinition, Element element) {
|
||||
beanDefinition.addConstructorArgValue(element.getAttribute(DIRECTORY_ATTRIBUTE));
|
||||
String directoryLocation = element.getAttribute(DIRECTORY_ATTRIBUTE);
|
||||
if (!directoryLocation.startsWith(ResourceLoader.CLASSPATH_URL_PREFIX)
|
||||
&& !isUrl(directoryLocation)) {
|
||||
directoryLocation = "file:" + directoryLocation;
|
||||
}
|
||||
beanDefinition.addConstructorArgValue(directoryLocation);
|
||||
String fileFilter = element.getAttribute(FILE_FILTER_ATTRIBUTE);
|
||||
String filenameFilter = element.getAttribute(FILENAME_FILTER_ATTRIBUTE);
|
||||
if (StringUtils.hasText(fileFilter) && StringUtils.hasText(filenameFilter)) {
|
||||
@@ -74,4 +82,14 @@ public class FileSourceParser extends AbstractDirectorySourceParser {
|
||||
super.postProcess(beanDefinition, element);
|
||||
}
|
||||
|
||||
private boolean isUrl(String directoryLocation) {
|
||||
try {
|
||||
new URL(directoryLocation);
|
||||
return true;
|
||||
}
|
||||
catch (MalformedURLException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -16,17 +16,18 @@
|
||||
|
||||
package org.springframework.integration.adapter.file.config;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileFilter;
|
||||
import java.io.FilenameFilter;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.beans.DirectFieldAccessor;
|
||||
import org.springframework.beans.factory.BeanCreationException;
|
||||
import org.springframework.beans.factory.BeanDefinitionStoreException;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
@@ -135,4 +136,14 @@ public class FileSourceParserTests {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFileSourceWithFileAndNotDirectory() {
|
||||
try {
|
||||
new ClassPathXmlApplicationContext("fileSourceWithFileDirectory.xml", this.getClass());
|
||||
fail();
|
||||
} catch (BeanCreationException ex) {
|
||||
assertTrue(ex.getCause().getCause().getMessage().indexOf("is not a directory") > 0);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -30,6 +30,4 @@
|
||||
|
||||
<bean id="customFilenameFilter" class="org.springframework.integration.adapter.file.config.CustomFilenameFilter"/>
|
||||
|
||||
<context:property-placeholder/>
|
||||
|
||||
</beans>
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:si="http://www.springframework.org/schema/integration"
|
||||
xmlns:context="http://www.springframework.org/schema/context"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
|
||||
http://www.springframework.org/schema/context
|
||||
http://www.springframework.org/schema/context/spring-context-2.5.xsd
|
||||
http://www.springframework.org/schema/integration
|
||||
http://www.springframework.org/schema/integration/spring-integration-1.0.xsd">
|
||||
|
||||
<si:file-source id="fileSourceDefault" directory="fileSourceWithFileDirectory.xml"/>
|
||||
|
||||
</beans>
|
||||
Reference in New Issue
Block a user