commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From adammurd...@apache.org
Subject cvs commit: jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider DelegateFileObject.java
Date Sat, 23 Nov 2002 00:14:45 GMT
adammurdoch    2002/11/22 16:14:45

  Modified:    vfs/src/java/org/apache/commons/vfs/impl
                        VirtualFileSystem.java
               vfs/src/java/org/apache/commons/vfs/provider
                        DelegateFileObject.java
  Log:
  VirtualFileSystem:
  - Removed support for nested junctions, at least until non-nested junctions are handled
a little better.
  - addJunction() 'creates' the ancestors of the junction point.
  
  Revision  Changes    Path
  1.2       +93 -38    jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/impl/VirtualFileSystem.java
  
  Index: VirtualFileSystem.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/impl/VirtualFileSystem.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- VirtualFileSystem.java	1 Nov 2002 03:27:30 -0000	1.1
  +++ VirtualFileSystem.java	23 Nov 2002 00:14:45 -0000	1.2
  @@ -59,11 +59,13 @@
   import org.apache.commons.vfs.FileName;
   import org.apache.commons.vfs.FileSystemException;
   import org.apache.commons.vfs.NameScope;
  +import org.apache.commons.vfs.Capability;
   import org.apache.commons.vfs.provider.AbstractFileSystem;
   import org.apache.commons.vfs.provider.DelegateFileObject;
   import java.util.Map;
   import java.util.HashMap;
   import java.util.Iterator;
  +import java.util.Collection;
   
   /**
    * A logical file system, made up of set of junctions, or links, to files from
  @@ -71,6 +73,8 @@
    *
    * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a>
    * @version $Revision$ $Date$
  + *
  + * @todo Handle nested junctions.
    */
   public class VirtualFileSystem
       extends AbstractFileSystem
  @@ -83,73 +87,124 @@
       }
   
       /**
  +     * Adds the capabilities of this file system.
  +     */
  +    protected void addCapabilities( final Collection caps )
  +    {
  +        // TODO - this isn't really true
  +        caps.add( Capability.ATTRIBUTES );
  +        caps.add( Capability.CREATE );
  +        caps.add( Capability.DELETE );
  +        caps.add( Capability.JUNCTIONS );
  +        caps.add( Capability.LAST_MODIFIED );
  +        caps.add( Capability.LIST_CHILDREN );
  +        caps.add( Capability.READ_CONTENT );
  +        caps.add( Capability.SIGNING );
  +        caps.add( Capability.WRITE_CONTENT );
  +    }
  +
  +    /**
        * Creates a file object.  This method is called only if the requested
        * file is not cached.
        */
       protected FileObject createFile( final FileName name )
           throws FileSystemException
       {
  -        FileObject realFile;
  -
  -        // Check if junction point itself
  -        realFile = (FileObject)junctions.get( name );
  -        if ( realFile == null )
  -        {
  -            // Find longest matching junction
  -            FileName matchingName = null;
  -            FileObject matchingFile = null;
  -            for ( Iterator iterator = junctions.entrySet().iterator(); iterator.hasNext();
)
  -            {
  -                final Map.Entry entry = (Map.Entry)iterator.next();
  -                final FileName junctionPoint = (FileName)entry.getKey();
  -                if ( junctionPoint.isDescendent( name )
  -                     && ( matchingName == null || matchingName.isDescendent( junctionPoint
) ) )
  -                {
  -                    matchingName = junctionPoint;
  -                    matchingFile = (FileObject)entry.getValue();
  -                }
  -            }
  -
  -            if ( matchingName != null )
  -            {
  -                // Lookup the name
  -                final String relName = matchingName.getRelativeName( name );
  -                realFile = matchingFile.resolveFile( relName, NameScope.DESCENDENT );
  -            }
  +        // 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 = (FileObject)junctions.get( junctionPoint );
  +            final String relName = junctionPoint.getRelativeName( name );
  +            file = junctionFile.resolveFile( relName, NameScope.DESCENDENT_OR_SELF );
  +        }
  +        else
  +        {
  +            file = null;
           }
   
  -        return new DelegateFileObject( name, this, realFile );
  +        // Return a wrapper around the file
  +        return new DelegateFileObject( name, this, file );
       }
   
       /**
        * Adds a junction to this file system.
        */
  -    public void addJunction( final FileName junctionPoint,
  +    public void addJunction( final String junctionPoint,
                                final FileObject targetFile )
           throws FileSystemException
       {
  -        if ( !getRootName().getRootURI().equals( junctionPoint.getRootURI() ) )
  +        final FileName junctionName = getRootName().resolveName( junctionPoint );
  +
  +        // Check for nested junction - these are not supported yet
  +        if ( getJunctionForFile( junctionName ) != null )
           {
  -            throw new FileSystemException( "vfs.provider/mismatched-fs-for-name.error",
new Object[] { junctionPoint, getRootName() } );
  +            throw new FileSystemException( "impl/nested-junction.error", junctionName );
           }
   
  -        if ( junctions.containsKey( junctionPoint ) )
  +        // Add to junction table
  +        junctions.put( junctionName, targetFile );
  +
  +        // Create ancestors of junction point
  +        FileName childName = junctionName;
  +        boolean done = false;
  +        for ( FileName parentName = childName.getParent();
  +              !done && parentName != null;
  +              childName = parentName, parentName = parentName.getParent() )
           {
  -            // TODO - something
  +            DelegateFileObject file = (DelegateFileObject)getFile( parentName );
  +            if ( file == null )
  +            {
  +                file = new DelegateFileObject( parentName, this, null );
  +                putFile( file );
  +            }
  +            else
  +            {
  +                done = file.exists();
  +            }
  +            file.attachChild( childName.getBaseName() );
           }
  -        junctions.put( junctionPoint, targetFile );
   
  -        // TODO - merge into ancestors of junction point
  +        // TODO - attach all cached children of the junction point to their real file
       }
   
       /**
        * Removes a junction from this file system.
        */
  -    public void removeJuntion( final FileName junctionPoint )
  +    public void removeJuntion( final String junctionPoint )
           throws FileSystemException
       {
  -        junctions.remove( junctionPoint );
  +        final FileName junctionName = getRootName().resolveName( junctionPoint );
  +        junctions.remove( junctionName );
   
           // TODO - remove from parents of junction point
  +        // TODO - detach all cached children of the junction point from their real file
  +    }
  +
  +    /**
  +     * Locates the junction point for the junction containing the given file.
  +     */
  +    private FileName getJunctionForFile( final FileName name )
  +    {
  +        if ( junctions.containsKey( name ) )
  +        {
  +            // The name points to the junction point directly
  +            return name;
  +        }
  +
  +        // Find matching junction
  +        for ( Iterator iterator = junctions.keySet().iterator(); iterator.hasNext(); )
  +        {
  +            final FileName junctionPoint = (FileName)iterator.next();
  +            if ( junctionPoint.isDescendent( name ) )
  +            {
  +                return junctionPoint;
  +            }
  +        }
  +
  +        // None
  +        return null;
       }
   }
  
  
  
  1.2       +58 -13    jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/DelegateFileObject.java
  
  Index: DelegateFileObject.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/DelegateFileObject.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- DelegateFileObject.java	1 Nov 2002 03:27:30 -0000	1.1
  +++ DelegateFileObject.java	23 Nov 2002 00:14:45 -0000	1.2
  @@ -58,6 +58,8 @@
   import java.io.InputStream;
   import java.io.OutputStream;
   import java.security.cert.Certificate;
  +import java.util.HashSet;
  +import java.util.Set;
   import org.apache.commons.vfs.FileChangeEvent;
   import org.apache.commons.vfs.FileListener;
   import org.apache.commons.vfs.FileName;
  @@ -72,12 +74,14 @@
    * @version $Revision$ $Date$
    *
    * @todo Deal with case where backing file == null
  + * @todo Extract subclass that overlays the children
    */
   public class DelegateFileObject
       extends AbstractFileObject
       implements FileListener
   {
  -    private final FileObject file;
  +    private FileObject file;
  +    private final Set children = new HashSet();
   
       public DelegateFileObject( final FileName name,
                                  final AbstractFileSystem fileSystem,
  @@ -87,6 +91,21 @@
           this.file = file;
       }
   
  +    public void attachChild( final String baseName ) throws FileSystemException
  +    {
  +        if ( children.add( baseName ) )
  +        {
  +            detach();
  +        }
  +    }
  +
  +    public void setFile( final FileObject file ) throws FileSystemException
  +    {
  +        this.file = file;
  +        children.clear();
  +        detach();
  +    }
  +
       /**
        * Attaches this file object to its file resource.
        */
  @@ -115,14 +134,19 @@
        */
       protected FileType doGetType() throws Exception
       {
  -        if ( file != null && file.exists() )
  +        if ( file != null )
           {
  -            return file.getType();
  +            if ( file.exists() )
  +            {
  +                return file.getType();
  +            }
           }
  -        else
  +        else if ( children.size() > 0 )
           {
  -            return null;
  +            return FileType.FOLDER;
           }
  +
  +        return null;
       }
   
       /**
  @@ -130,7 +154,14 @@
        */
       protected boolean doIsReadable() throws FileSystemException
       {
  -        return file.isReadable();
  +        if ( file != null )
  +        {
  +            return file.isReadable();
  +        }
  +        else
  +        {
  +            return true;
  +        }
       }
   
       /**
  @@ -138,7 +169,14 @@
        */
       protected boolean doIsWriteable() throws FileSystemException
       {
  -        return file.isWriteable();
  +        if ( file != null )
  +        {
  +            return file.isWriteable();
  +        }
  +        else
  +        {
  +            return false;
  +        }
       }
   
       /**
  @@ -146,13 +184,20 @@
        */
       protected String[] doListChildren() throws Exception
       {
  -        final FileObject[] children = file.getChildren();
  -        final String[] childNames = new String[ children.length ];
  -        for ( int i = 0; i < children.length; i++ )
  +        if ( file != null )
  +        {
  +            final FileObject[] children = file.getChildren();
  +            final String[] childNames = new String[ children.length ];
  +            for ( int i = 0; i < children.length; i++ )
  +            {
  +                childNames[ i ] = children[ i ].getName().getBaseName();
  +            }
  +            return childNames;
  +        }
  +        else
           {
  -            childNames[ i ] = children[ i ].getName().getBaseName();
  +            return (String[])children.toArray( new String[ children.size() ] );
           }
  -        return childNames;
       }
   
       /**
  
  
  

--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message