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;
}
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;
}
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;
}
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;
}
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;
}
Aggregations