Search in sources :

Example 46 with File

use of java.io.File in project camel by apache.

the class FtpBrowsableEndpointTest method testBrowsableThreeFilesRecursive.

@Test
public void testBrowsableThreeFilesRecursive() throws Exception {
    template.sendBodyAndHeader(getFtpUrl(), "A", Exchange.FILE_NAME, "a.txt");
    template.sendBodyAndHeader(getFtpUrl(), "B", Exchange.FILE_NAME, "foo/b.txt");
    template.sendBodyAndHeader(getFtpUrl(), "C", Exchange.FILE_NAME, "bar/c.txt");
    FtpEndpoint<?> endpoint = context.getEndpoint(getFtpUrl() + "&recursive=true&sortBy=file:name", FtpEndpoint.class);
    assertNotNull(endpoint);
    MemoryIdempotentRepository repo = (MemoryIdempotentRepository) endpoint.getInProgressRepository();
    assertEquals(0, repo.getCacheSize());
    List<Exchange> list = endpoint.getExchanges();
    assertNotNull(list);
    assertEquals(3, list.size());
    assertEquals("a.txt", list.get(0).getIn().getHeader(Exchange.FILE_NAME));
    assertEquals("c.txt", list.get(1).getIn().getHeader(Exchange.FILE_NAME_ONLY));
    assertEquals("b.txt", list.get(2).getIn().getHeader(Exchange.FILE_NAME_ONLY));
    // the in progress repo should not leak
    assertEquals(0, repo.getCacheSize());
    // and the files is still there
    File fileA = new File(FTP_ROOT_DIR + "/browse/a.txt");
    assertTrue("File should exist " + fileA, fileA.exists());
    File fileB = new File(FTP_ROOT_DIR + "/browse/foo/b.txt");
    assertTrue("File should exist " + fileB, fileB.exists());
    File fileC = new File(FTP_ROOT_DIR + "/browse/bar/c.txt");
    assertTrue("File should exist " + fileC, fileC.exists());
}
Also used : Exchange(org.apache.camel.Exchange) File(java.io.File) MemoryIdempotentRepository(org.apache.camel.processor.idempotent.MemoryIdempotentRepository) Test(org.junit.Test)

Example 47 with File

use of java.io.File in project camel by apache.

the class FtpBrowsableEndpointTest method testBrowsableOneFile.

@Test
public void testBrowsableOneFile() throws Exception {
    template.sendBodyAndHeader(getFtpUrl(), "A", Exchange.FILE_NAME, "a.txt");
    FtpEndpoint<?> endpoint = context.getEndpoint(getFtpUrl(), FtpEndpoint.class);
    assertNotNull(endpoint);
    MemoryIdempotentRepository repo = (MemoryIdempotentRepository) endpoint.getInProgressRepository();
    assertEquals(0, repo.getCacheSize());
    List<Exchange> list = endpoint.getExchanges();
    assertNotNull(list);
    assertEquals(1, list.size());
    assertEquals("a.txt", list.get(0).getIn().getHeader(Exchange.FILE_NAME));
    // the in progress repo should not leak
    assertEquals(0, repo.getCacheSize());
    // and the file is still there
    File file = new File(FTP_ROOT_DIR + "/browse/a.txt");
    assertTrue("File should exist " + file, file.exists());
}
Also used : Exchange(org.apache.camel.Exchange) File(java.io.File) MemoryIdempotentRepository(org.apache.camel.processor.idempotent.MemoryIdempotentRepository) Test(org.junit.Test)

Example 48 with File

use of java.io.File in project camel by apache.

the class MarkerFileExclusiveReadLockStrategy method prepareOnStartup.

@Override
public void prepareOnStartup(GenericFileOperations<File> operations, GenericFileEndpoint<File> endpoint) {
    if (deleteOrphanLockFiles) {
        String dir = endpoint.getConfiguration().getDirectory();
        File file = new File(dir);
        LOG.debug("Prepare on startup by deleting orphaned lock files from: {}", dir);
        Pattern excludePattern = endpoint.getExcludePattern();
        Pattern includePattern = endpoint.getIncludePattern();
        String endpointPath = endpoint.getConfiguration().getDirectory();
        StopWatch watch = new StopWatch();
        deleteLockFiles(file, endpoint.isRecursive(), endpointPath, endpoint.getFilter(), endpoint.getAntFilter(), excludePattern, includePattern);
        // log anything that takes more than a second
        if (watch.taken() > 1000) {
            LOG.info("Prepared on startup by deleting orphaned lock files from: {} took {} millis to complete.", dir, watch.taken());
        }
    }
}
Also used : Pattern(java.util.regex.Pattern) GenericFile(org.apache.camel.component.file.GenericFile) File(java.io.File) StopWatch(org.apache.camel.util.StopWatch)

