Solving INT-185, INT-207, INT 201 - refactoring FileSource and FileTarget, splitting AbstractFileMapper in MessageCreator and MessageMapper, adding namespace support for configurable MessageCreator in FileSource and filename generator in FileTarget. Backup directory is not supported anymore, instead FileSource will not delete files and will ignore files already processed.

This commit is contained in:
Marius Bogoevici
2008-05-23 01:04:24 +00:00
parent 052b59bdd0
commit 2085270dc2
27 changed files with 651 additions and 172 deletions

View File

@@ -0,0 +1,34 @@
/*
* 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 org.springframework.integration.message.GenericMessage;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.MessageCreator;
/**
* @author Marius Bogoevici
*/
public class CustomMessageCreator implements MessageCreator<File, String>{
public Message<String> createMessage(File object) {
return new GenericMessage<String> (object.getAbsolutePath());
}
}

View File

@@ -0,0 +1,33 @@
/*
* 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.util.Date;
import org.springframework.integration.adapter.file.FileNameGenerator;
import org.springframework.integration.message.Message;
/**
* @author Marius Bogoevici
*/
public class CustomNameGenerator implements FileNameGenerator{
public String generateFileName(Message<?> message) {
return "file" + new Date().getTime();
}
}

View File

@@ -17,28 +17,91 @@
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 org.junit.Test;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.beans.factory.BeanDefinitionStoreException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.ConfigurationException;
import org.springframework.integration.adapter.file.ByteArrayFileMessageCreator;
import org.springframework.integration.adapter.file.FileMessageCreator;
import org.springframework.integration.adapter.file.FileSource;
import org.springframework.integration.adapter.file.TextFileMessageCreator;
/**
* @author Mark Fisher
* @author Marius Bogoevici
*/
public class FileSourceParserTests {
@Test
public void testFileSource() {
public void testFileSourceDefaultType() {
ApplicationContext context = new ClassPathXmlApplicationContext("fileSourceParserTests.xml", this.getClass());
FileSource fileSource = (FileSource) context.getBean("fileSource");
FileSource fileSource = (FileSource) context.getBean("fileSourceDefault");
DirectFieldAccessor sourceAccessor = new DirectFieldAccessor(fileSource);
File directory = (File) sourceAccessor.getPropertyValue("directory");
Object messageCreator = sourceAccessor.getPropertyValue("messageCreator");
assertEquals(System.getProperty("java.io.tmpdir"), directory.getAbsolutePath());
assertTrue(messageCreator instanceof FileMessageCreator);
}
@Test
public void testFileSourceTextType() {
ApplicationContext context = new ClassPathXmlApplicationContext("fileSourceParserTests.xml", this.getClass());
FileSource fileSource = (FileSource) context.getBean("fileSourceText");
DirectFieldAccessor sourceAccessor = new DirectFieldAccessor(fileSource);
File directory = (File) sourceAccessor.getPropertyValue("directory");
Object messageCreator = sourceAccessor.getPropertyValue("messageCreator");
assertEquals(System.getProperty("java.io.tmpdir"), directory.getAbsolutePath());
assertTrue(messageCreator instanceof TextFileMessageCreator);
}
@Test
public void testFileSourceBinaryType() {
ApplicationContext context = new ClassPathXmlApplicationContext("fileSourceParserTests.xml", this.getClass());
FileSource fileSource = (FileSource) context.getBean("fileSourceBinary");
DirectFieldAccessor sourceAccessor = new DirectFieldAccessor(fileSource);
File directory = (File) sourceAccessor.getPropertyValue("directory");
Object messageCreator = sourceAccessor.getPropertyValue("messageCreator");
assertEquals(System.getProperty("java.io.tmpdir"), directory.getAbsolutePath());
assertTrue(messageCreator instanceof ByteArrayFileMessageCreator);
}
@Test
public void testFileSourceFileType() {
ApplicationContext context = new ClassPathXmlApplicationContext("fileSourceParserTests.xml", this.getClass());
FileSource fileSource = (FileSource) context.getBean("fileSourceFile");
DirectFieldAccessor sourceAccessor = new DirectFieldAccessor(fileSource);
File directory = (File) sourceAccessor.getPropertyValue("directory");
Object messageCreator = sourceAccessor.getPropertyValue("messageCreator");
assertEquals(System.getProperty("java.io.tmpdir"), directory.getAbsolutePath());
assertTrue(messageCreator instanceof FileMessageCreator);
}
@Test
public void testFileSourceCustomType() {
ApplicationContext context = new ClassPathXmlApplicationContext("fileSourceParserTests.xml", this.getClass());
FileSource fileSource = (FileSource) context.getBean("fileSourceCustom");
DirectFieldAccessor sourceAccessor = new DirectFieldAccessor(fileSource);
File directory = (File) sourceAccessor.getPropertyValue("directory");
Object messageCreator = sourceAccessor.getPropertyValue("messageCreator");
assertEquals(System.getProperty("java.io.tmpdir"), directory.getAbsolutePath());
assertTrue(messageCreator instanceof CustomMessageCreator);
}
@Test
public void testInvalidFileSource() {
try {
ApplicationContext context = new ClassPathXmlApplicationContext("invalidFileSourceTests.xml", this.getClass());
fail();
} catch (BeanDefinitionStoreException e) {
assertTrue(e.getCause() instanceof ConfigurationException);
}
}
}

View File

@@ -17,24 +17,44 @@
package org.springframework.integration.adapter.file.config;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.io.File;
import org.junit.Test;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.adapter.file.DefaultFileNameGenerator;
import org.springframework.integration.adapter.file.FileTarget;
import org.springframework.integration.message.Target;
import org.springframework.integration.adapter.file.SimpleFileMessageMapper;
/**
* @author Mark Fisher
* @author Marius Bogoevici
*/
public class FileTargetParserTests {
@Test
public void testFileTarget() {
ApplicationContext context = new ClassPathXmlApplicationContext("fileTargetParserTests.xml", this.getClass());
Target target = (Target) context.getBean("target");
assertEquals(FileTarget.class, target.getClass());
FileTarget target = (FileTarget) context.getBean("target");
DirectFieldAccessor targetFieldAccessor = new DirectFieldAccessor(target);
SimpleFileMessageMapper messageMapper = (SimpleFileMessageMapper) targetFieldAccessor.getPropertyValue("messageMapper");
DirectFieldAccessor mapperAccessor = new DirectFieldAccessor(messageMapper);
assertEquals(System.getProperty("java.io.tmpdir"), ((File) mapperAccessor.getPropertyValue("parentDirectory")).getAbsolutePath());
assertTrue(mapperAccessor.getPropertyValue("fileNameGenerator") instanceof DefaultFileNameGenerator);
}
@Test
public void testFileTargetWithCustomFilenameGenerator() {
ApplicationContext context = new ClassPathXmlApplicationContext("fileTargetParserTests.xml", this.getClass());
FileTarget target = (FileTarget) context.getBean("targetWithCustomNameGenerator");
DirectFieldAccessor targetFieldAccessor = new DirectFieldAccessor(target);
SimpleFileMessageMapper messageMapper = (SimpleFileMessageMapper) targetFieldAccessor.getPropertyValue("messageMapper");
DirectFieldAccessor mapperAccessor = new DirectFieldAccessor(messageMapper);
assertEquals(System.getProperty("java.io.tmpdir"), ((File) mapperAccessor.getPropertyValue("parentDirectory")).getAbsolutePath());
assertTrue(mapperAccessor.getPropertyValue("fileNameGenerator") instanceof CustomNameGenerator);
}
}

View File

@@ -10,7 +10,17 @@
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}"/>
<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}"/>
<bean id="customMessageCreator" class="org.springframework.integration.adapter.file.config.CustomMessageCreator"/>
<context:property-placeholder/>

View File

@@ -15,6 +15,10 @@
<si:channel id="testChannel"/>
<si:file-target id="target" directory="${java.io.tmpdir}"/>
<si:file-target id="targetWithCustomNameGenerator" name-generator="customFileNameGenerator" directory="${java.io.tmpdir}"/>
<bean id="customFileNameGenerator" class="org.springframework.integration.adapter.file.config.CustomNameGenerator"/>
<context:property-placeholder/>

View File

@@ -0,0 +1,19 @@
<?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="fileSourceCustom" type="text" message-creator="customMessageCreator" directory="${java.io.tmpdir}"/>
<bean id="customMessageCreator" class="org.springframework.integration.adapter.file.config.CustomMessageCreator"/>
<context:property-placeholder/>
</beans>