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

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

Introduction

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

Prototype

public boolean exists() throws FileSystemException;

Source Link

Document

Determines if this file exists.

Usage

From source file:org.vivoweb.harvester.util.FileAide.java

/**
 * Deletes the file at the given path/* www  .  j  ava  2s  . c  om*/
 * @param path the path to delete
 * @return true if deleted, false otherwise
 * @throws IOException error resolving path
 */
public static boolean delete(String path) throws IOException {
    FileObject file = getFileObject(path);
    if (!file.exists()) {
        return true;
    }
    if (isFolder(path)) {
        try {
            for (FileObject subFile : file.findFiles(new AllFileSelector())) {
                try {
                    String subPath = subFile.getName().getPath();
                    if (!subPath.equals(path)) {
                        delete(subPath);
                    }
                } catch (FileSystemException e) {
                    //log.trace(e.getMessage(), e);
                }
            }
            file.delete(new AllFileSelector());
        } catch (FileSystemException e) {
            //log.trace(e.getMessage(), e);
            throw new IOException("Error deleting file!");
        }
    }
    return file.delete();
}

From source file:plugin.games.data.trans.step.fileoutput.TextFileOutput.java

private void createParentFolder(String filename) throws Exception {
    // Check for parent folder
    FileObject parentfolder = null;
    try {/*  w  w w.j  a v  a 2 s.  c o  m*/
        // Get parent folder
        parentfolder = KettleVFS.getFileObject(filename).getParent();
        if (parentfolder.exists()) {
            if (isDetailed())
                logDetailed(BaseMessages.getString(PKG, "TextFileOutput.Log.ParentFolderExist",
                        parentfolder.getName()));
        } else {
            if (isDetailed())
                logDetailed(BaseMessages.getString(PKG, "TextFileOutput.Log.ParentFolderNotExist",
                        parentfolder.getName()));
            if (meta.isCreateParentFolder()) {
                parentfolder.createFolder();
                if (isDetailed())
                    logDetailed(BaseMessages.getString(PKG, "TextFileOutput.Log.ParentFolderCreated",
                            parentfolder.getName()));
            } else {
                throw new KettleException(BaseMessages.getString(PKG,
                        "TextFileOutput.Log.ParentFolderNotExistCreateIt", parentfolder.getName(), filename));
            }
        }
    } finally {
        if (parentfolder != null) {
            try {
                parentfolder.close();
            } catch (Exception ex) {
            }
            ;
        }
    }
}

From source file:pt.webdetails.cpf.repository.vfs.VfsRepositoryAccess.java

private FileObject setRepoPath(String path) {
    FileSystemManager fileSystemManager;
    try {//from w  w  w  . ja  v a2s  .c om
        if (!path.endsWith("" + File.separatorChar)) {
            path += File.separatorChar;
        }
        fileSystemManager = VFS.getManager();
        FileObject fileObject;
        fileObject = fileSystemManager.resolveFile(path);
        if (fileObject == null) {
            throw new IOException("File cannot be resolved: " + path);
        }
        if (!fileObject.exists()) {
            throw new IOException("File does not exist: " + path);
        }
        return fileObject;
    } catch (Exception e) {
        log.error("Error setting path for repository: " + path, e);
    }
    return null;
}

From source file:pt.webdetails.cpf.repository.vfs.VfsRepositoryAccess.java

public SaveFileStatus copySolutionFile(String fromFilePath, String toFilePath) throws IOException {
    try {/* w  w w.ja  v a 2 s  .  com*/
        FileObject to = resolveFile(repo, toFilePath);
        FileObject from = resolveFile(repo, fromFilePath);
        to.copyFrom(from, Selectors.SELECT_SELF);
        if (to != null && to.exists() && to.isReadable()) {
            return SaveFileStatus.OK;
        }
    } catch (Exception e) {
        log.error("Cannot copy from " + fromFilePath + " to " + toFilePath, e);
    }
    return SaveFileStatus.FAIL;
}

