Added namespace support for 'file-filter' and 'filename-filter' properties of FileSource (INT-242).

This commit is contained in:
Mark Fisher
2008-06-03 17:13:39 +00:00
parent 6334aedfe1
commit bfabc1a14b
7 changed files with 160 additions and 13 deletions

View File

@@ -27,6 +27,8 @@
<xsd:attribute name="directory" type="xsd:string" use="required"/>
<xsd:attribute name="type" type="fileSourceType" use="optional"/>
<xsd:attribute name="message-creator" type="xsd:string" use="optional"/>
<xsd:attribute name="file-filter" type="xsd:string"/>
<xsd:attribute name="filename-filter" type="xsd:string"/>
</xsd:complexType>
</xsd:element>

View File

@@ -19,7 +19,9 @@ package org.springframework.integration.adapter.file.config;
import org.w3c.dom.Element;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.integration.ConfigurationException;
import org.springframework.integration.adapter.file.FileSource;
import org.springframework.util.StringUtils;
/**
* Parser for the &lt;file-source/&gt; element.
@@ -31,6 +33,10 @@ public class FileSourceParser extends AbstractDirectorySourceParser {
public static final String DIRECTORY_ATTRIBUTE = "directory";
public static final String FILE_FILTER_ATTRIBUTE = "file-filter";
public static final String FILENAME_FILTER_ATTRIBUTE = "filename-filter";
public FileSourceParser() {
super(false);
@@ -44,12 +50,27 @@ public class FileSourceParser extends AbstractDirectorySourceParser {
@Override
protected boolean isEligibleAttribute(String attributeName) {
return !DIRECTORY_ATTRIBUTE.equals(attributeName) && super.isEligibleAttribute(attributeName);
return !DIRECTORY_ATTRIBUTE.equals(attributeName) &&
!FILE_FILTER_ATTRIBUTE.equals(attributeName) &&
!FILENAME_FILTER_ATTRIBUTE.equals(attributeName) &&
super.isEligibleAttribute(attributeName);
}
@Override
protected void postProcess(BeanDefinitionBuilder beanDefinition, Element element) {
beanDefinition.addConstructorArgValue(element.getAttribute(DIRECTORY_ATTRIBUTE));
beanDefinition.addConstructorArgValue(element.getAttribute(DIRECTORY_ATTRIBUTE));
String fileFilter = element.getAttribute(FILE_FILTER_ATTRIBUTE);
String filenameFilter = element.getAttribute(FILENAME_FILTER_ATTRIBUTE);
if (StringUtils.hasText(fileFilter) && StringUtils.hasText(filenameFilter)) {
throw new ConfigurationException("FileSource does not support both '" +
FILE_FILTER_ATTRIBUTE + "' and '" + FILENAME_FILTER_ATTRIBUTE + "'.");
}
else if (StringUtils.hasText(fileFilter)) {
beanDefinition.addPropertyReference("fileFilter", fileFilter);
}
else if (StringUtils.hasText(filenameFilter)) {
beanDefinition.addPropertyReference("filenameFilter", filenameFilter);
}
super.postProcess(beanDefinition, element);
}

View File

@@ -0,0 +1,31 @@
/*
* Copyright 2002-2008 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.adapter.file.config;
import java.io.File;
import java.io.FileFilter;
/**
* @author Mark Fisher
*/
public class CustomFileFilter implements FileFilter {
public boolean accept(File pathname) {
return false;
}
}

View File

@@ -0,0 +1,31 @@
/*
* Copyright 2002-2008 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.adapter.file.config;
import java.io.File;
import java.io.FilenameFilter;
/**
* @author Mark Fisher
*/
public class CustomFilenameFilter implements FilenameFilter {
public boolean accept(File dir, String name) {
return false;
}
}

View File

