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 Fri, 01 Nov 2002 03:27:30 GMT
adammurdoch    2002/10/31 19:27:30

  Added:       vfs/src/java/org/apache/commons/vfs/impl
                        VirtualFileProvider.java VirtualFileSystem.java
               vfs/src/java/org/apache/commons/vfs/provider
                        DelegateFileObject.java
  Log:
  Added a purely virtual FileSystem implementation, that provides some initial support for
junctions.
  
  Revision  Changes    Path
  1.1                  jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/impl/VirtualFileProvider.java
  
  Index: VirtualFileProvider.java
  ===================================================================
  /* ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2002 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   */
  package org.apache.commons.vfs.impl;
  
  import org.apache.commons.vfs.FileName;
  import org.apache.commons.vfs.FileObject;
  import org.apache.commons.vfs.FileSystemException;
  import org.apache.commons.vfs.provider.AbstractVfsContainer;
  import org.apache.commons.vfs.provider.DefaultFileName;
  import org.apache.commons.vfs.provider.UriParser;
  
  
  /**
   * A virtual filesystem provider.
   *
   * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a>
   * @version $Revision: 1.1 $ $Date: 2002/11/01 03:27:30 $
   */
  public class VirtualFileProvider
      extends AbstractVfsContainer
  {
      private final UriParser parser = new UriParser();
  
      /**
       * Creates a virtual file system, with the supplied file as its root.
       */
      public FileObject createFileSystem( final FileObject rootFile )
          throws FileSystemException
      {
          final FileName rootName = new DefaultFileName( parser, rootFile.getName().getURI(),
"/" );
          final VirtualFileSystem fs = new VirtualFileSystem( rootName );
          addComponent( fs );
          fs.addJunction( rootName, rootFile );
          return fs.getRoot();
      }
  
      /**
       * Creates an empty virtual file system.
       */
      public FileObject createFileSystem( final String rootUri ) throws FileSystemException
      {
          final FileName rootName = new DefaultFileName( parser, rootUri, "/" );
          final VirtualFileSystem fs = new VirtualFileSystem( rootName );
          addComponent( fs );
          return fs.getRoot();
      }
  }
  
  
  
  1.1                  jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/impl/VirtualFileSystem.java
  
  Index: VirtualFileSystem.java
  ===================================================================
  /* ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2002 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   */
  package org.apache.commons.vfs.impl;
  
  import org.apache.commons.vfs.FileObject;
  import org.apache.commons.vfs.FileName;
  import org.apache.commons.vfs.FileSystemException;
  import org.apache.commons.vfs.NameScope;
  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;
  
  /**
   * A logical file system, made up of set of junctions, or links, to files from
   * other file systems.
   *
   * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a>
   * @version $Revision: 1.1 $ $Date: 2002/11/01 03:27:30 $
   */
  public class VirtualFileSystem
      extends AbstractFileSystem
  {
      private final Map junctions = new HashMap();
  
      public VirtualFileSystem( final FileName rootName )
      {
          super( rootName, null );
      }
  
      /**
       * 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 );
              }
          }
  
          return new DelegateFileObject( name, this, realFile );
      }
  
      /**
       * Adds a junction to this file system.
       */
      public void addJunction( final FileName junctionPoint,
                               final FileObject targetFile )
          throws FileSystemException
      {
          if ( !getRootName().getRootURI().equals( junctionPoint.getRootURI() ) )
          {
              throw new FileSystemException( "vfs.provider/mismatched-fs-for-name.error",
new Object[] { junctionPoint, getRootName() } );
          }
  
          if ( junctions.containsKey( junctionPoint ) )
          {
              // TODO - something
          }
          junctions.put( junctionPoint, targetFile );
  
          // TODO - merge into ancestors of junction point
      }
  
      /**
       * Removes a junction from this file system.
       */
      public void removeJuntion( final FileName junctionPoint )
          throws FileSystemException
      {
          junctions.remove( junctionPoint );
  
          // TODO - remove from parents of junction point
      }
  }
  
  
  
  1.1                  jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/DelegateFileObject.java
  
  Index: DelegateFileObject.java
  ===================================================================
  /* ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2002 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   */
  package org.apache.commons.vfs.provider;
  
  import java.io.InputStream;
  import java.io.OutputStream;
  import java.security.cert.Certificate;
  import org.apache.commons.vfs.FileChangeEvent;
  import org.apache.commons.vfs.FileListener;
  import org.apache.commons.vfs.FileName;
  import org.apache.commons.vfs.FileObject;
  import org.apache.commons.vfs.FileSystemException;
  import org.apache.commons.vfs.FileType;
  
  /**
   * A file backed by another file.
   *
   * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a>
   * @version $Revision: 1.1 $ $Date: 2002/11/01 03:27:30 $
   *
   * @todo Deal with case where backing file == null
   */
  public class DelegateFileObject
      extends AbstractFileObject
      implements FileListener
  {
      private final FileObject file;
  
      public DelegateFileObject( final FileName name,
                                 final AbstractFileSystem fileSystem,
                                 final FileObject file )
      {
          super( name, fileSystem );
          this.file = file;
      }
  
      /**
       * Attaches this file object to its file resource.
       */
      protected void doAttach() throws Exception
      {
          if ( file != null )
          {
              file.getFileSystem().addListener( file, this );
          }
      }
  
      /**
       * Detaches this file object from its file resource.
       */
      protected void doDetach() throws FileSystemException
      {
          if ( file != null )
          {
              file.getFileSystem().removeListener( file, this );
          }
      }
  
      /**
       * Determines the type of the file, returns null if the file does not
       * exist.
       */
      protected FileType doGetType() throws Exception
      {
          if ( file != null && file.exists() )
          {
              return file.getType();
          }
          else
          {
              return null;
          }
      }
  
      /**
       * Determines if this file can be read.
       */
      protected boolean doIsReadable() throws FileSystemException
      {
          return file.isReadable();
      }
  
      /**
       * Determines if this file can be written to.
       */
      protected boolean doIsWriteable() throws FileSystemException
      {
          return file.isWriteable();
      }
  
      /**
       * Lists the children of the file.
       */
      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++ )
          {
              childNames[ i ] = children[ i ].getName().getBaseName();
          }
          return childNames;
      }
  
      /**
       * Creates this file as a folder.
       */
      protected void doCreateFolder() throws Exception
      {
          file.createFolder();
      }
  
      /**
       * Deletes the file.
       */
      protected void doDelete() throws Exception
      {
          file.delete();
      }
  
      /**
       * Returns the size of the file content (in bytes).  Is only called if
       * {@link #doGetType} returns {@link FileType#FILE}.
       */
      protected long doGetContentSize() throws Exception
      {
          return file.getContent().getSize();
      }
  
      /**
       * Called from {@link DefaultFileContent#getAttribute}.
       */
      protected Object doGetAttribute( final String attrName )
          throws Exception
      {
          return file.getContent().getAttribute( attrName );
      }
  
      /**
       * Called from {@link DefaultFileContent#setAttribute}.
       */
      protected void doSetAttribute( final String atttrName,
                                     final Object value )
          throws Exception
      {
          file.getContent().setAttribute( atttrName, value );
      }
  
      /**
       * Called from {@link DefaultFileContent#getCertificates}.
       */
      protected Certificate[] doGetCertificates() throws FileSystemException
      {
          return file.getContent().getCertificates();
      }
  
      /**
       * Called from {@link DefaultFileContent#getLastModifiedTime}.
       */
      protected long doGetLastModifiedTime() throws Exception
      {
          return file.getContent().getLastModifiedTime();
      }
  
      /**
       * Called from {@link DefaultFileContent#setLastModifiedTime}.
       */
      protected void doSetLastModifiedTime( final long modtime )
          throws Exception
      {
          file.getContent().setLastModifiedTime( modtime );
      }
  
      /**
       * Creates an input stream to read the file content from.
       */
      protected InputStream doGetInputStream() throws Exception
      {
          return file.getContent().getInputStream();
      }
  
      /**
       * Creates an output stream to write the file content to.
       */
      protected OutputStream doGetOutputStream() throws Exception
      {
          return file.getContent().getOutputStream();
      }
  
      /**
       * Called when a file is created.
       */
      public void fileCreated( final FileChangeEvent event ) throws FileSystemException
      {
          handleCreate( file.getType() );
      }
  
      /**
       * Called when a file is deleted.
       */
      public void fileDeleted( final FileChangeEvent event )
      {
          handleDelete();
      }
  }
  
  
  

--
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