Example usage for org.apache.commons.io FileUtils iterateFiles

List of usage examples for org.apache.commons.io FileUtils iterateFiles

Introduction

In this page you can find the example usage for org.apache.commons.io FileUtils iterateFiles.

Prototype

public static Iterator iterateFiles(File directory, String[] extensions, boolean recursive) 

Source Link

Document

Allows iteration over the files in a given directory (and optionally its subdirectories) which match an array of extensions.

Usage

From source file:com.referencelogic.xmlormupload.main.XmlormuploadMain.java

public void run() {
    try {// ww w.j  a  v  a2 s  .co m
        XMLConfiguration config = new XMLConfiguration(configFileName);
        String sourceDir = config.getString("source.path");
        List allowedExtensions = config.getList("source.extensions");
        int poolSize = config.getInt("threadpool.size");

        String groovySourceDir = config.getString("domainclasses.path");
        List groovyAllowedExtensions = config.getList("domainclasses.extensions");

        if (isDebugging) {
            log.debug("Loaded configuration successfully. Reading groovy class list from: " + groovySourceDir
                    + " with allowed extensions " + groovyAllowedExtensions);
        }

        if (isDebugging) {
            log.debug("Loaded configuration successfully. Reading file list from: " + sourceDir
                    + " with allowed extensions " + allowedExtensions);
        }
        Iterator iter = FileUtils.iterateFiles(new File(sourceDir),
                (String[]) allowedExtensions.toArray(new String[allowedExtensions.size()]), true);
        if (poolSize < 1) {
            poolSize = 5;
        }

        exec = Executors.newFixedThreadPool(poolSize);

        GroovyClassLoader gcl = new GroovyClassLoader();

        ClassLoader ojcl = Thread.currentThread().getContextClassLoader();

        boolean allFilesResolved = false;
        while (!allFilesResolved) {
            Iterator groovyIter = FileUtils.iterateFiles(new File(groovySourceDir),
                    (String[]) groovyAllowedExtensions.toArray(new String[groovyAllowedExtensions.size()]),
                    true);

            allFilesResolved = true;

            while (groovyIter.hasNext()) {
                File groovyFile = (File) groovyIter.next();
                log.info("Trying to parse file " + groovyFile);
                try {
                    Class clazz = gcl.parseClass(groovyFile);
                } catch (IOException ioe) {
                    log.error("Unable to read file " + groovyFile + " to parse class ", ioe);
                } catch (Exception e) {
                    log.error("Unable to parse file " + groovyFile + " ex:" + e);
                    allFilesResolved = false;
                }
            }

        }

        Thread.currentThread().setContextClassLoader(gcl);

        Configuration hibernateConfig = new Configuration();

        SessionFactory sf;
        if (!matchRegex) {
            sf = hibernateConfig.configure(new File("hibernate.config.xml")).buildSessionFactory();
        } else {
            sf = hibernateConfig.configure(new File("hibernate.update.config.xml")).buildSessionFactory();
        }

        log.info("Opened session");

        while (iter.hasNext()) {
            File file = (File) iter.next();
            String filePath = "";
            try {
                filePath = file.getCanonicalPath();
                log.debug("Canonical path being processed is: " + filePath);
            } catch (IOException ioe) {
                log.warn("Unable to get canonical path from file", ioe);
            }
            log.debug("Is matchRegex true? " + matchRegex);
            log.debug("Does filePath match regexStr?" + filePath.matches(matchRegexStr));
            if ((!matchRegex) || (matchRegex && filePath.matches(matchRegexStr))) {
                exec.execute(new Xmlormuploader(file, config, gcl, sf));
            }
        }

        exec.shutdown();
        try {
            while (!exec.isTerminated()) {
                exec.awaitTermination(30, TimeUnit.SECONDS);
            }
        } catch (InterruptedException ie) {
            // Do nothing, going to close database connection anyway        
        }

        sf.close();

    } catch (ConfigurationException cex) {
        log.fatal("Unable to load config file " + configFileName + " to determine configuration.", cex);
    }
}

