INT-1614 moved 'synchronizeToLocalDirectory' to the base class and changed Session.ls() to return an array rather than Collection
This commit is contained in:
@@ -17,7 +17,6 @@
|
||||
package org.springframework.integration.file.remote.session;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.util.Collection;
|
||||
import java.util.Queue;
|
||||
import java.util.concurrent.ArrayBlockingQueue;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
@@ -121,7 +120,7 @@ public class CachingSessionFactory implements SessionFactory, DisposableBean {
|
||||
return this.targetSession.rm(path);
|
||||
}
|
||||
|
||||
public <F> Collection<F> ls(String path) {
|
||||
public <F> F[] ls(String path) {
|
||||
return this.targetSession.ls(path);
|
||||
}
|
||||
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
package org.springframework.integration.file.remote.session;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.util.Collection;
|
||||
|
||||
/**
|
||||
* Common abstraction for a Session with a remote File system.
|
||||
@@ -31,7 +30,7 @@ public interface Session {
|
||||
|
||||
boolean rm(String path);
|
||||
|
||||
<F> Collection<F> ls(String path);
|
||||
<F> F[] ls(String path);
|
||||
|
||||
InputStream get(String source);
|
||||
|
||||
|
||||
@@ -19,6 +19,7 @@ package org.springframework.integration.file.remote.synchronizer;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
@@ -30,6 +31,7 @@ import org.springframework.integration.file.filters.FileListFilter;
|
||||
import org.springframework.integration.file.remote.session.Session;
|
||||
import org.springframework.integration.file.remote.session.SessionFactory;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
|
||||
/**
|
||||
* Base class charged with knowing how to connect to a remote file system,
|
||||
@@ -122,6 +124,18 @@ public abstract class AbstractInboundFileSynchronizer<F> implements InboundFileS
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract void synchronizeToLocalDirectory(String remoteDirectoryPath, File localDirectory, Session session) throws IOException;
|
||||
private void synchronizeToLocalDirectory(String remoteDirectoryPath, File localDirectory, Session session) throws IOException {
|
||||
F[] files = session.ls(remoteDirectoryPath);
|
||||
if (!ObjectUtils.isEmpty(files)) {
|
||||
Collection<F> filteredFiles = this.filterFiles(files);
|
||||
for (F file : filteredFiles) {
|
||||
if (file != null) {
|
||||
this.copyFileToLocalDirectory(remoteDirectoryPath, file, localDirectory, session);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract boolean copyFileToLocalDirectory(String remoteDirectoryPath, F file, File localDirectory, Session session) throws IOException;
|
||||
|
||||
}
|
||||
|
||||
@@ -20,7 +20,6 @@ import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Collection;
|
||||
|
||||
import org.apache.commons.net.ftp.FTPFile;
|
||||
|
||||
@@ -29,7 +28,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.CollectionUtils;
|
||||
import org.springframework.util.FileCopyUtils;
|
||||
|
||||
/**
|
||||
@@ -49,19 +47,7 @@ public class FtpInboundFileSynchronizer extends AbstractInboundFileSynchronizer<
|
||||
|
||||
|
||||
@Override
|
||||
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 file : filteredFiles) {
|
||||
if (file != null) {
|
||||
copyFileToLocalDirectory(remoteDirectoryPath, file, localDirectory, session);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private boolean copyFileToLocalDirectory(String remoteDirectoryPath, FTPFile ftpFile, File localDirectory, Session session) throws IOException {
|
||||
protected boolean copyFileToLocalDirectory(String remoteDirectoryPath, FTPFile ftpFile, File localDirectory, Session session) throws IOException {
|
||||
if (!ftpFile.isFile()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -19,9 +19,6 @@ package org.springframework.integration.ftp.session;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
@@ -63,20 +60,15 @@ public class FtpSession implements Session {
|
||||
}
|
||||
|
||||
@SuppressWarnings({"unchecked", "rawtypes"})
|
||||
public <F> Collection<F> ls(String path) {
|
||||
public FTPFile[] ls(String path) {
|
||||
try {
|
||||
FTPFile[] files = this.client.listFiles(path);
|
||||
ArrayList list = new ArrayList();
|
||||
for (FTPFile file : files) {
|
||||
list.add(file);
|
||||
}
|
||||
return list;
|
||||
return this.client.listFiles(path);
|
||||
}
|
||||
catch (IOException e) {
|
||||
if (logger.isWarnEnabled()) {
|
||||
logger.warn("failed to list files", e);
|
||||
}
|
||||
return Collections.EMPTY_LIST;
|
||||
return new FTPFile[0];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -20,24 +20,22 @@ import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Collection;
|
||||
|
||||
import org.springframework.integration.MessagingException;
|
||||
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.CollectionUtils;
|
||||
import org.springframework.util.FileCopyUtils;
|
||||
|
||||
import com.jcraft.jsch.ChannelSftp;
|
||||
import com.jcraft.jsch.ChannelSftp.LsEntry;
|
||||
|
||||
/**
|
||||
* Handles the synchronization between a remote SFTP directory and a local mount.
|
||||
*
|
||||
* @author Josh Long
|
||||
* @author Oleg Zhurakousky
|
||||
* @author Mark Fisher
|
||||
* @since 2.0
|
||||
*/
|
||||
public class SftpInboundFileSynchronizer extends AbstractInboundFileSynchronizer<ChannelSftp.LsEntry> {
|
||||
@@ -48,19 +46,7 @@ public class SftpInboundFileSynchronizer extends AbstractInboundFileSynchronizer
|
||||
|
||||
|
||||
@Override
|
||||
protected void synchronizeToLocalDirectory(String remoteDirectoryPath, File localDirectory, Session session) throws IOException {
|
||||
Collection<LsEntry> files = session.ls(remoteDirectoryPath);
|
||||
if (!CollectionUtils.isEmpty(files)) {
|
||||
Collection<LsEntry> filteredFiles = this.filterFiles(files.toArray(new LsEntry[]{}));
|
||||
for (LsEntry file : filteredFiles) {
|
||||
if (file != null) {
|
||||
copyFileToLocalDirectory(remoteDirectoryPath, file, localDirectory, session);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private boolean copyFileToLocalDirectory(String remoteDirectoryPath, ChannelSftp.LsEntry entry, File localDirectory, Session session) throws IOException {
|
||||
protected boolean copyFileToLocalDirectory(String remoteDirectoryPath, ChannelSftp.LsEntry entry, File localDirectory, Session session) throws IOException {
|
||||
if (entry == null || entry.getAttrs() == null || entry.getAttrs().isDir() || entry.getAttrs().isLink()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -17,8 +17,7 @@
|
||||
package org.springframework.integration.sftp.session;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Vector;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
@@ -28,6 +27,7 @@ import org.springframework.util.Assert;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import com.jcraft.jsch.ChannelSftp;
|
||||
import com.jcraft.jsch.ChannelSftp.LsEntry;
|
||||
import com.jcraft.jsch.JSch;
|
||||
import com.jcraft.jsch.JSchException;
|
||||
import com.jcraft.jsch.SftpException;
|
||||
@@ -155,17 +155,26 @@ public class SftpSession implements Session {
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public <F> Collection<F> ls(String path) {
|
||||
public LsEntry[] ls(String path) {
|
||||
Assert.state(channel != null, "session is not connected");
|
||||
try {
|
||||
return channel.ls(path);
|
||||
Vector<?> lsEntries = channel.ls(path);
|
||||
if (lsEntries != null) {
|
||||
LsEntry[] entries = new LsEntry[lsEntries.size()];
|
||||
for (int i = 0; i < lsEntries.size(); i++) {
|
||||
Object next = lsEntries.get(i);
|
||||
Assert.state(next instanceof LsEntry, "expected only LsEntry instances from channel.ls()");
|
||||
entries[i] = (LsEntry) next;
|
||||
}
|
||||
return entries;
|
||||
}
|
||||
}
|
||||
catch (SftpException e) {
|
||||
if (logger.isWarnEnabled()) {
|
||||
logger.warn("ls failed", e);
|
||||
}
|
||||
return Collections.EMPTY_LIST;
|
||||
}
|
||||
return new LsEntry[0];
|
||||
}
|
||||
|
||||
public InputStream get(String source) {
|
||||
|
||||
Reference in New Issue
Block a user