Example usage for org.apache.commons.vfs FileObject close

List of usage examples for org.apache.commons.vfs FileObject close

Introduction

In this page you can find the example usage for org.apache.commons.vfs FileObject close.

Prototype

public void close() throws FileSystemException;

Source Link

Document

Closes this file, and its content.

Usage

From source file:org.sonatype.gshell.commands.bsf.ScriptCommand.java

private Object exec(final CommandContext context) throws Exception {
    assert context != null;
    IO io = context.getIo();//from   ww w  .  j  a v  a2s  .  co m

    FileObject cwd = fileSystemAccess.getCurrentDirectory(context.getVariables());
    FileObject file = fileSystemAccess.resolveFile(cwd, path);

    if (!file.exists()) {
        io.error("File not found: {}", file.getName()); // TODO: i18n
        return Result.FAILURE;
    } else if (!file.getType().hasContent()) {
        io.error("File has not content: {}", file.getName()); // TODO: i18n
        return Result.FAILURE;
    } else if (!file.isReadable()) {
        io.error("File is not readable: {}", file.getName()); // TODO: i18n
        return Result.FAILURE;
    }

    if (language == null) {
        language = detectLanguage(file);
    }

    BSFEngine engine = createEngine(context);

    byte[] bytes = FileUtil.getContent(file);
    String script = new String(bytes);

    log.info("Evaluating file ({}): {}", language, path); // TODO: i18n

    try {
        return engine.eval(file.getName().getBaseName(), 1, 1, script);
    } finally {
        engine.terminate();
        file.close();
    }
}

From source file:org.sonatype.gshell.commands.vfs.ListDirectoryCommand.java

private void listChildren(final IO io, final FileObject dir) throws Exception {
    assert io != null;
    assert dir != null;

    FileObject[] files;//from  w  w w  .j a va2 s .  c o m

    if (includeHidden) {
        files = dir.getChildren();
    } else {
        FileFilter filter = new FileFilter() {
            public boolean accept(final FileSelectInfo selection) {
                assert selection != null;

                try {
                    return !selection.getFile().isHidden();
                } catch (FileSystemException e) {
                    throw new RuntimeException(e);
                }
            }
        };

        files = dir.findFiles(new FileFilterSelector(filter));
    }

    ConsoleReader reader = new ConsoleReader(io.streams.in, io.out, io.getTerminal());

    reader.setPaginationEnabled(false);

    List<String> names = new ArrayList<String>(files.length);
    List<FileObject> dirs = new LinkedList<FileObject>();

    for (FileObject file : files) {
        String fileName = file.getName().getBaseName();

        if (FileObjects.hasChildren(file)) {
            fileName += FileName.SEPARATOR;

            if (recursive) {
                dirs.add(file);
            }
        }

        names.add(fileName);

        file.close();
    }

    if (longList) {
        for (String name : names) {
            io.out.println(name);
        }
    } else {
        reader.printColumns(names);
    }

    if (!dirs.isEmpty()) {
        for (FileObject subdir : dirs) {
            io.out.println();
            io.out.print(subdir.getName().getBaseName());
            io.out.print(":");
            listChildren(io, subdir);
        }
    }

    dir.close();
}

From source file:org.sonatype.gshell.vfs.FileObjects.java

public static void close(final FileObject... files) {
    if (files != null && files.length != 0) {
        for (FileObject file : files) {
            if (file != null) {
                try {
                    file.close();
                } catch (FileSystemException e) {
                    log.trace("Failed to close file: " + file, e);
                }//w ww  .j a  va 2s  .  c om
            }
        }
    }
}

From source file:plugin.games.data.trans.step.fileoutput.TextFileOutput.java

private void createParentFolder(String filename) throws Exception {
    // Check for parent folder
    FileObject parentfolder = null;
    try {//from   w ww . j a v a 2  s.c om
        // Get parent folder
        parentfolder = KettleVFS.getFileObject(filename).getParent();
        if (parentfolder.exists()) {
            if (isDetailed())
                logDetailed(BaseMessages.getString(PKG, "TextFileOutput.Log.ParentFolderExist",
                        parentfolder.getName()));
        } else {
            if (isDetailed())
                logDetailed(BaseMessages.getString(PKG, "TextFileOutput.Log.ParentFolderNotExist",
                        parentfolder.getName()));
            if (meta.isCreateParentFolder()) {
                parentfolder.createFolder();
                if (isDetailed())
                    logDetailed(BaseMessages.getString(PKG, "TextFileOutput.Log.ParentFolderCreated",
                            parentfolder.getName()));
            } else {
                throw new KettleException(BaseMessages.getString(PKG,
                        "TextFileOutput.Log.ParentFolderNotExistCreateIt", parentfolder.getName(), filename));
            }
        }
    } finally {
        if (parentfolder != null) {
            try {
                parentfolder.close();
            } catch (Exception ex) {
            }
            ;
        }
    }
}

From source file:unitTests.dataspaces.VFSFactoryTest.java

@Test
public void testLocalFileProvider() throws Exception {
    FileObject fo = null;
    try {/*from   ww w . ja va2 s  .  c o m*/
        fo = manager.resolveFile(testFile.getCanonicalPath());
        assertTrue(fo.exists());

        final InputStream ios = fo.getContent().getInputStream();
        final BufferedReader reader = new BufferedReader(new InputStreamReader(ios));
        assertEquals("test", reader.readLine());
    } finally {
        if (fo != null)
            fo.close();
    }
}