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:ome.services.scripts.ScriptRepoHelper.java

@SuppressWarnings("unchecked")
public Iterator<File> iterate() {
    return FileUtils.iterateFiles(dir, SCRIPT_FILTER, TrueFileFilter.TRUE);
}

From source file:org.adscale.testtodoc.TestToDoc.java

protected void handleDir(String dirName, List<String> tests) throws Exception {
    File dir = new File(dirName);
    Iterator<File> iterator = FileUtils.iterateFiles(dir, new String[] { "class" }, true);

    while (iterator.hasNext()) {
        File file = iterator.next();
        if (file.isFile()) {
            String clazzPath = file.getCanonicalPath().substring(dir.getCanonicalPath().length() + 1,
                    file.getCanonicalPath().length());
            boolean isClass = clazzPath.endsWith(".class");
            if (isClass) {
                String clazzName = massageToClassPath(clazzPath);
                URLClassLoader classLoader = createClassLoader(dirName);
                Class<?> clazz = loadClass(classLoader, clazzName);
                handleClass(tests, clazz);
            }/*from   w  ww. j av a  2s .  com*/
        } else {
            System.out.println("handle dir");
        }
    }
}

From source file:org.ala.hbase.RepoDataLoader.java

/**
 * Scan through the supplied directories.
 *
 * @param dirs/*w  ww . j a va  2  s . c o m*/
 */
public void scanDirectory(File[] dirs) {

    int filesRead = 0;
    int propertiesSynced = 0;

    for (File currentDir : dirs) {
        logger.info("Reading directory: " + currentDir.getAbsolutePath());
        Iterator<File> fileIterator = FileUtils.iterateFiles(currentDir, null, true);
        while (fileIterator.hasNext()) {
            File currentFile = fileIterator.next();
            if (currentFile.getName().equals(FileType.RDF.toString())) {
                filesRead++;
                String infosourceId = currentFile.getParentFile().getParentFile().getParentFile().getName();
                String infoSourceUid = infoSourceDAO.getUidByInfosourceId(String.valueOf(infosourceId));
                //read the dublin core in the same directory - determine if its an image
                try {
                    logger.info("Reading file: " + currentFile.getAbsolutePath());
                    FileReader reader = new FileReader(currentFile);
                    List<Triple> triples = TurtleUtils.readTurtle(reader);
                    //close the reader
                    reader.close();

                    String currentSubject = null;
                    List<Triple> splitBySubject = new ArrayList<Triple>();

                    String guid = null;
                    //iterate through triple, splitting the triples by subject
                    for (Triple triple : triples) {

                        if (currentSubject == null) {
                            currentSubject = triple.subject;
                        } else if (!currentSubject.equals(triple.subject)) {
                            //sync these triples
                            //                        /data/bie/1036/23/235332/rdf

                            guid = sync(currentFile, splitBySubject, infosourceId, infoSourceUid);
                            if (guid != null && guid.trim().length() > 0) {
                                propertiesSynced++;
                            }
                            //clear list
                            splitBySubject.clear();
                            currentSubject = triple.subject;
                        }
                        splitBySubject.add(triple);
                    }

                    //sort out the buffer
                    if (!splitBySubject.isEmpty()) {
                        guid = sync(currentFile, splitBySubject, infosourceId, infoSourceUid);
                        if (guid != null && guid.trim().length() > 0) {
                            propertiesSynced++;
                        }
                    }

                    if (gList && guid != null) {
                        guidOut.write((guid + "\n").getBytes());
                    }

                    guidList.add(guid);

                } catch (Exception e) {
                    logger.error("Error reading triples from file: '" + currentFile.getAbsolutePath() + "', "
                            + e.getMessage(), e);
                }
            }
        }
        logger.info("InfosourceId: " + currentDir.getName() + " - Files read: " + filesRead
                + ", files matched: " + propertiesSynced);
        totalFilesRead += filesRead;
        totalPropertiesSynced += propertiesSynced;
    }
}

From source file:org.ala.spatial.web.services.GDMWSController.java

public static void processTransformedGrids(String pid, String outputdir) {
    System.out.println("About to iterate thru' files in " + outputdir);
    try {// w w  w.ja v a 2s.c  o  m

        String url = "";
        String extra = "";
        String username = AlaspatialProperties.getGeoserverUsername();
        String password = AlaspatialProperties.getGeoserverPassword();

        Iterator<File> files = FileUtils.iterateFiles(new File(outputdir), new String[] { "grd" }, false);
        while (files.hasNext()) {
            File f = files.next();
            if (f.getName().startsWith("domain")) {
                continue;
            }
            String lyr = f.getName().substring(0, f.getName().length() - 4);
            System.out.println("Converting " + lyr);
            SpatialTransformer.convertDivaToAsc(outputdir + lyr, outputdir + lyr + ".asc");

            String[] infiles = { outputdir + lyr + ".asc", outputdir + lyr + ".prj" };
            String ascZipFile = outputdir + lyr + ".zip";
            Zipper.zipFiles(infiles, ascZipFile);

            url = AlaspatialProperties.getGeoserverUrl() + "/rest/workspaces/ALA/coveragestores/gdm_" + lyr
                    + "_" + pid + "/file.arcgrid?coverageName=gdm_" + lyr + "_" + pid;
            // Upload the file to GeoServer using REST calls
            System.out.println("Uploading file: " + ascZipFile + " to \n" + url);
            UploadSpatialResource.loadResource(url, extra, username, password, ascZipFile);
        }

    } catch (Exception e) {
        System.out.println("Unable to generate and upload transformed grids");
        e.printStackTrace(System.out);
    }
}

