From d9ebf2657eb5ab8ea3ca28c10fef074a60f8c7b4 Mon Sep 17 00:00:00 2001 From: Mark Fisher Date: Fri, 19 Nov 2010 12:01:39 -0500 Subject: [PATCH] INT-1614 naming changes for synchronizers and the interface's strategy method itself --- ...a => AbstractInboundFileSynchronizer.java} | 8 +---- ...nboundFileSynchronizingMessageSource.java} | 11 +++---- .../synchronizer/InboundFileSynchronizer.java | 31 +++++++++++++++++++ ...tpInboundRemoteFileSystemSynchronizer.java | 15 +++++---- ...eFileSystemSynchronizingMessageSource.java | 15 ++++++--- ...boundRemoteFileSystemSynchronizerTest.java | 3 +- .../sftp/inbound/SftpInboundSynchronizer.java | 13 ++++---- ...SftpInboundSynchronizingMessageSource.java | 9 +++--- ...oundRemoteFileSystemSynchronizerTests.java | 2 +- ...oundRemoteFileSystemSynchronizerTests.java | 20 +++++++++--- 10 files changed, 85 insertions(+), 42 deletions(-) rename spring-integration-file/src/main/java/org/springframework/integration/file/synchronizer/{AbstractInboundRemoteFileSystemSychronizer.java => AbstractInboundFileSynchronizer.java} (94%) rename spring-integration-file/src/main/java/org/springframework/integration/file/synchronizer/{AbstractInboundRemoteFileSystemSynchronizingMessageSource.java => AbstractInboundFileSynchronizingMessageSource.java} (93%) create mode 100644 spring-integration-file/src/main/java/org/springframework/integration/file/synchronizer/InboundFileSynchronizer.java diff --git a/spring-integration-file/src/main/java/org/springframework/integration/file/synchronizer/AbstractInboundRemoteFileSystemSychronizer.java b/spring-integration-file/src/main/java/org/springframework/integration/file/synchronizer/AbstractInboundFileSynchronizer.java similarity index 94% rename from spring-integration-file/src/main/java/org/springframework/integration/file/synchronizer/AbstractInboundRemoteFileSystemSychronizer.java rename to spring-integration-file/src/main/java/org/springframework/integration/file/synchronizer/AbstractInboundFileSynchronizer.java index 71ccff9e69..fded0f1157 100644 --- a/spring-integration-file/src/main/java/org/springframework/integration/file/synchronizer/AbstractInboundRemoteFileSystemSychronizer.java +++ b/spring-integration-file/src/main/java/org/springframework/integration/file/synchronizer/AbstractInboundFileSynchronizer.java @@ -23,7 +23,6 @@ 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.file.filters.FileListFilter; /** @@ -36,7 +35,7 @@ import org.springframework.integration.file.filters.FileListFilter; * * @author Josh Long */ -public abstract class AbstractInboundRemoteFileSystemSychronizer implements InitializingBean { +public abstract class AbstractInboundFileSynchronizer implements InboundFileSynchronizer, InitializingBean { protected final Log logger = LogFactory.getLog(this.getClass()); @@ -94,11 +93,6 @@ public abstract class AbstractInboundRemoteFileSystemSychronizer implements I return (this.filter != null) ? this.filter.filterFiles(files) : Arrays.asList(files); } - /** - * This is the callback where the subclasses must synchronize. - */ - protected abstract void syncRemoteToLocalFileSystem(Resource localDirectory); - /** * Strategy interface to expose a hook for dispatching, moving, or deleting diff --git a/spring-integration-file/src/main/java/org/springframework/integration/file/synchronizer/AbstractInboundRemoteFileSystemSynchronizingMessageSource.java b/spring-integration-file/src/main/java/org/springframework/integration/file/synchronizer/AbstractInboundFileSynchronizingMessageSource.java similarity index 93% rename from spring-integration-file/src/main/java/org/springframework/integration/file/synchronizer/AbstractInboundRemoteFileSystemSynchronizingMessageSource.java rename to spring-integration-file/src/main/java/org/springframework/integration/file/synchronizer/AbstractInboundFileSynchronizingMessageSource.java index 1005a13e3e..424054b4d6 100644 --- a/spring-integration-file/src/main/java/org/springframework/integration/file/synchronizer/AbstractInboundRemoteFileSystemSynchronizingMessageSource.java +++ b/spring-integration-file/src/main/java/org/springframework/integration/file/synchronizer/AbstractInboundFileSynchronizingMessageSource.java @@ -47,14 +47,13 @@ import org.springframework.util.Assert; * specific). *