From source file:com.codspire.mojo.artifactlookup.LookupForDependency.java

private List<ProcessingStatus> loadArtifacts(File fileOrFolder, boolean recursive) {
    if (log.isDebugEnabled()) {
        log.debug("Artifact Location: " + fileOrFolder + " Find recursive = " + recursive);
    }/*from  w  w w . ja va2s. co  m*/

    List<ProcessingStatus> artifactsDetails = new ArrayList<ProcessingStatus>();

    if (fileOrFolder.isFile()) {
        artifactsDetails.add(new ProcessingStatus(fileOrFolder, getChecksumForFile(fileOrFolder)));
    } else {
        Iterator<File> iterateFiles = FileUtils.iterateFiles(fileOrFolder,
                plugInConfig.getStringArray(ARTIFACT_FILE_EXTENSIONS), recursive);

        while (iterateFiles.hasNext()) {
            File file = iterateFiles.next();
            artifactsDetails.add(new ProcessingStatus(file, getChecksumForFile(file)));
        }
    }

    return artifactsDetails;
}

From source file:edu.umn.msi.tropix.common.io.FileUtilsImpl.java

public Iterator<File> iterateFiles(final File directory, final String[] extensions, final boolean recursive) {
    @SuppressWarnings("unchecked")
    final Iterator<File> fileIterator = FileUtils.iterateFiles(directory, extensions, recursive);
    return fileIterator;
}

From source file:com.graphaware.test.integration.GraphAwareIntegrationTest.java

/**
 * Find all classes on classpath (only .class files) that have a method annotated with {@link Procedure}.
 *
 * @return classes with procedures./*from   w w w . j  a v  a  2s  . c o  m*/
 */
private Iterable<Class> proceduresOnClassPath() {
    Enumeration<URL> urls;

    try {
        urls = this.getClass().getClassLoader().getResources("");
    } catch (IOException e) {
        throw new RuntimeException();
    }

    List<Class> classes = new ArrayList<>();

    while (urls.hasMoreElements()) {
        Iterator<File> fileIterator;
        File directory;

        try {
            directory = new File(urls.nextElement().toURI());
            fileIterator = FileUtils.iterateFiles(directory, new String[] { "class" }, true);
        } catch (URISyntaxException e) {
            throw new RuntimeException(e);
        }

        while (fileIterator.hasNext()) {
            File file = fileIterator.next();
            try {
                String path = file.getAbsolutePath();
                Class<?> candidate = Class
                        .forName(path.substring(directory.getAbsolutePath().length() + 1, path.length() - 6)
                                .replaceAll("\\/", "."));

                for (Method m : candidate.getDeclaredMethods()) {
                    if (m.isAnnotationPresent(Procedure.class)) {
                        classes.add(candidate);
                    }
                }

            } catch (ClassNotFoundException e) {
                throw new RuntimeException(e);
            }
        }
    }

    return classes;
}

From source file:com.chingo247.structureapi.schematic.SchematicManager.java