From source file:org.ala.util.CleanupRepository.java

/**
 * @param args/*ww w .ja v  a 2 s . c  om*/
 */
public static void main(String[] args) throws Exception {

    String filePath = null;
    Date thresholdDate = null;
    try {
        filePath = args[0];
        thresholdDate = DateUtils.parseDate(args[1], new String[] { "yyyyMMdd" });
    } catch (Exception e) {
        System.out.println("Usage: <absolute-file-path> <delete-before date (yyyyMMdd)>");
        System.exit(0);
    }

    //iterate through directory

    File file = new File(filePath);
    File[] dirs = file.listFiles((FileFilter) DirectoryFileFilter.DIRECTORY);

    int deleting = 0;
    int keeping = 0;

    for (File currentDir : dirs) {
        System.out.println("Reading directory: " + currentDir.getAbsolutePath());
        Iterator<File> fileIterator = FileUtils.iterateFiles(currentDir, null, true);
        while (fileIterator.hasNext()) {
            File currentFile = fileIterator.next();
            Date lastModified = new Date(currentFile.lastModified());
            if (lastModified.before(thresholdDate)) {
                SimpleDateFormat sf = new SimpleDateFormat("dd MMM yyyy");
                System.out.println("Candidate for deletion: " + currentFile.getAbsolutePath()
                        + " last modified: " + sf.format(lastModified));
                FileUtils.forceDelete(currentFile);
                deleting++;
            } else {
                keeping++;
            }
        }
    }

    System.out.println("Keeping :" + keeping + ", deleting :" + deleting);

    System.out.println("Use the following command to remove empty directories.");

    System.out.println("find . -depth -empty -type d -exec rmdir {} \\;");
}

From source file:org.ala.util.RebuildFromFS.java

/**
 * Rebuild the house keeping database from the filesystem.
 * /*from   www  .  j ava 2  s  .c  o  m*/
 * @param directoryRoot
 */
private void rebuildDatabase(String directoryRoot) {
    File file = new File(directoryRoot);

    File[] dirs = file.listFiles((FileFilter) DirectoryFileFilter.DIRECTORY);

    for (File currentDir : dirs) {
        System.out.println("Reading directory: " + currentDir.getAbsolutePath());
        Iterator<File> fileIterator = FileUtils.iterateFiles(currentDir, null, true);
        while (fileIterator.hasNext()) {
            File currentFile = fileIterator.next();
            if (currentFile.getName().equals(FileType.DC.toString())) {
                //lets make a record

                try {
                    Map<String, String> properties = repositoryFileUtils.readDcFileAsMap(currentFile);

                    Document document = new Document();
                    document.setFilePath(currentFile.getParentFile().getAbsolutePath());
                    Date lastModified = new Date(currentFile.lastModified());
                    document.setInfoSourceId(Integer
                            .parseInt(currentFile.getParentFile().getParentFile().getParentFile().getName()));
                    document.setCreated(lastModified);
                    document.setModified(lastModified);
                    document.setUri(properties.get(Predicates.DC_IDENTIFIER.toString()));
                    document.setMimeType(properties.get(Predicates.DC_FORMAT.toString()));

                    String parentGuid = properties.get(Predicates.DC_IS_PART_OF.toString());
                    if (parentGuid != null) {
                        Document parentDocument = documentDao.getByUri(parentGuid);
                        if (parentDocument != null) {
                            document.setParentDocumentId(parentDocument.getId());
                        } else {
                            logger.warn("Unable to find doc for parent guid: " + parentGuid);
                        }
                    }

                    int documentId = Integer.parseInt(currentFile.getParentFile().getName());
                    document.setId(documentId);
                    Document doc = documentDao.getByUri(document.getUri());
                    if (doc == null) {
                        documentDao.save(document);
                    } else {
                        documentDao.update(document);
                    }
                } catch (Exception e) {
                    System.err.println(
                            "Problem with " + currentFile.getAbsolutePath() + ", error:" + e.getMessage());
                }
            }
        }
    }
}

From source file:org.apache.flume.source.syncdir.SyncDirFileLineReader.java

/**
 * Find the next file in the directory by walking through directory tree.
 *
 * @return the next file//from  w ww  .ja  v a  2s  .  com
 */
