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

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

Introduction

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

Prototype

FileObject getParent() throws FileSystemException;

Source Link

Document

Returns the folder that contains this file.

Usage

From source file:pl.otros.vfs.browser.util.VFSUtils.java

/**
 * Returns whether a folder contains a given file
 *
 * @param folder A folder/*from   w  ww.ja v a 2  s . c o  m*/
 * @param file   A file
 * @return whether a folder contains a given file
 */
public static boolean isParent(FileObject folder, FileObject file) {
    try {
        FileObject parent = file.getParent();

        return parent != null && parent.equals(folder);

    } catch (FileSystemException ex) {
        return false;
    }
}

From source file:pl.otros.vfs.browser.VfsBrowser.java

License:asdf

/**
 * Caller responsible for ensuring called from EDT.
 * We purposefully do not update UI other than indirectly reflecting
 * the selection.//  w w w. jav a2 s . c  o m
 * Current use case is that we are finished with the browser window.
 */
private void loadAndSelSingleFile(FileObject fileObject) throws FileSystemException {
    vfsTableModel.setContent(new FileObject[] { new ParentFileObject(fileObject.getParent()), fileObject, });
    tableFiles.getSelectionModel().setSelectionInterval(1, 1);
}

From source file:pt.webdetails.cpk.utils.ZipUtil.java

private FileName getTopFileName(List<FileObject> files) {
    FileName topFileName = null;/*  w  w w .j ava  2 s .c om*/
    try {
        if (!files.isEmpty()) {
            topFileName = files.get(0).getParent().getName();
        }
        for (FileObject file : files) {
            logger.debug(file.getParent().getName().getPath());
            FileName myFileName = file.getParent().getName();
            if (topFileName.getURI().length() > myFileName.getURI().length()) {
                topFileName = myFileName;
            }
        }
    } catch (Exception exception) {
        logger.error(exception);
    }
    return topFileName;
}

From source file:sf.net.experimaestro.connectors.UnixScriptProcessBuilder.java

@Override
final public XPMProcess start() throws LaunchException, IOException {
    final FileObject runFile = connector.resolveFile(path);
    final FileObject basepath = runFile.getParent();
    final String baseName = runFile.getName().getBaseName();

    try (CommandContext env = new CommandContext.FolderContext(connector, basepath, baseName)) {
        // Prepare the commands
        commands().prepare(env);//from ww  w  . j a va 2s  . c  o m

        // First generate the run file
        PrintWriter writer = new PrintWriter(runFile.getContent().getOutputStream());

        writer.format("#!%s%n", shPath);

        writer.format("# Experimaestro generated task: %s%n", path);
        writer.println();

        // A command fails if any of the piped commands fail
        writer.println("set -o pipefail");
        writer.println();

        writer.println();
        if (environment() != null) {
            for (Map.Entry<String, String> pair : environment().entrySet())
                writer.format("export %s=\"%s\"%n", pair.getKey(), protect(pair.getValue(), QUOTED_SPECIAL));
        }

        if (directory() != null) {
            writer.format("cd \"%s\"%n", protect(env.resolve(directory()), QUOTED_SPECIAL));
        }

        if (!lockFiles.isEmpty()) {
            writer.format("%n# Checks that the locks are set%n");
            for (String lockFile : lockFiles) {
                writer.format("test -f %s || exit 017%n", lockFile);
            }
        }

        writer.format(
                "%n%n# Set traps to cleanup (remove locks and temporary files, kill remaining processes) when exiting%n%n");
        writer.format("trap cleanup EXIT SIGINT SIGTERM%n");
        writer.format("cleanup() {%n");
        for (String file : lockFiles) {
            writer.format("  rm -f %s;%n", file);
        }

        commands().forEachCommand(Streams.propagate(c -> {
            final CommandContext.NamedPipeRedirections namedRedirections = env.getNamedRedirections(c, false);
            for (FileObject file : Iterables.concat(namedRedirections.outputRedirections,
                    namedRedirections.errorRedirections)) {
                writer.format("  rm -f %s;%n", env.resolve(file));
            }
        }));

        // Kills remaining processes
        writer.println("  jobs -pr | xargs kill");
        writer.format("}%n%n");

        // Write the command
        final StringWriter sw = new StringWriter();
        PrintWriter exitWriter = new PrintWriter(sw);
        exitWriter.format("code=$?; if test $code -ne 0; then%n");
        if (exitCodePath != null)
            exitWriter.format(" echo $code > \"%s\"%n", protect(exitCodePath, QUOTED_SPECIAL));
        exitWriter.format(" exit $code%n");
        exitWriter.format("fi%n");

        String exitScript = sw.toString();

        writer.format("%n%n");

        switch (input.type()) {
        case INHERIT:
            break;
        case READ:
            writer.format("cat \"%s\" | ", connector.resolve(input.file()));
            break;
        default:
            throw new UnsupportedOperationException("Unsupported input redirection type: " + input.type());
        }

        writer.println("(");

        // The prepare all the commands
        writeCommands(env, writer, commands());

        writer.print(") ");

        writeRedirection(writer, output, 1);
        writeRedirection(writer, error, 2);

        writer.println();
        writer.print(exitScript);

        if (exitCodePath != null)
            writer.format("echo 0 > \"%s\"%n", protect(exitCodePath, QUOTED_SPECIAL));
        if (donePath != null)
            writer.format("touch \"%s\"%n", protect(donePath, QUOTED_SPECIAL));

        writer.close();

        // Set the file as executable
        runFile.setExecutable(true, false);

        processBuilder.command(protect(path, SHELL_SPECIAL));

        processBuilder.detach(true);
        processBuilder.redirectOutput(output);
        processBuilder.redirectError(error);

        processBuilder.job(job);

        return processBuilder.start();
    } catch (Exception e) {
        throw new LaunchException(e);
    }

}

From source file:sf.net.experimaestro.manager.js.JSFileObject.java

@JSHelp(value = "Get the n<sup>th</sup> ancestor of this file object", arguments = @JSArguments(@JSArgument(type = "Integer", name = "levels")))
@JSFunction("get_ancestor")
public JSFileObject get_ancestor(int level) throws FileSystemException {
    if (level < 0)
        throw new IllegalArgumentException("Level is negative (" + level + ")");

    FileObject ancestor = this.file;
    while (--level >= 0)
        ancestor = ancestor.getParent();

    return new JSFileObject(ancestor);
}

From source file:sf.net.experimaestro.scheduler.ResourceLocator.java

/**
 * Resolve a path relative to this resource
 *
 * @param path   The path/*from  w w w.j a  va 2s . c o m*/
 * @param parent Is the path relative to the parent?
 * @return A new resource from object
 * @throws FileSystemException If something goes wrong while accessing the file system
 */
public ResourceLocator resolvePath(String path, boolean parent) throws FileSystemException {
    // Get the current file
    FileObject file = getFile();

    // Get the target (revert to file if null)
    FileObject target = (parent ? file.getParent() : file);
    if (target == null)
        target = file;

    target = target.resolveFile(path);
    return new ResourceLocator(connector, target.getName().getPath());
}