* This class is to be used as a pair with an implementation of - * {@link AbstractInboundRemoteFileSystemSychronizer}. The synchronizer must + * {@link AbstractInboundFileSynchronizer}. The synchronizer must * handle the work of actually connecting to the remote file system and * delivering new {@link File}s. * * @author Josh Long */ -public abstract class AbstractInboundRemoteFileSystemSynchronizingMessageSource> - extends MessageProducerSupport implements MessageSource { +public abstract class AbstractInboundFileSynchronizingMessageSource extends MessageProducerSupport implements MessageSource { /** * Extension used when downloading files. We change it right after we know it's downloaded. @@ -71,7 +70,7 @@ public abstract class AbstractInboundRemoteFileSystemSynchronizingMessageSource< * An implementation that will handle the chores of actually connecting to and synching up * the remote file system with the local one, in an inbound direction. */ - protected volatile S synchronizer; + protected volatile AbstractInboundFileSynchronizer synchronizer; /** * Directory to which things should be synched locally. @@ -93,7 +92,7 @@ public abstract class AbstractInboundRemoteFileSystemSynchronizingMessageSource< this.autoCreateDirectories = autoCreateDirectories; } - public void setSynchronizer(S synchronizer) { + public void setSynchronizer(AbstractInboundFileSynchronizer synchronizer) { this.synchronizer = synchronizer; } @@ -151,7 +150,7 @@ public abstract class AbstractInboundRemoteFileSystemSynchronizingMessageSource< Assert.state(this.synchronizer != null, "synchronizer must not be null"); Message message = this.fileSource.receive(); if (message == null) { - this.synchronizer.syncRemoteToLocalFileSystem(this.localDirectory); + this.synchronizer.synchronizeToLocalDirectory(this.localDirectory); message = this.fileSource.receive(); } return message; diff --git a/spring-integration-file/src/main/java/org/springframework/integration/file/synchronizer/InboundFileSynchronizer.java b/spring-integration-file/src/main/java/org/springframework/integration/file/synchronizer/InboundFileSynchronizer.java new file mode 100644 index 0000000000..7f87881767 --- /dev/null +++ b/spring-integration-file/src/main/java/org/springframework/integration/file/synchronizer/InboundFileSynchronizer.java @@ -0,0 +1,31 @@ +/* + * Copyright 2002-2010 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.synchronizer; + +import org.springframework.core.io.Resource; + +/** + * Strategy for synchronizing from a remote File system to a local directory. + * + * @author Mark Fisher + * @since 2.0 + */ +public interface InboundFileSynchronizer { + + void synchronizeToLocalDirectory(Resource localDirectory); + +} diff --git a/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/inbound/FtpInboundRemoteFileSystemSynchronizer.java b/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/inbound/FtpInboundRemoteFileSystemSynchronizer.java index 4f56d29d65..169da302ec 100644 --- a/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/inbound/FtpInboundRemoteFileSystemSynchronizer.java +++ b/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/inbound/FtpInboundRemoteFileSystemSynchronizer.java @@ -29,8 +29,8 @@ import org.apache.commons.net.ftp.FTPFile; import org.springframework.core.io.Resource; import org.springframework.integration.MessagingException; -import org.springframework.integration.file.synchronizer.AbstractInboundRemoteFileSystemSychronizer; -import org.springframework.integration.file.synchronizer.AbstractInboundRemoteFileSystemSynchronizingMessageSource; +import org.springframework.integration.file.synchronizer.AbstractInboundFileSynchronizer; +import org.springframework.integration.file.synchronizer.AbstractInboundFileSynchronizingMessageSource; import org.springframework.integration.ftp.client.FtpClientPool; import org.springframework.util.Assert; @@ -40,7 +40,7 @@ import org.springframework.util.Assert; * @author Iwein Fuld * @author Josh Long */ -public class FtpInboundRemoteFileSystemSynchronizer extends AbstractInboundRemoteFileSystemSychronizer { +public class FtpInboundRemoteFileSystemSynchronizer extends AbstractInboundFileSynchronizer { private volatile FtpClientPool clientPool; @@ -54,15 +54,14 @@ public class FtpInboundRemoteFileSystemSynchronizer extends AbstractInboundRemot this.clientPool = clientPool; } - public void afterPropertiesSet() throws Exception { + public void afterPropertiesSet() { Assert.notNull(this.clientPool, "clientPool must not be null"); if (this.shouldDeleteSourceFile) { this.setEntryAcknowledgmentStrategy(new DeletionEntryAcknowledgmentStrategy()); } } - @Override - protected void syncRemoteToLocalFileSystem(Resource localDirectory) { + public void synchronizeToLocalDirectory(Resource localDirectory) { try { FTPClient client = this.clientPool.getClient(); Assert.state(client != null, @@ -93,7 +92,7 @@ public class FtpInboundRemoteFileSystemSynchronizer extends AbstractInboundRemot String localFileName = localDirectory.getFile().getPath() + "/" + remoteFileName; File localFile = new File(localFileName); if (!localFile.exists()) { - String tempFileName = localFileName + AbstractInboundRemoteFileSystemSynchronizingMessageSource.INCOMPLETE_EXTENSION; + String tempFileName = localFileName + AbstractInboundFileSynchronizingMessageSource.INCOMPLETE_EXTENSION; File file = new File(tempFileName); FileOutputStream fos = new FileOutputStream(file); try { @@ -122,7 +121,7 @@ public class FtpInboundRemoteFileSystemSynchronizer extends AbstractInboundRemot /** * An acknowledgment strategy that deletes the file. */ - private static class DeletionEntryAcknowledgmentStrategy implements AbstractInboundRemoteFileSystemSychronizer.EntryAcknowledgmentStrategy { + private static class DeletionEntryAcknowledgmentStrategy implements EntryAcknowledgmentStrategy { private final Log logger = LogFactory.getLog(this.getClass()); diff --git a/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/inbound/FtpInboundRemoteFileSystemSynchronizingMessageSource.java b/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/inbound/FtpInboundRemoteFileSystemSynchronizingMessageSource.java index aee995461e..199a07fab8 100644 --- a/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/inbound/FtpInboundRemoteFileSystemSynchronizingMessageSource.java +++ b/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/inbound/FtpInboundRemoteFileSystemSynchronizingMessageSource.java @@ -18,7 +18,8 @@ package org.springframework.integration.ftp.inbound; import org.apache.commons.net.ftp.FTPFile; -import org.springframework.integration.file.synchronizer.AbstractInboundRemoteFileSystemSynchronizingMessageSource; +import org.springframework.integration.file.synchronizer.AbstractInboundFileSynchronizer; +import org.springframework.integration.file.synchronizer.AbstractInboundFileSynchronizingMessageSource; import org.springframework.integration.ftp.client.FtpClientPool; /** @@ -27,8 +28,7 @@ import org.springframework.integration.ftp.client.FtpClientPool; * @author Iwein Fuld * @author Josh Long */ -public class FtpInboundRemoteFileSystemSynchronizingMessageSource - extends AbstractInboundRemoteFileSystemSynchronizingMessageSource { +public class FtpInboundRemoteFileSystemSynchronizingMessageSource extends AbstractInboundFileSynchronizingMessageSource { private volatile FtpClientPool clientPool; @@ -41,10 +41,17 @@ public class FtpInboundRemoteFileSystemSynchronizingMessageSource return "ftp:inbound-channel-adapter"; } + @Override + public void setSynchronizer(AbstractInboundFileSynchronizer synchronizer) { + super.setSynchronizer(synchronizer); + } + @Override protected void onInit() { super.onInit(); - this.synchronizer.setClientPool(this.clientPool); + if (this.synchronizer instanceof FtpInboundRemoteFileSystemSynchronizer) { + ((FtpInboundRemoteFileSystemSynchronizer) this.synchronizer).setClientPool(this.clientPool); + } } } diff --git a/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/inbound/FtpInboundRemoteFileSystemSynchronizerTest.java b/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/inbound/FtpInboundRemoteFileSystemSynchronizerTest.java index a30b802147..57ab7bfd58 100644 --- a/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/inbound/FtpInboundRemoteFileSystemSynchronizerTest.java +++ b/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/inbound/FtpInboundRemoteFileSystemSynchronizerTest.java @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.springframework.integration.ftp.inbound; import static org.mockito.Mockito.mock; @@ -71,7 +72,7 @@ public class FtpInboundRemoteFileSystemSynchronizerTest { syncronizer.afterPropertiesSet(); Resource localDirectory = new FileSystemResource(System.getProperty("java.io.tmpdir")); - syncronizer.syncRemoteToLocalFileSystem(localDirectory); + syncronizer.synchronizeToLocalDirectory(localDirectory); verify(ftpClient, times(1)).retrieveFile(Mockito.anyString(), Mockito.any(OutputStream.class)); verify(ftpClient, times(1)).deleteFile(Mockito.anyString()); diff --git a/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/inbound/SftpInboundSynchronizer.java b/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/inbound/SftpInboundSynchronizer.java index 79647b0103..1f4dc83a8f 100644 --- a/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/inbound/SftpInboundSynchronizer.java +++ b/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/inbound/SftpInboundSynchronizer.java @@ -25,8 +25,8 @@ import org.apache.commons.io.IOUtils; import org.springframework.core.io.Resource; import org.springframework.integration.MessagingException; -import org.springframework.integration.file.synchronizer.AbstractInboundRemoteFileSystemSychronizer; -import org.springframework.integration.file.synchronizer.AbstractInboundRemoteFileSystemSynchronizingMessageSource; +import org.springframework.integration.file.synchronizer.AbstractInboundFileSynchronizer; +import org.springframework.integration.file.synchronizer.AbstractInboundFileSynchronizingMessageSource; import org.springframework.integration.sftp.session.SftpSession; import org.springframework.integration.sftp.session.SftpSessionFactory; import org.springframework.util.Assert; @@ -40,7 +40,7 @@ import com.jcraft.jsch.ChannelSftp; * @author Oleg Zhurakousky * @since 2.0 */ -public class SftpInboundSynchronizer extends AbstractInboundRemoteFileSystemSychronizer { +public class SftpInboundSynchronizer extends AbstractInboundFileSynchronizer { /** * the path on the remote mount @@ -107,9 +107,8 @@ public class SftpInboundSynchronizer extends AbstractInboundRemoteFileSystemSych return false; } - @Override @SuppressWarnings("unchecked") - protected void syncRemoteToLocalFileSystem(Resource localDirectory) { + public void synchronizeToLocalDirectory(Resource localDirectory) { SftpSession session = null; try { session = this.sessionFactory.getSession(); @@ -147,7 +146,7 @@ public class SftpInboundSynchronizer extends AbstractInboundRemoteFileSystemSych FileOutputStream fileOutputStream = null; try { File tmpLocalTarget = new File(localFile.getAbsolutePath() + - AbstractInboundRemoteFileSystemSynchronizingMessageSource.INCOMPLETE_EXTENSION); + AbstractInboundFileSynchronizingMessageSource.INCOMPLETE_EXTENSION); fileOutputStream = new FileOutputStream(tmpLocalTarget); String remoteFqPath = this.remotePath + "/" + entry.getFilename(); in = sftpSession.get(remoteFqPath); @@ -178,7 +177,7 @@ public class SftpInboundSynchronizer extends AbstractInboundRemoteFileSystemSych } - private class DeletionEntryAcknowledgmentStrategy implements AbstractInboundRemoteFileSystemSychronizer.EntryAcknowledgmentStrategy { + private class DeletionEntryAcknowledgmentStrategy implements AbstractInboundFileSynchronizer.EntryAcknowledgmentStrategy { public void acknowledge(Object useful, ChannelSftp.LsEntry msg) throws Exception { SftpSession sftpSession = (SftpSession) useful; diff --git a/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/inbound/SftpInboundSynchronizingMessageSource.java b/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/inbound/SftpInboundSynchronizingMessageSource.java index bbaadf7510..14e16867cd 100644 --- a/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/inbound/SftpInboundSynchronizingMessageSource.java +++ b/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/inbound/SftpInboundSynchronizingMessageSource.java @@ -21,7 +21,7 @@ import java.util.regex.Pattern; import org.springframework.integration.MessagingException; import org.springframework.integration.file.FileReadingMessageSource; -import org.springframework.integration.file.synchronizer.AbstractInboundRemoteFileSystemSynchronizingMessageSource; +import org.springframework.integration.file.synchronizer.AbstractInboundFileSynchronizingMessageSource; import org.springframework.integration.sftp.filters.SftpPatternMatchingFileListFilter; import com.jcraft.jsch.ChannelSftp; @@ -33,8 +33,7 @@ import com.jcraft.jsch.ChannelSftp; * @author Oleg Zhurakousky * @since 2.0 */ -public class SftpInboundSynchronizingMessageSource - extends AbstractInboundRemoteFileSystemSynchronizingMessageSource { +public class SftpInboundSynchronizingMessageSource extends AbstractInboundFileSynchronizingMessageSource { private volatile Pattern filenamePattern; @@ -68,7 +67,9 @@ public class SftpInboundSynchronizingMessageSource if (this.filenamePattern != null) { SftpPatternMatchingFileListFilter filter = new SftpPatternMatchingFileListFilter(this.filenamePattern); this.synchronizer.setFilter(filter); - this.synchronizer.setAutoCreateDirectories(this.autoCreateDirectories); + if (this.synchronizer instanceof SftpInboundSynchronizer) { + ((SftpInboundSynchronizer) this.synchronizer).setAutoCreateDirectories(this.autoCreateDirectories); + } } } catch (RuntimeException e) { diff --git a/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/impl/SftpInboundRemoteFileSystemSynchronizerTests.java b/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/impl/SftpInboundRemoteFileSystemSynchronizerTests.java index a7c46893c8..376326d06c 100644 --- a/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/impl/SftpInboundRemoteFileSystemSynchronizerTests.java +++ b/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/impl/SftpInboundRemoteFileSystemSynchronizerTests.java @@ -27,7 +27,7 @@ import org.junit.Test; import org.springframework.core.io.FileSystemResource; import org.springframework.core.io.Resource; -import org.springframework.integration.file.synchronizer.AbstractInboundRemoteFileSystemSychronizer.EntryAcknowledgmentStrategy; +import org.springframework.integration.file.synchronizer.AbstractInboundFileSynchronizer.EntryAcknowledgmentStrategy; import org.springframework.integration.sftp.inbound.SftpInboundSynchronizer; import org.springframework.integration.sftp.session.SftpSession; import org.springframework.integration.sftp.session.SftpSessionFactory; diff --git a/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/inbound/SftpInboundRemoteFileSystemSynchronizerTests.java b/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/inbound/SftpInboundRemoteFileSystemSynchronizerTests.java index 1c8b6ae7ff..43bd805eaf 100644 --- a/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/inbound/SftpInboundRemoteFileSystemSynchronizerTests.java +++ b/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/inbound/SftpInboundRemoteFileSystemSynchronizerTests.java @@ -16,6 +16,7 @@ package org.springframework.integration.sftp.inbound; +import static org.mockito.Mockito.atLeast; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; @@ -23,11 +24,13 @@ import static org.mockito.Mockito.when; import java.io.File; import java.io.FileInputStream; +import java.io.InputStream; import java.util.Vector; -import org.junit.Ignore; import org.junit.Test; import org.mockito.Mockito; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; import org.springframework.core.io.FileSystemResource; import org.springframework.core.io.Resource; @@ -47,7 +50,6 @@ public class SftpInboundRemoteFileSystemSynchronizerTests { @SuppressWarnings({ "rawtypes", "unchecked" }) @Test - @Ignore public void testCopyFileToLocalDir() throws Exception { File file = new File(System.getProperty("java.io.tmpdir") + "/foo.txt"); if (file.exists()){ @@ -65,7 +67,7 @@ public class SftpInboundRemoteFileSystemSynchronizerTests { SftpSession sftpSession = mock(SftpSession.class); when(sessionFactory.getSession()).thenReturn(sftpSession); - ChannelSftp channel = mock(ChannelSftp.class); + final ChannelSftp channel = mock(ChannelSftp.class); when(channel.get((String) Mockito.any())).thenReturn(new FileInputStream(new File("template.mf"))); Vector entries = new Vector(); LsEntry entry = mock(LsEntry.class); @@ -77,14 +79,24 @@ public class SftpInboundRemoteFileSystemSynchronizerTests { entries.add(entry); when(channel.ls("foo/bar")).thenReturn(entries); when(filter.filterFiles((Object[]) Mockito.any())).thenReturn(entries); + + when(sftpSession.get(Mockito.anyString())).thenAnswer(new Answer() { + public InputStream answer(InvocationOnMock invocation) + throws Throwable { + String filePath = (String) invocation.getArguments()[0]; + return channel.get(filePath); + } + }); syncronizer.setShouldDeleteSourceFile(true); syncronizer.afterPropertiesSet(); Resource localDirectory = new FileSystemResource(System.getProperty("java.io.tmpdir")); - syncronizer.syncRemoteToLocalFileSystem(localDirectory); + syncronizer.synchronizeToLocalDirectory(localDirectory); verify(sessionFactory, times(1)).getSession(); + verify(attr, atLeast(1)).isDir(); // will add more validation, but for now this test is mainly to get the test coverage up } + }