Search in sources :

Example 1 with GenericFile

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;
}
Also used : TypeConverter(org.apache.camel.TypeConverter) GenericFile(org.apache.camel.component.file.GenericFile) FallbackConverter(org.apache.camel.FallbackConverter)

Example 2 with GenericFile

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;
}
Also used : TypeConverter(org.apache.camel.TypeConverter) GenericFile(org.apache.camel.component.file.GenericFile) FallbackConverter(org.apache.camel.FallbackConverter)

Example 3 with GenericFile

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;
}
Also used : GenericFile(org.apache.camel.component.file.GenericFile) File(java.io.File) GenericFile(org.apache.camel.component.file.GenericFile)

Example 4 with GenericFile

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;
}
Also used : GenericFile(org.apache.camel.component.file.GenericFile) File(java.io.File)

Example 5 with GenericFile

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);
        }
    }
}
Also used : GenericFile(org.apache.camel.component.file.GenericFile) File(java.io.File)

Aggregations

GenericFile (org.apache.camel.component.file.GenericFile)34 File (java.io.File)25 IOException (java.io.IOException)11 InputStream (java.io.InputStream)11 Exchange (org.apache.camel.Exchange)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)7 GenericFileOperationFailedException (org.apache.camel.component.file.GenericFileOperationFailedException)7 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 CamelExchangeException (org.apache.camel.CamelExchangeException)4 Message (org.apache.camel.Message)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 FileOutputStream (java.io.FileOutputStream)3 OutputStream (java.io.OutputStream)3 Serializable (java.io.Serializable)3 FallbackConverter (org.apache.camel.FallbackConverter)3 TypeConverter (org.apache.camel.TypeConverter)3 WrappedFile (org.apache.camel.WrappedFile)3 FileEndpoint (org.apache.camel.component.file.FileEndpoint)3 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)3 Test (org.junit.Test)3