Example usage for org.apache.commons.vfs2 FileObject getChildren

List of usage examples for org.apache.commons.vfs2 FileObject getChildren

Introduction

In this page you can find the example usage for org.apache.commons.vfs2 FileObject getChildren.

Prototype

FileObject[] getChildren() throws FileSystemException;

Source Link

Document

Lists the children of this file.

Usage

From source file:de.innovationgate.wgpublisher.design.fs.FileSystemDesignManager.java

private List<ModuleFile> getModuleFiles(int type, FileObject dir, String prefix, String category)
        throws WGDesignSyncException, FileSystemException {

    // Find the valid suffixes for the type: Standard plus registered conversions
    Set<String> suffixes = new HashSet<String>();
    suffixes.add(getFileStandardSuffix(type, category));
    for (ModuleDefinition def : _core.getModuleRegistry()
            .getModulesForType(DesignResourceConversionModuleType.class).values()) {
        DesignResourceConversionProperties props = (DesignResourceConversionProperties) def.getProperties();
        if (props.getDesignType() == type
                && (props.getCodeType() == null || props.getCodeType().equals(category))) {
            for (String sfx : props.getSuffixes()) {
                suffixes.add("." + sfx);
            }/*from  ww w  .  j av a 2s.  c om*/
        }
    }

    List<ModuleFile> moduleFiles = new ArrayList<ModuleFile>();
    if (dir == null || !dir.exists()) {
        return moduleFiles;
    }

    FileObject[] files = dir.getChildren();
    if (files == null) {
        throw new WGDesignSyncException("Cannot collect files from directory '" + dir.getName().getPathDecoded()
                + "'. Please verify directory existence.");
    }

    FileObject file;
    for (int i = 0; i < files.length; i++) {
        file = files[i];
        if (!isValidDesignFile(file)) {
            continue;
        }
        if (file.getType().equals(FileType.FOLDER)) {
            if (file.getName().getBaseName().equals(DesignDirectory.NAME_METADATADIR)) {
                continue;
            }
            String newPrefix = (prefix.trim().equals("") ? file.getName().getBaseName().toLowerCase()
                    : prefix + DIRECTORY_DIVIDER + file.getName().getBaseName().toLowerCase());
            moduleFiles.addAll(getModuleFiles(type, file, newPrefix, category));
        } else {
            // Test the file suffix
            if (!suffixes.contains("." + file.getName().getExtension())) {
                continue;
            }
            moduleFiles.add(new ModuleFile(file, prefix, category, type));
        }
    }

    return moduleFiles;

}

From source file:com.yenlo.synapse.transport.vfs.VFSTransportListener.java

/**
 * Search for files that match the given regex pattern and create a list
 * Then process each of these files and update the status of the scan on
 * the poll table//from  w  w  w  .j av a2 s  .  c  om
 * @param entry the poll table entry for the scan
 * @param fileURI the file or directory to be scanned
 */
