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);
}
}
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);
}
}
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());
}
}
}
}
}
}
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);
}
}
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);
}
}
Aggregations