Search in sources :

Example 26 with FileObject

use of org.apache.commons.vfs2.FileObject in project jackrabbit by apache.

the class VFSBackend method getTouchFileObject.

/**
     * Returns the touch file for the fileObject.
     * If there's no corresponding touch file existing, then returns null when {@code create} is false.
     * When {@code create} is true, it creates a new touch file if no corresponding touch file exists.
     *
     * @param fileObject file object
     * @param create create a touch file if not existing
     * @return touch file object
     * @throws DataStoreException if any file system exception occurs
     */
protected FileObject getTouchFileObject(FileObject fileObject, boolean create) throws DataStoreException {
    try {
        FileObject folderObject = fileObject.getParent();
        String touchFileName = fileObject.getName().getBaseName() + TOUCH_FILE_NAME_SUFFIX;
        FileObject touchFileObject = folderObject.getChild(touchFileName);
        if (touchFileObject == null && create) {
            touchFileObject = folderObject.resolveFile(touchFileName);
            touchFileObject.createFile();
            touchFileObject = folderObject.getChild(touchFileName);
        }
        return touchFileObject;
    } catch (FileSystemException e) {
        throw new DataStoreException("Touch file object not resolved: " + fileObject.getName().getFriendlyURI(), e);
    }
}
Also used : FileSystemException(org.apache.commons.vfs2.FileSystemException) DataStoreException(org.apache.jackrabbit.core.data.DataStoreException) FileObject(org.apache.commons.vfs2.FileObject)

Example 27 with FileObject

use of org.apache.commons.vfs2.FileObject in project jackrabbit by apache.

the class VFSBackend method deleteOlderRecursive.

/**
     * Deletes any descendant record files under {@code folderObject} if the record files are older than {@code timestamp},
     * and push all the deleted record identifiers into {@code deleteIdSet}.
     * @param deleteIdSet set to store all the deleted record identifiers
     * @param folderObject folder object to start with
     * @param timestamp timestamp
     * @throws FileSystemException if any file system exception occurs
     * @throws DataStoreException if any file system exception occurs
     */
private void deleteOlderRecursive(Set<DataIdentifier> deleteIdSet, FileObject folderObject, long timestamp) throws FileSystemException, DataStoreException {
    FileType type;
    DataIdentifier identifier;
    for (FileObject fileObject : VFSUtils.getChildFileOrFolders(folderObject)) {
        type = fileObject.getType();
        if (type == FileType.FOLDER) {
            deleteOlderRecursive(deleteIdSet, fileObject, timestamp);
            synchronized (this) {
                if (!VFSUtils.hasAnyChildFileOrFolder(fileObject)) {
                    fileObject.delete();
                }
            }
        } else if (type == FileType.FILE) {
            long lastModified = getLastModifiedTime(fileObject);
            if (lastModified < timestamp) {
                identifier = new DataIdentifier(fileObject.getName().getBaseName());
                if (getDataStore().confirmDelete(identifier)) {
                    getDataStore().deleteFromCache(identifier);
                    if (LOG.isInfoEnabled()) {
                        LOG.info("Deleting old file " + fileObject.getName().getFriendlyURI() + " modified: " + new Timestamp(lastModified).toString() + " length: " + fileObject.getContent().getSize());
                    }
                    if (deleteRecordFileObject(fileObject)) {
                        deleteIdSet.add(identifier);
                    } else {
                        LOG.warn("Failed to delete old file " + fileObject.getName().getFriendlyURI());
                    }
                }
            }
        }
    }
}
Also used : DataIdentifier(org.apache.jackrabbit.core.data.DataIdentifier) FileType(org.apache.commons.vfs2.FileType) FileObject(org.apache.commons.vfs2.FileObject) Timestamp(java.sql.Timestamp)

Example 28 with FileObject

use of org.apache.commons.vfs2.FileObject in project jackrabbit by apache.

the class VFSBackend method deleteEmptyParentFolders.

/**
     * Deletes the parent folders of {@code fileObject} if a parent folder is empty.
     * @param fileObject fileObject to start with
     * @throws DataStoreException if any file system exception occurs
     */
private void deleteEmptyParentFolders(FileObject fileObject) throws DataStoreException {
    try {
        String baseFolderUri = getBaseFolderObject().getName().getFriendlyURI() + "/";
        FileObject parentFolder = fileObject.getParent();
        // child of the base directory and if it is empty
        while (parentFolder.getName().getFriendlyURI().startsWith(baseFolderUri)) {
            if (VFSUtils.hasAnyChildFileOrFolder(parentFolder)) {
                break;
            }
            boolean deleted = parentFolder.delete();
            LOG.debug("Deleted parent folder [{}] of file [{}]: {}", new Object[] { parentFolder, fileObject.getName().getFriendlyURI(), deleted });
            parentFolder = parentFolder.getParent();
        }
    } catch (IOException e) {
        LOG.warn("Error in parents deletion for " + fileObject.getName().getFriendlyURI(), e);
    }
}
Also used : FileObject(org.apache.commons.vfs2.FileObject) IOException(java.io.IOException)

