Example usage for org.apache.commons.vfs FileSystemManager resolveFile

List of usage examples for org.apache.commons.vfs FileSystemManager resolveFile

Introduction

In this page you can find the example usage for org.apache.commons.vfs FileSystemManager resolveFile.

Prototype

public FileObject resolveFile(File baseFile, String name) throws FileSystemException;

Source Link

Document

Locates a file by name.

Usage

From source file:egovframework.rte.fdl.filehandling.EgovFileUtil.java

/**
 * <p>//from  w  w w. j  a va2 s.  co m
 * ? ? ? .
 * </p>
 * @param filepath
 *        <code>String</code>
 * @return ? ?
 * @throws Exception
 */
public static FileObject getFileObject(final String filepath) throws Exception {
    FileSystemManager mgr = VFS.getManager();

    return mgr.resolveFile(mgr.resolveFile(System.getProperty("user.dir")), filepath);
}

From source file:com.learningobjects.community.abgm.logic.ControllerJob.java

private void download(String sourceUrl, File destinationFile) throws IOException {
    logger.log(Level.INFO, "Downloading or copying file from " + sourceUrl + " to " + destinationFile);
    FileObject sourceFileObject = null;
    OutputStream outputStream = null;
    try {//from ww w.  j a v  a 2 s  .  c  o m
        // special case sftp so that new hosts work out of the box. other options could go here too
        SftpFileSystemConfigBuilder sftpFileSystemConfigBuilder = SftpFileSystemConfigBuilder.getInstance();
        FileSystemOptions fileSystemOptions = new FileSystemOptions();
        sftpFileSystemConfigBuilder.setStrictHostKeyChecking(fileSystemOptions, "no");
        // actually try to get the file
        FileSystemManager fsManager = VFS.getManager();
        sourceFileObject = fsManager.resolveFile(sourceUrl, fileSystemOptions);
        FileContent sourceFileContent = sourceFileObject.getContent();
        InputStream inputStream = sourceFileContent.getInputStream();
        outputStream = new FileOutputStream(destinationFile);
        // do the copy - this is probably a dupe of commons io, and many others
        byte[] buffer = new byte[8192];
        int length;
        while ((length = inputStream.read(buffer)) > 0) {
            outputStream.write(buffer, 0, length);
        }
    } finally {
        if (outputStream != null) {
            outputStream.close();
        }
        if (sourceFileObject != null) {
            sourceFileObject.close(); // this will close the fileContent object, too
        }
    }
}

From source file:com.panet.imeta.core.plugins.PluginLoader.java

/**
 * This method does the actual plugin configuration and should be called
 * after load()/*from   www  . java 2 s.com*/
 * 
 * @return a collection containing the <code>JobPlugin</code> objects
 *         loaded.
 * @throws KettleConfigException
 */
private void doConfig() throws KettleConfigException {
    synchronized (locs) {
        String sjar = "." + JAR;

        for (PluginLocation plugin : locs) {
            // check to see if the resource type is present
            File base = new File(System.getProperty("user.dir"));
            try {
                FileSystemManager mgr = VFS.getManager();
                FileObject fobj = mgr.resolveFile(base, plugin.getLocation());
                if (fobj.isReadable()) {
                    String name = fobj.getName().getURI();
                    int jindex = name.indexOf(sjar);
                    int nlen = name.length();
                    boolean isJar = jindex == nlen - 4 || jindex == nlen - 6;

                    try {

                        if (isJar)
                            build(fobj, true);
                        else {
                            // loop thru folder
                            for (FileObject childFolder : fobj.getChildren()) {
                                boolean isAlsoJar = childFolder.getName().getURI().endsWith(sjar);

                                // ignore anything that is not a folder or a
                                // jar
                                if (!isAlsoJar && childFolder.getType() != FileType.FOLDER) {
                                    continue;
                                }

                                // ignore any subversion or CVS directories
                                if (childFolder.getName().getBaseName().equalsIgnoreCase(".svn")) {
                                    continue;
                                } else if (childFolder.getName().getBaseName().equalsIgnoreCase(".cvs")) {
                                    continue;
                                }
                                try {
                                    build(childFolder, isAlsoJar);
                                } catch (KettleConfigException e) {
                                    log.logError(Plugin.PLUGIN_LOADER, e.getMessage());
                                    continue;
                                }
                            }

                        }

                    } catch (FileSystemException e) {
                        log.logError(Plugin.PLUGIN_LOADER, e.getMessage());
                        continue;
                    } catch (KettleConfigException e) {
                        log.logError(Plugin.PLUGIN_LOADER, e.getMessage());
                        continue;
                    }

                } else {
                    log.logDebug(Plugin.PLUGIN_LOADER, fobj + " does not exist, ignoring this.");
                }
            } catch (Exception e) {
                throw new KettleConfigException(e);
            }

        }
    }
}

