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

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

Introduction

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

Prototype

boolean exists() throws FileSystemException;

Source Link

Document

Determines if this file exists.

Usage

From source file:se.kth.hopsworks.zeppelin.notebook.repo.FSNotebookRepo.java

@Override
public Note get(String noteId) throws IOException {
    FileObject rootDir = fsManager.resolveFile(getPath("/"));
    FileObject projectDir = rootDir.resolveFile(this.project.getName(), NameScope.CHILD);
    FileObject noteDir = projectDir.resolveFile(noteId, NameScope.CHILD);

    if (noteDir.exists() && isDirectory(noteDir)) {
        return getNote(noteDir);
    }//w  w  w. ja v a  2s  .c o m

    noteDir = rootDir.resolveFile(noteId, NameScope.CHILD);
    return getNote(noteDir);
}

From source file:se.kth.hopsworks.zeppelin.notebook.repo.FSNotebookRepo.java

@Override
public void save(Note note) throws IOException {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.setPrettyPrinting();/*  w  ww . ja  va2 s  . c  om*/
    Gson gson = gsonBuilder.create();
    String json = gson.toJson(note);

    FileObject rootDir = getRootDir();

    FileObject projectDir = rootDir.resolveFile(this.project.getName(), NameScope.CHILD);
    if (!projectDir.exists()) {
        projectDir.createFolder();
    }
    if (!isDirectory(projectDir)) {
        throw new IOException(projectDir.getName().toString() + " is not a directory");
    }

    FileObject noteDir = projectDir.resolveFile(note.id(), NameScope.CHILD);

    if (!noteDir.exists()) {
        noteDir.createFolder();
    }
    if (!isDirectory(noteDir)) {
        throw new IOException(noteDir.getName().toString() + " is not a directory");
    }

    FileObject noteJson = noteDir.resolveFile("note.json", NameScope.CHILD);
    // false means not appending. creates file if not exists
    OutputStream out = noteJson.getContent().getOutputStream(false);
    out.write(json.getBytes(conf.getString(ConfVars.ZEPPELIN_ENCODING)));
    out.close();
    noteJson.moveTo(noteDir.resolveFile("note.json", NameScope.CHILD));
}

From source file:se.kth.hopsworks.zeppelin.notebook.repo.FSNotebookRepo.java

@Override
public void remove(String noteId) throws IOException {
    FileObject rootDir = fsManager.resolveFile(getPath("/"));
    FileObject projectDir = rootDir.resolveFile(this.project.getName(), NameScope.CHILD);
    if (!projectDir.exists() || !isDirectory(projectDir)) {
        // no project dir
        return;//from   w  w w. j av a2s . co  m
    }
    FileObject noteDir = projectDir.resolveFile(noteId, NameScope.CHILD);

    if (!noteDir.exists()) {
        // nothing to do
        return;
    }

    if (!isDirectory(noteDir)) {
        // it is not look like zeppelin note savings
        throw new IOException("Can not remove " + noteDir.getName().toString());
    }

    noteDir.delete(Selectors.SELECT_SELF_AND_CHILDREN);
}

From source file:se.kth.hopsworks.zeppelin.notebook.repo.FSNotebookRepo.java

private LinkedList<FileObject> getNotes(FileObject root, FileObject proj) throws IOException {
    FileObject[] rootchildren = root.getChildren();
    FileObject[] projChildren;//from  ww  w.  java  2 s  .c o m
    LinkedList<FileObject> children = new LinkedList<>();
    if (isDirectory(proj)) {
        projChildren = proj.getChildren();
        if (projChildren != null) {
            children = new LinkedList<>(Arrays.asList(projChildren));
        }
    }
    // add notes in the root dir that are not project specific ex. tutorials
    for (FileObject f : rootchildren) {
        if (isDirectory(f)) {
            FileObject noteJson = f.resolveFile("note.json", NameScope.CHILD);
            if (noteJson.exists() && !listContainsNote(children, f)) {
                children.add(f);
            }
        }
    }

    return children;
}

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();/*w  w  w  .  ja  v  a  2 s.  c  o m*/
            } 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.connectors.XPMProcess.java

