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.ow2.proactive_grid_cloud_portal.dataspace.FileSystem.java

public static void copy(FileObject fo, OutputStream os) throws IOException {
    fo.refresh();
    Closer closer = Closer.create();/*  ww w  . ja v a  2  s  . c om*/
    closer.register(os);
    try {
        InputStream is = fo.getContent().getInputStream();
        closer.register(is);
        ByteStreams.copy(is, os);
    } catch (IOException ioe) {
        throw closer.rethrow(ioe);
    } finally {
        closer.close();
    }
}

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

public static boolean isEmpty(FileObject fo) throws FileSystemException {
    fo.refresh();
    FileObject[] children = fo.getChildren();
    return children == null || children.length == 0;
}

From source file:org.pentaho.googledrive.vfs.GoogleDriveFileSystem.java

private synchronized FileObject processFile(FileName name, boolean useCache) throws FileSystemException {
    if (!super.getRootName().getRootURI().equals(name.getRootURI())) {
        throw new FileSystemException("vfs.provider/mismatched-fs-for-name.error",
                new Object[] { name, super.getRootName(), name.getRootURI() });
    } else {/* w  w  w .ja v a 2 s  .  c  o  m*/
        FileObject file;
        if (useCache) {
            file = super.getFileFromCache(name);
        } else {
            file = null;
        }

        if (file == null) {
            try {
                file = this.createFile((AbstractFileName) name);
            } catch (Exception var5) {
                return null;
            }

            file = super.decorateFileObject(file);
            if (useCache) {
                super.putFileToCache(file);
            }
        }

        if (super.getFileSystemManager().getCacheStrategy().equals(CacheStrategy.ON_RESOLVE)) {
            file.refresh();
        }

        return file;
    }
}

From source file:org.wso2.carbon.transport.file.connector.sender.VFSClientConnector.java

@Override
public boolean send(CarbonMessage carbonMessage, CarbonCallback carbonCallback, Map<String, String> map)
        throws ClientConnectorException {
    FtpFileSystemConfigBuilder.getInstance().setPassiveMode(opts, true);
    String fileURI = map.get(Constants.FILE_URI);
    String action = map.get(Constants.ACTION);
    FileType fileType;//from   w  ww . j av  a2 s . c om
    ByteBuffer byteBuffer;
    InputStream inputStream = null;
    OutputStream outputStream = null;
    try {
        FileSystemManager fsManager = VFS.getManager();
        FileObject path = fsManager.resolveFile(fileURI, opts);
        fileType = path.getType();
        switch (action) {

        case Constants.CREATE:
            boolean isFolder = Boolean.parseBoolean(map.getOrDefault("create-folder", "false"));
            if (path.exists()) {
                throw new ClientConnectorException("File already exists: " + path.getName().getURI());
            }
            if (isFolder) {
                path.createFolder();
            } else {
                path.createFile();
            }
            break;
        case Constants.WRITE:
            if (!path.exists()) {
                path.createFile();
                path.refresh();
                fileType = path.getType();
            }
            if (fileType == FileType.FILE) {
                if (carbonMessage instanceof BinaryCarbonMessage) {
                    BinaryCarbonMessage binaryCarbonMessage = (BinaryCarbonMessage) carbonMessage;
                    byteBuffer = binaryCarbonMessage.readBytes();
                } else {
                    throw new ClientConnectorException("Carbon message received is not a BinaryCarbonMessage");
                }
                byte[] bytes = byteBuffer.array();
                if (map.get(Constants.APPEND) != null) {
                    outputStream = path.getContent()
                            .getOutputStream(Boolean.parseBoolean(map.get(Constants.APPEND)));
                } else {
                    outputStream = path.getContent().getOutputStream();
                }
                outputStream.write(bytes);
                outputStream.flush();
            }
            break;
        case Constants.DELETE:
            if (path.exists()) {
                int filesDeleted = path.delete(Selectors.SELECT_ALL);
                if (logger.isDebugEnabled()) {
                    logger.debug(filesDeleted + " files successfully deleted");
                }
            } else {
                throw new ClientConnectorException(
                        "Failed to delete file: " + path.getName().getURI() + " not found");
            }
            break;
        case Constants.COPY:
            if (path.exists()) {
                String destination = map.get("destination");
                FileObject dest = fsManager.resolveFile(destination, opts);
                dest.copyFrom(path, Selectors.SELECT_ALL);
            } else {
                throw new ClientConnectorException(
                        "Failed to copy file: " + path.getName().getURI() + " not found");
            }
            break;
        case Constants.MOVE:
            if (path.exists()) {
                //TODO: Improve this to fix issue #331
                String destination = map.get("destination");
                FileObject newPath = fsManager.resolveFile(destination, opts);
                FileObject parent = newPath.getParent();
                if (parent != null && !parent.exists()) {
                    parent.createFolder();
                }
                if (!newPath.exists()) {
                    path.moveTo(newPath);
                } else {
                    throw new ClientConnectorException("The file at " + newPath.getURL().toString()
                            + " already exists or it is a directory");
                }
            } else {
                throw new ClientConnectorException(
                        "Failed to move file: " + path.getName().getURI() + " not found");
            }
            break;
        case Constants.READ:
            if (path.exists()) {
                //TODO: Do not assume 'path' always refers to a file
                inputStream = path.getContent().getInputStream();
                byte[] bytes = toByteArray(inputStream);
                BinaryCarbonMessage message = new BinaryCarbonMessage(ByteBuffer.wrap(bytes), true);
                message.setProperty(org.wso2.carbon.messaging.Constants.DIRECTION,
                        org.wso2.carbon.messaging.Constants.DIRECTION_RESPONSE);
                carbonMessageProcessor.receive(message, carbonCallback);
            } else {
                throw new ClientConnectorException(
                        "Failed to read file: " + path.getName().getURI() + " not found");
            }
            break;
        case Constants.EXISTS:
            TextCarbonMessage message = new TextCarbonMessage(Boolean.toString(path.exists()));
            message.setProperty(org.wso2.carbon.messaging.Constants.DIRECTION,
                    org.wso2.carbon.messaging.Constants.DIRECTION_RESPONSE);
            carbonMessageProcessor.receive(message, carbonCallback);
            break;
        default:
            return false;
        }
    } catch (RuntimeException e) {
        throw new ClientConnectorException("Runtime Exception occurred : " + e.getMessage(), e);
    } catch (Exception e) {
        throw new ClientConnectorException("Exception occurred while processing file: " + e.getMessage(), e);
    } finally {
        closeQuietly(inputStream);
        closeQuietly(outputStream);
    }
    return true;
}

