Search in sources :

Example 1 with FileType

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

the class VFSTestUtils method deleteAllDescendantFiles.

/**
     * Deletes all the descendant files under the folder.
     * @param folderObject folder object
     * @throws FileSystemException if any file system exception occurs
     * @throws DataStoreException if any file system exception occurs
     */
static void deleteAllDescendantFiles(FileObject folderObject) throws FileSystemException, DataStoreException {
    List<FileObject> children = VFSUtils.getChildFileOrFolders(folderObject);
    FileType fileType;
    for (FileObject child : children) {
        fileType = child.getType();
        if (fileType == FileType.FILE) {
            boolean deleted = child.delete();
            if (!deleted) {
                LOG.warn("File not deleted: {}", child.getName().getFriendlyURI());
            }
        } else if (fileType == FileType.FOLDER) {
            deleteAllDescendantFiles(child);
        }
    }
}
Also used : FileType(org.apache.commons.vfs2.FileType) FileObject(org.apache.commons.vfs2.FileObject)

Example 2 with FileType

use of org.apache.commons.vfs2.FileType 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 3 with FileType

use of org.apache.commons.vfs2.FileType 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)

Aggregations

FileObject (org.apache.commons.vfs2.FileObject)3 FileType (org.apache.commons.vfs2.FileType)3 Timestamp (java.sql.Timestamp)1 ArrayList (java.util.ArrayList)1 FileSystemException (org.apache.commons.vfs2.FileSystemException)1 DataIdentifier (org.apache.jackrabbit.core.data.DataIdentifier)1 DataStoreException (org.apache.jackrabbit.core.data.DataStoreException)1