/**
 * Returns the error code//from ww  w .  ja va  2  s  . c o m
 */
public int exitValue() {
    // Check for done file
    try {
        if (job.getLocator().resolve(connector, Resource.DONE_EXTENSION).exists())
            return 0;

        // If the job is not done, check the ".code" file to get the error code
        // and otherwise return -1
        final FileObject codeFile = job.getMainConnector()
                .resolveFile(job.getLocator().getPath() + Job.CODE_EXTENSION);
        if (codeFile.exists()) {
            final InputStream stream = codeFile.getContent().getInputStream();
            final BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
            String s = reader.readLine();
            int code = s != null ? Integer.parseInt(s) : -1;
            reader.close();
            return code;
        }
    } catch (IOException e) {
        throw new IllegalThreadStateException();
    }

    // The process failed, but we do not know how
    return -1;

}

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

/**
 * Asynchronous check the state of the job monitor
 *//* w  w  w. ja v  a2 s .  c o  m*/
public void check() throws Exception {
    if (!isRunning()) {
        // We are not running: send a message
        LOGGER.debug("End of job [%s]", job);
        final FileObject file = job.getLocator().resolve(connector, Resource.CODE_EXTENSION);
        final long time = file.exists() ? file.getContent().getLastModifiedTime() : -1;
        job.notify(new EndOfJobMessage(exitValue(), time));
        dispose();
    }
}

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

@Override
synchronized protected DependencyStatus _accept(Scheduler scheduler, Resource from) {
    Resource resource = getFrom(scheduler, from);
    resource.init(scheduler);/*w  w w.j  a  v a2  s.co m*/
    final FileObject file;
    try {
        file = resource.getFileWithExtension(Resource.LOCK_EXTENSION);
        return file.exists() ? DependencyStatus.WAIT : DependencyStatus.OK_LOCK;
    } catch (FileSystemException e) {
        LOGGER.error(e, "Error while checking the presence of lock file for [%s]", from);
        return DependencyStatus.ERROR;

    }

}

From source file:sftpexamples.SftpUtility.java

public static boolean move(String hostName, String username, String password, String remoteSrcFilePath,
        String remoteDestFilePath) {
    StandardFileSystemManager manager = new StandardFileSystemManager();

    try {//from ww w .  j a  va 2s.c o m
        manager.init();

        // Create remote object
        FileObject remoteFile = manager.resolveFile(
                createConnectionString(hostName, username, password, remoteSrcFilePath),
                createDefaultOptions());
        FileObject remoteDestFile = manager.resolveFile(
                createConnectionString(hostName, username, password, remoteDestFilePath),
                createDefaultOptions());

        if (remoteFile.exists()) {
            remoteFile.moveTo(remoteDestFile);
            ;
            System.out.println("Move remote file success");
            return true;
        } else {
            System.out.println("Source file doesn't exist");
            return false;
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        manager.close();
    }
}

From source file:sftpexamples.SftpUtility.java

/**
 * Method to delete the specified file from the remote system
 *
 * @param hostName HostName of the server
 * @param username UserName to login//from   w w w .  jav  a  2  s  . c  om
 * @param password Password to login
 * @param localFilePath LocalFilePath. Should contain the entire local file
 * path - Directory and Filename with \\ as separator
 * @param remoteFilePath remoteFilePath. Should contain the entire remote
 * file path - Directory and Filename with / as separator
 */
public static void delete(String hostName, String username, String password, String remoteFilePath) {
    StandardFileSystemManager manager = new StandardFileSystemManager();

    try {
        manager.init();

        // Create remote object
        FileObject remoteFile = manager.resolveFile(
                createConnectionString(hostName, username, password, remoteFilePath), createDefaultOptions());

        if (remoteFile.exists()) {
            remoteFile.delete();
            System.out.println("Delete remote file success");
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        manager.close();
    }
}