public synchronized void load(File directory) {
    Preconditions.checkArgument(directory.isDirectory());

    Iterator<File> fit = FileUtils.iterateFiles(directory, new String[] { "schematic" }, true);

    if (fit.hasNext()) {
        ForkJoinPool pool = new ForkJoinPool(Runtime.getRuntime().availableProcessors()); // only create the pool if we have schematics
        Map<Long, SchematicDataNode> alreadyHere = Maps.newHashMap();
        Map<Long, SchematicDataNode> needsUpdating = Maps.newHashMap();

        List<SchematicProcessor> tasks = Lists.newArrayList();
        List<Schematic> alreadyDone = Lists.newArrayList();
        XXHasher hasher = new XXHasher();

        try (Transaction tx = graph.beginTx()) {
            Collection<SchematicDataNode> schematicNodes = schematicRepository
                    .findAfterDate(System.currentTimeMillis() - TWO_DAYS);
            for (SchematicDataNode node : schematicNodes) {
                if (!node.hasRotation()) {
                    needsUpdating.put(node.getXXHash64(), node);
                    continue;
                }//from w w  w  .j  a v  a2s  .  co m
                alreadyHere.put(node.getXXHash64(), node);
            }

            // Process the schematics that need to be loaded
            while (fit.hasNext()) {
                File schematicFile = fit.next();
                try {
                    long checksum = hasher.hash64(schematicFile);
                    // Only load schematic data that wasn't yet loaded...
                    SchematicDataNode existingData = alreadyHere.get(checksum);
                    if (existingData != null) {
                        Schematic s = new DefaultSchematic(schematicFile, existingData.getWidth(),
                                existingData.getHeight(), existingData.getLength(),
                                existingData.getAxisOffset());
                        alreadyDone.add(s);
                    } else if (getSchematic(checksum) == null) {
                        SchematicProcessor processor = new SchematicProcessor(schematicFile);
                        tasks.add(processor);
                        pool.execute(processor);
                    }
                } catch (IOException ex) {
                    Logger.getLogger(SchematicManager.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            tx.success();
        }

        // Wait for the processes the finish and queue them for bulk insert
        List<Schematic> newSchematics = Lists.newArrayList();
        try {
            for (SchematicProcessor sp : tasks) {
                Schematic schematic = sp.get();
                if (schematic != null) {
                    newSchematics.add(schematic);
                }
            }
        } catch (Exception ex) {
            Logger.getLogger(SchematicManager.class.getName()).log(Level.SEVERE, null, ex);
        }

        // Close the pool!
        pool.shutdown();

        int updated = 0;
        // Update the database
        try (Transaction tx = graph.beginTx()) {
            for (Schematic data : alreadyDone) {
                SchematicDataNode sdn = schematicRepository.findByHash(data.getHash());
                sdn.setLastImport(System.currentTimeMillis());
            }
            for (Schematic newData : newSchematics) {
                if (needsUpdating.get(newData.getHash()) != null) {
                    SchematicDataNode dataNode = schematicRepository.findByHash(newData.getHash());
                    dataNode.setRotation(newData.getAxisOffset());
                    updated++;
                    continue;
                }
                String name = newData.getFile().getName();
                long xxhash = newData.getHash();
                int width = newData.getWidth();
                int height = newData.getHeight();
                int length = newData.getLength();
                int axisOffset = newData.getAxisOffset();
                schematicRepository.addSchematic(name, xxhash, width, height, length, axisOffset,
                        System.currentTimeMillis());
            }

            // Delete unused
            int removed = 0;
            for (SchematicDataNode sdn : schematicRepository
                    .findBeforeDate(System.currentTimeMillis() - TWO_DAYS)) {
                sdn.delete();
                removed++;
            }
            if (removed > 0) {
                System.out.println("[SettlerCraft]: Deleted " + removed + " schematic(s) from cache");
            }

            if (updated > 0) {
                System.out.println("[SettlerCraft]: Updated " + updated + " schematic(s) from cache");
            }

            tx.success();
        }

        synchronized (schematics) {
            for (Schematic schematic : newSchematics) {
                schematics.put(schematic.getHash(), schematic);
            }
            for (Schematic schematic : alreadyDone) {
                schematics.put(schematic.getHash(), schematic);
            }
        }

    }

}

From source file:com.github.born2snipe.maven.plugin.idea.packaging.PackagingMojo.java

private File buildPluginJarFile() {
    ZipBuilder zipBuilder = new ZipBuilder(buildDir);

    Iterator<File> iterator = FileUtils.iterateFiles(buildOutputDir, null, true);
    while (iterator.hasNext()) {
        File file = iterator.next();
        String entry = convertToEntryPath(file);
        zipBuilder.withEntry(entry, inputStreamFor(file));
    }//from   ww w. j ava2  s  . c  o  m
    zipBuilder.withEntry("META-INF/MANIFEST.MF", "Manifest-Version: 1.0\n" + "Created-By: IntelliJ IDEA\n");
    return zipBuilder.build(pluginName + ".jar");
}

From source file:com.github.ithildir.liferay.mobile.go.GoSDKBuilder.java

protected void copyResource(String name, String destination) throws IOException {

    File destinationDir = new File(destination);

    destinationDir = new File(destinationDir.getAbsoluteFile(), CharPool.SLASH + name);

    URL sourceURL = getClass().getResource(CharPool.SLASH + name);
    URLConnection sourceConnection = sourceURL.openConnection();

    if (sourceConnection instanceof JarURLConnection) {
        copyJarResource((JarURLConnection) sourceConnection, destinationDir);
    } else {/*from w w w . ja va 2s .  c o m*/
        File sourceDir = new File(sourceURL.getPath());

        FileUtils.copyDirectory(sourceDir, destinationDir);
    }

    Iterator<File> itr = FileUtils.iterateFiles(destinationDir, new String[] { "copy" }, true);

    while (itr.hasNext()) {
        File file = itr.next();

        String cleanPath = FilenameUtils.removeExtension(file.getAbsolutePath());

        File cleanFile = new File(cleanPath);

        if (!cleanFile.exists()) {
            FileUtils.moveFile(file, new File(cleanPath));
        } else {
            file.delete();
        }
    }
}

From source file:com.liferay.mobile.sdk.windows.WindowsSDKBuilder.java

protected void copyResource(String name, String destination) throws IOException {

    File destinationDir = new File(destination);

    destinationDir = destinationDir.getAbsoluteFile();

    URL sourceURL = getClass().getResource(CharPool.SLASH + name);
    URLConnection sourceConnection = sourceURL.openConnection();

    if (sourceConnection instanceof JarURLConnection) {
        copyJarResource((JarURLConnection) sourceConnection, destinationDir);
    } else {//from   w w w.j a  v a2  s  .c  om
        File sourceDir = new File(sourceURL.getPath());

        FileUtils.copyDirectory(sourceDir, destinationDir);
    }

    Iterator<File> itr = FileUtils.iterateFiles(destinationDir, new String[] { "copy" }, true);

    while (itr.hasNext()) {
        File file = itr.next();

        String cleanPath = FilenameUtils.removeExtension(file.getAbsolutePath());

        File cleanFile = new File(cleanPath);

        if (!cleanFile.exists()) {
            FileUtils.moveFile(file, new File(cleanPath));
        } else {
            file.delete();
        }
    }
}

From source file:com.genericworkflownodes.knime.base.data.port.FileStorePrefixURIPortObject.java

/**
 * Triggers the re-indexing of the current content of the port, by
 * collecting all files that share the given prefix. This method should be
 * called after all content was generated inside the file store.
 *///from w ww  .  j  a  v a2s.  com
public void collectFiles() {
    // we have generated a list of files based on a prefix
    final File f = new File(getPrefix());

    Iterator<File> fIt = FileUtils.iterateFiles(f.getParentFile(), TrueFileFilter.INSTANCE,
            TrueFileFilter.INSTANCE);

    while (fIt.hasNext()) {
        File tf = fIt.next();
        if (!tf.isDirectory()) {
            final String abs_path = tf.getAbsolutePath();
            if (abs_path.startsWith(getPrefix())) {
                // get relative path to filestore as string
                File relPath = new File(tf.getName());
                File parent = tf.getParentFile();
                while (!parent.equals(getFileStoreRootDirectory())) {
                    relPath = new File(parent.getName(), relPath.toString());
                    parent = parent.getParentFile();
                }

                // register the found file at the underlying
                // FileStoreURIPortObject
                registerFile(relPath.toString());
            }
        }
    }

}

From source file:edu.ku.brc.specify.tasks.subpane.JasperReportsCache.java

/**
 * Clears the compiled files that are JVM dependent.
 * @param cachePath the path to the cache
 *///from w w w . jav a  2s .com
protected static void clearJasperFilesOnly(final File cachePath) {
    try {
        for (Iterator<?> iter = FileUtils.iterateFiles(cachePath, new String[] { "jasper" }, false); iter
                .hasNext();) {
            FileUtils.forceDelete((File) iter.next());
        }
    } catch (Exception ex) {
        edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount();
        edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(JasperReportsCache.class, ex);
        log.error(ex);
    }
}