Search in sources :

Example 16 with FileObject

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

the class VFSBackend method deleteRecord.

/**
     * {@inheritDoc}
     */
@Override
public void deleteRecord(DataIdentifier identifier) throws DataStoreException {
    FileObject fileObject = getExistingFileObject(identifier);
    if (fileObject != null) {
        deleteRecordFileObject(fileObject);
        deleteEmptyParentFolders(fileObject);
    }
}
Also used : FileObject(org.apache.commons.vfs2.FileObject)

Example 17 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 18 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 19 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 20 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)

Aggregations

FileObject (org.apache.commons.vfs2.FileObject)19 DataStoreException (org.apache.jackrabbit.core.data.DataStoreException)8 IOException (java.io.IOException)7 FileSystemException (org.apache.commons.vfs2.FileSystemException)7 FileType (org.apache.commons.vfs2.FileType)3 Gson (com.google.gson.Gson)2 GsonBuilder (com.google.gson.GsonBuilder)2 InputStream (java.io.InputStream)2 OutputStream (java.io.OutputStream)2 URISyntaxException (java.net.URISyntaxException)2 LinkedList (java.util.LinkedList)2 DataIdentifier (org.apache.jackrabbit.core.data.DataIdentifier)2 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 URI (java.net.URI)1 Timestamp (java.sql.Timestamp)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 RepositoryException (javax.jcr.RepositoryException)1 FileContent (org.apache.commons.vfs2.FileContent)1