OPEN - issue INT-364: add namespace support to file project

http://jira.springframework.org/browse/INT-364
Added basic namespace support and parser test. Made inputDirectory a Resource instead of a File
This commit is contained in:
Iwein Fuld
2008-09-21 18:32:44 +00:00
parent d20a8977be
commit 9f5e625d3b
10 changed files with 196 additions and 19 deletions

View File

@@ -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<File>, MessageDeliveryAware<File>, InitializingBean {
public class PollableFileSource implements PollableSource<File>, MessageDeliveryAware<File> {
private static final Log logger = LogFactory.getLog(PollableFileSource.class);
@@ -58,8 +59,16 @@ public class PollableFileSource implements PollableSource<File>, 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<File>, 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<File> receive() throws MessagingException {
refreshQueue();
Message<File> message = null;

View File

@@ -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());
}
}

View File

@@ -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());
}
}

View File

@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns="http://www.springframework.org/schema/integration/file"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:tool="http://www.springframework.org/schema/tool"
xmlns:integration="http://www.springframework.org/schema/integration"
targetNamespace="http://www.springframework.org/schema/integration/file"
elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xsd:import namespace="http://www.springframework.org/schema/beans"/>
<xsd:import namespace="http://www.springframework.org/schema/tool"/>
<xsd:import namespace="http://www.springframework.org/schema/integration"/>
<xsd:annotation>
<xsd:documentation><![CDATA[
Defines the configuration elements for Spring Integration Stream-based Channel Adapters.
]]></xsd:documentation>
</xsd:annotation>
<xsd:element name="inbound-channel-adapter">
<xsd:annotation>
<xsd:documentation>
Configures a source that reads from stdin (System.in).
</xsd:documentation>
</xsd:annotation>
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="integration:poller" minOccurs="0" maxOccurs="1"/>
</xsd:sequence>
<xsd:attribute name="id" type="xsd:string"/>
<xsd:attribute name="directory" type="xsd:string"/>
<xsd:attribute name="channel" type="xsd:string"/>
<xsd:attribute name="filter" type="xsd:string"/>
</xsd:complexType>
</xsd:element>
</xsd:schema>

View File

@@ -0,0 +1 @@
http\://www.springframework.org/schema/integration/file=org.springframework.integration.file.config.FileNamespaceHandler

View File

@@ -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

View File

@@ -4,7 +4,7 @@
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- under test -->
<bean id="pollableFileSource" class="org.springframework.integration.file.PollableFileSource"
p:inputDirectory="${java.io.tmpdir}/PollableFileSourceIntegrationTests"
p:inputDirectory="file:${java.io.tmpdir}/PollableFileSourceIntegrationTests"
p:filter-ref="compositeFilter" />
<!-- customized filter -->
<bean id="compositeFilter"

View File

@@ -15,10 +15,7 @@
*/
package org.springframework.integration.file;
import static org.easymock.EasyMock.expect;
import static org.easymock.classextension.EasyMock.createMock;
import static org.easymock.classextension.EasyMock.replay;
import static org.easymock.classextension.EasyMock.verify;
import static org.easymock.classextension.EasyMock.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
@@ -27,6 +24,7 @@ import java.io.File;
import org.junit.Before;
import org.junit.Test;
import org.springframework.core.io.Resource;
import org.springframework.integration.message.Message;
/**
@@ -39,17 +37,25 @@ public class PollableFileSourceTests {
private File inputDirectoryMock = createMock(File.class);
private File inputDirectory;
private Resource inputDirectoryResourceMock = createNiceMock(Resource.class);
private File fileMock = createMock(File.class);
private Object[] allMocks = new Object[] { inputDirectoryMock, fileMock };
private Object[] allMocks = new Object[] { inputDirectoryMock, fileMock, inputDirectoryResourceMock };
public void prepResource() throws Exception {
expect(inputDirectoryResourceMock.exists()).andReturn(true).anyTimes();
expect(inputDirectoryResourceMock.getFile()).andReturn(inputDirectoryMock).anyTimes();
expect(inputDirectoryMock.canRead()).andReturn(true);
replay(inputDirectoryResourceMock, inputDirectoryMock);
}
@Before
public void initialize() throws Exception {
prepResource();
this.pollableFileSource = new PollableFileSource();
pollableFileSource.setInputDirectory(inputDirectory);
pollableFileSource.setInputDirectory(inputDirectoryMock);
pollableFileSource.setInputDirectory(inputDirectoryResourceMock);
reset(allMocks);
}
@Test

View File

@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/integration/file"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:integration="http://www.springframework.org/schema/integration"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration-1.0.xsd
http://www.springframework.org/schema/integration/file
http://www.springframework.org/schema/integration/file/spring-integration-file-1.0.xsd">
<inbound-channel-adapter id="inputDirPoller"
directory="file:${java.io.tmpdir}" filter="filter" />
<beans:bean id="filter"
class="org.springframework.integration.file.CompositeFileFilter">
<beans:constructor-arg>
<beans:list></beans:list>
</beans:constructor-arg>
</beans:bean>
<beans:bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" />
</beans:beans>

View File

@@ -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);
}
}