You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by ad...@apache.org on 2002/10/25 13:05:08 UTC

cvs commit: jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/impl DefaultFileReplicator.java DefaultFileSystemManager.java DefaultProviderContext.java

adammurdoch    2002/10/25 04:05:08

  Modified:    vfs/src/java/org/apache/commons/vfs/provider
                        FileSystemProviderContext.java
               vfs/src/java/org/apache/commons/vfs/impl
                        DefaultFileReplicator.java
                        DefaultFileSystemManager.java
                        DefaultProviderContext.java
  Added:       vfs/src/java/org/apache/commons/vfs/provider
                        TemporaryFileStore.java
  Log:
  Added TemporaryFileStore, a service that providers can use for keeping
  local temporary files.
  
  Revision  Changes    Path
  1.5       +6 -1      jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/FileSystemProviderContext.java
  
  Index: FileSystemProviderContext.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/FileSystemProviderContext.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- FileSystemProviderContext.java	23 Oct 2002 11:59:40 -0000	1.4
  +++ FileSystemProviderContext.java	25 Oct 2002 11:05:08 -0000	1.5
  @@ -91,8 +91,13 @@
       FileReplicator getReplicator() throws FileSystemException;
   
       /**
  +     * Locates a temporary file store for the provider to use.
  +     */
  +    TemporaryFileStore getTemporaryFileStore() throws FileSystemException;
  +    
  +    /**
        * Returns a {@link FileObject} for a local file.
        */
  -    FileObject getFile( File file )
  +    FileObject toFileObject( File file )
           throws FileSystemException;
   }
  
  
  
  1.1                  jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/TemporaryFileStore.java
  
  Index: TemporaryFileStore.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.File;
  import org.apache.commons.vfs.FileSystemException;
  
  /**
   * Manages a repository of temporary local files.
   *
   * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a>
   * @version $Revision: 1.1 $ $Date: 2002/10/25 11:05:08 $
   */
  public interface TemporaryFileStore
  {
      /**
       * Allocates a new temporary file.  The file (and all its descendents)
       * will be deleted when this store is closed.
       *
       * @param basename  The name of the file.
       */
      File allocateFile( String basename ) throws FileSystemException;
  }
  
  
  
  1.11      +30 -14    jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/impl/DefaultFileReplicator.java
  
  Index: DefaultFileReplicator.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/impl/DefaultFileReplicator.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- DefaultFileReplicator.java	23 Oct 2002 11:59:40 -0000	1.10
  +++ DefaultFileReplicator.java	25 Oct 2002 11:05:08 -0000	1.11
  @@ -65,28 +65,41 @@
   import org.apache.commons.vfs.util.Messages;
   import org.apache.commons.vfs.provider.AbstractVfsComponent;
   import org.apache.commons.vfs.provider.FileReplicator;
  +import org.apache.commons.vfs.provider.TemporaryFileStore;
   
   /**
  - * A simple file replicator.
  + * A simple file replicator and temporary file store.
    *
    * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a>
    * @version $Revision$ $Date$
    */
   public final class DefaultFileReplicator
       extends AbstractVfsComponent
  -    implements FileReplicator
  +    implements FileReplicator, TemporaryFileStore
   {
   
       private final ArrayList copies = new ArrayList();
       private File tempDir;
       private long filecount;
   
  +    public DefaultFileReplicator( final File tempDir )
  +    {
  +        this.tempDir = tempDir;
  +    }
  +
  +    public DefaultFileReplicator()
  +    {
  +    }
  +
       /**
        * Initialises this component.
        */
       public void init() throws FileSystemException
       {
  -        tempDir = new File( "vfs_cache" ).getAbsoluteFile();
  +        if ( tempDir == null )
  +        {
  +            tempDir = new File( "vfs_cache" ).getAbsoluteFile();
  +        }
           filecount = new Random().nextInt() & 0xffff;
       }
   
  @@ -98,10 +111,11 @@
           // Delete the temporary files
           while ( copies.size() > 0 )
           {
  -            final FileObject file = (FileObject)copies.remove( 0 );
  +            final File file = (File)copies.remove( 0 );
               try
               {
  -                file.delete( Selectors.SELECT_ALL );
  +                final FileObject fileObject = getContext().toFileObject( file );
  +                fileObject.delete( Selectors.SELECT_ALL );
               }
               catch ( final FileSystemException e )
               {
  @@ -119,14 +133,19 @@
       }
   
       /**
  -     * Generates a new temp file name.
  +     * Allocates a new temporary file.
        */
  -    private File generateTempFile( String prefix )
  +    public File allocateFile( final String baseName )
       {
           // Create a unique-ish file name
  -        final String basename = prefix + "_" + filecount + ".tmp";
  +        final String basename = baseName + "_" + filecount + ".tmp";
           filecount++;
  -        return new File( tempDir, basename );
  +        final File file = new File( tempDir, basename );
  +
  +        // Keep track to delete later
  +        copies.add( file );
  +
  +        return file;
       }
   
       /**
  @@ -137,14 +156,11 @@
           throws FileSystemException
       {
           final String basename = srcFile.getName().getBaseName();
  -        final File file = generateTempFile( basename );
  +        final File file = allocateFile( basename );
   
           // Copy from the source file
  -        final FileObject destFile = getContext().getFile( file );
  +        final FileObject destFile = getContext().toFileObject( file );
           destFile.copyFrom( srcFile, selector );
  -
  -        // Keep track of the copy
  -        copies.add( destFile );
   
           return file;
       }
  
  
  
  1.13      +48 -18    jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/impl/DefaultFileSystemManager.java
  
  Index: DefaultFileSystemManager.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/impl/DefaultFileSystemManager.java,v
  retrieving revision 1.12
  retrieving revision 1.13
  diff -u -r1.12 -r1.13
  --- DefaultFileSystemManager.java	25 Oct 2002 03:57:29 -0000	1.12
  +++ DefaultFileSystemManager.java	25 Oct 2002 11:05:08 -0000	1.13
  @@ -61,6 +61,7 @@
   import java.util.ArrayList;
   import java.util.HashMap;
   import java.util.Map;
  +import java.util.Iterator;
   import org.apache.commons.logging.Log;
   import org.apache.commons.logging.LogFactory;
   import org.apache.commons.vfs.FileObject;
  @@ -72,6 +73,7 @@
   import org.apache.commons.vfs.provider.LocalFileProvider;
   import org.apache.commons.vfs.provider.UriParser;
   import org.apache.commons.vfs.provider.VfsComponent;
  +import org.apache.commons.vfs.provider.TemporaryFileStore;
   
   /**
    * A default file system manager implementation.  To use this class:
  @@ -121,6 +123,8 @@
       private final DefaultProviderContext context =
           new DefaultProviderContext( this );
   
  +    private TemporaryFileStore tempFileStore;
  +
       /**
        * Returns the logger used by this manager.
        */
  @@ -206,6 +210,17 @@
       }
   
       /**
  +     * Sets the temporary file store to use.  The manager takes care of all
  +     * lifecycle management.
  +     */
  +    public void setTemporaryFileStore( final TemporaryFileStore tempFileStore )
  +        throws FileSystemException
  +    {
  +        setupComponent( tempFileStore );
  +        this.tempFileStore = tempFileStore;
  +    }
  +
  +    /**
        * Sets the logger to use.
        */
       public void setLogger( final Log log )
  @@ -214,7 +229,7 @@
       }
   
       /**
  -     * Adds a component to the set of components owned by this manager.
  +     * Initialises a component, if it has not already been initialised.
        */
       private void setupComponent( final Object component )
           throws FileSystemException
  @@ -233,14 +248,18 @@
       }
   
       /**
  -     * Closes a component.
  +     * Closes a component, if it has not already been closed.
        */
       private void closeComponent( final Object component )
       {
  -        if ( component instanceof VfsComponent )
  +        if ( component != null && components.contains( component ) )
           {
  -            final VfsComponent vfsComponent = (VfsComponent)component;
  -            vfsComponent.close();
  +            if ( component instanceof VfsComponent )
  +            {
  +                final VfsComponent vfsComponent = (VfsComponent)component;
  +                vfsComponent.close();
  +            }
  +            components.remove( component );
           }
       }
   
  @@ -260,32 +279,43 @@
       }
   
       /**
  +     * Returns the temporary file store.
  +     * @return The file store.  Never returns null.
  +     */
  +    public TemporaryFileStore getTemporaryFileStore()
  +        throws FileSystemException
  +    {
  +        if ( tempFileStore == null )
  +        {
  +            throw new FileSystemException( "vfs.impl/no-temp-file-store.error" );
  +        }
  +        return tempFileStore;
  +    }
  +
  +    /**
        * Closes all files created by this manager, and cleans up any temporary
        * files.  Also closes all providers and the replicator.
        */
       public void close()
       {
  -        // Dispose the components (making sure we only dispose each provider
  -        // only once).  Close the replicator last.
  -        for ( int i = 0; i < components.size(); i++ )
  -        {
  -            Object component = components.get( i );
  -            if ( component == fileReplicator )
  -            {
  -                continue;
  -            }
  -            closeComponent( component );
  -        }
  -        if ( fileReplicator != null )
  +        // Close the providers.
  +        for ( Iterator iterator = providers.values().iterator(); iterator.hasNext(); )
           {
  -            closeComponent( fileReplicator );
  +            final Object provider = iterator.next();
  +            closeComponent( provider );
           }
   
  +        // Close the other components
  +        closeComponent( defaultProvider );
  +        closeComponent( fileReplicator );
  +        closeComponent( tempFileStore );
  +
           components.clear();
           providers.clear();
           localFileProvider = null;
           defaultProvider = null;
           fileReplicator = null;
  +        tempFileStore = null;
       }
   
       /**
  
  
  
  1.6       +10 -1     jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/impl/DefaultProviderContext.java
  
  Index: DefaultProviderContext.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/impl/DefaultProviderContext.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- DefaultProviderContext.java	23 Oct 2002 11:59:40 -0000	1.5
  +++ DefaultProviderContext.java	25 Oct 2002 11:05:08 -0000	1.6
  @@ -60,6 +60,7 @@
   import org.apache.commons.vfs.FileSystemException;
   import org.apache.commons.vfs.provider.FileReplicator;
   import org.apache.commons.vfs.provider.FileSystemProviderContext;
  +import org.apache.commons.vfs.provider.TemporaryFileStore;
   
   /**
    * A provider context implementation.
  @@ -98,7 +99,7 @@
       /**
        * Returns a {@link FileObject} for a local file.
        */
  -    public FileObject getFile( File file )
  +    public FileObject toFileObject( File file )
           throws FileSystemException
       {
           return manager.toFileObject( file );
  @@ -110,5 +111,13 @@
       public FileReplicator getReplicator() throws FileSystemException
       {
           return manager.getReplicator();
  +    }
  +
  +    /**
  +     * Locates a temporary file store for the provider to use.
  +     */
  +    public TemporaryFileStore getTemporaryFileStore() throws FileSystemException
  +    {
  +        return manager.getTemporaryFileStore();
       }
   }
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>