From 25e20b73c1be9cbdabe9c379206a4b4f73ac9d98 Mon Sep 17 00:00:00 2001 From: Mark Fisher Date: Sun, 21 Nov 2010 14:54:04 -0500 Subject: [PATCH] INT-1614 refactoring as much as possible into base class --- .../AbstractInboundFileSynchronizer.java | 19 ++++++++---- .../inbound/FtpInboundFileSynchronizer.java | 11 ++----- .../inbound/SftpInboundFileSynchronizer.java | 30 +++++-------------- ...oundRemoteFileSystemSynchronizerTests.java | 4 +-- 4 files changed, 25 insertions(+), 39 deletions(-) diff --git a/spring-integration-file/src/main/java/org/springframework/integration/file/remote/synchronizer/AbstractInboundFileSynchronizer.java b/spring-integration-file/src/main/java/org/springframework/integration/file/remote/synchronizer/AbstractInboundFileSynchronizer.java index 8e24d72d87..5e70994a50 100644 --- a/spring-integration-file/src/main/java/org/springframework/integration/file/remote/synchronizer/AbstractInboundFileSynchronizer.java +++ b/spring-integration-file/src/main/java/org/springframework/integration/file/remote/synchronizer/AbstractInboundFileSynchronizer.java @@ -47,10 +47,15 @@ public abstract class AbstractInboundFileSynchronizer implements InboundFileS protected final Log logger = LogFactory.getLog(this.getClass()); + /** + * the path on the remote mount + */ + private volatile String remotePath; + /** * the {@link SessionFactory} for acquiring remote file Sessions. */ - private volatile SessionFactory sessionFactory; + private final SessionFactory sessionFactory; /** * An {@link FileListFilter} that runs against the remote file system view. @@ -73,6 +78,10 @@ public abstract class AbstractInboundFileSynchronizer implements InboundFileS } + public void setRemotePath(String remotePath) { + this.remotePath = remotePath; + } + public void setFilter(FileListFilter filter) { this.filter = filter; } @@ -81,8 +90,8 @@ public abstract class AbstractInboundFileSynchronizer implements InboundFileS this.shouldDeleteSourceFile = shouldDeleteSourceFile; } - public void afterPropertiesSet() { - Assert.notNull(this.sessionFactory, "sessionFactory must not be null"); + public final void afterPropertiesSet() { + Assert.notNull(this.remotePath, "remotePath must not be null"); } protected final List filterFiles(F[] files) { @@ -94,7 +103,7 @@ public abstract class AbstractInboundFileSynchronizer implements InboundFileS try { session = this.sessionFactory.getSession(); Assert.state(session != null, "failed to acquire a Session"); - this.synchronizeToLocalDirectory(localDirectory, session); + this.synchronizeToLocalDirectory(this.remotePath, localDirectory, session); } catch (IOException e) { throw new MessagingException("Problem occurred while synchronizing remote to local directory", e); @@ -113,6 +122,6 @@ public abstract class AbstractInboundFileSynchronizer implements InboundFileS } } - protected abstract void synchronizeToLocalDirectory(File localDirectory, Session session) throws IOException; + protected abstract void synchronizeToLocalDirectory(String remoteDirectoryPath, File localDirectory, Session session) throws IOException; } diff --git a/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/inbound/FtpInboundFileSynchronizer.java b/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/inbound/FtpInboundFileSynchronizer.java index 335d260019..4abf62258f 100644 --- a/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/inbound/FtpInboundFileSynchronizer.java +++ b/spring-integration-ftp/src/main/java/org/springframework/integration/ftp/inbound/FtpInboundFileSynchronizer.java @@ -41,9 +41,6 @@ import org.springframework.util.FileCopyUtils; */ public class FtpInboundFileSynchronizer extends AbstractInboundFileSynchronizer { - private volatile String remotePath; - - /** * Create a synchronizer with the {@link SessionFactory} used to acquire {@link Session} instances. */ @@ -52,13 +49,9 @@ public class FtpInboundFileSynchronizer extends AbstractInboundFileSynchronizer< } - public void setRemotePath(String remotePath) { - this.remotePath = remotePath; - } - @Override - protected void synchronizeToLocalDirectory(File localDirectory, Session session) throws IOException { - Collection files = session.ls(this.remotePath); + protected void synchronizeToLocalDirectory(String remoteDirectoryPath, File localDirectory, Session session) throws IOException { + Collection files = session.ls(remoteDirectoryPath); if (!CollectionUtils.isEmpty(files)) { Collection filteredFiles = this.filterFiles(files.toArray(new FTPFile[]{})); for (FTPFile ftpFile : filteredFiles) { diff --git a/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/inbound/SftpInboundFileSynchronizer.java b/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/inbound/SftpInboundFileSynchronizer.java index c3ec29b4d5..8869109c4c 100644 --- a/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/inbound/SftpInboundFileSynchronizer.java +++ b/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/inbound/SftpInboundFileSynchronizer.java @@ -27,7 +27,6 @@ import org.springframework.integration.file.remote.session.Session; import org.springframework.integration.file.remote.session.SessionFactory; import org.springframework.integration.file.remote.synchronizer.AbstractInboundFileSynchronizer; import org.springframework.integration.file.remote.synchronizer.AbstractInboundFileSynchronizingMessageSource; -import org.springframework.util.Assert; import org.springframework.util.FileCopyUtils; import com.jcraft.jsch.ChannelSftp; @@ -41,40 +40,25 @@ import com.jcraft.jsch.ChannelSftp; */ public class SftpInboundFileSynchronizer extends AbstractInboundFileSynchronizer { - /** - * the path on the remote mount - */ - private volatile String remotePath; - - public SftpInboundFileSynchronizer(SessionFactory sessionFactory) { super(sessionFactory); } - public void setRemotePath(String remotePath) { - this.remotePath = remotePath; - } - - public void afterPropertiesSet() { - super.afterPropertiesSet(); - Assert.notNull(this.remotePath, "'remotePath' must not be null"); - } - @Override - protected void synchronizeToLocalDirectory(File localDirectory, Session session) throws IOException { - Collection beforeFilter = session.ls(remotePath); + protected void synchronizeToLocalDirectory(String remoteDirectoryPath, File localDirectory, Session session) throws IOException { + Collection beforeFilter = session.ls(remoteDirectoryPath); ChannelSftp.LsEntry[] entries = (beforeFilter == null) ? new ChannelSftp.LsEntry[0] : beforeFilter.toArray(new ChannelSftp.LsEntry[beforeFilter.size()]); Collection files = this.filterFiles(entries); for (ChannelSftp.LsEntry lsEntry : files) { if ((lsEntry != null) && !lsEntry.getAttrs().isDir() && !lsEntry.getAttrs().isLink()) { - copyFromRemoteToLocalDirectory(session, lsEntry, localDirectory); + copyFromRemoteToLocalDirectory(remoteDirectoryPath, lsEntry, localDirectory, session); } } } - private boolean copyFromRemoteToLocalDirectory(Session session, ChannelSftp.LsEntry entry, File localDirectory) throws IOException { + private boolean copyFromRemoteToLocalDirectory(String remoteDirectoryPath, ChannelSftp.LsEntry entry, File localDirectory, Session session) throws IOException { File localFile = new File(localDirectory, entry.getFilename()); if (!localFile.exists()) { InputStream in = null; @@ -83,7 +67,7 @@ public class SftpInboundFileSynchronizer extends AbstractInboundFileSynchronizer File tmpLocalTarget = new File(localFile.getAbsolutePath() + AbstractInboundFileSynchronizingMessageSource.INCOMPLETE_EXTENSION); fileOutputStream = new FileOutputStream(tmpLocalTarget); - String remoteFqPath = this.remotePath + "/" + entry.getFilename(); + String remoteFqPath = remoteDirectoryPath + File.separator + entry.getFilename(); in = session.get(remoteFqPath); try { FileCopyUtils.copy(in, fileOutputStream); @@ -94,7 +78,7 @@ public class SftpInboundFileSynchronizer extends AbstractInboundFileSynchronizer } if (tmpLocalTarget.renameTo(localFile)) { if (this.shouldDeleteSourceFile) { - this.deleteRemoteFile(session, entry); + this.deleteRemoteFile(remoteDirectoryPath, session, entry); } } return true; @@ -113,7 +97,7 @@ public class SftpInboundFileSynchronizer extends AbstractInboundFileSynchronizer } } - private void deleteRemoteFile(Session session, ChannelSftp.LsEntry msg) { + private void deleteRemoteFile(String remotePath, Session session, ChannelSftp.LsEntry msg) { String remoteFqPath = remotePath + "/" + msg.getFilename(); session.rm(remoteFqPath); if (logger.isDebugEnabled()) { 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 87faec5356..22df941459 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 @@ -66,13 +66,13 @@ public class SftpInboundRemoteFileSystemSynchronizerTests { public void testCopyAndRenameWhenLocalFileExists() throws Exception { SftpInboundFileSynchronizer synchronizer = new SftpInboundFileSynchronizer(mock(SessionFactory.class)); Method method = - ReflectionUtils.findMethod(synchronizer.getClass(), "copyFromRemoteToLocalDirectory", Session.class, LsEntry.class, File.class); + ReflectionUtils.findMethod(synchronizer.getClass(), "copyFromRemoteToLocalDirectory", String.class, LsEntry.class, File.class, Session.class); method.setAccessible(true); Session session = mock(Session.class); LsEntry entry = mock(LsEntry.class); when(entry.getFilename()).thenReturn("foo.txt"); File localDir = new File("target"); - boolean success = (Boolean) method.invoke(synchronizer, session, entry, localDir); + boolean success = (Boolean) method.invoke(synchronizer, "remoteDir", entry, localDir, session); assertTrue(success); } /**