FtpSource no longer uses a MessageCreator. Instead, it uses MessageBuilder internally, and any Message customization can be applied with Transformers.

This commit is contained in:
Mark Fisher
2008-09-25 15:39:14 +00:00
parent d30fe71dc2
commit ec304b2c10
10 changed files with 51 additions and 174 deletions

View File

@@ -25,11 +25,10 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.MessageCreator;
import org.springframework.integration.message.MessageBuilder;
import org.springframework.integration.message.MessageDeliveryAware;
import org.springframework.integration.message.MessagingException;
import org.springframework.integration.message.PollableSource;
import org.springframework.util.Assert;
/**
* Base class for implementing a PollableSource that creates messages from files
@@ -46,17 +45,13 @@ public abstract class AbstractDirectorySource<T> implements PollableSource<T>, M
private final Backlog<FileSnapshot> backlog;
private final MessageCreator<T, T> messageCreator;
public AbstractDirectorySource(MessageCreator<T, T> messageCreator) {
this(messageCreator, null);
public AbstractDirectorySource() {
this(null);
}
public AbstractDirectorySource(MessageCreator<T, T> messageCreator, Comparator<FileSnapshot> comparator) {
public AbstractDirectorySource(Comparator<FileSnapshot> comparator) {
this.backlog = comparator == null ? new Backlog<FileSnapshot>() : new Backlog<FileSnapshot>(comparator);
Assert.notNull(messageCreator, "The MessageCreator must not be null");
this.messageCreator = messageCreator;
}
@@ -64,10 +59,6 @@ public abstract class AbstractDirectorySource<T> implements PollableSource<T>, M
return this.backlog;
}
public MessageCreator<T, T> getMessageCreator() {
return this.messageCreator;
}
public final Message<T> receive() {
try {
refreshSnapshotAndMarkProcessing(this.backlog);
@@ -89,17 +80,16 @@ public abstract class AbstractDirectorySource<T> implements PollableSource<T>, M
/**
* Hook point for implementors to create the next message that should be
* received. Implementations can use a File by File approach (like
* FileSource). In cases where retrieval could be expensive because of
* network latency, a batched approach could be implemented here. See
* FtpSource for an example.
* received. Implementations can use a File by File approach or in cases
* where retrieval could be expensive because of network latency, a batched
* approach could be implemented here. See FtpSource for an example.
*
* @return the next message containing (part of) the unprocessed content of
* the directory
* @throws IOException
*/
protected Message<T> buildNextMessage() throws IOException {
return this.messageCreator.createMessage(retrieveNextPayload());
return MessageBuilder.withPayload(retrieveNextPayload()).build();
}
public void onSend(Message<T> message) {

View File

@@ -25,8 +25,6 @@ import java.util.List;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.springframework.integration.message.DefaultMessageCreator;
import org.springframework.integration.message.MessageCreator;
import org.springframework.util.Assert;
/**
@@ -46,11 +44,6 @@ public class FtpSource extends AbstractDirectorySource<List<File>> {
public FtpSource(FTPClientPool clientPool) {
this(new DefaultMessageCreator<List<File>>(), clientPool);
}
public FtpSource(MessageCreator<List<File>, List<File>> messageCreator, FTPClientPool clientPool) {
super(messageCreator);
this.clientPool = clientPool;
}
@@ -84,8 +77,8 @@ public class FtpSource extends AbstractDirectorySource<List<File>> {
* if files couldn't be parsed
*/
if (ftpFile != null) {
FileSnapshot fileSnapshot = new FileSnapshot(ftpFile.getName(), ftpFile.getTimestamp()
.getTimeInMillis(), ftpFile.getSize());
FileSnapshot fileSnapshot = new FileSnapshot(ftpFile.getName(),
ftpFile.getTimestamp().getTimeInMillis(), ftpFile.getSize());
snapshot.add(fileSnapshot);
}
}
@@ -101,8 +94,7 @@ public class FtpSource extends AbstractDirectorySource<List<File>> {
List<File> files = new ArrayList<File>();
List<FileSnapshot> toDo = this.getBacklog().getProcessingBuffer();
for (FileSnapshot fileSnapshot : toDo) {
// some awkwardness here because the local path may be different
// from the remote path
// local path may be different from the remote path
File file = new File(this.localWorkingDirectory, fileSnapshot.getFileName());
if (file.exists()) {
file.delete();

View File

@@ -47,6 +47,7 @@ public class QueuedFTPClientPool implements FTPClientPool {
private static final String DEFAULT_REMOTE_WORKING_DIRECTORY = "/";
private final Queue<FTPClient> pool;
private volatile FTPClientConfig config;
@@ -59,13 +60,25 @@ public class QueuedFTPClientPool implements FTPClientPool {
private volatile String password;
private volatile FTPClientFactory factory = new DefaultFactory();
private volatile FTPClientFactory factory = new DefaultFTPClientFactory();
private final Log log = LogFactory.getLog(this.getClass());
private volatile String remoteWorkingDirectory = DEFAULT_REMOTE_WORKING_DIRECTORY;
// setters
public QueuedFTPClientPool() {
this(DEFAULT_POOL_SIZE);
}
/**
* @param maxPoolSize the maximum size of the pool
*/
public QueuedFTPClientPool(int maxPoolSize) {
pool = new ArrayBlockingQueue<FTPClient>(maxPoolSize);
}
public void setConfig(FTPClientConfig config) {
Assert.notNull(config);
this.config = config;
@@ -101,22 +114,11 @@ public class QueuedFTPClientPool implements FTPClientPool {
this.factory = factory;
}
public QueuedFTPClientPool() {
this(DEFAULT_POOL_SIZE);
}
/**
* @param maxPoolSize the maximum size of the pool
*/
public QueuedFTPClientPool(int maxPoolSize) {
pool = new ArrayBlockingQueue<FTPClient>(maxPoolSize);
}
/**
* Returns an active FTPClient connected to the configured server. When no
* clients are available in the queue a new client is created with the
* factory.
*
* <p>
* It is possible that released clients are disconnected by the remote
* server (@see {@link FTPClient#sendNoOp()}. In this case getClient is
* called recursively to obtain a client that is still alive. For this
@@ -157,7 +159,7 @@ public class QueuedFTPClientPool implements FTPClientPool {
}
}
private class DefaultFactory implements FTPClientFactory {
private class DefaultFTPClientFactory implements FTPClientFactory {
public FTPClient getClient() throws SocketException, IOException {
FTPClient client = new FTPClient();
@@ -192,4 +194,5 @@ public class QueuedFTPClientPool implements FTPClientPool {
return client;
}
}
}

View File

@@ -25,7 +25,6 @@ import org.springframework.integration.config.AbstractPollingInboundChannelAdapt
import org.springframework.integration.config.IntegrationNamespaceUtils;
import org.springframework.integration.ftp.FtpSource;
import org.springframework.integration.ftp.QueuedFTPClientPool;
import org.springframework.util.StringUtils;
/**
* Parser for the &lt;inbound-channel-adapter/&gt; element of the 'ftp' namespace.
@@ -39,11 +38,6 @@ public class FtpInboundChannelAdapterParser extends AbstractPollingInboundChanne
@Override
protected String parseSource(Element element, ParserContext parserContext) {
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(FtpSource.class);
String messageCreatorReference = element.getAttribute("message-creator");
if (StringUtils.hasText(messageCreatorReference)) {
builder.addConstructorArgReference(messageCreatorReference);
}
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "local-working-directory");
String username = element.getAttribute("username");
String password = element.getAttribute("password");
String host = element.getAttribute("host");
@@ -56,6 +50,7 @@ public class FtpInboundChannelAdapterParser extends AbstractPollingInboundChanne
queuedFTPClientPool.setPort(Integer.parseInt(port));
queuedFTPClientPool.setRemoteWorkingDirectory(remoteWorkingDirectory);
builder.addConstructorArgValue(queuedFTPClientPool);
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "local-working-directory");
return BeanDefinitionReaderUtils.registerWithGeneratedName(
builder.getBeanDefinition(), parserContext.getRegistry());
}

View File

@@ -24,13 +24,13 @@
</xsd:documentation>
</xsd:annotation>
<xsd:attribute name="id" type="xsd:string"/>
<xsd:attribute name="channel" type="xsd:string"/>
<xsd:attribute name="username" type="xsd:string" use="optional"/>
<xsd:attribute name="password" type="xsd:string" use="optional"/>
<xsd:attribute name="host" type="xsd:string" use="required"/>
<xsd:attribute name="port" type="xsd:int" use="optional"/>
<xsd:attribute name="local-working-directory" type="xsd:string" use="required"/>
<xsd:attribute name="remote-working-directory" type="xsd:string" use="optional"/>
<xsd:attribute name="message-creator" type="xsd:string" use="optional"/>
</xsd:complexType>
</xsd:element>

View File

@@ -18,7 +18,6 @@ package org.springframework.integration.ftp;
import static org.easymock.EasyMock.eq;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.getCurrentArguments;
import static org.easymock.EasyMock.isA;
import static org.easymock.classextension.EasyMock.createMock;
import static org.easymock.classextension.EasyMock.createNiceMock;
@@ -41,17 +40,12 @@ import java.util.concurrent.CountDownLatch;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.oro.io.Perl5FilenameFilter;
import org.easymock.IAnswer;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.integration.ftp.FTPClientPool;
import org.springframework.integration.ftp.FtpSource;
import org.springframework.integration.message.GenericMessage;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.MessageCreator;
/**
* @author Iwein Fuld
@@ -59,28 +53,27 @@ import org.springframework.integration.message.MessageCreator;
@SuppressWarnings("unchecked")
public class FtpSourceTests {
private MessageCreator<List<File>, List<File>> messageCreator = createMock(MessageCreator.class);
private FTPClient ftpClient = createMock(FTPClient.class);
private FTPFile ftpFile = createMock(FTPFile.class);
private FTPClientPool ftpClientPool = createNiceMock(FTPClientPool.class);
@Before
public void liberalPool() throws Exception {
expect(ftpClientPool.getClient()).andReturn(ftpClient).anyTimes();
}
private Object[] globalMocks = new Object[] { messageCreator, ftpClient, ftpFile, ftpClientPool };
private Object[] globalMocks = new Object[] { ftpClient, ftpFile, ftpClientPool };
private FtpSource ftpSource;
private Long size = 100l;
@Before
public void liberalPool() throws Exception {
expect(ftpClientPool.getClient()).andReturn(ftpClient).anyTimes();
}
@Before
public void initializeFtpSource() {
ftpSource = new FtpSource(messageCreator, ftpClientPool);
ftpSource = new FtpSource(ftpClientPool);
}
@Before
@@ -88,14 +81,11 @@ public class FtpSourceTests {
reset(globalMocks);
}
@Test
public void retrieveSingleFile() throws Exception {
expect(ftpClient.listFiles()).andReturn(mockedFTPFilesNamed("test1"));
expect(ftpClient.retrieveFile(eq("test1"), isA(OutputStream.class))).andReturn(true);
// create message
expect(messageCreator.createMessage(isA(List.class))).andReturn(
new GenericMessage(Arrays.asList(new File("test1"))));
replay(globalMocks);
Message<List<File>> received = ftpSource.receive();
ftpSource.onSend(received);
@@ -122,12 +112,9 @@ public class FtpSourceTests {
public void retrieveMultipleFiles() throws Exception {
// get files
expect(ftpClient.listFiles()).andReturn(mockedFTPFilesNamed("test1", "test2")).times(2);
expect(ftpClient.retrieveFile(eq("test1"), isA(OutputStream.class))).andReturn(true);
expect(ftpClient.retrieveFile(eq("test2"), isA(OutputStream.class))).andReturn(true);
// create message
List<File> files = Arrays.asList(new File("test1"), new File("test2"));
expect(messageCreator.createMessage(isA(List.class))).andReturn(new GenericMessage(files));
replay(globalMocks);
Message receivedFiles = ftpSource.receive();
@@ -147,14 +134,11 @@ public class FtpSourceTests {
expect(ftpClient.retrieveFile(eq("test2"), isA(OutputStream.class))).andReturn(true);
// second run, change the date so the messages should be retrieved again
// expect(ftpClient.isConnected()).andReturn(true);
FTPFile[] mockedFTPFiles2 = mockedFTPFilesNamed("test1", "test2");
expect(ftpClient.listFiles()).andReturn(mockedFTPFiles2);
expect(ftpClient.retrieveFile(eq("test1"), isA(OutputStream.class))).andReturn(true);
expect(ftpClient.retrieveFile(eq("test2"), isA(OutputStream.class))).andReturn(true);
// create message
List<File> files = Arrays.asList(new File("test1"), new File("test2"));
expect(messageCreator.createMessage(isA(List.class))).andReturn(new GenericMessage(files)).times(2);
replay(globalMocks);
Message receivedFiles = ftpSource.receive();
@@ -180,13 +164,6 @@ public class FtpSourceTests {
// second run
expect(ftpClient.retrieveFile(eq("test3"), isA(OutputStream.class))).andReturn(true);
// create message
expect(messageCreator.createMessage(isA(List.class))).andAnswer(new IAnswer<Message<List<File>>>() {
public Message<List<File>> answer() throws Throwable {
return new GenericMessage(getCurrentArguments()[0]);
}
}).times(2);
replay(globalMocks);
Message<List<File>> receivedFiles1 = ftpSource.receive();
ftpSource.onSend(receivedFiles1);
@@ -219,12 +196,6 @@ public class FtpSourceTests {
expect(ftpClient.listFiles()).andReturn(mockedFTPFiles);
expect(ftpClient.retrieveFile(eq("test5"), isA(OutputStream.class))).andReturn(true);
// create message
expect(messageCreator.createMessage(isA(List.class))).andAnswer(new IAnswer<Message<List<File>>>() {
public Message<List<File>> answer() throws Throwable {
return new GenericMessage(getCurrentArguments()[0]);
}
}).times(3);
replay(globalMocks);
recorded.countDown();
@@ -263,13 +234,10 @@ public class FtpSourceTests {
public void onFailure() throws Exception {
expect(ftpClient.listFiles()).andReturn(mockedFTPFilesNamed("test1")).times(2);
expect(ftpClient.retrieveFile(eq("test1"), isA(OutputStream.class))).andReturn(true).times(2);
// create message
expect(messageCreator.createMessage(isA(List.class))).andReturn(
new GenericMessage(Arrays.asList(new File("test1")))).times(2);
replay(globalMocks);
Message<List<File>> received = ftpSource.receive();
ftpSource.onFailure(received, new Exception("just a test"));
assertEquals(received, ftpSource.receive());
assertEquals(received.getPayload(), ftpSource.receive().getPayload());
verify(globalMocks);
}

View File

@@ -1,34 +0,0 @@
/*
* 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.ftp.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

@@ -17,7 +17,6 @@
package org.springframework.integration.ftp.config;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.io.File;
@@ -26,7 +25,6 @@ import org.junit.Test;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.message.DefaultMessageCreator;
/**
* @author Mark Fisher
@@ -36,10 +34,10 @@ import org.springframework.integration.message.DefaultMessageCreator;
public class FtpInboundChannelAdapterParserTests {
@Test
public void ftpSourceWithDefaultMessageCreator() {
public void ftpInboundChannelAdapter() {
ApplicationContext context = new ClassPathXmlApplicationContext(
"ftpInboundChannelAdapterParserTests.xml", this.getClass());
Object adapter = context.getBean("default.adapter");
Object adapter = context.getBean("adapter");
DirectFieldAccessor sourceAccessor = new DirectFieldAccessor(
new DirectFieldAccessor(adapter).getPropertyValue("source"));
DirectFieldAccessor poolAccessor = new DirectFieldAccessor(
@@ -50,27 +48,6 @@ public class FtpInboundChannelAdapterParserTests {
assertEquals("/remote", poolAccessor.getPropertyValue("remoteWorkingDirectory"));
assertEquals("testUser", poolAccessor.getPropertyValue("username"));
assertEquals("testPassword", poolAccessor.getPropertyValue("password"));
Object messageCreator = sourceAccessor.getPropertyValue("messageCreator");
assertTrue(messageCreator instanceof DefaultMessageCreator);
}
@Test
public void ftpSourceWithCustomMessageCreator() {
ApplicationContext context = new ClassPathXmlApplicationContext(
"ftpInboundChannelAdapterParserTests.xml", this.getClass());
Object adapter = context.getBean("custom.adapter");
DirectFieldAccessor sourceAccessor = new DirectFieldAccessor(
new DirectFieldAccessor(adapter).getPropertyValue("source"));
DirectFieldAccessor poolAccessor = new DirectFieldAccessor(
sourceAccessor.getPropertyValue("clientPool"));
assertEquals("testHost", poolAccessor.getPropertyValue("host"));
assertEquals(2121, poolAccessor.getPropertyValue("port"));
assertEquals(new File("/local"), sourceAccessor.getPropertyValue("localWorkingDirectory"));
assertEquals("/remote", poolAccessor.getPropertyValue("remoteWorkingDirectory"));
assertEquals("testUser", poolAccessor.getPropertyValue("username"));
assertEquals("testPassword", poolAccessor.getPropertyValue("password"));
Object messageCreator = sourceAccessor.getPropertyValue("messageCreator");
assertTrue(messageCreator instanceof CustomMessageCreator);
}
}

View File

@@ -28,9 +28,7 @@ import org.junit.Test;
import org.springframework.integration.ftp.FtpSource;
import org.springframework.integration.ftp.QueuedFTPClientPool;
import org.springframework.integration.message.GenericMessage;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.MessageCreator;
/**
* @author Iwein Fuld
@@ -53,12 +51,6 @@ public class FtpSourceIntegrationTests {
private FtpSource ftpSource;
private MessageCreator<List<File>, List<File>> messageCreator = new MessageCreator<List<File>, List<File>>() {
public Message<List<File>> createMessage(List<File> object) {
return new GenericMessage<List<File>>(object);
}
};
@BeforeClass
public static void initializeEnvironment() {
@@ -69,7 +61,7 @@ public class FtpSourceIntegrationTests {
@Before
public void initializeFtpSource() throws Exception {
QueuedFTPClientPool queuedFTPClientPool = new QueuedFTPClientPool();
ftpSource = new FtpSource(messageCreator, queuedFTPClientPool);
ftpSource = new FtpSource(queuedFTPClientPool);
queuedFTPClientPool.setHost("localhost");
queuedFTPClientPool.setUsername("ftp-user");
queuedFTPClientPool.setPassword("kaas");

View File

@@ -2,29 +2,23 @@
<beans:beans xmlns="http://www.springframework.org/schema/integration/ftp"
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/ftp
http://www.springframework.org/schema/integration/ftp/spring-integration-ftp-1.0.xsd">
<inbound-channel-adapter id="default"
<integration:channel id="channel"/>
<inbound-channel-adapter id="adapter"
channel="channel"
host="testHost"
port="2121"
local-working-directory="/local"
remote-working-directory="/remote"
username="testUser"
password="testPassword"/>
<inbound-channel-adapter id="custom"
host="testHost"
port="2121"
local-working-directory="/local"
remote-working-directory="/remote"
username="testUser"
password="testPassword"
message-creator="customMessageCreator"/>
<beans:bean id="customMessageCreator"
class="org.springframework.integration.ftp.config.CustomMessageCreator"/>
</beans:beans>