Search in sources :

Example 26 with IOException

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;
}
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 27 with IOException

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);
    }
}
Also used : CamelContext(org.apache.camel.CamelContext) IOException(java.io.IOException) Method(java.lang.reflect.Method) ExpressionIllegalSyntaxException(org.apache.camel.ExpressionIllegalSyntaxException) IOException(java.io.IOException) FactoryFinder(org.apache.camel.spi.FactoryFinder)

Example 28 with IOException

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;
}
Also used : Path(java.nio.file.Path) HashMap(java.util.HashMap) IOException(java.io.IOException) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map)

Example 29 with IOException

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;
}
Also used : BufferedInputStream(java.io.BufferedInputStream) IOException(java.io.IOException) Properties(java.util.Properties)

Example 30 with IOException

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);
}
Also used : Scanner(java.util.Scanner) IOException(java.io.IOException) File(java.io.File)

Aggregations

IOException (java.io.IOException)41104 File (java.io.File)7663 InputStream (java.io.InputStream)4105 Test (org.junit.Test)3557 ArrayList (java.util.ArrayList)3023 FileInputStream (java.io.FileInputStream)2674 FileOutputStream (java.io.FileOutputStream)2358 FileNotFoundException (java.io.FileNotFoundException)2146 BufferedReader (java.io.BufferedReader)2028 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1761 InputStreamReader (java.io.InputStreamReader)1677 URL (java.net.URL)1608 HashMap (java.util.HashMap)1552 ByteArrayInputStream (java.io.ByteArrayInputStream)1534 OutputStream (java.io.OutputStream)1349 Path (org.apache.hadoop.fs.Path)1316 Map (java.util.Map)1212 List (java.util.List)1042 Properties (java.util.Properties)994 ServletException (javax.servlet.ServletException)916