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 name, NameScope scope) throws FileSystemException;

Source Link

Document

Finds a file relative to this file.

Usage

From source file:com.ewcms.publication.deploy.provider.DeployOperatorBase.java

/**
 * // www  .j a  v  a  2 s.c o  m
 * 
 * @param root
 *            
 * @param path
 *            
 * @return
 * @throws FileSystemException
 */
protected FileObject getTargetFileObject(FileObject root, String path) throws FileSystemException {
    FileObject out = root.resolveFile(path, NameScope.DESCENDENT);
    if (!out.exists()) {
        out.createFile();
    }
    return out;
}

From source file:com.seeburger.vfs2.util.VFSClassLoader.java

/**
 * Searches through the search path of for the first class or resource
 * with specified name./*  www  . j a  v a  2 s .  com*/
 * @param name The resource to load.
 * @return The Resource.
 * @throws FileSystemException if an error occurs.
 */
private Resource loadResource(final String name) throws FileSystemException {
    final Iterator<FileObject> it = resources.iterator();
    while (it.hasNext()) {
        final FileObject baseFile = it.next();
        final FileObject file = baseFile.resolveFile(name, NameScope.DESCENDENT_OR_SELF);
        if (file.exists()) {
            return new Resource(name, baseFile, file);
        }
    }

    return null;
}

From source file:com.seeburger.vfs2.util.VFSClassLoader.java

/**
 * Returns an Enumeration of all the resources in the search path
 * with the specified name.//from   w w w.  j  av a 2s  .  co  m
 * @param name The resources to find.
 * @return An Enumeration of the resources associated with the name.
 */
@Override
protected Enumeration<URL> findResources(final String name) throws IOException {
    ArrayList<URL> result = new ArrayList<URL>(2);
    final Iterator<FileObject> it = resources.iterator();
    while (it.hasNext()) {
        final FileObject baseFile = it.next();
        final FileObject file = baseFile.resolveFile(name, NameScope.DESCENDENT_OR_SELF);
        if (file.exists()) {
            result.add(new Resource(name, baseFile, file).getURL());
        }
    }

    return Collections.enumeration(result);
}

From source file:net.sf.jabb.web.action.VfsTreeAction.java

protected List<JsTreeNodeData> getChildNodes(FileObject rootFile, String parentPath)
        throws FileSystemException {
    List<JsTreeNodeData> childNodes = new LinkedList<JsTreeNodeData>();
    FileObject parent = null;// w  w w .j  a  va 2  s . c o  m
    if (requestData.isRoot()) {
        parent = rootFile;
    } else {
        parent = rootFile.resolveFile(parentPath, NameScope.DESCENDENT_OR_SELF);
    }
    for (FileObject child : parent.getChildren()) {
        JsTreeNodeData childNode = populateTreeNodeData(rootFile, child);
        childNodes.add(childNode);
    }
    if (sortByName || folderFirst) {
        Collections.sort(childNodes, new Comparator<JsTreeNodeData>() {

            @Override
            public int compare(JsTreeNodeData o1, JsTreeNodeData o2) {
                int result = 0;
                if (folderFirst) {
                    String t1 = o1.getAttr().get("rel").toString();
                    String t2 = o2.getAttr().get("rel").toString();
                    if (t1.equalsIgnoreCase(t2)) {
                        result = 0;
                    } else if ("file".equalsIgnoreCase(t2)) {
                        result = -1;
                    } else if ("file".equalsIgnoreCase(t1)) {
                        result = 1;
                    } else {
                        result = t1.compareToIgnoreCase(t2);
                    }
                }
                if (result == 0 && sortByName) {
                    String n1 = o1.getData().toString();
                    String n2 = o2.getData().toString();
                    result = n1.compareToIgnoreCase(n2);
                }
                return result;
            }

        });
    }
    return childNodes;
}

From source file:net.sf.jabb.web.action.VfsTreeAction.java

/**
 * AJAX tree functions//from  w  ww. j  av  a2  s  . com
 */