From source file:pt.webdetails.cpf.repository.vfs.VfsRepositoryAccess.java

public InputStream getResourceInputStream(String file) throws FileNotFoundException {
    try {/*from  ww  w.ja v  a 2 s . c  o  m*/
        FileObject fo = repo.resolveFile(file);
        if (fo.exists()) {
            return fo.getContent().getInputStream();
        }
    } catch (Exception e) {
        log.error("Cannot getResourceInputStream for: " + file, e);
    }
    throw new FileNotFoundException("Cannot get input stream for: " + file);
}

From source file:pt.webdetails.cpf.repository.vfs.VfsRepositoryAccess.java

public String getSolutionPath(String arg0) {
    String path = "";
    FileObject file = null;
    try {/*  w  ww .ja va2s.  c o  m*/
        path = repo.getURL().toURI().getPath();
    } catch (FileSystemException ex) {
        Logger.getLogger(VfsRepositoryAccess.class.getName()).log(Level.SEVERE, null, ex);
    } catch (URISyntaxException ex) {
        Logger.getLogger(VfsRepositoryAccess.class.getName()).log(Level.SEVERE, null, ex);
    }
    if (arg0 == null || arg0.isEmpty()) {
        return path;
    }
    if (arg0.startsWith("/")) {
        arg0 = arg0.substring(1);
    }
    try {
        file = resolveFile(repo, arg0);
        if (file != null && file.exists()) {
            return path + "/" + arg0;
        }
    } catch (Exception ex) {
        Logger.getLogger(VfsRepositoryAccess.class.getName()).log(Level.SEVERE, null, ex);
    }
    return null;
}

From source file:pt.webdetails.cpf.repository.vfs.VfsRepositoryAccess.java

public boolean removeFile(String file) {
    try {//from   ww  w .  j  av a 2 s .com
        FileObject f = resolveFile(repo, file);
        if (f.exists()) {
            return f.delete();
        }
        return true;
    } catch (Exception e) {
        throw new RuntimeException("Cannot delete file: " + file, e);
    }
}

From source file:pt.webdetails.cpf.repository.vfs.VfsRepositoryAccess.java

public boolean resourceExists(String file) {
    try {/*from  w w w .ja va  2  s. co  m*/
        FileObject ir = resolveFile(repo, file);
        return ir.exists();
    } catch (Exception e) {
        throw new RuntimeException("Cannot check if repo file exists: " + file, e);
    }
}

From source file:pt.webdetails.cpf.repository.vfs.VfsRepositoryAccess.java

public IRepositoryFile[] listRepositoryFiles(IRepositoryFileFilter fileFilter) {
    try {/*from   w w  w.  ja v  a  2s . c om*/
        FileObject[] files = repo.getChildren();
        List<IRepositoryFile> repoFiles = new ArrayList<IRepositoryFile>();
        for (FileObject file : files) {
            if (file.exists() && file.isReadable() && file.getType().equals(FileType.FILE)) {
                IRepositoryFile repoFile = new VfsRepositoryFile(repo, file);
                if (fileFilter == null || fileFilter.accept(repoFile)) {
                    repoFiles.add(repoFile);
                }
            }
        }
        return repoFiles.toArray(new IRepositoryFile[] {});
    } catch (FileSystemException e) {
        throw new RuntimeException("Cannot list repo files", e);
    }
}

From source file:pt.webdetails.cpf.repository.VfsRepositoryTest.java

/**
 * Please do note this will remove the folder and all subfolders and files Used for testing purposes only
 *
 * @param file/*  www .j a  v  a2 s  .c  o  m*/
 * @return
 */
public int removeUnsafe(String file) {
    try {
        if (file.equals(".")) {
            return repo.delete(Selectors.EXCLUDE_SELF);
        }
        FileObject f = resolveFile(repo, file);
        if (f.exists()) {
            return f.delete(Selectors.SELECT_ALL);
        }
        return -1;
    } catch (Exception e) {
        throw new RuntimeException("Cannot delete file: " + file, e);
    }
}