From source file:org.wso2.carbon.transport.remotefilesystem.client.connector.contractimpl.VFSClientConnectorImpl.java

@Override
public void send(RemoteFileSystemMessage message) {
    FtpFileSystemConfigBuilder.getInstance().setPassiveMode(opts, true);
    String fileURI = connectorConfig.get(Constants.URI);
    String action = connectorConfig.get(Constants.ACTION);
    FileType fileType;/*from   w w w .  j  ava 2 s . c  o m*/
    ByteBuffer byteBuffer;
    InputStream inputStream = null;
    OutputStream outputStream = null;
    FileObject path = null;
    try {
        FileSystemManager fsManager = VFS.getManager();
        path = fsManager.resolveFile(fileURI, opts);
        fileType = path.getType();
        switch (action) {
        case Constants.CREATE:
            boolean isFolder = Boolean
                    .parseBoolean(connectorConfig.getOrDefault(Constants.CREATE_FOLDER, "false"));
            if (path.exists()) {
                throw new RemoteFileSystemConnectorException("File already exists: " + path.getName().getURI());
            }
            if (isFolder) {
                path.createFolder();
            } else {
                path.createFile();
            }
            break;
        case Constants.WRITE:
            if (!path.exists()) {
                path.createFile();
                path.refresh();
                fileType = path.getType();
            }
            if (fileType == FileType.FILE) {
                byteBuffer = message.getBytes();
                byte[] bytes = byteBuffer.array();
                if (connectorConfig.get(Constants.APPEND) != null) {
                    outputStream = path.getContent()
                            .getOutputStream(Boolean.parseBoolean(connectorConfig.get(Constants.APPEND)));
                } else {
                    outputStream = path.getContent().getOutputStream();
                }
                outputStream.write(bytes);
                outputStream.flush();
            }
            break;
        case Constants.DELETE:
            if (path.exists()) {
                int filesDeleted = path.delete(Selectors.SELECT_ALL);
                if (logger.isDebugEnabled()) {
                    logger.debug(filesDeleted + " files successfully deleted");
                }
            } else {
                throw new RemoteFileSystemConnectorException(
                        "Failed to delete file: " + path.getName().getURI() + " not found");
            }
            break;
        case Constants.COPY:
            if (path.exists()) {
                String destination = connectorConfig.get(Constants.DESTINATION);
                FileObject dest = fsManager.resolveFile(destination, opts);
                dest.copyFrom(path, Selectors.SELECT_ALL);
            } else {
                throw new RemoteFileSystemConnectorException(
                        "Failed to copy file: " + path.getName().getURI() + " not found");
            }
            break;
        case Constants.MOVE:
            if (path.exists()) {
                //TODO: Improve this to fix issue #331
                String destination = connectorConfig.get(Constants.DESTINATION);
                FileObject newPath = fsManager.resolveFile(destination, opts);
                FileObject parent = newPath.getParent();
                if (parent != null && !parent.exists()) {
                    parent.createFolder();
                }
                if (!newPath.exists()) {
                    path.moveTo(newPath);
                } else {
                    throw new RemoteFileSystemConnectorException("The file at " + newPath.getURL().toString()
                            + " already exists or it is a directory");
                }
            } else {
                throw new RemoteFileSystemConnectorException(
                        "Failed to move file: " + path.getName().getURI() + " not found");
            }
            break;
        case Constants.READ:
            if (path.exists()) {
                //TODO: Do not assume 'path' always refers to a file
                inputStream = path.getContent().getInputStream();
                byte[] bytes = toByteArray(inputStream);
                RemoteFileSystemMessage fileContent = new RemoteFileSystemMessage(ByteBuffer.wrap(bytes));
                remoteFileSystemListener.onMessage(fileContent);
            } else {
                throw new RemoteFileSystemConnectorException(
                        "Failed to read file: " + path.getName().getURI() + " not found");
            }
            break;
        case Constants.EXISTS:
            RemoteFileSystemMessage fileContent = new RemoteFileSystemMessage(Boolean.toString(path.exists()));
            remoteFileSystemListener.onMessage(fileContent);
            break;
        default:
            break;
        }
        remoteFileSystemListener.done();
    } catch (RemoteFileSystemConnectorException | IOException e) {
        remoteFileSystemListener.onError(e);
    } finally {
        if (path != null) {
            try {
                path.close();
            } catch (FileSystemException e) {
                //Do nothing.
            }
        }
        closeQuietly(inputStream);
        closeQuietly(outputStream);
    }
}