private void scanFileOrDirectory(final PollTableEntry entry, String fileURI) {

    FileObject fileObject = null;

    //TODO : Trying to make the correct URL out of the malformed one.
    if (fileURI.contains("vfs:")) {
        fileURI = fileURI.substring(fileURI.indexOf("vfs:") + 4);
    }

    if (log.isDebugEnabled()) {
        log.debug("Scanning directory or file : " + VFSUtils.maskURLPassword(fileURI));
    }

    boolean wasError = true;
    int retryCount = 0;
    int maxRetryCount = entry.getMaxRetryCount();
    long reconnectionTimeout = entry.getReconnectTimeout();

    while (wasError) {
        try {
            retryCount++;
            fileObject = fsManager.resolveFile(fileURI);

            if (fileObject == null) {
                log.error("fileObject is null");
                throw new FileSystemException("fileObject is null");
            }

            wasError = false;

        } catch (FileSystemException e) {
            if (retryCount >= maxRetryCount) {
                processFailure(
                        "Repeatedly failed to resolve the file URI: " + VFSUtils.maskURLPassword(fileURI), e,
                        entry);
                return;
            } else {
                log.warn("Failed to resolve the file URI: " + VFSUtils.maskURLPassword(fileURI)
                        + ", in attempt " + retryCount + ", " + e.getMessage() + " Retrying in "
                        + reconnectionTimeout + " milliseconds.");
            }
        }

        if (wasError) {
            try {
                Thread.sleep(reconnectionTimeout);
            } catch (InterruptedException e2) {
                log.error("Thread was interrupted while waiting to reconnect.", e2);
            }
        }
    }

    try {
        if (fileObject.exists() && fileObject.isReadable()) {

            entry.setLastPollState(PollTableEntry.NONE);
            FileObject[] children = null;
            try {
                children = fileObject.getChildren();
            } catch (FileNotFolderException ignored) {
            } catch (FileSystemException ex) {
                log.error(ex.getMessage(), ex);
            }

            // if this is a file that would translate to a single message
            if (children == null || children.length == 0) {
                boolean isFailedRecord = false;
                if (entry.getMoveAfterMoveFailure() != null) {
                    isFailedRecord = isFailedRecord(fileObject, entry);
                }

                if (fileObject.getType() == FileType.FILE && !isFailedRecord) {
                    if (!entry.isFileLockingEnabled()
                            || (entry.isFileLockingEnabled() && VFSUtils.acquireLock(fsManager, fileObject))) {
                        try {
                            processFile(entry, fileObject);
                            entry.setLastPollState(PollTableEntry.SUCCSESSFUL);
                            metrics.incrementMessagesReceived();

                        } catch (AxisFault e) {
                            logException("Error processing File URI : " + fileObject.getName(), e);
                            entry.setLastPollState(PollTableEntry.FAILED);
                            metrics.incrementFaultsReceiving();
                        }

                        try {
                            moveOrDeleteAfterProcessing(entry, fileObject);
                        } catch (AxisFault axisFault) {
                            logException("File object '" + fileObject.getURL().toString() + "' "
                                    + "cloud not be moved", axisFault);
                            entry.setLastPollState(PollTableEntry.FAILED);
                            String timeStamp = VFSUtils.getSystemTime(entry.getFailedRecordTimestampFormat());
                            addFailedRecord(entry, fileObject, timeStamp);
                        }
                        if (entry.isFileLockingEnabled()) {
                            VFSUtils.releaseLock(fsManager, fileObject);
                            if (log.isDebugEnabled()) {
                                log.debug("Removed the lock file '" + fileObject.toString()
                                        + ".lock' of the file '" + fileObject.toString());
                            }
                        }
                    } else if (log.isDebugEnabled()) {
                        log.debug("Couldn't get the lock for processing the file : " + fileObject.getName());
                    } else if (isFailedRecord) {
                        if (entry.isFileLockingEnabled()) {
                            VFSUtils.releaseLock(fsManager, fileObject);
                        }
                        // schedule a cleanup task if the file is there
                        if (fsManager.resolveFile(fileObject.getURL().toString()) != null
                                && removeTaskState == STATE_STOPPED
                                && entry.getMoveAfterMoveFailure() != null) {
                            workerPool.execute(new FileRemoveTask(entry, fileObject));
                        }
                        if (log.isDebugEnabled()) {
                            log.debug("File '" + fileObject.getURL() + "' has been marked as a failed"
                                    + " record, it will not process");
                        }
                    }
                }

            } else {
                int failCount = 0;
                int successCount = 0;
                int processCount = 0;
                Integer iFileProcessingInterval = entry.getFileProcessingInterval();
                Integer iFileProcessingCount = entry.getFileProcessingCount();

                if (log.isDebugEnabled()) {
                    log.debug("File name pattern : " + entry.getFileNamePattern());
                }
                for (FileObject child : children) {
                    //skipping *.lock file
                    if (child.getName().getBaseName().endsWith(".lock")) {
                        continue;
                    }
                    boolean isFailedRecord = false;
                    if (entry.getMoveAfterMoveFailure() != null) {
                        isFailedRecord = isFailedRecord(child, entry);
                    }

                    if (entry.getFileNamePattern() != null
                            && child.getName().getBaseName().matches(entry.getFileNamePattern())) {
                        //child's file name matches the file name pattern
                        //now we try to get the lock and process
                        if (log.isDebugEnabled()) {
                            log.debug("Matching file : " + child.getName().getBaseName());
                        }

                        if ((!entry.isFileLockingEnabled()
                                || (entry.isFileLockingEnabled() && VFSUtils.acquireLock(fsManager, child)))
                                && !isFailedRecord) {
                            //process the file
                            try {
                                if (log.isDebugEnabled()) {
                                    log.debug("Processing file :" + child);
                                }
                                processCount++;
                                processFile(entry, child);
                                successCount++;
                                // tell moveOrDeleteAfterProcessing() file was success
                                entry.setLastPollState(PollTableEntry.SUCCSESSFUL);
                                metrics.incrementMessagesReceived();

                            } catch (Exception e) {
                                logException("Error processing File URI : " + child.getName(), e);
                                failCount++;
                                // tell moveOrDeleteAfterProcessing() file failed
                                entry.setLastPollState(PollTableEntry.FAILED);
                                metrics.incrementFaultsReceiving();
                            }
                            //skipping un-locking file if failed to do delete/move after process
                            boolean skipUnlock = false;
                            try {
                                moveOrDeleteAfterProcessing(entry, child);
                            } catch (AxisFault axisFault) {
                                logException(
                                        "File object '" + child.getURL().toString()
                                                + "'cloud not be moved, will remain in \"locked\" state",
                                        axisFault);
                                skipUnlock = true;
                                failCount++;
                                entry.setLastPollState(PollTableEntry.FAILED);
                                String timeStamp = VFSUtils
                                        .getSystemTime(entry.getFailedRecordTimestampFormat());
                                addFailedRecord(entry, child, timeStamp);
                            }
                            // if there is a failure or not we'll try to release the lock
                            if (entry.isFileLockingEnabled() && !skipUnlock) {
                                VFSUtils.releaseLock(fsManager, child);
                            }
                        }
                    } else if (entry.getFileNamePattern() != null
                            && !child.getName().getBaseName().matches(entry.getFileNamePattern())) {
                        //child's file name does not match the file name pattern
                        if (log.isDebugEnabled()) {
                            log.debug("Non-Matching file : " + child.getName().getBaseName());
                        }
                    } else if (isFailedRecord) {
                        //it is a failed record
                        if (entry.isFileLockingEnabled()) {
                            VFSUtils.releaseLock(fsManager, child);
                            VFSUtils.releaseLock(fsManager, fileObject);
                        }
                        if (fsManager.resolveFile(child.getURL().toString()) != null
                                && removeTaskState == STATE_STOPPED
                                && entry.getMoveAfterMoveFailure() != null) {
                            workerPool.execute(new FileRemoveTask(entry, child));
                        }
                        if (log.isDebugEnabled()) {
                            log.debug("File '" + fileObject.getURL()
                                    + "' has been marked as a failed record, it will not " + "process");
                        }
                    }

                    if (iFileProcessingInterval != null && iFileProcessingInterval > 0) {
                        try {
                            if (log.isDebugEnabled()) {
                                log.debug("Put the VFS processor to sleep for : " + iFileProcessingInterval);
                            }
                            Thread.sleep(iFileProcessingInterval);
                        } catch (InterruptedException ie) {
                            log.error("Unable to set the interval between file processors." + ie);
                        }
                    } else if (iFileProcessingCount != null && iFileProcessingCount <= processCount) {
                        break;
                    }
                }

                if (failCount == 0 && successCount > 0) {
                    entry.setLastPollState(PollTableEntry.SUCCSESSFUL);
                } else if (successCount == 0 && failCount > 0) {
                    entry.setLastPollState(PollTableEntry.FAILED);
                } else {
                    entry.setLastPollState(PollTableEntry.WITH_ERRORS);
                }
            }

            // processing of this poll table entry is complete
            long now = System.currentTimeMillis();
            entry.setLastPollTime(now);
            entry.setNextPollTime(now + entry.getPollInterval());

        } else if (log.isDebugEnabled()) {
            log.debug("Unable to access or read file or directory : " + VFSUtils.maskURLPassword(fileURI) + "."
                    + " Reason: "
                    + (fileObject.exists()
                            ? (fileObject.isReadable() ? "Unknown reason" : "The file can not be read!")
                            : "The file does not exists!"));
        }
        onPollCompletion(entry);
    } catch (FileSystemException e) {
        processFailure("Error checking for existence and readability : " + VFSUtils.maskURLPassword(fileURI), e,
                entry);
    }
}