public String execute() {
    normalizeTreeRequest();
    JsTreeResult result = new JsTreeResult();

    final AllFileSelector ALL_FILES = new AllFileSelector();
    FileSystemManager fsManager = null;
    FileObject rootFile = null;
    FileObject file = null;
    FileObject referenceFile = null;
    try {
        fsManager = VfsUtility.getManager();
        rootFile = fsManager.resolveFile(rootPath, fsOptions);

        if (JsTreeRequest.OP_GET_CHILDREN.equalsIgnoreCase(requestData.getOperation())) {
            String parentPath = requestData.getId();
            List<JsTreeNodeData> nodes = null;
            try {
                nodes = getChildNodes(rootFile, parentPath);
                if (requestData.isRoot() && rootNodeName != null) { // add root node
                    JsTreeNodeData rootNode = new JsTreeNodeData();
                    rootNode.setData(rootNodeName);
                    Map<String, Object> attr = new HashMap<String, Object>();
                    rootNode.setAttr(attr);
                    attr.put("id", ".");
                    attr.put("rel", "root");
                    attr.put("fileType", FileType.FOLDER.toString());
                    rootNode.setChildren(nodes);
                    rootNode.setState(JsTreeNodeData.STATE_OPEN);
                    nodes = new LinkedList<JsTreeNodeData>();
                    nodes.add(rootNode);
                }
            } catch (Exception e) {
                log.error("Cannot get child nodes for: " + parentPath, e);
                nodes = new LinkedList<JsTreeNodeData>();
            }
            responseData = nodes;
        } else if (JsTreeRequest.OP_REMOVE_NODE.equalsIgnoreCase(requestData.getOperation())) {
            String path = requestData.getId();
            try {
                file = rootFile.resolveFile(path, NameScope.DESCENDENT);
                boolean wasDeleted = false;
                if (file.getType() == FileType.FILE) {
                    wasDeleted = file.delete();
                } else {
                    wasDeleted = file.delete(ALL_FILES) > 0;
                }
                result.setStatus(wasDeleted);
            } catch (Exception e) {
                result.setStatus(false);
                log.error("Cannot delete: " + path, e);
            }
            responseData = result;
        } else if (JsTreeRequest.OP_CREATE_NODE.equalsIgnoreCase(requestData.getOperation())) {
            String parentPath = requestData.getReferenceId();
            String name = requestData.getTitle();
            try {
                referenceFile = rootFile.resolveFile(parentPath, NameScope.DESCENDENT_OR_SELF);
                file = referenceFile.resolveFile(name, NameScope.CHILD);
                file.createFolder();
                result.setStatus(true);
                result.setId(rootFile.getName().getRelativeName(file.getName()));
            } catch (Exception e) {
                result.setStatus(false);
                log.error("Cannot create folder '" + name + "' under '" + parentPath + "'", e);
            }
            responseData = result;
        } else if (JsTreeRequest.OP_RENAME_NODE.equalsIgnoreCase(requestData.getOperation())) {
            String path = requestData.getId();
            String name = requestData.getTitle();
            try {
                referenceFile = rootFile.resolveFile(path, NameScope.DESCENDENT);
                file = referenceFile.getParent().resolveFile(name, NameScope.CHILD);
                referenceFile.moveTo(file);
                result.setStatus(true);
            } catch (Exception e) {
                result.setStatus(false);
                log.error("Cannot rename '" + path + "' to '" + name + "'", e);
            }
            responseData = result;
        } else if (JsTreeRequest.OP_MOVE_NODE.equalsIgnoreCase(requestData.getOperation())) {
            String newParentPath = requestData.getReferenceId();
            String originalPath = requestData.getId();
            try {
                referenceFile = rootFile.resolveFile(originalPath, NameScope.DESCENDENT);
                file = rootFile.resolveFile(newParentPath, NameScope.DESCENDENT_OR_SELF)
                        .resolveFile(referenceFile.getName().getBaseName(), NameScope.CHILD);
                if (requestData.isCopy()) {
                    file.copyFrom(referenceFile, ALL_FILES);
                } else {
                    referenceFile.moveTo(file);
                }
                result.setStatus(true);
            } catch (Exception e) {
                result.setStatus(false);
                log.error("Cannot move '" + originalPath + "' to '" + newParentPath + "'", e);
            }
            responseData = result;
        }
    } catch (FileSystemException e) {
        log.error("Cannot perform file operation.", e);
    } finally {
        VfsUtility.close(fsManager, file, referenceFile, rootFile);
    }
    return SUCCESS;
}

From source file:org.apache.zeppelin.notebook.repo.OldVFSNotebookRepo.java

private Note getNote(FileObject noteDir) throws IOException {
    if (!isDirectory(noteDir)) {
        throw new IOException(noteDir.getName().toString() + " is not a directory");
    }// w  w  w . j a v  a2s. c  om

    FileObject noteJson = noteDir.resolveFile("note.json", NameScope.CHILD);
    if (!noteJson.exists()) {
        throw new IOException(noteJson.getName().toString() + " not found");
    }

    FileContent content = noteJson.getContent();
    InputStream ins = content.getInputStream();
    String json = IOUtils.toString(ins, conf.getString(ConfVars.ZEPPELIN_ENCODING));
    ins.close();

    return Note.fromJson(json);
}

From source file:org.apache.zeppelin.notebook.repo.OldVFSNotebookRepo.java

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

    return getNote(noteDir);
}

From source file:org.apache.zeppelin.notebook.repo.OldVFSNotebookRepo.java

@Override
public synchronized void save(Note note, AuthenticationInfo subject) throws IOException {
    LOG.info("Saving note:" + note.getId());
    String json = note.toJson();/*from w w  w  .  j a  v  a2s .c  o m*/

    FileObject rootDir = getRootDir();

    FileObject noteDir = rootDir.resolveFile(note.getId(), 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:org.apache.zeppelin.notebook.repo.OldVFSNotebookRepo.java

@Override
public void remove(String noteId, AuthenticationInfo subject) throws IOException {
    FileObject rootDir = fsManager.resolveFile(getPath("/"));
    FileObject noteDir = rootDir.resolveFile(noteId, NameScope.CHILD);

    if (!noteDir.exists()) {
        // nothing to do
        return;/*from   ww  w . j  a v a  2  s.  c o  m*/
    }

    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:org.apache.zeppelin.notebook.repo.VFSNotebookRepo.java

private Note getNote(FileObject noteDir) throws IOException {
    if (!isDirectory(noteDir)) {
        throw new IOException(noteDir.getName().toString() + " is not a directory");
    }/*from w  w  w .jav  a  2  s . co m*/

    FileObject noteJson = noteDir.resolveFile("note.json", NameScope.CHILD);
    if (!noteJson.exists()) {
        throw new IOException(noteJson.getName().toString() + " not found");
    }

    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.setPrettyPrinting();
    Gson gson = gsonBuilder.create();

    FileContent content = noteJson.getContent();
    InputStream ins = content.getInputStream();
    String json = IOUtils.toString(ins, conf.getString(ConfVars.ZEPPELIN_ENCODING));
    ins.close();

    Note note = gson.fromJson(json, Note.class);
    //    note.setReplLoader(replLoader);
    //    note.jobListenerFactory = jobListenerFactory;

    for (Paragraph p : note.getParagraphs()) {
        if (p.getStatus() == Status.PENDING || p.getStatus() == Status.RUNNING) {
            p.setStatus(Status.ABORT);
        }
    }

    return note;
}