Search in sources :

Example 1 with ResettableFileListFilter

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;
}
Also used : MessagingException(org.springframework.messaging.MessagingException) ResettableFileListFilter(org.springframework.integration.file.filters.ResettableFileListFilter) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) MessagingException(org.springframework.messaging.MessagingException) IOException(java.io.IOException) BeansException(org.springframework.beans.BeansException)

Example 2 with ResettableFileListFilter

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"));
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ResettableFileListFilter(org.springframework.integration.file.filters.ResettableFileListFilter) MessagingException(org.springframework.messaging.MessagingException) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) CountDownLatch(java.util.concurrent.CountDownLatch) File(java.io.File) Test(org.junit.Test)

Aggregations

File (java.io.File)2 ResettableFileListFilter (org.springframework.integration.file.filters.ResettableFileListFilter)2 MessagingException (org.springframework.messaging.MessagingException)2 BufferedOutputStream (java.io.BufferedOutputStream)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 Test (org.junit.Test)1 BeansException (org.springframework.beans.BeansException)1 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)1