From source file:mondrian.spi.impl.ApacheVfsVirtualFileHandler.java

public InputStream readVirtualFile(String url) throws FileSystemException {
    // Treat catalogUrl as an Apache VFS (Virtual File System) URL.
    // VFS handles all of the usual protocols (http:, file:)
    // and then some.
    FileSystemManager fsManager = VFS.getManager();
    if (fsManager == null) {
        throw Util.newError("Cannot get virtual file system manager");
    }/*from   w  w w .  j  ava  2 s  .  c  o m*/

    // Workaround VFS bug.
    if (url.startsWith("file://localhost")) {
        url = url.substring("file://localhost".length());
    }
    if (url.startsWith("file:")) {
        url = url.substring("file:".length());
    }

    //work around for VFS bug not closing http sockets
    // (Mondrian-585)
    if (url.startsWith("http")) {
        try {
            return new URL(url).openStream();
        } catch (IOException e) {
            throw Util.newError("Could not read URL: " + url);
        }
    }

    File userDir = new File("").getAbsoluteFile();
    FileObject file = fsManager.resolveFile(userDir, url);
    FileContent fileContent = null;
    try {
        // Because of VFS caching, make sure we refresh to get the latest
        // file content. This refresh may possibly solve the following
        // workaround for defect MONDRIAN-508, but cannot be tested, so we
        // will leave the work around for now.
        file.refresh();

        // Workaround to defect MONDRIAN-508. For HttpFileObjects, verifies
        // the URL of the file retrieved matches the URL passed in.  A VFS
        // cache bug can cause it to treat URLs with different parameters
        // as the same file (e.g. http://blah.com?param=A,
        // http://blah.com?param=B)
        if (file instanceof HttpFileObject && !file.getName().getURI().equals(url)) {
            fsManager.getFilesCache().removeFile(file.getFileSystem(), file.getName());

            file = fsManager.resolveFile(userDir, url);
        }

        if (!file.isReadable()) {
            throw Util.newError("Virtual file is not readable: " + url);
        }

        fileContent = file.getContent();
    } finally {
        file.close();
    }

    if (fileContent == null) {
        throw Util.newError("Cannot get virtual file content: " + url);
    }

    return fileContent.getInputStream();
}

From source file:mondrian.olap.Util.java

/**
 * Gets content via Apache VFS. File must exist and have content
 *
 * @param url String/*w  w w.j  a va2  s  .  c  om*/
 * @return Apache VFS FileContent for further processing
 * @throws FileSystemException on error
 */
public static InputStream readVirtualFile(String url) throws FileSystemException {
    // Treat catalogUrl as an Apache VFS (Virtual File System) URL.
    // VFS handles all of the usual protocols (http:, file:)
    // and then some.
    FileSystemManager fsManager = VFS.getManager();
    if (fsManager == null) {
        throw newError("Cannot get virtual file system manager");
    }

    // Workaround VFS bug.
    if (url.startsWith("file://localhost")) {
        url = url.substring("file://localhost".length());
    }
    if (url.startsWith("file:")) {
        url = url.substring("file:".length());
    }

    // work around for VFS bug not closing http sockets
    // (Mondrian-585)
    if (url.startsWith("http")) {
        try {
            return new URL(url).openStream();
        } catch (IOException e) {
            throw newError("Could not read URL: " + url);
        }
    }

    File userDir = new File("").getAbsoluteFile();
    FileObject file = fsManager.resolveFile(userDir, url);
    FileContent fileContent = null;
    try {
        // Because of VFS caching, make sure we refresh to get the latest
        // file content. This refresh may possibly solve the following
        // workaround for defect MONDRIAN-508, but cannot be tested, so we
        // will leave the work around for now.
        file.refresh();

        // Workaround to defect MONDRIAN-508. For HttpFileObjects, verifies
        // the URL of the file retrieved matches the URL passed in.  A VFS
        // cache bug can cause it to treat URLs with different parameters
        // as the same file (e.g. http://blah.com?param=A,
        // http://blah.com?param=B)
        if (file instanceof HttpFileObject && !file.getName().getURI().equals(url)) {
            fsManager.getFilesCache().removeFile(file.getFileSystem(), file.getName());

            file = fsManager.resolveFile(userDir, url);
        }

        if (!file.isReadable()) {
            throw newError("Virtual file is not readable: " + url);
        }

        fileContent = file.getContent();
    } finally {
        file.close();
    }

    if (fileContent == null) {
        throw newError("Cannot get virtual file content: " + url);
    }

    return fileContent.getInputStream();
}

