diff --git a/org.springframework.integration.file/src/main/java/org/springframework/integration/file/PollableFileSource.java b/org.springframework.integration.file/src/main/java/org/springframework/integration/file/PollableFileSource.java index b9fa4ff8d3..06002669a4 100644 --- a/org.springframework.integration.file/src/main/java/org/springframework/integration/file/PollableFileSource.java +++ b/org.springframework.integration.file/src/main/java/org/springframework/integration/file/PollableFileSource.java @@ -17,6 +17,7 @@ package org.springframework.integration.file; import java.io.File; import java.io.FileFilter; +import java.io.IOException; import java.util.HashSet; import java.util.List; import java.util.Queue; @@ -25,7 +26,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.message.GenericMessage; import org.springframework.integration.message.Message; import org.springframework.integration.message.MessageDeliveryAware; @@ -48,7 +49,7 @@ import org.springframework.util.Assert; * * @author Iwein Fuld */ -public class PollableFileSource implements PollableSource, MessageDeliveryAware, InitializingBean { +public class PollableFileSource implements PollableSource, MessageDeliveryAware { private static final Log logger = LogFactory.getLog(PollableFileSource.class); @@ -58,8 +59,16 @@ public class PollableFileSource implements PollableSource, MessageDelivery private volatile FileListFilter filter = new AcceptOnceFileFilter(); - public void setInputDirectory(File inputDirectory) { - this.inputDirectory = inputDirectory; + 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); + } + Assert.isTrue(this.inputDirectory.canRead(), "No read permissions on " + this.inputDirectory); } /** @@ -75,12 +84,6 @@ public class PollableFileSource implements PollableSource, MessageDelivery this.filter = filter; } - public void afterPropertiesSet() throws Exception { - Assert.notNull(inputDirectory, "inputDirectory cannot be null"); - Assert.isTrue(this.inputDirectory.exists(), inputDirectory + " doesn't exist."); - Assert.isTrue(this.inputDirectory.canRead(), "No read permissions on " + inputDirectory); - } - public Message receive() throws MessagingException { refreshQueue(); Message message = null; diff --git a/org.springframework.integration.file/src/main/java/org/springframework/integration/file/config/FileInboundChannelAdapterParser.java b/org.springframework.integration.file/src/main/java/org/springframework/integration/file/config/FileInboundChannelAdapterParser.java new file mode 100644 index 0000000000..04bf84d83f --- /dev/null +++ b/org.springframework.integration.file/src/main/java/org/springframework/integration/file/config/FileInboundChannelAdapterParser.java @@ -0,0 +1,27 @@ +package org.springframework.integration.file.config; + +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.AbstractPollingInboundChannelAdapterParser; +import org.springframework.integration.file.PollableFileSource; +import org.springframework.util.StringUtils; +import org.w3c.dom.Element; + +public class FileInboundChannelAdapterParser extends AbstractPollingInboundChannelAdapterParser { + + @Override + protected String parseSource(Element element, ParserContext parserContext) { + BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(PollableFileSource.class); + String directory = element.getAttribute("directory"); + if (StringUtils.hasText(directory)) { + builder.addPropertyValue("inputDirectory", directory); + } + String filter = element.getAttribute("filter"); + if (StringUtils.hasText(filter)){ + builder.addPropertyReference("filter", filter); + } + return BeanDefinitionReaderUtils.registerWithGeneratedName(builder.getBeanDefinition(), parserContext.getRegistry()); + } + +} diff --git a/org.springframework.integration.file/src/main/java/org/springframework/integration/file/config/FileNamespaceHandler.java b/org.springframework.integration.file/src/main/java/org/springframework/integration/file/config/FileNamespaceHandler.java new file mode 100644 index 0000000000..4f043c3c44 --- /dev/null +++ b/org.springframework.integration.file/src/main/java/org/springframework/integration/file/config/FileNamespaceHandler.java @@ -0,0 +1,11 @@ +package org.springframework.integration.file.config; + +import org.springframework.beans.factory.xml.NamespaceHandlerSupport; + +public class FileNamespaceHandler extends NamespaceHandlerSupport { + + public void init() { + registerBeanDefinitionParser("inbound-channel-adapter", new FileInboundChannelAdapterParser()); + } + +} diff --git a/org.springframework.integration.file/src/main/java/org/springframework/integration/file/config/spring-integration-file-1.0.xsd b/org.springframework.integration.file/src/main/java/org/springframework/integration/file/config/spring-integration-file-1.0.xsd new file mode 100644 index 0000000000..21df8bb4ef --- /dev/null +++ b/org.springframework.integration.file/src/main/java/org/springframework/integration/file/config/spring-integration-file-1.0.xsd @@ -0,0 +1,38 @@ + + + + + + + + + + + + + + + Configures a source that reads from stdin (System.in). + + + + + + + + + + + + + + diff --git a/org.springframework.integration.file/src/main/resources/META-INF/spring.handlers b/org.springframework.integration.file/src/main/resources/META-INF/spring.handlers new file mode 100644 index 0000000000..3c9cb0f55e --- /dev/null +++ b/org.springframework.integration.file/src/main/resources/META-INF/spring.handlers @@ -0,0 +1 @@ +http\://www.springframework.org/schema/integration/file=org.springframework.integration.file.config.FileNamespaceHandler \ No newline at end of file diff --git a/org.springframework.integration.file/src/main/resources/META-INF/spring.schemas b/org.springframework.integration.file/src/main/resources/META-INF/spring.schemas new file mode 100644 index 0000000000..cbf84b3d96 --- /dev/null +++ b/org.springframework.integration.file/src/main/resources/META-INF/spring.schemas @@ -0,0 +1 @@ +http\://www.springframework.org/schema/integration/file/spring-integration-file-1.0.xsd=org/springframework/integration/file/config/spring-integration-file-1.0.xsd \ No newline at end of file diff --git a/org.springframework.integration.file/src/test/java/org/springframework/integration/file/PollableFileSourceIntegrationTests-context.xml b/org.springframework.integration.file/src/test/java/org/springframework/integration/file/PollableFileSourceIntegrationTests-context.xml index 3a92943f39..b834a26b15 100644 --- a/org.springframework.integration.file/src/test/java/org/springframework/integration/file/PollableFileSourceIntegrationTests-context.xml +++ b/org.springframework.integration.file/src/test/java/org/springframework/integration/file/PollableFileSourceIntegrationTests-context.xml @@ -4,7 +4,7 @@ xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> + + + + + + + + + \ No newline at end of file diff --git a/org.springframework.integration.file/src/test/java/org/springframework/integration/file/config/FileInboundChannelAdapterParserTests.java b/org.springframework.integration.file/src/test/java/org/springframework/integration/file/config/FileInboundChannelAdapterParserTests.java new file mode 100644 index 0000000000..d55abb6bc3 --- /dev/null +++ b/org.springframework.integration.file/src/test/java/org/springframework/integration/file/config/FileInboundChannelAdapterParserTests.java @@ -0,0 +1,69 @@ +/* + * 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.file.config; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.io.File; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.DirectFieldAccessor; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.integration.channel.MessageChannel; +import org.springframework.integration.file.CompositeFileFilter; +import org.springframework.integration.file.PollableFileSource; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +/** + * @author Mark Fisher + */ +@ContextConfiguration +@RunWith(SpringJUnit4ClassRunner.class) +public class FileInboundChannelAdapterParserTests { + + @Autowired(required=true) + MessageChannel channel; + + @Autowired(required=true) + PollableFileSource source; + + DirectFieldAccessor accessor; + + @Before public void init(){ + accessor= new DirectFieldAccessor(source); + } + + @Test + public void channelName() throws Exception { + assertEquals("Channel should be available under specified id","inputDirPoller", channel.getName()); + } + + @Test + public void inputDirectory() { + assertEquals("'inputDir' should be set",System.getProperty("java.io.tmpdir"), ((File) accessor.getPropertyValue("inputDirectory")).getPath()); + } + + @Test + public void filter() throws Exception { + assertTrue("'filter' should be set", accessor.getPropertyValue("filter") instanceof CompositeFileFilter); + } + +}