diff --git a/org.springframework.integration.adapter/src/main/java/org/springframework/integration/adapter/config/spring-integration-adapters-1.0.xsd b/org.springframework.integration.adapter/src/main/java/org/springframework/integration/adapter/config/spring-integration-adapters-1.0.xsd index ecf7553937..27b8664acd 100644 --- a/org.springframework.integration.adapter/src/main/java/org/springframework/integration/adapter/config/spring-integration-adapters-1.0.xsd +++ b/org.springframework.integration.adapter/src/main/java/org/springframework/integration/adapter/config/spring-integration-adapters-1.0.xsd @@ -27,6 +27,8 @@ + + diff --git a/org.springframework.integration.adapter/src/main/java/org/springframework/integration/adapter/file/config/FileSourceParser.java b/org.springframework.integration.adapter/src/main/java/org/springframework/integration/adapter/file/config/FileSourceParser.java index 808fff2eae..d7876c61e5 100644 --- a/org.springframework.integration.adapter/src/main/java/org/springframework/integration/adapter/file/config/FileSourceParser.java +++ b/org.springframework.integration.adapter/src/main/java/org/springframework/integration/adapter/file/config/FileSourceParser.java @@ -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 <file-source/> 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); } diff --git a/org.springframework.integration.adapter/src/test/java/org/springframework/integration/adapter/file/config/CustomFileFilter.java b/org.springframework.integration.adapter/src/test/java/org/springframework/integration/adapter/file/config/CustomFileFilter.java new file mode 100644 index 0000000000..792e3da4c6 --- /dev/null +++ b/org.springframework.integration.adapter/src/test/java/org/springframework/integration/adapter/file/config/CustomFileFilter.java @@ -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; + } + +} diff --git a/org.springframework.integration.adapter/src/test/java/org/springframework/integration/adapter/file/config/CustomFilenameFilter.java b/org.springframework.integration.adapter/src/test/java/org/springframework/integration/adapter/file/config/CustomFilenameFilter.java new file mode 100644 index 0000000000..8885d32fa9 --- /dev/null +++ b/org.springframework.integration.adapter/src/test/java/org/springframework/integration/adapter/file/config/CustomFilenameFilter.java @@ -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; + } + +} diff --git a/org.springframework.integration.adapter/src/test/java/org/springframework/integration/adapter/file/config/FileSourceParserTests.java b/org.springframework.integration.adapter/src/test/java/org/springframework/integration/adapter/file/config/FileSourceParserTests.java index e30ef276de..ade7022f2f 100644 --- a/org.springframework.integration.adapter/src/test/java/org/springframework/integration/adapter/file/config/FileSourceParserTests.java +++ b/org.springframework.integration.adapter/src/test/java/org/springframework/integration/adapter/file/config/FileSourceParserTests.java @@ -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(); } } diff --git a/org.springframework.integration.adapter/src/test/java/org/springframework/integration/adapter/file/config/fileSourceParserTests.xml b/org.springframework.integration.adapter/src/test/java/org/springframework/integration/adapter/file/config/fileSourceParserTests.xml index 375fe37934..8086603fc2 100644 --- a/org.springframework.integration.adapter/src/test/java/org/springframework/integration/adapter/file/config/fileSourceParserTests.xml +++ b/org.springframework.integration.adapter/src/test/java/org/springframework/integration/adapter/file/config/fileSourceParserTests.xml @@ -11,17 +11,25 @@ http://www.springframework.org/schema/integration/spring-integration-1.0.xsd"> - + - + - + - - - + + + + + + + + + + + diff --git a/org.springframework.integration.adapter/src/test/java/org/springframework/integration/adapter/file/config/fileSourceWithTooManyFilters.xml b/org.springframework.integration.adapter/src/test/java/org/springframework/integration/adapter/file/config/fileSourceWithTooManyFilters.xml new file mode 100644 index 0000000000..28ffbb394a --- /dev/null +++ b/org.springframework.integration.adapter/src/test/java/org/springframework/integration/adapter/file/config/fileSourceWithTooManyFilters.xml @@ -0,0 +1,24 @@ + + + + + + + + + + + +