From source file:org.jclouds.vfs.tools.blobstore.BlobStoreShell.java

private void ls(FileSystemManager mg, FileObject wd, final String[] cmd) throws FileSystemException {
    int pos = 1;// w  w  w .  j a v  a2 s.c o m
    final boolean recursive;
    if (cmd.length > pos && cmd[pos].equals("-R")) {
        recursive = true;
        pos++;
    } else {
        recursive = false;
    }

    final FileObject file;
    if (cmd.length > pos) {
        file = mg.resolveFile(wd, cmd[pos]);
    } else {
        file = wd;
    }

    if (file.getType() == FileType.FOLDER) {
        // List the contents
        System.out.println("Contents of " + file.getName().getFriendlyURI());
        listChildren(file, recursive, "");
    } else {
        // Stat the file
        System.out.println(file.getName());
        final FileContent content = file.getContent();
        System.out.println("Size: " + content.getSize() + " bytes.");
        final DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM);
        final String lastMod = dateFormat.format(new Date(content.getLastModifiedTime()));
        System.out.println("Last modified: " + lastMod);
    }
}

From source file:org.mule.transports.vfs.VFSConnector.java

protected FileObject resolveFile(String file) throws FileSystemException {
    FileSystemManager fsm = this.createFileSystemManager();
    FileObject fo = fsm.resolveFile(file, fileSystemOptions);
    return fo;//from   ww w  .  ja v  a2  s .c  o m
}

From source file:org.pentaho.di.core.vfs.KettleVFS.java

public static FileObject getFileObject(String vfsFilename, VariableSpace space, FileSystemOptions fsOptions)
        throws KettleFileException {
    try {//  ww w  .j  a  va  2s. c  o  m
        FileSystemManager fsManager = getInstance().getFileSystemManager();

        // We have one problem with VFS: if the file is in a subdirectory of the current one: somedir/somefile
        // In that case, VFS doesn't parse the file correctly.
        // We need to put file: in front of it to make it work.
        // However, how are we going to verify this?
        //
        // We are going to see if the filename starts with one of the known protocols like file: zip: ram: smb: jar: etc.
        // If not, we are going to assume it's a file.
        //
        boolean relativeFilename = true;
        String[] schemes = fsManager.getSchemes();
        for (int i = 0; i < schemes.length && relativeFilename; i++) {
            if (vfsFilename.startsWith(schemes[i] + ":")) {
                relativeFilename = false;
                // We have a VFS URL, load any options for the file system driver
                fsOptions = buildFsOptions(space, fsOptions, vfsFilename, schemes[i]);
            }
        }

        String filename;
        if (vfsFilename.startsWith("\\\\")) {
            File file = new File(vfsFilename);
            filename = file.toURI().toString();
        } else {
            if (relativeFilename) {
                File file = new File(vfsFilename);
                filename = file.getAbsolutePath();
            } else {
                filename = vfsFilename;
            }
        }

        FileObject fileObject = null;

        if (fsOptions != null) {
            fileObject = fsManager.resolveFile(filename, fsOptions);
        } else {
            fileObject = fsManager.resolveFile(filename);
        }

        return fileObject;
    } catch (IOException e) {
        throw new KettleFileException(
                "Unable to get VFS File object for filename '" + vfsFilename + "' : " + e.getMessage());
    }
}