Example 29 with FileObject

use of org.apache.commons.vfs2.FileObject in project jackrabbit by apache.

the class VFSUtils method getChildrenOfTypes.

private static List<FileObject> getChildrenOfTypes(FileObject folderObject, Set<FileType> fileTypes) throws DataStoreException {
    try {
        String folderBaseName = folderObject.getName().getBaseName();
        FileObject[] children = folderObject.getChildren();
        List<FileObject> files = new ArrayList<FileObject>(children.length);
        String childBaseName;
        for (int i = 0; i < children.length; i++) {
            childBaseName = children[i].getName().getBaseName();
            FileType fileType = null;
            try {
                fileType = children[i].getType();
            } catch (FileSystemException notDetermineTypeEx) {
                if (folderBaseName.equals(childBaseName)) {
                // Ignore this case.
                // Some WebDAV server or VFS seems to include the folder itself as child as imaginary file type,
                // and throw FileSystemException saying "Could not determine the type of file" in this case.
                } else {
                    throw notDetermineTypeEx;
                }
            }
            if (fileType != null && fileTypes.contains(fileType)) {
                files.add(children[i]);
            }
        }
        return files;
    } catch (FileSystemException e) {
        throw new DataStoreException("Could not find children under " + folderObject.getName().getFriendlyURI(), e);
    }
}
Also used : FileSystemException(org.apache.commons.vfs2.FileSystemException) DataStoreException(org.apache.jackrabbit.core.data.DataStoreException) FileType(org.apache.commons.vfs2.FileType) ArrayList(java.util.ArrayList) FileObject(org.apache.commons.vfs2.FileObject)

Example 30 with FileObject

use of org.apache.commons.vfs2.FileObject in project metron by apache.

the class VFSClassloaderUtil method resolve.

/**
 * Resolve a set of URIs into FileObject objects.
 * This is not recursive. The URIs can refer directly to a file or directory or an optional regex at the end.
 * (NOTE: This is NOT a glob).
 * @param vfs The file system manager to use to resolve URIs
 * @param uris comma separated URIs and URI + globs
 * @return
 * @throws FileSystemException
 */
static FileObject[] resolve(FileSystemManager vfs, String uris) throws FileSystemException {
    if (uris == null) {
        return new FileObject[0];
    }
    ArrayList<FileObject> classpath = new ArrayList<>();
    for (String path : uris.split(",")) {
        path = path.trim();
        if (path.equals("")) {
            continue;
        }
        FileObject fo = vfs.resolveFile(path);
        switch(fo.getType()) {
            case FILE:
            case FOLDER:
                classpath.add(fo);
                break;
            case IMAGINARY:
                // assume its a pattern
                String pattern = fo.getName().getBaseName();
                if (fo.getParent() != null && fo.getParent().getType() == FileType.FOLDER) {
                    FileObject[] children = fo.getParent().getChildren();
                    for (FileObject child : children) {
                        if (child.getType() == FileType.FILE && child.getName().getBaseName().matches(pattern)) {
                            classpath.add(child);
                        }
                    }
                } else {
                    LOG.warn("ignoring classpath entry {}", fo);
                }
                break;
            default:
                LOG.warn("ignoring classpath entry {}", fo);
                break;
        }
    }
    return classpath.toArray(new FileObject[classpath.size()]);
}
Also used : ArrayList(java.util.ArrayList) FileObject(org.apache.commons.vfs2.FileObject)

Aggregations

FileObject (org.apache.commons.vfs2.FileObject)39 FileSystemException (org.apache.commons.vfs2.FileSystemException)14 IOException (java.io.IOException)8 DataStoreException (org.apache.jackrabbit.core.data.DataStoreException)8 Session (org.ow2.proactive_grid_cloud_portal.common.Session)7 ArrayList (java.util.ArrayList)4 InputStream (java.io.InputStream)3 URI (java.net.URI)3 HttpSession (javax.servlet.http.HttpSession)3 FileSystemManager (org.apache.commons.vfs2.FileSystemManager)3 FileType (org.apache.commons.vfs2.FileType)3 TaskFlowJob (org.ow2.proactive.scheduler.common.job.TaskFlowJob)3 Gson (com.google.gson.Gson)2 GsonBuilder (com.google.gson.GsonBuilder)2 FileInputStream (java.io.FileInputStream)2 OutputStream (java.io.OutputStream)2 URISyntaxException (java.net.URISyntaxException)2 LinkedList (java.util.LinkedList)2 ExecutionException (java.util.concurrent.ExecutionException)2 FileSelector (org.apache.commons.vfs2.FileSelector)2