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

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

Introduction

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

Prototype

void refresh() throws FileSystemException;

Source Link

Document

This will prepare the fileObject to get resynchronized with the underlying file system if required.

Usage

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

@Override
public FileObject resolveFile(final FileName name) throws FileSystemException {

    synchronized (this) {
        if (null == this.fs) {
            final String hdfsUri = name.getRootURI();
            final Configuration conf = new Configuration(true);
            conf.set(org.apache.hadoop.fs.FileSystem.FS_DEFAULT_NAME_KEY, hdfsUri);
            this.fs = null;
            try {
                fs = org.apache.hadoop.fs.FileSystem.get(conf);
            } catch (final IOException e) {
                log.error("Error connecting to filesystem " + hdfsUri, e);
                throw new FileSystemException("Error connecting to filesystem " + hdfsUri, e);
            }//from  w  w  w  .j ava 2s . c  om
        }
    }

    boolean useCache = (null != getContext().getFileSystemManager().getFilesCache());
    FileObject file;
    if (useCache) {
        file = this.getFileFromCache(name);
    } else {
        file = null;
    }
    if (null == file) {
        String path = null;
        try {
            path = URLDecoder.decode(name.getPath(), UTF_8.name());
        } catch (final UnsupportedEncodingException e) {
            path = name.getPath();
        }
        final Path filePath = new Path(path);
        file = new HdfsFileObject((AbstractFileName) name, this, fs, filePath);
        if (useCache) {
            this.putFileToCache(file);
        }

    }

    /**
     * resync the file information if requested
     */
    if (getFileSystemManager().getCacheStrategy().equals(CacheStrategy.ON_RESOLVE)) {
        file.refresh();
    }

    return file;
}

From source file:org.esupportail.portlet.filemanager.services.vfs.VfsAccessImpl.java

private FileObject cd(String path, SharedUserPortletParameters userParameters) {
    try {//from www .  j av a 2  s  .c  o  m
        // assure that it'as already opened
        this.open(userParameters);

        FileObject returnValue = null;

        if (path == null || path.length() == 0) {
            returnValue = root;
        } else {
            returnValue = root.resolveFile(path);
        }

        //Added for GIP Recia : make sure that the file is up to date
        returnValue.refresh();
        return returnValue;
    } catch (FileSystemException fse) {
        throw new EsupStockException(fse);
    }
}

From source file:org.jahia.modules.external.vfs.VFSDataSource.java

@Override
public List<ExternalData> getChildrenNodes(String path) throws RepositoryException {
    try {// w w  w  . j  av a 2  s  .co  m
        if (!path.endsWith(JCR_CONTENT_SUFFIX)) {
            FileObject fileObject = getFile(path);
            if (fileObject.getType() == FileType.FILE) {
                final FileContent content = fileObject.getContent();
                return Collections.singletonList(getFileContent(content));
            } else if (fileObject.getType() == FileType.FOLDER) {
                fileObject.refresh(); //in case of folder, refresh because it could be changed external               
                FileObject[] files = fileObject.getChildren();
                if (files.length > 0) {
                    List<ExternalData> children = new LinkedList<ExternalData>();
                    for (FileObject object : files) {
                        if (getSupportedNodeTypes().contains(getDataType(object))) {
                            children.add(getFile(object));
                            if (object.getType() == FileType.FILE) {
                                children.add(getFileContent(object.getContent()));
                            }
                        }
                    }
                    return children;
                } else {
                    return Collections.emptyList();
                }
            } else {
                if (fileObject.exists()) {
                    logger.warn("Found non file or folder entry at path {}, maybe an alias. VFS file type: {}",
                            fileObject, fileObject.getType());
                } else {
                    throw new PathNotFoundException(path);
                }
            }
        }
    } catch (FileSystemException e) {
        logger.error("Cannot get node children", e);
    }

    return Collections.emptyList();
}

From source file:org.ow2.proactive_grid_cloud_portal.dataspace.FileSystem.java