From source file:tain.kr.test.vfs.v01.TestVfs2FilehandleService.java

@Test
public void testCaching() throws Exception {

    String path = TestVfs2FilehandleService.class.getResource("").getPath();
    if (flag)/*from  w w  w . j  ava 2s .c o m*/
        System.out.printf("[%s]\n", path);

    String testFolder = path + "/testfolder";
    FileSystemManager manager = VFS.getManager();

    FileObject scratchFolder = manager.resolveFile(testFolder);

    // testfolder    
    scratchFolder.delete(Selectors.EXCLUDE_SELF);

    FileObject file = manager.resolveFile(path + "/testfolder/dummy.txt");
    file.createFile();

    //  Manager 
    DefaultFileSystemManager fs = new DefaultFileSystemManager();
    fs.setFilesCache(manager.getFilesCache());

    // zip, jar, tgz, tar, tbz2, file
    if (!fs.hasProvider("file")) {
        fs.addProvider("file", new DefaultLocalFileProvider());
    }

    fs.setCacheStrategy(CacheStrategy.ON_RESOLVE);
    fs.init();

    //   
    FileObject foBase2 = fs.resolveFile(testFolder);
    if (flag)
        System.out.printf("## scratchFolder.getName().getPath() : %s\n", scratchFolder.getName().getPath());

    FileObject cachedFolder = foBase2.resolveFile(scratchFolder.getName().getPath());

    //   
    FileObject[] fos = cachedFolder.getChildren();
    assertFalse(contains(fos, "file1.txt"));

    // 
    scratchFolder.resolveFile("file1.txt").createFile();

    //  
    // BUT cachedFolder    
    fos = cachedFolder.getChildren();
    assertFalse(contains(fos, "file1.txt"));

    // 
    cachedFolder.refresh();
    //  
    fos = cachedFolder.getChildren();
    assertTrue(contains(fos, "file1.txt"));
}