use of org.apache.camel.component.file.GenericFile in project camel by apache.
the class MyOtherTypeConverter method convertTo.
@FallbackConverter
public static Object convertTo(Class<?> type, Exchange exchange, Object value, TypeConverterRegistry registry) {
// use a fallback type converter so we can convert the embedded body if the value is GenericFile
if (GenericFile.class.isAssignableFrom(value.getClass())) {
GenericFile<?> file = (GenericFile<?>) value;
Class<?> from = file.getBody().getClass();
// maybe from is already the type we want
if (from.isAssignableFrom(type)) {
return file.getBody();
}
// no then try to lookup a type converter
TypeConverter tc = registry.lookup(type, from);
if (tc != null) {
Object body = file.getBody();
return tc.convertTo(type, exchange, body);
}
}
return null;
}
use of org.apache.camel.component.file.GenericFile in project camel by apache.
the class MyTypeConverter method convertTo.
@FallbackConverter
public static Object convertTo(Class<?> type, Exchange exchange, Object value, TypeConverterRegistry registry) {
// use a fallback type converter so we can convert the embedded body if the value is GenericFile
if (GenericFile.class.isAssignableFrom(value.getClass())) {
GenericFile<?> file = (GenericFile<?>) value;
Class<?> from = file.getBody().getClass();
// maybe from is already the type we want
if (from.isAssignableFrom(type)) {
return file.getBody();
}
// no then try to lookup a type converter
TypeConverter tc = registry.lookup(type, from);
if (tc != null) {
Object body = file.getBody();
return tc.convertTo(type, exchange, body);
}
}
return null;
}
use of org.apache.camel.component.file.GenericFile 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;
}
use of org.apache.camel.component.file.GenericFile 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;
}
use of org.apache.camel.component.file.GenericFile 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);
}
}
}
Aggregations