From source file:org.aludratest.service.file.impl.AbstractFileAction.java

/** Lists all child elements of the given folder which match the filter.
 * @param filePath the path of the directory to read
 * @param filter a filter for choosing files
 * @return a List of all children that match the filter */
public List<String> getChildren(String filePath, FileFilter filter) {
    File.verifyFilePath(filePath);
    try {//ww w. ja  va2s . com
        FileObject parent = configuration.getFileObject(filePath);
        parent.refresh();
        FileObject[] candidates = parent.getChildren();
        List<String> filePaths = new ArrayList<String>();
        if (candidates != null) {
            for (FileObject candidate : candidates) {
                String path = configuration.pathFromRoot(candidate);
                FileInfo info = new FileInfoImpl(candidate, path);
                if (filter == null || filter.accept(info)) {
                    filePaths.add(pathFromRoot(candidate));
                }
            }
        }
        if (!filePaths.isEmpty()) {
            logger.debug("Found children: {}", filePaths);
        } else {
            logger.debug("No children found for filter {}", filter);
        }
        return filePaths;
    } catch (IOException e) {
        throw new TechnicalException("Error retrivieving child objects", e);
    }
}

From source file:org.aludratest.service.file.impl.AbstractFileAction.java

/** Lists FileInfos for all child elements of the given folder which match the filter.
 * @param dirPath the file path of the directory
 * @param filter the file filter to apply
 * @return a list of the directory's child items that match the filter */
