Search in sources :

Example 51 with File

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

the class MarkerFileExclusiveReadLockStrategy method doReleaseExclusiveReadLock.

protected void doReleaseExclusiveReadLock(GenericFileOperations<File> operations, GenericFile<File> file, Exchange exchange) throws Exception {
    if (!markerFile) {
        // if not using marker file then nothing to release
        return;
    }
    boolean acquired = exchange.getProperty(asReadLockKey(file, Exchange.FILE_LOCK_FILE_ACQUIRED), false, Boolean.class);
    // only release the file if camel get the lock before
    if (acquired) {
        String lockFileName = exchange.getProperty(asReadLockKey(file, Exchange.FILE_LOCK_FILE_NAME), String.class);
        File lock = new File(lockFileName);
        if (lock.exists()) {
            LOG.trace("Unlocking file: {}", lockFileName);
            boolean deleted = FileUtil.deleteFile(lock);
            LOG.trace("Lock file: {} was deleted: {}", lockFileName, deleted);
        }
    }
}
Also used : GenericFile(org.apache.camel.component.file.GenericFile) File(java.io.File)

Example 52 with File

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

the class FileLockExclusiveReadLockStrategy 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());
    LOG.trace("Waiting for exclusive read lock to file: {}", target);
    FileChannel channel = null;
    RandomAccessFile randomAccessFile = null;
    boolean exclusive = false;
    FileLock lock = null;
    try {
        randomAccessFile = new RandomAccessFile(target, "rw");
        // try to acquire rw lock on the file before we can consume it
        channel = randomAccessFile.getChannel();
        StopWatch watch = new StopWatch();
        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: " + target);
                    // we could not get the lock within the timeout period, so return false
                    return false;
                }
            }
            // get the lock using either try lock or not depending on if we are using timeout or not
            try {
                lock = timeout > 0 ? channel.tryLock() : channel.lock();
            } catch (IllegalStateException ex) {
            // Also catch the OverlappingFileLockException here. Do nothing here                    
            }
            if (lock != null) {
                LOG.trace("Acquired exclusive read lock: {} to file: {}", lock, target);
                exclusive = true;
            } else {
                boolean interrupted = sleep();
                if (interrupted) {
                    // we were interrupted while sleeping, we are likely being shutdown so return false
                    return false;
                }
            }
        }
    } catch (IOException e) {
        // such as AntiVirus or MS Office that has special locks for it's supported files
        if (timeout == 0) {
            // if not using timeout, then we cant retry, so return false
            return false;
        }
        LOG.debug("Cannot acquire read lock. Will try again.", e);
        boolean interrupted = sleep();
        if (interrupted) {
            // we were interrupted while sleeping, we are likely being shutdown so return false
            return false;
        }
    } finally {
        // close channels if we did not grab the lock
        if (!exclusive) {
            IOHelper.close(channel, "while acquiring exclusive read lock for file: " + target, LOG);
            IOHelper.close(randomAccessFile, "while acquiring exclusive read lock for file: " + target, LOG);
            // and also must release super lock
            super.releaseExclusiveReadLockOnAbort(operations, file, exchange);
        }
    }
    // store read-lock state
    exchange.setProperty(asReadLockKey(file, Exchange.FILE_LOCK_EXCLUSIVE_LOCK), lock);
    exchange.setProperty(asReadLockKey(file, Exchange.FILE_LOCK_RANDOM_ACCESS_FILE), randomAccessFile);
    // we grabbed the lock
    return true;
}
Also used : RandomAccessFile(java.io.RandomAccessFile) FileChannel(java.nio.channels.FileChannel) FileLock(java.nio.channels.FileLock) IOException(java.io.IOException) RandomAccessFile(java.io.RandomAccessFile) GenericFile(org.apache.camel.component.file.GenericFile) File(java.io.File) StopWatch(org.apache.camel.util.StopWatch)

Example 53 with File

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

the class GenericFileProcessStrategySupport method deleteLocalWorkFile.

protected void deleteLocalWorkFile(Exchange exchange) {
    // delete local work file, if it was used (eg by ftp component)
    File local = exchange.getIn().getHeader(Exchange.FILE_LOCAL_WORK_PATH, File.class);
    if (local != null && local.exists()) {
        boolean deleted = FileUtil.deleteFile(local);
        log.trace("Local work file: {} was deleted: {}", local, deleted);
    }
}
Also used : GenericFile(org.apache.camel.component.file.GenericFile) File(java.io.File)

Example 54 with File

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

the class MarkerFileExclusiveReadLockStrategy method deleteLockFiles.

private static void deleteLockFiles(File dir, boolean recursive, String endpointPath, GenericFileFilter filter, GenericFileFilter antFilter, Pattern excludePattern, Pattern includePattern) {
    File[] files = dir.listFiles();
    if (files == null || files.length == 0) {
        return;
    }
    for (File file : files) {
        if (file.getName().startsWith(".")) {
            // files starting with dot should be skipped
            continue;
        }
        // filter unwanted files and directories to avoid traveling everything
        if (filter != null || antFilter != null || excludePattern != null || includePattern != null) {
            if (!acceptFile(file, endpointPath, filter, antFilter, excludePattern, includePattern)) {
                continue;
            }
        }
        if (file.getName().endsWith(FileComponent.DEFAULT_LOCK_FILE_POSTFIX)) {
            LOG.warn("Deleting orphaned lock file: " + file);
            FileUtil.deleteFile(file);
        } else if (recursive && file.isDirectory()) {
            deleteLockFiles(file, true, endpointPath, filter, antFilter, excludePattern, includePattern);
        }
    }
}
Also used : GenericFile(org.apache.camel.component.file.GenericFile) File(java.io.File)

Example 55 with File

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

the class GenericFileProducer method writeFile.

public void writeFile(Exchange exchange, String fileName) throws GenericFileOperationFailedException {
    // build directory if auto create is enabled
    if (endpoint.isAutoCreate()) {
        // we must normalize it (to avoid having both \ and / in the name which confuses java.io.File)
        String name = FileUtil.normalizePath(fileName);
        // use java.io.File to compute the file path
        File file = new File(name);
        String directory = file.getParent();
        boolean absolute = FileUtil.isAbsolute(file);
        if (directory != null) {
            if (!operations.buildDirectory(directory, absolute)) {
                log.debug("Cannot build directory [{}] (could be because of denied permissions)", directory);
            }
        }
    }
    // upload
    if (log.isTraceEnabled()) {
        log.trace("About to write [{}] to [{}] from exchange [{}]", new Object[] { fileName, getEndpoint(), exchange });
    }
    boolean success = operations.storeFile(fileName, exchange);
    if (!success) {
        throw new GenericFileOperationFailedException("Error writing file [" + fileName + "]");
    }
    log.debug("Wrote [{}] to [{}]", fileName, getEndpoint());
}
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