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

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

Introduction

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

Prototype

public boolean exists() throws FileSystemException;

Source Link

Document

Determines if this file exists.

Usage

From source file:org.rzo.yajsw.config.YajswConfigurationImpl.java

private boolean fileExists(String file) {
    try {//  w ww.j av  a 2s.  c o m
        String current = System.getProperty("javax.xml.parsers.DocumentBuilderFactory");
        System.setProperty("javax.xml.parsers.DocumentBuilderFactory",
                "com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl");
        DefaultFileSystemManager fsManager = (DefaultFileSystemManager) VFS.getManager();
        if (current != null)
            System.setProperty("javax.xml.parsers.DocumentBuilderFactory", current);
        else
            System.clearProperty("javax.xml.parsers.DocumentBuilderFactory");
        FileObject f = VFSUtils.resolveFile(".", file);
        return f.exists();
    } catch (FileSystemException e) {
        e.printStackTrace();
        return false;
    }
}

From source file:org.saiku.adhoc.providers.impl.standalone.StandaloneCdaProvider.java

public void setPath(String path) {

    FileSystemManager fileSystemManager;
    try {/*from  w  w  w.  j a v a  2  s  .com*/
        fileSystemManager = VFS.getManager();

        FileObject fileObject;
        fileObject = fileSystemManager.resolveFile(path);
        if (fileObject == null) {
            throw new IOException("File cannot be resolved: " + path);
        }
        if (!fileObject.exists()) {
            throw new IOException("File does not exist: " + path);
        }
        repoURL = fileObject.getURL();
        if (repoURL == null) {
            throw new Exception("Cannot load connection repository from path: " + path);
        } else {
            load();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

}

From source file:org.saiku.service.importer.impl.LegacyImporterImpl.java

private void setPath(String path) {

    FileSystemManager fileSystemManager;
    try {//w ww .  ja  v a2 s  .co  m
        fileSystemManager = VFS.getManager();

        FileObject fileObject;
        fileObject = fileSystemManager.resolveFile(path);
        if (fileObject == null) {
            throw new IOException("File cannot be resolved: " + path);
        }
        if (!fileObject.exists()) {
            throw new IOException("File does not exist: " + path);
        }
        repoURL = fileObject.getURL();
        if (repoURL == null) {
            throw new Exception("Cannot load connection repository from path: " + path);
        } else {
            //load();
        }
    } catch (Exception e) {
        LOG.error("Exception", e);
    }

}

From source file:org.saiku.web.rest.resources.BasicRepositoryResource.java

public void setPath(String path) throws Exception {

    FileSystemManager fileSystemManager;
    try {/*w  w  w. j  a  va 2s .  c o m*/
        if (!path.endsWith("" + File.separatorChar)) {
            path += File.separatorChar;
        }
        fileSystemManager = VFS.getManager();
        FileObject fileObject;
        fileObject = fileSystemManager.resolveFile(path);
        if (fileObject == null) {
            throw new IOException("File cannot be resolved: " + path);
        }
        if (!fileObject.exists()) {
            throw new IOException("File does not exist: " + path);
        }
        repo = fileObject;
    } catch (Exception e) {
        e.printStackTrace();
    }

}

From source file:org.saiku.web.rest.resources.BasicRepositoryResource2.java

public void setPath(String path) throws Exception {
    FileSystemManager fileSystemManager;
    try {//from  ww w  .j  a  va 2  s .c  o m
        if (!path.endsWith("" + File.separatorChar)) {
            path += File.separatorChar;
        }
        fileSystemManager = VFS.getManager();
        FileObject fileObject;
        fileObject = fileSystemManager.resolveFile(path);
        if (fileObject == null) {
            throw new IOException("File cannot be resolved: " + path);
        }
        if (!fileObject.exists()) {
            throw new IOException("File does not exist: " + path);
        }
        repo = fileObject;
    } catch (Exception e) {
        log.error("Error setting path for repository: " + path, e);
    }
}

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  . ja  v a 2 s  .c  o  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.CopyCommand.java

public Object execute(final CommandContext context) throws Exception {
    assert context != null;
    IO io = context.getIo();/* w w  w  .j a  v a 2s. co  m*/

    FileObject source = resolveFile(context, sourcePath);
    FileObject target = resolveFile(context, targetPath);

    new FileObjectAssert(source).exists();

    // TODO: Validate more

    if (target.exists() && target.getType().hasChildren()) {
        target = target.resolveFile(source.getName().getBaseName());
    }

    log.info("Copying {} -> {}", source, target);

    target.copyFrom(source, Selectors.SELECT_ALL);

    FileObjects.close(source, target);

    return Result.SUCCESS;
}

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

public Object execute(final CommandContext context) throws Exception {
    assert context != null;
    IO io = context.getIo();/*  w w  w.  j a  v  a 2 s . c om*/

    FileObject file = resolveFile(context, path);

    io.println("URL: {}", file.getURL());
    io.println("Name: {}", file.getName());
    io.println("BaseName: {}", file.getName().getBaseName());
    io.println("Extension: {}", file.getName().getExtension());
    io.println("Path: {}", file.getName().getPath());
    io.println("Scheme: {}", file.getName().getScheme());
    io.println("URI: {}", file.getName().getURI());
    io.println("Root URI: {}", file.getName().getRootURI());
    io.println("Parent: {}", file.getName().getParent());
    io.println("Type: {}", file.getType());
    io.println("Exists: {}", file.exists());
    io.println("Readable: {}", file.isReadable());
    io.println("Writeable: {}", file.isWriteable());
    io.println("Root path: {}", file.getFileSystem().getRoot().getName().getPath());

    if (file.exists()) {
        FileContent content = file.getContent();
        FileContentInfo contentInfo = content.getContentInfo();
        io.println("Content type: {}", contentInfo.getContentType());
        io.println("Content encoding: {}", contentInfo.getContentEncoding());

        try {
            // noinspection unchecked
            Map<String, Object> attrs = content.getAttributes();
            if (attrs != null && !attrs.isEmpty()) {
                io.println("Attributes:");
                for (Map.Entry<String, Object> entry : attrs.entrySet()) {
                    io.println("    {}='{}'", entry.getKey(), entry.getValue());
                }
            }
        } catch (FileSystemException e) {
            io.println("File attributes are NOT supported");
        }

        try {
            Certificate[] certs = content.getCertificates();
            if (certs != null && certs.length != 0) {
                io.println("Certificate:");
                for (Certificate cert : certs) {
                    io.println("    {}", cert);
                }
            }
        } catch (FileSystemException e) {
            io.println("File certificates are NOT supported");
        }

        if (file.getType().equals(FileType.FILE)) {
            io.println("Size: {} bytes", content.getSize());
        } else if (file.getType().hasChildren() && file.isReadable()) {
            FileObject[] children = file.getChildren();
            io.println("Directory with {} files", children.length);

            for (int iterChildren = 0; iterChildren < children.length; iterChildren++) {
                io.println("#{}:{}", iterChildren, children[iterChildren].getName());
                if (iterChildren > 5) {
                    break;
                }
            }
        }

        io.println("Last modified: {}",
                DateFormat.getInstance().format(new Date(content.getLastModifiedTime())));
    } else {
        io.println("The file does not exist");
    }

    FileObjects.close(file);

    return Result.SUCCESS;
}

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

public Object execute(final CommandContext context) throws Exception {
    assert context != null;

    FileObject file = resolveFile(context, path);

    try {/*from w  w w  .  j  a v a  2 s. com*/
        if (!file.exists()) {
            file.createFile();
        }

        file.getContent().setLastModifiedTime(System.currentTimeMillis());
    } finally {
        FileObjects.close(file);
    }

    return Result.SUCCESS;
}

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

public void setCurrentDirectory(final Variables vars, final FileObject dir) throws FileSystemException {
    assert vars != null;
    assert dir != null;

    log.trace("Setting CWD: {}", dir);

    // Make sure that the given file object exists and is really a directory
    if (!dir.exists()) {
        throw new RuntimeException("Directory not found: " + dir.getName());
    } else if (!dir.getType().hasChildren()) {
        throw new RuntimeException("File can not contain children: " + dir.getName());
    }/*  w  w w . jav a 2  s .c  om*/

    vars.parent().set(CWD, dir);
}