protected List<FileInfo> getChildInfos(String dirPath, FileFilter filter) {
    File.verifyFilePath(dirPath);
    try {//from w  ww  . j av  a2  s .  c  om
        FileObject parent = configuration.getFileObject(dirPath);
        parent.refresh();
        FileObject[] candidates = parent.getChildren();
        List<FileInfo> infos = new ArrayList<FileInfo>();
        if (candidates != null) {
            for (FileObject candidate : candidates) {
                FileInfo fileInfo = new FileInfoImpl(candidate, configuration.pathFromRoot(candidate));
                if (filter == null || filter.accept(fileInfo)) {
                    infos.add(fileInfo);
                }
            }
        }
        return infos;
    } catch (IOException e) {
        throw new TechnicalException("Error retrivieving child objects", e);
    }
}

From source file:org.aludratest.service.file.impl.FileActionImpl.java

/** Lists all child elements of the given folder which match the filter. */
@Override/*from  w  ww . j  av  a  2s  . c  om*/
public List<String> getChildren(String filePath, FileFilter filter) {
    FileUtil.verifyFilePath(filePath);
    try {
        FileObject parent = configuration.getFileObject(filePath);
        parent.refresh();
        FileObject[] candidates = parent.getChildren();
        List<String> filePaths = new ArrayList<String>();
        if (candidates != null) {
            for (FileObject candidate : candidates) {
                if (filter == null || filter.accept(new FileInfo(candidate))) {
                    filePaths.add(pathFromRoot(candidate));
                }
            }
        }
        if (filePaths.size() > 0) {
            LOGGER.info("Found children: {}", filePaths);
        } else {
            LOGGER.info("No children found for filter {}", filter);
        }
        return filePaths;
    } catch (FileSystemException e) {
        throw new TechnicalException("Error retrivieving child objects", e);
    }
}

From source file:org.apache.accumulo.start.classloader.vfs.AccumuloReloadingVFSClassLoaderTest.java

@Test
public void testConstructor() throws Exception {
    FileObject testDir = vfs.resolveFile(folder1.getRoot().toURI().toString());
    FileObject[] dirContents = testDir.getChildren();

    AccumuloReloadingVFSClassLoader arvcl = new AccumuloReloadingVFSClassLoader(folderPath, vfs,
            new ReloadingClassLoader() {
                @Override/*from  w w  w . j a v  a  2 s . c  o  m*/
                public ClassLoader getClassLoader() {
                    return ClassLoader.getSystemClassLoader();
                }
            }, true);

    VFSClassLoader cl = (VFSClassLoader) arvcl.getClassLoader();

    FileObject[] files = cl.getFileObjects();
    Assert.assertArrayEquals(createFileSystems(dirContents), files);

    arvcl.close();
}