@@ -21,6 +21,8 @@ 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 org.junit.Test;
@@ -84,10 +86,20 @@ public class FileSourceParserTests {
assertTrue(messageCreator instanceof FileMessageCreator);
}
@Test(expected=ConfigurationException.class)
public void testInvalidFileSource() throws Throwable {
try {
new ClassPathXmlApplicationContext("invalidFileSourceTests.xml", this.getClass());
fail();
} catch (BeanDefinitionStoreException e) {
throw e.getCause();
}
}
@Test
public void testFileSourceCustomType() {
public void testFileSourceWithCustomMessageCreator() {
ApplicationContext context = new ClassPathXmlApplicationContext("fileSourceParserTests.xml", this.getClass());
FileSource fileSource = (FileSource) context.getBean("fileSourceCustom");
FileSource fileSource = (FileSource) context.getBean("fileSourceWithCustomMessageCreator");
DirectFieldAccessor sourceAccessor = new DirectFieldAccessor(fileSource);
File directory = (File) sourceAccessor.getPropertyValue("directory");
Object messageCreator = sourceAccessor.getPropertyValue("messageCreator");
@@ -96,12 +108,30 @@ public class FileSourceParserTests {
}
@Test
public void testInvalidFileSource() {
public void testFileSourceWithCustomFileFilter() {
ApplicationContext context = new ClassPathXmlApplicationContext("fileSourceParserTests.xml", this.getClass());
FileSource fileSource = (FileSource) context.getBean("fileSourceWithCustomFileFilter");
DirectFieldAccessor accessor = new DirectFieldAccessor(fileSource);
FileFilter filter = (FileFilter) context.getBean("customFileFilter");
assertEquals(filter, accessor.getPropertyValue("fileFilter"));
}
@Test
public void testFileSourceWithCustomFilenameFilter() {
ApplicationContext context = new ClassPathXmlApplicationContext("fileSourceParserTests.xml", this.getClass());
FileSource fileSource = (FileSource) context.getBean("fileSourceWithCustomFilenameFilter");
DirectFieldAccessor accessor = new DirectFieldAccessor(fileSource);
FilenameFilter filter = (FilenameFilter) context.getBean("customFilenameFilter");
assertEquals(filter, accessor.getPropertyValue("filenameFilter"));
}
@Test(expected=ConfigurationException.class)
public void testFileSourceWithFileFilterAndFilenameFilterNotAllowed() throws Throwable {
try {
new ClassPathXmlApplicationContext("invalidFileSourceTests.xml", this.getClass());
new ClassPathXmlApplicationContext("fileSourceWithTooManyFilters.xml", this.getClass());
fail();
} catch (BeanDefinitionStoreException e) {
assertTrue(e.getCause() instanceof ConfigurationException);
throw e.getCause();
}
}

View File

@@ -11,17 +11,25 @@
http://www.springframework.org/schema/integration/spring-integration-1.0.xsd">
<si:file-source id="fileSourceDefault" directory="${java.io.tmpdir}"/>
<si:file-source id="fileSourceText" type="text" directory="${java.io.tmpdir}"/>
<si:file-source id="fileSourceBinary" type="binary" directory="${java.io.tmpdir}"/>
<si:file-source id="fileSourceFile" type="file" directory="${java.io.tmpdir}"/>
<si:file-source id="fileSourceCustom" message-creator="customMessageCreator" directory="${java.io.tmpdir}"/>
<si:file-source id="fileSourceWithCustomMessageCreator" message-creator="customMessageCreator" directory="${java.io.tmpdir}"/>
<si:file-source id="fileSourceWithCustomFileFilter" file-filter="customFileFilter" directory="${java.io.tmpdir}"/>
<si:file-source id="fileSourceWithCustomFilenameFilter" filename-filter="customFilenameFilter" directory="${java.io.tmpdir}"/>
<bean id="customMessageCreator" class="org.springframework.integration.adapter.file.config.CustomMessageCreator"/>
<bean id="customFileFilter" class="org.springframework.integration.adapter.file.config.CustomFileFilter"/>
<bean id="customFilenameFilter" class="org.springframework.integration.adapter.file.config.CustomFilenameFilter"/>
<context:property-placeholder/>
</beans>

View File

@@ -0,0 +1,24 @@
<?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="fileSource"
directory="${java.io.tmpdir}"
file-filter="customFileFilter"
filename-filter="customFilenameFilter"/>
<bean id="customFileFilter" class="org.springframework.integration.adapter.file.config.CustomFileFilter"/>
<bean id="customFilenameFilter" class="org.springframework.integration.adapter.file.config.CustomFilenameFilter"/>
<context:property-placeholder/>
</beans>