use of org.springframework.integration.file.filters.ResettableFileListFilter in project spring-integration by spring-projects.
the class AbstractInboundFileSynchronizer method copyFileToLocalDirectory.
protected boolean copyFileToLocalDirectory(String remoteDirectoryPath, F remoteFile, File localDirectory, Session<F> session) throws IOException {
String remoteFileName = this.getFilename(remoteFile);
String localFileName = this.generateLocalFileName(remoteFileName);
String remoteFilePath = remoteDirectoryPath != null ? (remoteDirectoryPath + this.remoteFileSeparator + remoteFileName) : remoteFileName;
if (!this.isFile(remoteFile)) {
if (this.logger.isDebugEnabled()) {
this.logger.debug("cannot copy, not a file: " + remoteFilePath);
}
return false;
}
long modified = getModified(remoteFile);
File localFile = new File(localDirectory, localFileName);
boolean exists = localFile.exists();
if (!exists || (this.preserveTimestamp && modified != localFile.lastModified())) {
if (!exists && localFileName.replaceAll("/", Matcher.quoteReplacement(File.separator)).contains(File.separator)) {
// NOSONAR - will fail on the writing below
localFile.getParentFile().mkdirs();
}
boolean transfer = true;
if (exists && !localFile.delete()) {
transfer = false;
if (this.logger.isInfoEnabled()) {
this.logger.info("Cannot delete local file '" + localFile + "' in order to transfer modified remote file '" + remoteFile + "'. " + "The local file may be busy in some other process.");
}
}
boolean renamed = false;
if (transfer) {
String tempFileName = localFile.getAbsolutePath() + this.temporaryFileSuffix;
File tempFile = new File(tempFileName);
OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(tempFile));
try {
session.read(remoteFilePath, outputStream);
} catch (Exception e) {
if (e instanceof RuntimeException) {
throw (RuntimeException) e;
} else {
throw new MessagingException("Failure occurred while copying '" + remoteFilePath + "' from the remote to the local directory", e);
}
} finally {
try {
outputStream.close();
} catch (Exception ignored2) {
}
}
renamed = tempFile.renameTo(localFile);
if (!renamed) {
if (localFile.delete()) {
renamed = tempFile.renameTo(localFile);
if (!renamed && this.logger.isInfoEnabled()) {
this.logger.info("Cannot rename '" + tempFileName + "' to local file '" + localFile + "' after deleting. " + "The local file may be busy in some other process.");
}
} else if (this.logger.isInfoEnabled()) {
this.logger.info("Cannot delete local file '" + localFile + "'. The local file may be busy in some other process.");
}
}
}
if (renamed) {
if (this.deleteRemoteFiles) {
session.remove(remoteFilePath);
if (this.logger.isDebugEnabled()) {
this.logger.debug("deleted remote file: " + remoteFilePath);
}
}
if (this.preserveTimestamp) {
localFile.setLastModified(modified);
}
return true;
} else if (this.filter instanceof ResettableFileListFilter) {
if (this.logger.isInfoEnabled()) {
this.logger.info("Reverting the remote file '" + remoteFile + "' from the filter for a subsequent transfer attempt");
}
((ResettableFileListFilter<F>) this.filter).remove(remoteFile);
}
} else if (this.logger.isWarnEnabled()) {
this.logger.warn("The remote file '" + remoteFile + "' has not been transferred " + "to the existing local file '" + localFile + "'. Consider removing the local file.");
}
return false;
}
use of org.springframework.integration.file.filters.ResettableFileListFilter in project spring-integration by spring-projects.
the class FileInboundTransactionTests method testNoTx.
@Test
public void testNoTx() throws Exception {
Object scanner = TestUtils.getPropertyValue(pseudoTx.getMessageSource(), "scanner");
assertThat(scanner.getClass().getName(), containsString("FileReadingMessageSource$WatchServiceDirectoryScanner"));
@SuppressWarnings("unchecked") ResettableFileListFilter<File> fileListFilter = spy(TestUtils.getPropertyValue(scanner, "filter", ResettableFileListFilter.class));
new DirectFieldAccessor(scanner).setPropertyValue("filter", fileListFilter);
final CountDownLatch latch = new CountDownLatch(1);
final AtomicBoolean crash = new AtomicBoolean();
input.subscribe(message -> {
if (crash.get()) {
throw new MessagingException("eek");
}
latch.countDown();
});
pseudoTx.start();
File file = new File(tmpDir.getRoot(), "si-test1/foo");
file.createNewFile();
Message<?> result = successChannel.receive(60000);
assertNotNull(result);
assertEquals(Boolean.TRUE, result.getPayload());
assertFalse(file.delete());
crash.set(true);
file = new File(tmpDir.getRoot(), "si-test1/bar");
file.createNewFile();
result = failureChannel.receive(60000);
assertNotNull(result);
assertTrue(file.delete());
assertEquals("foo", result.getPayload());
pseudoTx.stop();
assertFalse(transactionManager.getCommitted());
assertFalse(transactionManager.getRolledBack());
verify(fileListFilter).remove(new File(tmpDir.getRoot(), "si-test1/foo"));
}
Aggregations