Example 49 with File

use of java.io.File in project camel by apache.

the class MarkerFileExclusiveReadLockStrategy method acceptFile.

@SuppressWarnings("unchecked")
private static boolean acceptFile(File file, String endpointPath, GenericFileFilter filter, GenericFileFilter antFilter, Pattern excludePattern, Pattern includePattern) {
    GenericFile gf = new GenericFile();
    gf.setEndpointPath(endpointPath);
    gf.setFile(file);
    gf.setFileNameOnly(file.getName());
    gf.setFileLength(file.length());
    gf.setDirectory(file.isDirectory());
    // must use FileUtil.isAbsolute to have consistent check for whether the file is
    // absolute or not. As windows do not consider \ paths as absolute where as all
    // other OS platforms will consider \ as absolute. The logic in Camel mandates
    // that we align this for all OS. That is why we must use FileUtil.isAbsolute
    // to return a consistent answer for all OS platforms.
    gf.setAbsolute(FileUtil.isAbsolute(file));
    gf.setAbsoluteFilePath(file.getAbsolutePath());
    gf.setLastModified(file.lastModified());
    // compute the file path as relative to the starting directory
    File path;
    String endpointNormalized = FileUtil.normalizePath(endpointPath);
    if (file.getPath().startsWith(endpointNormalized + File.separator)) {
        // skip duplicate endpoint path
        path = new File(ObjectHelper.after(file.getPath(), endpointNormalized + File.separator));
    } else {
        path = new File(file.getPath());
    }
    if (path.getParent() != null) {
        gf.setRelativeFilePath(path.getParent() + File.separator + file.getName());
    } else {
        gf.setRelativeFilePath(path.getName());
    }
    // the file name should be the relative path
    gf.setFileName(gf.getRelativeFilePath());
    if (filter != null) {
        if (!filter.accept(gf)) {
            return false;
        }
    }
    if (antFilter != null) {
        if (!antFilter.accept(gf)) {
            return false;
        }
    }
    // exclude take precedence over include
    if (excludePattern != null) {
        if (excludePattern.matcher(file.getName()).matches()) {
            return false;
        }
    }
    if (includePattern != null) {
        if (!includePattern.matcher(file.getName()).matches()) {
            return false;
        }
    }
    return true;
}
Also used : GenericFile(org.apache.camel.component.file.GenericFile) File(java.io.File) GenericFile(org.apache.camel.component.file.GenericFile)

Example 50 with File

use of java.io.File in project camel by apache.

the class MarkerFileExclusiveReadLockStrategy method acquireExclusiveReadLock.

@Override
public boolean acquireExclusiveReadLock(GenericFileOperations<File> operations, GenericFile<File> file, Exchange exchange) throws Exception {
    if (!markerFile) {
        // if not using marker file then we assume acquired
        return true;
    }
    String lockFileName = getLockFileName(file);
    LOG.trace("Locking the file: {} using the lock file name: {}", file, lockFileName);
    // create a plain file as marker filer for locking (do not use FileLock)
    boolean acquired = FileUtil.createNewFile(new File(lockFileName));
    // store read-lock state
    exchange.setProperty(asReadLockKey(file, Exchange.FILE_LOCK_FILE_ACQUIRED), acquired);
    exchange.setProperty(asReadLockKey(file, Exchange.FILE_LOCK_FILE_NAME), lockFileName);
    return acquired;
}
Also used : GenericFile(org.apache.camel.component.file.GenericFile) File(java.io.File)

Aggregations

File (java.io.File)45560 IOException (java.io.IOException)9550 Test (org.junit.Test)9009 FileOutputStream (java.io.FileOutputStream)3388 ArrayList (java.util.ArrayList)3273 FileInputStream (java.io.FileInputStream)2915 InputStream (java.io.InputStream)1783 FileNotFoundException (java.io.FileNotFoundException)1743 URL (java.net.URL)1649 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1478 Test (org.testng.annotations.Test)1411 HashMap (java.util.HashMap)1404 RandomAccessFile (java.io.RandomAccessFile)1154 FileWriter (java.io.FileWriter)1081 Properties (java.util.Properties)1019 ZipFile (java.util.zip.ZipFile)959 JarFile (java.util.jar.JarFile)866 List (java.util.List)850 BufferedReader (java.io.BufferedReader)840 OutputStream (java.io.OutputStream)811