INT-740 Added FileWritingMessageHandlerFactoryBean

This commit is contained in:
Mark Fisher
2009-07-18 19:26:47 +00:00
parent 714595a6c1
commit cacf92f44a
5 changed files with 282 additions and 6 deletions

View File

@@ -42,12 +42,9 @@ abstract class FileWritingMessageHandlerBeanDefinitionBuilder {
if (!StringUtils.hasText(directory)) {
parserContext.getReaderContext().error("directory is required", element);
}
if (directory.indexOf(':') == -1) {
directory = "file:" + directory;
}
BeanDefinitionBuilder builder = BeanDefinitionBuilder
.genericBeanDefinition("org.springframework.integration.file.FileWritingMessageHandler");
builder.addConstructorArgValue(directory);
.genericBeanDefinition("org.springframework.integration.file.config.FileWritingMessageHandlerFactoryBean");
builder.addPropertyValue("directory", directory);
if (StringUtils.hasText(outputChannelBeanName)) {
builder.addPropertyReference("outputChannel", outputChannelBeanName);
}

View File

@@ -0,0 +1,175 @@
/*
* Copyright 2002-2009 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 org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceEditor;
import org.springframework.core.io.ResourceLoader;
import org.springframework.integration.channel.ChannelResolver;
import org.springframework.integration.core.MessageChannel;
import org.springframework.integration.file.FileNameGenerator;
import org.springframework.integration.file.FileWritingMessageHandler;
import org.springframework.util.Assert;
/**
* @author Mark Fisher
* @since 1.0.3
*/
public class FileWritingMessageHandlerFactoryBean implements FactoryBean, BeanFactoryAware, ResourceLoaderAware {
private volatile FileWritingMessageHandler handler;
private volatile ResourceLoader resourceLoader;
private volatile BeanFactory beanFactory;
private volatile String directory;
private volatile MessageChannel outputChannel;
private volatile ChannelResolver channelResolver;
private volatile String charset;
private volatile FileNameGenerator fileNameGenerator;
private volatile Boolean deleteSourceFiles;
private volatile Boolean autoCreateDirectory;
private volatile Boolean requiresReply;
private volatile Long sendTimeout;
private volatile Integer order;
private final Object initializationMonitor = new Object();
public void setResourceLoader(ResourceLoader resourceLoader) {
this.resourceLoader = resourceLoader;
}
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
this.beanFactory = beanFactory;
}
public void setDirectory(String directory) {
Assert.hasText(directory, "directory must not be empty");
if (directory.indexOf(':') == -1) {
directory = "file:" + directory;
}
this.directory = directory;
}
public void setOutputChannel(MessageChannel outputChannel) {
this.outputChannel = outputChannel;
}
public void setChannelResolver(ChannelResolver channelResolver) {
this.channelResolver = channelResolver;
}
public void setCharset(String charset) {
this.charset = charset;
}
public void setFileNameGenerator(FileNameGenerator fileNameGenerator) {
this.fileNameGenerator = fileNameGenerator;
}
public void setDeleteSourceFiles(Boolean deleteSourceFiles) {
this.deleteSourceFiles = deleteSourceFiles;
}
public void setAutoCreateDirectory(Boolean autoCreateDirectory) {
this.autoCreateDirectory = autoCreateDirectory;
}
public void setRequiresReply(Boolean requiresReply) {
this.requiresReply = requiresReply;
}
public void setSendTimeout(Long sendTimeout) {
this.sendTimeout = sendTimeout;
}
public void setOrder(Integer order) {
this.order = order;
}
public Object getObject() throws Exception {
if (this.handler == null) {
initHandler();
}
return this.handler;
}
public Class<?> getObjectType() {
return FileWritingMessageHandler.class;
}
public boolean isSingleton() {
return true;
}
private void initHandler() {
synchronized (this.initializationMonitor) {
if (this.handler != null) {
return;
}
ResourceEditor editor = new ResourceEditor(this.resourceLoader);
editor.setAsText(this.directory);
this.handler = new FileWritingMessageHandler((Resource) editor.getValue());
if (this.outputChannel != null) {
this.handler.setOutputChannel(this.outputChannel);
}
if (this.channelResolver != null) {
this.handler.setChannelResolver(this.channelResolver);
}
if (this.charset != null) {
this.handler.setCharset(this.charset);
}
if (this.fileNameGenerator != null) {
this.handler.setFileNameGenerator(this.fileNameGenerator);
}
if (this.deleteSourceFiles != null) {
this.handler.setDeleteSourceFiles(this.deleteSourceFiles);
}
if (this.autoCreateDirectory != null) {
this.handler.setAutoCreateDirectory(this.autoCreateDirectory);
}
if (this.requiresReply != null) {
this.handler.setRequiresReply(this.requiresReply);
}
if (this.sendTimeout != null) {
this.handler.setSendTimeout(this.sendTimeout);
}
if (this.order != null) {
this.handler.setOrder(this.order);
}
this.handler.setBeanFactory(this.beanFactory);
this.handler.afterPropertiesSet();
}
}
}

View File

@@ -0,0 +1,33 @@
<?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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
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/util http://www.springframework.org/schema/util/spring-util-2.5.xsd
http://www.springframework.org/schema/integration/file http://www.springframework.org/schema/integration/file/spring-integration-file-1.0.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-1.0.xsd">
<integration:channel id="input1"/>
<integration:channel id="input2"/>
<outbound-channel-adapter id="adapter"
channel="input1"
directory="${outputdir}"
auto-startup="false">
</outbound-channel-adapter>
<outbound-gateway id="gateway"
request-channel="input2"
directory="${outputdir}"
auto-startup="false">
</outbound-gateway>
<context:property-placeholder location="classpath:org/springframework/integration/file/config/test.properties" />
</beans:beans>

View File

@@ -0,0 +1,70 @@
/*
* Copyright 2002-2009 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 java.io.File;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.io.ClassPathResource;
import org.springframework.integration.endpoint.EventDrivenConsumer;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author Iwein Fuld
* @author Mark Fisher
* @since 1.0.3
*/
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class FileOutboundAdaptersWithClasspathInPropertiesTests {
@Autowired
@Qualifier("adapter")
private EventDrivenConsumer adapter;
@Autowired
@Qualifier("gateway")
private EventDrivenConsumer gateway;
@Test
public void outboundChannelAdapter() throws Exception {
DirectFieldAccessor accessor = new DirectFieldAccessor(
new DirectFieldAccessor(adapter).getPropertyValue("handler"));
File expected = new ClassPathResource("").getFile();
File actual = (File) accessor.getPropertyValue("destinationDirectory");
assertEquals("'destinationDirectory' should be set", expected, actual);
}
@Test
public void outboundGateway() throws Exception {
DirectFieldAccessor accessor = new DirectFieldAccessor(
new DirectFieldAccessor(gateway).getPropertyValue("handler"));
File expected = new ClassPathResource("").getFile();
File actual = (File) accessor.getPropertyValue("destinationDirectory");
assertEquals("'destinationDirectory' should be set", expected, actual);
}
}

View File

@@ -1 +1,2 @@
inputdir=classpath:
inputdir=classpath:
outputdir=classpath: