INT-1614 using File instead of Resource for localDirectory
This commit is contained in:
@@ -21,7 +21,6 @@ import java.io.FileNotFoundException;
|
||||
import java.util.Arrays;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.integration.Message;
|
||||
import org.springframework.integration.MessagingException;
|
||||
import org.springframework.integration.core.MessageSource;
|
||||
@@ -75,7 +74,7 @@ public abstract class AbstractInboundFileSynchronizingMessageSource<F> extends M
|
||||
/**
|
||||
* Directory to which things should be synched locally.
|
||||
*/
|
||||
protected volatile Resource localDirectory;
|
||||
protected volatile File localDirectory;
|
||||
|
||||
/**
|
||||
* The actual {@link FileReadingMessageSource} that monitors the local filesystem once files are synched.
|
||||
@@ -96,7 +95,7 @@ public abstract class AbstractInboundFileSynchronizingMessageSource<F> extends M
|
||||
this.synchronizer = synchronizer;
|
||||
}
|
||||
|
||||
public void setLocalDirectory(Resource localDirectory) {
|
||||
public void setLocalDirectory(File localDirectory) {
|
||||
this.localDirectory = localDirectory;
|
||||
}
|
||||
|
||||
@@ -115,10 +114,10 @@ public abstract class AbstractInboundFileSynchronizingMessageSource<F> extends M
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("The '" + this.localDirectory + "' directory doesn't exist; Will create.");
|
||||
}
|
||||
this.localDirectory.getFile().mkdirs();
|
||||
this.localDirectory.mkdirs();
|
||||
}
|
||||
else {
|
||||
throw new FileNotFoundException(this.localDirectory.getFilename());
|
||||
throw new FileNotFoundException(this.localDirectory.getName());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -127,7 +126,7 @@ public abstract class AbstractInboundFileSynchronizingMessageSource<F> extends M
|
||||
*/
|
||||
this.fileSource = new FileReadingMessageSource();
|
||||
this.fileSource.setFilter(this.buildFilter());
|
||||
this.fileSource.setDirectory(this.localDirectory.getFile());
|
||||
this.fileSource.setDirectory(this.localDirectory);
|
||||
this.fileSource.afterPropertiesSet();
|
||||
this.synchronizer.afterPropertiesSet();
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
|
||||
package org.springframework.integration.file.remote.synchronizer;
|
||||
|
||||
import org.springframework.core.io.Resource;
|
||||
import java.io.File;
|
||||
|
||||
/**
|
||||
* Strategy for synchronizing from a remote File system to a local directory.
|
||||
@@ -26,6 +26,6 @@ import org.springframework.core.io.Resource;
|
||||
*/
|
||||
public interface InboundFileSynchronizer {
|
||||
|
||||
void synchronizeToLocalDirectory(Resource localDirectory);
|
||||
void synchronizeToLocalDirectory(File localDirectory);
|
||||
|
||||
}
|
||||
|
||||
@@ -27,7 +27,6 @@ import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.apache.commons.net.ftp.FTPFile;
|
||||
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.integration.MessagingException;
|
||||
import org.springframework.integration.file.remote.session.Session;
|
||||
import org.springframework.integration.file.remote.session.SessionFactory;
|
||||
@@ -69,7 +68,7 @@ public class FtpInboundFileSynchronizer extends AbstractInboundFileSynchronizer<
|
||||
}
|
||||
}
|
||||
|
||||
public void synchronizeToLocalDirectory(Resource localDirectory) {
|
||||
public void synchronizeToLocalDirectory(File localDirectory) {
|
||||
try {
|
||||
Session session = this.sessionFactory.getSession();
|
||||
Assert.state(session != null, "failed to acquire an FTP Session");
|
||||
@@ -93,11 +92,11 @@ public class FtpInboundFileSynchronizer extends AbstractInboundFileSynchronizer<
|
||||
}
|
||||
}
|
||||
|
||||
private boolean copyFileToLocalDirectory(Session session, FTPFile ftpFile, Resource localDirectory)
|
||||
private boolean copyFileToLocalDirectory(Session session, FTPFile ftpFile, File localDirectory)
|
||||
throws IOException, FileNotFoundException {
|
||||
|
||||
String remoteFileName = ftpFile.getName();
|
||||
String localFileName = localDirectory.getFile().getPath() + "/" + remoteFileName;
|
||||
String localFileName = localDirectory.getPath() + "/" + remoteFileName;
|
||||
File localFile = new File(localFileName);
|
||||
if (!localFile.exists()) {
|
||||
String tempFileName = localFileName + AbstractInboundFileSynchronizingMessageSource.INCOMPLETE_EXTENSION;
|
||||
|
||||
@@ -23,7 +23,6 @@ import java.util.Collection;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.integration.MessagingException;
|
||||
import org.springframework.integration.file.remote.session.Session;
|
||||
import org.springframework.integration.file.remote.session.SessionFactory;
|
||||
@@ -70,7 +69,7 @@ public class SftpInboundFileSynchronizer extends AbstractInboundFileSynchronizer
|
||||
}
|
||||
}
|
||||
|
||||
public void synchronizeToLocalDirectory(Resource localDirectory) {
|
||||
public void synchronizeToLocalDirectory(File localDirectory) {
|
||||
Session session = null;
|
||||
try {
|
||||
session = this.sessionFactory.getSession();
|
||||
@@ -105,9 +104,8 @@ public class SftpInboundFileSynchronizer extends AbstractInboundFileSynchronizer
|
||||
}
|
||||
}
|
||||
|
||||
private boolean copyFromRemoteToLocalDirectory(Session session, ChannelSftp.LsEntry entry, Resource localDir) throws Exception {
|
||||
File fileForLocalDir = localDir.getFile();
|
||||
File localFile = new File(fileForLocalDir, entry.getFilename());
|
||||
private boolean copyFromRemoteToLocalDirectory(Session session, ChannelSftp.LsEntry entry, File localDirectory) throws Exception {
|
||||
File localFile = new File(localDirectory, entry.getFilename());
|
||||
if (!localFile.exists()) {
|
||||
InputStream in = null;
|
||||
FileOutputStream fileOutputStream = null;
|
||||
|
||||
@@ -54,15 +54,15 @@ public class SftpInboundFileSynchronizingMessageSource extends AbstractInboundFi
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("The '" + this.localDirectory + "' directory doesn't exist; Will create.");
|
||||
}
|
||||
this.localDirectory.getFile().mkdirs();
|
||||
this.localDirectory.mkdirs();
|
||||
}
|
||||
else {
|
||||
throw new FileNotFoundException(this.localDirectory.getFilename());
|
||||
throw new FileNotFoundException(this.localDirectory.getName());
|
||||
}
|
||||
}
|
||||
// Forwards files once they appear in the {@link #localDirectory}.
|
||||
this.fileSource = new FileReadingMessageSource();
|
||||
this.fileSource.setDirectory(this.localDirectory.getFile());
|
||||
this.fileSource.setDirectory(this.localDirectory);
|
||||
this.fileSource.afterPropertiesSet();
|
||||
if (this.filenamePattern != null) {
|
||||
SftpPatternMatchingFileListFilter filter = new SftpPatternMatchingFileListFilter(this.filenamePattern);
|
||||
|
||||
@@ -67,12 +67,12 @@ 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, Resource.class);
|
||||
ReflectionUtils.findMethod(synchronizer.getClass(), "copyFromRemoteToLocalDirectory", Session.class, LsEntry.class, File.class);
|
||||
method.setAccessible(true);
|
||||
Session session = mock(Session.class);
|
||||
LsEntry entry = mock(LsEntry.class);
|
||||
when(entry.getFilename()).thenReturn("foo.txt");
|
||||
Resource localDir = new FileSystemResource(new File("target"));
|
||||
File localDir = new File("target");
|
||||
boolean success = (Boolean) method.invoke(synchronizer, session, entry, localDir);
|
||||
assertTrue(success);
|
||||
}
|
||||
|
||||
@@ -33,8 +33,6 @@ 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;
|
||||
import org.springframework.integration.file.filters.FileListFilter;
|
||||
import org.springframework.integration.file.remote.session.Session;
|
||||
import org.springframework.integration.file.remote.session.SessionFactory;
|
||||
@@ -93,7 +91,7 @@ public class SftpInboundRemoteFileSystemSynchronizerTests {
|
||||
syncronizer.setShouldDeleteSourceFile(true);
|
||||
syncronizer.afterPropertiesSet();
|
||||
|
||||
Resource localDirectory = new FileSystemResource(System.getProperty("java.io.tmpdir"));
|
||||
File localDirectory = new File(System.getProperty("java.io.tmpdir"));
|
||||
syncronizer.synchronizeToLocalDirectory(localDirectory);
|
||||
|
||||
verify(sessionFactory, times(1)).getSession();
|
||||
|
||||
Reference in New Issue
Block a user