Search in sources :

Example 56 with File

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

the class FileChangedExclusiveReadLockStrategy method acquireExclusiveReadLock.

@Override
public boolean acquireExclusiveReadLock(GenericFileOperations<File> operations, GenericFile<File> file, Exchange exchange) throws Exception {
    // must call super
    if (!super.acquireExclusiveReadLock(operations, file, exchange)) {
        return false;
    }
    File target = new File(file.getAbsoluteFilePath());
    boolean exclusive = false;
    LOG.trace("Waiting for exclusive read lock to file: {}", file);
    long lastModified = Long.MIN_VALUE;
    long length = Long.MIN_VALUE;
    StopWatch watch = new StopWatch();
    long startTime = (new Date()).getTime();
    while (!exclusive) {
        // timeout check
        if (timeout > 0) {
            long delta = watch.taken();
            if (delta > timeout) {
                CamelLogger.log(LOG, readLockLoggingLevel, "Cannot acquire read lock within " + timeout + " millis. Will skip the file: " + file);
                // we could not get the lock within the timeout period, so return false
                return false;
            }
        }
        long newLastModified = target.lastModified();
        long newLength = target.length();
        long newOlderThan = startTime + watch.taken() - minAge;
        LOG.trace("Previous last modified: {}, new last modified: {}", lastModified, newLastModified);
        LOG.trace("Previous length: {}, new length: {}", length, newLength);
        LOG.trace("New older than threshold: {}", newOlderThan);
        if (newLength >= minLength && ((minAge == 0 && newLastModified == lastModified && newLength == length) || (minAge != 0 && newLastModified < newOlderThan))) {
            LOG.trace("Read lock acquired.");
            exclusive = true;
        } else {
            // set new base file change information
            lastModified = newLastModified;
            length = newLength;
            boolean interrupted = sleep();
            if (interrupted) {
                // we were interrupted while sleeping, we are likely being shutdown so return false
                return false;
            }
        }
    }
    return exclusive;
}
Also used : GenericFile(org.apache.camel.component.file.GenericFile) File(java.io.File) Date(java.util.Date) StopWatch(org.apache.camel.util.StopWatch)

Example 57 with File

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

the class FileIdempotentChangedRepositoryReadLockStrategy method acquireExclusiveReadLock.

@Override
public boolean acquireExclusiveReadLock(GenericFileOperations<File> operations, GenericFile<File> file, Exchange exchange) throws Exception {
    // in clustered mode then another node may have processed the file so we must check here again if the file exists
    File path = file.getFile();
    if (!path.exists()) {
        return false;
    }
    // check if we can begin on this file
    String key = asKey(file);
    boolean answer = idempotentRepository.add(key);
    if (!answer) {
        // another node is processing the file so skip
        CamelLogger.log(LOG, readLockLoggingLevel, "Cannot acquire read lock. Will skip the file: " + file);
    }
    if (answer) {
        // if we acquired during idempotent then check changed also
        answer = changed.acquireExclusiveReadLock(operations, file, exchange);
        if (!answer) {
            // remove from idempontent as we did not acquire it from changed
            idempotentRepository.remove(key);
        }
    }
    return answer;
}
Also used : GenericFile(org.apache.camel.component.file.GenericFile) File(java.io.File)

Example 58 with File

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

the class FileIdempotentRenameRepositoryReadLockStrategy method acquireExclusiveReadLock.

@Override
public boolean acquireExclusiveReadLock(GenericFileOperations<File> operations, GenericFile<File> file, Exchange exchange) throws Exception {
    // in clustered mode then another node may have processed the file so we must check here again if the file exists
    File path = file.getFile();
    if (!path.exists()) {
        return false;
    }
    // check if we can begin on this file
    String key = asKey(file);
    boolean answer = idempotentRepository.add(key);
    if (!answer) {
        // another node is processing the file so skip
        CamelLogger.log(LOG, readLockLoggingLevel, "Cannot acquire read lock. Will skip the file: " + file);
    }
    if (answer) {
        // if we acquired during idempotent then check rename also
        answer = rename.acquireExclusiveReadLock(operations, file, exchange);
        if (!answer) {
            // remove from idempontent as we did not acquire it from changed
            idempotentRepository.remove(key);
        }
    }
    return answer;
}
Also used : GenericFile(org.apache.camel.component.file.GenericFile) File(java.io.File)

Example 59 with File

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

the class FileIdempotentRepositoryReadLockStrategy method acquireExclusiveReadLock.

@Override
public boolean acquireExclusiveReadLock(GenericFileOperations<File> operations, GenericFile<File> file, Exchange exchange) throws Exception {
    // in clustered mode then another node may have processed the file so we must check here again if the file exists
    File path = file.getFile();
    if (!path.exists()) {
        return false;
    }
    // check if we can begin on this file
    String key = asKey(file);
    boolean answer = idempotentRepository.add(key);
    if (!answer) {
        // another node is processing the file so skip
        CamelLogger.log(LOG, readLockLoggingLevel, "Cannot acquire read lock. Will skip the file: " + file);
    }
    return answer;
}
Also used : GenericFile(org.apache.camel.component.file.GenericFile) File(java.io.File)

Example 60 with File

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

the class FileConsumer method asGenericFile.

/**
     * Creates a new GenericFile<File> based on the given file.
     *
     * @param endpointPath the starting directory the endpoint was configured with
     * @param file the source file
     * @param probeContentType whether to probe the content type of the file or not
     * @return wrapped as a GenericFile
     */
public static GenericFile<File> asGenericFile(String endpointPath, File file, String charset, boolean probeContentType) {
    GenericFile<File> answer = new GenericFile<File>(probeContentType);
    // use file specific binding
    answer.setBinding(new FileBinding());
    answer.setCharset(charset);
    answer.setEndpointPath(endpointPath);
    answer.setFile(file);
    answer.setFileNameOnly(file.getName());
    answer.setFileLength(file.length());
    answer.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.
    answer.setAbsolute(FileUtil.isAbsolute(file));
    answer.setAbsoluteFilePath(file.getAbsolutePath());
    answer.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) {
        answer.setRelativeFilePath(path.getParent() + File.separator + file.getName());
    } else {
        answer.setRelativeFilePath(path.getName());
    }
    // the file name should be the relative path
    answer.setFileName(answer.getRelativeFilePath());
    // use file as body as we have converters if needed as stream
    answer.setBody(file);
    return answer;
}
Also used : 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