INT-1614 refactoring as much as possible into base class

This commit is contained in:
Mark Fisher
2010-11-21 14:54:04 -05:00
parent 72fcb154b8
commit 25e20b73c1
4 changed files with 25 additions and 39 deletions

View File

@@ -47,10 +47,15 @@ public abstract class AbstractInboundFileSynchronizer<F> 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 <emphasis>remote</emphasis> file system view.
@@ -73,6 +78,10 @@ public abstract class AbstractInboundFileSynchronizer<F> implements InboundFileS
}
public void setRemotePath(String remotePath) {
this.remotePath = remotePath;
}
public void setFilter(FileListFilter<F> filter) {
this.filter = filter;
}
@@ -81,8 +90,8 @@ public abstract class AbstractInboundFileSynchronizer<F> 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<F> filterFiles(F[] files) {
@@ -94,7 +103,7 @@ public abstract class AbstractInboundFileSynchronizer<F> 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<F> implements InboundFileS
}
}
protected abstract void synchronizeToLocalDirectory(File localDirectory, Session session) throws IOException;
protected abstract void synchronizeToLocalDirectory(String remoteDirectoryPath, File localDirectory, Session session) throws IOException;
}

View File

@@ -41,9 +41,6 @@ import org.springframework.util.FileCopyUtils;
*/
public class FtpInboundFileSynchronizer extends AbstractInboundFileSynchronizer<FTPFile> {
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<FTPFile> files = session.ls(this.remotePath);
protected void synchronizeToLocalDirectory(String remoteDirectoryPath, File localDirectory, Session session) throws IOException {
Collection<FTPFile> files = session.ls(remoteDirectoryPath);
if (!CollectionUtils.isEmpty(files)) {
Collection<FTPFile> filteredFiles = this.filterFiles(files.toArray(new FTPFile[]{}));
for (FTPFile ftpFile : filteredFiles) {

View File

@@ -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<ChannelSftp.LsEntry> {
/**
* 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<ChannelSftp.LsEntry> beforeFilter = session.ls(remotePath);
protected void synchronizeToLocalDirectory(String remoteDirectoryPath, File localDirectory, Session session) throws IOException {
Collection<ChannelSftp.LsEntry> beforeFilter = session.ls(remoteDirectoryPath);
ChannelSftp.LsEntry[] entries = (beforeFilter == null) ? new ChannelSftp.LsEntry[0] :
beforeFilter.toArray(new ChannelSftp.LsEntry[beforeFilter.size()]);
Collection<ChannelSftp.LsEntry> 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()) {

View File

@@ -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);
}
/**