public FileObject resolveFileInUserspace(String pathname) throws FileSystemException {
    FileObject answer = fsm.resolveFile(fsm.resolveFile(userspace), pathname);
    answer.refresh();
    return answer;
}

From source file:org.ow2.proactive_grid_cloud_portal.dataspace.FileSystem.java

public FileObject resolveFileInGlobalspace(String pathname) throws FileSystemException {
    FileObject answer = fsm.resolveFile(fsm.resolveFile(globalspace), pathname);
    answer.refresh();
    return answer;
}

From source file:org.ow2.proactive_grid_cloud_portal.dataspace.FileSystem.java

public FileObject resolveFile(String dirPath, String pathname) throws FileSystemException {
    FileObject answer = fsm
            .resolveFile(dirPath + (dirPath.endsWith(File.separator) ? "" : File.separator) + pathname);
    answer.refresh();
    return answer;
}

From source file:org.ow2.proactive_grid_cloud_portal.dataspace.FileSystem.java

public FileObject createFile(String pathname) throws FileSystemException {
    FileObject fo = fsm.resolveFile(pathname);
    fo.refresh();
    if (!fo.exists()) {
        fo.createFile();//from w w w. ja v  a  2 s .c  o m
    }
    return fo;
}

From source file:org.ow2.proactive_grid_cloud_portal.dataspace.FileSystem.java

public static ListFile list(FileObject fo, List<String> includes, List<String> excludes)
        throws FileSystemException {
    fo.refresh();
    ListFile answer = new ListFile();
    List<String> dirList = Lists.newArrayList();
    List<String> fileList = Lists.newArrayList();
    List<String> fullList = Lists.newArrayList();
    List<FileObject> foundFileObjects = new LinkedList<>();
    if (isNullOrEmpty(includes) && isNullOrEmpty(excludes)) {
        fo.findFiles(Selectors.SELECT_CHILDREN, false, foundFileObjects);
    } else {/*w w  w  .j a va2 s. c o  m*/
        FileSelector selector = new org.objectweb.proactive.extensions.dataspaces.vfs.selector.FileSelector(
                includes, excludes);
        fo.findFiles(selector, false, foundFileObjects);
    }

    for (FileObject child : foundFileObjects) {
        FileType type = child.getType();
        FileName childName = child.getName();
        switch (type) {
        case FOLDER:
            if (!child.equals(fo)) {
                // exclude root directory from the list
                String relativePath = fo.getName().getRelativeName(childName);
                dirList.add(relativePath);
                fullList.add(relativePath);
            }
            break;
        case FILE:
            String relativePath = fo.getName().getRelativeName(childName);
            fileList.add(relativePath);
            fullList.add(relativePath);
            break;
        default:
            throw new RuntimeException("Unknown : " + type);
        }
    }
    Collections.sort(dirList);
    Collections.sort(fileList);
    Collections.sort(fullList);
    answer.setDirectoryListing(dirList);
    answer.setFileListing(fileList);
    answer.setFullListing(fullList);
    return answer;
}

From source file:org.ow2.proactive_grid_cloud_portal.dataspace.FileSystem.java

public static List<FileObject> findFiles(FileObject root, List<String> includes, List<String> excludes)
        throws FileSystemException {
    root.refresh();
    List<FileObject> files = Lists.newArrayList();
    FileSelector selector = (isNullOrEmpty(includes) && isNullOrEmpty(excludes)) ? new AllFilesSelector()
            : new org.objectweb.proactive.extensions.dataspaces.vfs.selector.FileSelector(includes, excludes);
    root.findFiles(selector, true, files);
    return files;
}

From source file:org.ow2.proactive_grid_cloud_portal.dataspace.FileSystem.java

public static void copy(InputStream is, FileObject outFile) throws IOException {
    outFile.refresh();
    Closer closer = Closer.create();// ww  w .ja va  2s. c o m
    closer.register(is);
    try {
        OutputStream os = outFile.getContent().getOutputStream();
        closer.register(os);
        ByteStreams.copy(is, os);
    } catch (IOException ioe) {
        throw closer.rethrow(ioe);
    } finally {
        closer.close();
    }
}