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

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

Introduction

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

Prototype

FileObject resolveFile(String path) throws FileSystemException;

Source Link

Document

Finds a file, relative to this file.

Usage

From source file:org.wso2.carbon.transport.remotefilesystem.server.RemoteFileSystemConsumer.java

/**
 * Do the post processing actions./*  w ww. j  ava2 s. co  m*/
 *
 * @param file The file object which needs to be post processed
 * @param processSucceed Whether processing of file passed or not.
 */
synchronized void postProcess(FileObject file, boolean processSucceed)
        throws RemoteFileSystemConnectorException {
    String moveToDirectoryURI = null;
    FileType fileType = getFileType(file);
    if (processSucceed) {
        if (postProcessAction.equals(Constants.ACTION_MOVE)) {
            moveToDirectoryURI = fileProperties.get(Constants.MOVE_AFTER_PROCESS);
        }
    } else {
        if (postFailureAction.equals(Constants.ACTION_MOVE)) {
            moveToDirectoryURI = fileProperties.get(Constants.MOVE_AFTER_FAILURE);
        }
    }
    if (moveToDirectoryURI != null) {
        try {
            if (getFileType(fsManager.resolveFile(moveToDirectoryURI, fso)) == FileType.FILE) {
                moveToDirectoryURI = null;
                if (processSucceed) {
                    postProcessAction = Constants.ACTION_NONE;
                } else {
                    postFailureAction = Constants.ACTION_NONE;
                }
                log.warn("[" + serviceName + "] Cannot move file because provided location is not a folder."
                        + " File is kept at source.");
            }
        } catch (FileSystemException e) {
            remoteFileSystemListener.onError(new RemoteFileSystemConnectorException(
                    "Error occurred when resolving move destination file: "
                            + FileTransportUtils.maskURLPassword(listeningDirURI),
                    e));
        }
    }
    if (postProcessAction.equals(Constants.ACTION_NONE) && fileType == FileType.FOLDER) {
        return;
    }
    try {
        if (!(moveToDirectoryURI == null || fileType == FileType.FOLDER)) {
            FileObject moveToDirectory;
            String relativeName = file.getName().getURI().split(listeningDir.getName().getURI())[1];
            int index = relativeName.lastIndexOf(File.separator);
            moveToDirectoryURI += relativeName.substring(0, index);
            moveToDirectory = fsManager.resolveFile(moveToDirectoryURI, fso);
            String prefix;
            if (fileProperties.get(Constants.MOVE_TIMESTAMP_FORMAT) != null) {
                prefix = new SimpleDateFormat(fileProperties.get(Constants.MOVE_TIMESTAMP_FORMAT))
                        .format(new Date());
            } else {
                prefix = "";
            }
            //Forcefully create the folder(s) if does not exists
            String strForceCreateFolder = fileProperties.get(Constants.FORCE_CREATE_FOLDER);
            if (strForceCreateFolder != null && strForceCreateFolder.equalsIgnoreCase("true")
                    && !moveToDirectory.exists()) {
                moveToDirectory.createFolder();
            }
            FileObject destination = moveToDirectory.resolveFile(prefix + file.getName().getBaseName());
            if (log.isDebugEnabled()) {
                log.debug("Moving file: " + FileTransportUtils.maskURLPassword(file.getName().getBaseName()));
            }
            try {
                file.moveTo(destination);
                if (isFailRecord(file)) {
                    releaseFail(file);
                }
            } catch (FileSystemException e) {
                if (!isFailRecord(file)) {
                    markFailRecord(file);
                }
                remoteFileSystemListener.onError(new RemoteFileSystemConnectorException("[" + serviceName
                        + "] Error moving file: " + FileTransportUtils.maskURLPassword(file.toString()) + " to "
                        + FileTransportUtils.maskURLPassword(moveToDirectoryURI), e));
            }
        } else {
            if (log.isDebugEnabled()) {
                log.debug("Deleting file: " + FileTransportUtils.maskURLPassword(file.getName().getBaseName()));
            }
            try {
                if (!file.delete()) {
                    if (log.isDebugEnabled()) {
                        log.debug("Could not delete file: "
                                + FileTransportUtils.maskURLPassword(file.getName().getBaseName()));
                    }
                } else {
                    if (isFailRecord(file)) {
                        releaseFail(file);
                    }
                }
            } catch (FileSystemException e) {
                remoteFileSystemListener.onError(
                        new RemoteFileSystemConnectorException("[" + serviceName + "] Could not delete file: "
                                + FileTransportUtils.maskURLPassword(file.getName().getBaseName()), e));
            }
        }
    } catch (FileSystemException e) {
        if (!isFailRecord(file)) {
            markFailRecord(file);
            remoteFileSystemListener.onError(new RemoteFileSystemConnectorException(
                    "[" + serviceName + "] Error resolving directory to move file : "
                            + FileTransportUtils.maskURLPassword(moveToDirectoryURI),
                    e));
        }
    }
}

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

public FileObject getTemporaryFile(String prefix, String suffix) throws FileSystemException {
    FileObject tmpdir = getTemporaryDirectory();

    final int MAX_ATTEMPTS = 1000;

    for (int attempt = 0; attempt < MAX_ATTEMPTS; attempt++) {
        FileObject child = tmpdir.resolveFile(prefix + RandomStringUtils.random(10, chars) + suffix);
        if (!child.exists()) {
            try {
                child.createFile();//from  w ww  . j a va2  s .c  om
            } catch (Throwable t) {
            }
            return child;
        }
    }
    throw new FileSystemException(
            format("Could not find a proper temporary file name after %d attempts", MAX_ATTEMPTS));
}

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

private FileObject path(FileObject current, int i, Object arg) throws FileSystemException {
    if (arg == null)
        throw new IllegalArgumentException(String.format("Undefined element (index %d) in path", i));

    if (arg instanceof NativeArray)
        return path(current, (NativeArray) arg);

    if (arg instanceof JSJson) {
        Json json = ((JSJson) arg).getJson();
        if (json instanceof JsonArray)
            return path(current, (JsonArray) json);
    }//w  w w . j a  v  a 2s. co m

    String name = Context.toString(arg);
    current = current.resolveFile(name);
    return current;
}

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

/**
 * Resolve a path relative to this resource
 *
 * @param path   The path/*from w  ww  .  j  a v  a 2s.  c om*/
 * @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());
}

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

/**
 * Does a 'cp' command./*from w  w  w .ja  v a 2s  .co m*/
 */
private void cp(final String[] cmd) throws Exception {

    if (cmd.length < 3) {
        throw new Exception("USAGE: cp <src> <dest>");
    }

    final FileObject src = mgr.resolveFile(cwd, cmd[1]);
    FileObject dest = mgr.resolveFile(cwd, cmd[2]);

    if (dest.exists() && dest.getType() == FileType.FOLDER) {
        dest = dest.resolveFile(src.getName().getBaseName());
    }

    dest.copyFrom(src, Selectors.SELECT_ALL);
}

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 a v  a2s. 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"));
}