private Optional<ResettableFileLineReader> getNextFile() {
    if (null != filesIterator && !filesIterator.hasNext()) {
        filesIterator = null;
        return Optional.absent();
    }
    if (null == filesIterator) {
        filesIterator = FileUtils.iterateFiles(directory, new IOFileFilter() {
            @Override
            public boolean accept(File file) {
                if (!file.exists())
                    return false; // skip non-exist files in iterator
                if (file.isFile()) {
                    String fileStr = file.getName();
                    if (!(fileStr.endsWith(endFileSuffix) || fileStr.endsWith(statsFileSuffix)
                            || fileStr.endsWith(finishedStatsFileSuffix))) {
                        File finishedMarkFile = new File(file.getPath() + finishedStatsFileSuffix);
                        if (!finishedMarkFile.exists())
                            return true;
                    }
                }
                return false;
            }

            @Override
            public boolean accept(File dir, String name) {
                return false;
            }
        }, TrueFileFilter.INSTANCE);
    }

    File nextFile;
    boolean fileEnded;
    if (!filesIterator.hasNext())
        return Optional.absent();
    /* checking file's reading progress, skip if needed */
    nextFile = filesIterator.next();
    logger.debug("treating next file: {}", nextFile);
    fileEnded = new File(nextFile.getPath() + endFileSuffix).exists();
    if (fileEnded) {
        logger.debug("file {} marked as ended", nextFile);
    }
    try {
        ResettableFileLineReader file = new ResettableFileLineReader(nextFile, fileEnded, statsFilePrefix,
                statsFileSuffix, finishedStatsFileSuffix);
        return Optional.of(file);
    } catch (IOException e) {
        logger.error("Exception opening file: " + nextFile, e);
        return Optional.absent();
    }
}

From source file:org.apache.solr.kelvin.App.java

private void scanDirectory(String dirName) throws Exception {
    String extensions[] = { "json" };
    @SuppressWarnings("unchecked")
    Iterator<File> i = FileUtils.iterateFiles(new File(dirName), extensions, true);
    while (i.hasNext()) {
        testFiles.add(readFileToJsonNode(i.next()));
    }//from   ww  w .j a v a2s  . c  om
}

From source file:org.apache.synapse.config.xml.MultiXMLConfigurationBuilder.java

private static void createLocalEntries(SynapseConfiguration synapseConfig, String rootDirPath,
        Properties properties) {/*from  ww w. j  a  v a2s  .c o m*/

    File localEntriesDir = new File(rootDirPath, LOCAL_ENTRY_DIR);
    if (localEntriesDir.exists()) {
        if (log.isDebugEnabled()) {
            log.debug("Loading local entry definitions from : " + localEntriesDir.getPath());
        }

        Iterator entryDefinitions = FileUtils.iterateFiles(localEntriesDir, extensions, false);
        while (entryDefinitions.hasNext()) {
            File file = (File) entryDefinitions.next();
            try {
                OMElement document = getOMElement(file);
                Entry entry = SynapseXMLConfigurationFactory.defineEntry(synapseConfig, document, properties);
                if (entry != null) {
                    entry.setFileName(file.getName());
                    synapseConfig.getArtifactDeploymentStore().addArtifact(file.getAbsolutePath(),
                            entry.getKey());
                }
            } catch (Exception e) {
                String msg = "Local Entry configuration cannot be built from : " + file.getName();
                handleConfigurationError(SynapseConstants.FAIL_SAFE_MODE_LOCALENTRIES, msg, e);
            }
        }
    }
}

From source file:org.apache.synapse.config.xml.MultiXMLConfigurationBuilder.java

private static void createProxyServices(SynapseConfiguration synapseConfig, String rootDirPath,
        Properties properties) {/*w  w  w . ja va2 s  .  c  o  m*/

    File proxyServicesDir = new File(rootDirPath, PROXY_SERVICES_DIR);
    if (proxyServicesDir.exists()) {
        if (log.isDebugEnabled()) {
            log.debug("Loading proxy services from : " + proxyServicesDir.getPath());
        }

        Iterator proxyDefinitions = FileUtils.iterateFiles(proxyServicesDir, extensions, false);

        while (proxyDefinitions.hasNext()) {
            File file = (File) proxyDefinitions.next();
            try {
                OMElement document = getOMElement(file);
                ProxyService proxy = SynapseXMLConfigurationFactory.defineProxy(synapseConfig, document,
                        properties);
                if (proxy != null) {
                    proxy.setFileName(file.getName());
                    synapseConfig.getArtifactDeploymentStore().addArtifact(file.getAbsolutePath(),
                            proxy.getName());
                }
            } catch (Exception e) {
                String msg = "Proxy configuration cannot be built from : " + file.getName();
                handleConfigurationError(SynapseConstants.FAIL_SAFE_MODE_PROXY_SERVICES, msg, e);
            }
        }
    }
}