From source file:org.apache.accumulo.start.classloader.vfs.AccumuloReloadingVFSClassLoaderTest.java

@Test
public void testReloading() throws Exception {
    FileObject testDir = vfs.resolveFile(folder1.getRoot().toURI().toString());
    FileObject[] dirContents = testDir.getChildren();

    AccumuloReloadingVFSClassLoader arvcl = new AccumuloReloadingVFSClassLoader(folderPath, vfs,
            new ReloadingClassLoader() {
                @Override/*from  w w  w  .  java2  s. co  m*/
                public ClassLoader getClassLoader() {
                    return ClassLoader.getSystemClassLoader();
                }
            }, 1000, true);

    FileObject[] files = ((VFSClassLoader) arvcl.getClassLoader()).getFileObjects();
    Assert.assertArrayEquals(createFileSystems(dirContents), files);

    Class<?> clazz1 = arvcl.getClassLoader().loadClass("test.HelloWorld");
    Object o1 = clazz1.newInstance();
    Assert.assertEquals("Hello World!", o1.toString());

    // Check that the class is the same before the update
    Class<?> clazz1_5 = arvcl.getClassLoader().loadClass("test.HelloWorld");
    Assert.assertEquals(clazz1, clazz1_5);

    assertTrue(new File(folder1.getRoot(), "HelloWorld.jar").delete());

    // VFS-487 significantly wait to avoid failure
    Thread.sleep(7000);

    // Update the class
    FileUtils.copyURLToFile(this.getClass().getResource("/HelloWorld.jar"), folder1.newFile("HelloWorld2.jar"));

    // Wait for the monitor to notice
    // VFS-487 significantly wait to avoid failure
    Thread.sleep(7000);

    Class<?> clazz2 = arvcl.getClassLoader().loadClass("test.HelloWorld");
    Object o2 = clazz2.newInstance();
    Assert.assertEquals("Hello World!", o2.toString());

    // This is false because they are loaded by a different classloader
    Assert.assertFalse(clazz1.equals(clazz2));
    Assert.assertFalse(o1.equals(o2));

    arvcl.close();
}

From source file:org.apache.accumulo.start.classloader.vfs.AccumuloReloadingVFSClassLoaderTest.java

@Test
@Ignore/* w ww.  j  av  a2  s. co m*/
public void testFastDeleteAndReAdd() throws Exception {
    FileObject testDir = vfs.resolveFile(folder1.getRoot().toURI().toString());
    FileObject[] dirContents = testDir.getChildren();

    AccumuloReloadingVFSClassLoader arvcl = new AccumuloReloadingVFSClassLoader(folderPath, vfs,
            new ReloadingClassLoader() {

                @Override
                public ClassLoader getClassLoader() {
                    return ClassLoader.getSystemClassLoader();
                }
            }, 1000, true);

    FileObject[] files = ((VFSClassLoader) arvcl.getClassLoader()).getFileObjects();
    Assert.assertArrayEquals(createFileSystems(dirContents), files);

    Class<?> clazz1 = arvcl.getClassLoader().loadClass("test.HelloWorld");
    Object o1 = clazz1.newInstance();
    Assert.assertEquals("Hello World!", o1.toString());

    // Check that the class is the same before the update
    Class<?> clazz1_5 = arvcl.getClassLoader().loadClass("test.HelloWorld");
    Assert.assertEquals(clazz1, clazz1_5);

    assertTrue(new File(folder1.getRoot(), "HelloWorld.jar").delete());

    // Update the class
    FileUtils.copyURLToFile(this.getClass().getResource("/HelloWorld.jar"), folder1.newFile("HelloWorld.jar"));

    // Wait for the monitor to notice
    // VFS-487 significantly wait to avoid failure
    Thread.sleep(7000);

    Class<?> clazz2 = arvcl.getClassLoader().loadClass("test.HelloWorld");
    Object o2 = clazz2.newInstance();
    Assert.assertEquals("Hello World!", o2.toString());

    // This is false because they are loaded by a different classloader
    Assert.assertFalse(clazz1.equals(clazz2));
    Assert.assertFalse(o1.equals(o2));

    arvcl.close();
}

From source file:org.apache.accumulo.start.classloader.vfs.AccumuloReloadingVFSClassLoaderTest.java

