Search in sources :

Example 1 with DelegateFileObject

use of org.apache.commons.vfs2.provider.DelegateFileObject in project commons-vfs by apache.

the class VirtualFileSystem method addJunction.

/**
 * Adds a junction to this file system.
 *
 * @param junctionPoint The location of the junction.
 * @param targetFile The target file to base the junction on.
 * @throws FileSystemException if an error occurs.
 */
@Override
public void addJunction(final String junctionPoint, final FileObject targetFile) throws FileSystemException {
    final FileName junctionName = getFileSystemManager().resolveName(getRootName(), junctionPoint);
    // Check for nested junction - these are not supported yet
    if (getJunctionForFile(junctionName) != null) {
        throw new FileSystemException("vfs.impl/nested-junction.error", junctionName);
    }
    try {
        // Add to junction table
        junctions.put(junctionName, targetFile);
        // Attach to file
        final DelegateFileObject junctionFile = (DelegateFileObject) getFileFromCache(junctionName);
        if (junctionFile != null) {
            junctionFile.setFile(targetFile);
        }
        // Create ancestors of junction point
        FileName childName = junctionName;
        boolean done = false;
        for (AbstractFileName parentName = (AbstractFileName) childName.getParent(); !done && parentName != null; childName = parentName, parentName = (AbstractFileName) parentName.getParent()) {
            DelegateFileObject file = (DelegateFileObject) getFileFromCache(parentName);
            if (file == null) {
                file = new DelegateFileObject(parentName, this, null);
                putFileToCache(file);
            } else {
                done = file.exists();
            }
            // As this is the parent of our junction it has to be a folder
            file.attachChild(childName, FileType.FOLDER);
        }
    // TODO - attach all cached children of the junction point to their real file
    } catch (final Exception e) {
        throw new FileSystemException("vfs.impl/create-junction.error", junctionName, e);
    }
}
Also used : FileSystemException(org.apache.commons.vfs2.FileSystemException) AbstractFileName(org.apache.commons.vfs2.provider.AbstractFileName) FileName(org.apache.commons.vfs2.FileName) DelegateFileObject(org.apache.commons.vfs2.provider.DelegateFileObject) AbstractFileName(org.apache.commons.vfs2.provider.AbstractFileName) FileSystemException(org.apache.commons.vfs2.FileSystemException)

Example 2 with DelegateFileObject

use of org.apache.commons.vfs2.provider.DelegateFileObject in project commons-vfs by apache.

the class VirtualFileSystem method createFile.

/**
 * Creates a file object. This method is called only if the requested file is not cached.
 */
@Override
protected FileObject createFile(final AbstractFileName name) throws Exception {
    // Find the file that the name points to
    final FileName junctionPoint = getJunctionForFile(name);
    final FileObject file;
    if (junctionPoint != null) {
        // Resolve the real file
        final FileObject junctionFile = junctions.get(junctionPoint);
        final String relName = junctionPoint.getRelativeName(name);
        file = junctionFile.resolveFile(relName, NameScope.DESCENDENT_OR_SELF);
    } else {
        file = null;
    }
    // Return a wrapper around the file
    return new DelegateFileObject(name, this, file);
}
Also used : AbstractFileName(org.apache.commons.vfs2.provider.AbstractFileName) FileName(org.apache.commons.vfs2.FileName) DelegateFileObject(org.apache.commons.vfs2.provider.DelegateFileObject) FileObject(org.apache.commons.vfs2.FileObject) DelegateFileObject(org.apache.commons.vfs2.provider.DelegateFileObject)

Aggregations

FileName (org.apache.commons.vfs2.FileName)2 AbstractFileName (org.apache.commons.vfs2.provider.AbstractFileName)2 DelegateFileObject (org.apache.commons.vfs2.provider.DelegateFileObject)2 FileObject (org.apache.commons.vfs2.FileObject)1 FileSystemException (org.apache.commons.vfs2.FileSystemException)1