use of java.io.IOException 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;
}
use of java.io.IOException in project camel by apache.
the class GenericFileEndpoint method createGenericFileStrategy.
/**
* A strategy method to lazily create the file strategy
*/
@SuppressWarnings("unchecked")
protected GenericFileProcessStrategy<T> createGenericFileStrategy() {
Class<?> factory = null;
try {
FactoryFinder finder = getCamelContext().getFactoryFinder("META-INF/services/org/apache/camel/component/");
log.trace("Using FactoryFinder: {}", finder);
factory = finder.findClass(getScheme(), "strategy.factory.", CamelContext.class);
} catch (ClassNotFoundException e) {
log.trace("'strategy.factory.class' not found", e);
} catch (IOException e) {
log.trace("No strategy factory defined in 'META-INF/services/org/apache/camel/component/'", e);
}
if (factory == null) {
// use default
try {
log.trace("Using ClassResolver to resolve class: {}", DEFAULT_STRATEGYFACTORY_CLASS);
factory = this.getCamelContext().getClassResolver().resolveClass(DEFAULT_STRATEGYFACTORY_CLASS);
} catch (Exception e) {
log.trace("Cannot load class: {}", DEFAULT_STRATEGYFACTORY_CLASS, e);
}
// fallback and us this class loader
try {
if (log.isTraceEnabled()) {
log.trace("Using classloader: {} to resolve class: {}", this.getClass().getClassLoader(), DEFAULT_STRATEGYFACTORY_CLASS);
}
factory = this.getCamelContext().getClassResolver().resolveClass(DEFAULT_STRATEGYFACTORY_CLASS, this.getClass().getClassLoader());
} catch (Exception e) {
if (log.isTraceEnabled()) {
log.trace("Cannot load class: {} using classloader: " + this.getClass().getClassLoader(), DEFAULT_STRATEGYFACTORY_CLASS, e);
}
}
if (factory == null) {
throw new TypeNotPresentException(DEFAULT_STRATEGYFACTORY_CLASS + " class not found", null);
}
}
try {
Method factoryMethod = factory.getMethod("createGenericFileProcessStrategy", CamelContext.class, Map.class);
Map<String, Object> params = getParamsAsMap();
log.debug("Parameters for Generic file process strategy {}", params);
return (GenericFileProcessStrategy<T>) ObjectHelper.invokeMethod(factoryMethod, null, getCamelContext(), params);
} catch (NoSuchMethodException e) {
throw new TypeNotPresentException(factory.getSimpleName() + ".createGenericFileProcessStrategy method not found", e);
}
}
use of java.io.IOException in project camel by apache.
the class FileConsumer method pollDirectory.
@Override
protected boolean pollDirectory(String fileName, List<GenericFile<File>> fileList, int depth) {
log.trace("pollDirectory from fileName: {}", fileName);
depth++;
File directory = new File(fileName);
if (!directory.exists() || !directory.isDirectory()) {
log.debug("Cannot poll as directory does not exists or its not a directory: {}", directory);
if (getEndpoint().isDirectoryMustExist()) {
throw new GenericFileOperationFailedException("Directory does not exist: " + directory);
}
return true;
}
log.trace("Polling directory: {}", directory.getPath());
File[] dirFiles = directory.listFiles();
if (dirFiles == null || dirFiles.length == 0) {
// no files in this directory to poll
if (log.isTraceEnabled()) {
log.trace("No files found in directory: {}", directory.getPath());
}
return true;
} else {
// we found some files
if (log.isTraceEnabled()) {
log.trace("Found {} in directory: {}", dirFiles.length, directory.getPath());
}
}
List<File> files = Arrays.asList(dirFiles);
for (File file : dirFiles) {
// check if we can continue polling in files
if (!canPollMoreFiles(fileList)) {
return false;
}
// trace log as Windows/Unix can have different views what the file is?
if (log.isTraceEnabled()) {
log.trace("Found file: {} [isAbsolute: {}, isDirectory: {}, isFile: {}, isHidden: {}]", new Object[] { file, file.isAbsolute(), file.isDirectory(), file.isFile(), file.isHidden() });
}
// creates a generic file
GenericFile<File> gf = asGenericFile(endpointPath, file, getEndpoint().getCharset(), getEndpoint().isProbeContentType());
if (file.isDirectory()) {
if (endpoint.isRecursive() && depth < endpoint.getMaxDepth() && isValidFile(gf, true, files)) {
// recursive scan and add the sub files and folders
String subDirectory = fileName + File.separator + file.getName();
boolean canPollMore = pollDirectory(subDirectory, fileList, depth);
if (!canPollMore) {
return false;
}
}
} else {
// Windows can report false to a file on a share so regard it always as a file (if its not a directory)
if (depth >= endpoint.minDepth && isValidFile(gf, false, files)) {
log.trace("Adding valid file: {}", file);
// matched file so add
if (extendedAttributes != null) {
Path path = file.toPath();
Map<String, Object> allAttributes = new HashMap<>();
for (String attribute : extendedAttributes) {
try {
String prefix = null;
if (attribute.endsWith(":*")) {
prefix = attribute.substring(0, attribute.length() - 1);
} else if (attribute.equals("*")) {
prefix = "basic:";
}
if (ObjectHelper.isNotEmpty(prefix)) {
Map<String, Object> attributes = Files.readAttributes(path, attribute);
if (attributes != null) {
for (Map.Entry<String, Object> entry : attributes.entrySet()) {
allAttributes.put(prefix + entry.getKey(), entry.getValue());
}
}
} else if (!attribute.contains(":")) {
allAttributes.put("basic:" + attribute, Files.getAttribute(path, attribute));
} else {
allAttributes.put(attribute, Files.getAttribute(path, attribute));
}
} catch (IOException e) {
if (log.isDebugEnabled()) {
log.debug("Unable to read attribute {} on file {}", attribute, file, e);
}
}
}
gf.setExtendedAttributes(allAttributes);
}
fileList.add(gf);
}
}
}
return true;
}
use of java.io.IOException in project camel by apache.
the class Activator method loadProperties.
protected static Properties loadProperties(URL url) {
Properties properties = new Properties();
BufferedInputStream reader = null;
try {
reader = IOHelper.buffered(url.openStream());
properties.load(reader);
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
IOHelper.close(reader, "properties", LOG);
}
return properties;
}
use of java.io.IOException in project camel by apache.
the class FileStateRepository method loadStore.
/**
* Loads the given file store into the 1st level cache
*/
protected void loadStore() throws IOException {
// auto create starting directory if needed
if (!fileStore.exists()) {
LOG.debug("Creating filestore: {}", fileStore);
File parent = fileStore.getParentFile();
if (parent != null) {
parent.mkdirs();
}
boolean created = FileUtil.createNewFile(fileStore);
if (!created) {
throw new IOException("Cannot create filestore: " + fileStore);
}
}
LOG.trace("Loading to 1st level cache from state filestore: {}", fileStore);
cache.clear();
Scanner scanner = null;
try {
scanner = new Scanner(fileStore);
scanner.useDelimiter(STORE_DELIMITER);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
int separatorIndex = line.indexOf(KEY_VALUE_DELIMITER);
String key = line.substring(0, separatorIndex);
String value = line.substring(separatorIndex + KEY_VALUE_DELIMITER.length());
cache.put(key, value);
}
} catch (IOException e) {
throw ObjectHelper.wrapRuntimeCamelException(e);
} finally {
if (scanner != null) {
scanner.close();
}
}
LOG.debug("Loaded {} to the 1st level cache from state filestore: {}", cache.size(), fileStore);
}
Aggregations