@Test
@Ignore//  w w w.  ja va2  s  .  c  o  m
public void testModifiedClass() throws Exception {

    FileObject testDir = vfs.resolveFile(folder1.getRoot().toURI().toString());
    FileObject[] dirContents = testDir.getChildren();

    AccumuloReloadingVFSClassLoader arvcl = new AccumuloReloadingVFSClassLoader(folderPath, vfs,
            new ReloadingClassLoader() {
                @Override
                public ClassLoader getClassLoader() {
                    return ClassLoader.getSystemClassLoader();
                }
            }, 1000, true);

    FileObject[] files = ((VFSClassLoader) arvcl.getClassLoader()).getFileObjects();
    Assert.assertArrayEquals(createFileSystems(dirContents), files);

    ClassLoader loader1 = arvcl.getClassLoader();
    Class<?> clazz1 = loader1.loadClass("test.HelloWorld");
    Object o1 = clazz1.newInstance();
    Assert.assertEquals("Hello World!", o1.toString());

    // Check that the class is the same before the update
    Class<?> clazz1_5 = arvcl.getClassLoader().loadClass("test.HelloWorld");
    Assert.assertEquals(clazz1, clazz1_5);

    // java does aggressive caching of jar files. When using java code to read jar files that are created in the same second, it will only see the first jar
    // file
    Thread.sleep(1000);

    assertTrue(new File(folder1.getRoot(), "HelloWorld.jar").delete());

    // Update the class
    FileUtils.copyURLToFile(this.getClass().getResource("/HelloWorld2.jar"), folder1.newFile("HelloWorld.jar"));

    // Wait for the monitor to notice
    // VFS-487 significantly wait to avoid failure
    Thread.sleep(7000);

    Class<?> clazz2 = arvcl.getClassLoader().loadClass("test.HelloWorld");
    Object o2 = clazz2.newInstance();
    Assert.assertEquals("Hallo Welt", o2.toString());

    // This is false because they are loaded by a different classloader
    Assert.assertFalse(clazz1.equals(clazz2));
    Assert.assertFalse(o1.equals(o2));

    Class<?> clazz3 = loader1.loadClass("test.HelloWorld");
    Object o3 = clazz3.newInstance();
    Assert.assertEquals("Hello World!", o3.toString());

    arvcl.close();
}

From source file:org.apache.accumulo.start.classloader.vfs.ContextManagerTest.java

@Test
public void differentContexts() throws Exception {

    ContextManager cm = new ContextManager(vfs, new ReloadingClassLoader() {
        @Override//w  w w  . j ava 2  s  .com
        public ClassLoader getClassLoader() {
            return ClassLoader.getSystemClassLoader();
        }
    });

    cm.setContextConfig(new ContextsConfig() {
        @Override
        public ContextConfig getContextConfig(String context) {
            if (context.equals("CX1")) {
                return new ContextConfig(uri1, true);
            } else if (context.equals("CX2")) {
                return new ContextConfig(uri2, true);
            }
            return null;
        }
    });

    FileObject testDir = vfs.resolveFile(folder1.getRoot().toURI().toString());
    FileObject[] dirContents = testDir.getChildren();
    ClassLoader cl1 = cm.getClassLoader("CX1");
    FileObject[] files = ((VFSClassLoader) cl1).getFileObjects();
    Assert.assertArrayEquals(createFileSystems(dirContents), files);

    FileObject testDir2 = vfs.resolveFile(folder2.getRoot().toURI().toString());
    FileObject[] dirContents2 = testDir2.getChildren();
    ClassLoader cl2 = cm.getClassLoader("CX2");
    FileObject[] files2 = ((VFSClassLoader) cl2).getFileObjects();
    Assert.assertArrayEquals(createFileSystems(dirContents2), files2);

    Class<?> defaultContextClass = cl1.loadClass("test.HelloWorld");
    Object o1 = defaultContextClass.newInstance();
    Assert.assertEquals("Hello World!", o1.toString());

    Class<?> myContextClass = cl2.loadClass("test.HelloWorld");
    Object o2 = myContextClass.newInstance();
    Assert.assertEquals("Hello World!", o2.toString());

    Assert.assertFalse(defaultContextClass.equals(myContextClass));

    cm.removeUnusedContexts(new HashSet<String>());
}