commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ima...@apache.org
Subject cvs commit: jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/cache SoftRefFilesCache.java
Date Fri, 09 Jul 2004 21:02:52 GMT
imario      2004/07/09 14:02:52

  Modified:    vfs/src/java/org/apache/commons/vfs/provider
                        AbstractFileProvider.java AbstractFileSystem.java
                        AbstractVfsContainer.java
               vfs/src/test/org/apache/commons/vfs/test
                        AbstractTestSuite.java
               vfs/src/java/org/apache/commons/vfs/impl
                        DefaultFileSystemManager.java
               vfs/src/java/org/apache/commons/vfs FileSystem.java
               vfs/src/java/org/apache/commons/vfs/provider/res
                        ResourceFileProvider.java
               vfs/src/java/org/apache/commons/vfs/cache
                        SoftRefFilesCache.java
  Log:
  SoftRefFilesCache
  
  Now the cache also frees any reference to the filesystem within vfs. That way, a layered
filesystem (jar, zip) could also be correctly GCed.
  
  Revision  Changes    Path
  1.14      +10 -0     jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/AbstractFileProvider.java
  
  Index: AbstractFileProvider.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/AbstractFileProvider.java,v
  retrieving revision 1.13
  retrieving revision 1.14
  diff -u -r1.13 -r1.14
  --- AbstractFileProvider.java	4 Jul 2004 18:45:55 -0000	1.13
  +++ AbstractFileProvider.java	9 Jul 2004 21:02:51 -0000	1.14
  @@ -77,6 +77,7 @@
           addComponent(fs);
   
           FileSystemKey treeKey = new FileSystemKey(key, fs.getFileSystemOptions());
  +        ((AbstractFileSystem) fs).setCacheKey(treeKey);
           fileSystems.put(treeKey, fs);
       }
   
  @@ -108,5 +109,14 @@
                   fs.closeCommunicationLink();
               }
           }
  +    }
  +
  +    public void closeFileSystem(final FileSystem filesystem)
  +    {
  +        AbstractFileSystem fs = (AbstractFileSystem) filesystem;
  +
  +        fileSystems.remove(fs.getCacheKey());
  +        removeComponent(fileSystems);
  +        fs.close();
       }
   }
  
  
  
  1.30      +15 -1     jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/AbstractFileSystem.java
  
  Index: AbstractFileSystem.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/AbstractFileSystem.java,v
  retrieving revision 1.29
  retrieving revision 1.30
  diff -u -r1.29 -r1.30
  --- AbstractFileSystem.java	7 Jul 2004 20:01:35 -0000	1.29
  +++ AbstractFileSystem.java	9 Jul 2004 21:02:51 -0000	1.30
  @@ -75,6 +75,8 @@
        */
       private long useCount;
   
  +    private FileSystemKey cacheKey;
  +
       protected AbstractFileSystem(final FileName rootName,
                                    final FileObject parentLayer,
                                    final FileSystemOptions fileSystemOptions)
  @@ -101,6 +103,8 @@
       public void close()
       {
           closeCommunicationLink();
  +
  +        parentLayer = null;
       }
   
       /**
  @@ -136,7 +140,7 @@
       /**
        * Returns the name of the root of this file system.
        */
  -    protected FileName getRootName()
  +    public FileName getRootName()
       {
           return rootName;
       }
  @@ -445,6 +449,16 @@
       {
           useCount++;
   
  +    }
  +
  +    void setCacheKey(FileSystemKey cacheKey)
  +    {
  +        this.cacheKey = cacheKey;
  +    }
  +
  +    FileSystemKey getCacheKey()
  +    {
  +        return this.cacheKey;
       }
   
       /**
  
  
  
  1.8       +9 -1      jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/AbstractVfsContainer.java
  
  Index: AbstractVfsContainer.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/AbstractVfsContainer.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- AbstractVfsContainer.java	10 May 2004 20:09:42 -0000	1.7
  +++ AbstractVfsContainer.java	9 Jul 2004 21:02:51 -0000	1.8
  @@ -58,6 +58,14 @@
       }
   
       /**
  +     * Removes a sub-component from this component.
  +     */
  +    protected void removeComponent(final Object component)
  +    {
  +        components.remove(component);
  +    }
  +
  +    /**
        * Closes the sub-components of this component.
        */
       public void close()
  
  
  
  1.5       +3 -1      jakarta-commons-sandbox/vfs/src/test/org/apache/commons/vfs/test/AbstractTestSuite.java
  
  Index: AbstractTestSuite.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/vfs/src/test/org/apache/commons/vfs/test/AbstractTestSuite.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- AbstractTestSuite.java	7 Jul 2004 20:01:35 -0000	1.4
  +++ AbstractTestSuite.java	9 Jul 2004 21:02:51 -0000	1.5
  @@ -220,6 +220,8 @@
               System.out.println(message);
               // }
           }
  +        // System.in.read();
  +
           manager.close();
   
           // Make sure temp directory is empty or gone
  
  
  
  1.36      +11 -0     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.35
  retrieving revision 1.36
  diff -u -r1.35 -r1.36
  --- DefaultFileSystemManager.java	7 Jul 2004 20:01:35 -0000	1.35
  +++ DefaultFileSystemManager.java	9 Jul 2004 21:02:52 -0000	1.36
  @@ -18,6 +18,7 @@
   import org.apache.commons.logging.Log;
   import org.apache.commons.vfs.FileContentInfoFactory;
   import org.apache.commons.vfs.FileObject;
  +import org.apache.commons.vfs.FileSystem;
   import org.apache.commons.vfs.FileSystemConfigBuilder;
   import org.apache.commons.vfs.FileSystemException;
   import org.apache.commons.vfs.FileSystemManager;
  @@ -359,6 +360,7 @@
               fileContentInfoFactory = new FileContentInfoFilenameFactory();
           }
   
  +        setupComponent(filesCache);
           setupComponent(vfsProvider);
   
           init = true;
  @@ -620,6 +622,15 @@
       public URLStreamHandlerFactory getURLStreamHandlerFactory()
       {
           return new VfsStreamHandlerFactory();
  +    }
  +
  +    public void closeFileSystem(FileSystem filesystem)
  +    {
  +        FileProvider provider = (FileProvider) providers.get(filesystem.getRootName().getScheme());
  +        if (provider != null)
  +        {
  +            ((AbstractFileProvider) provider).closeFileSystem(filesystem);
  +        }
       }
   
       /**
  
  
  
  1.18      +6 -1      jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/FileSystem.java
  
  Index: FileSystem.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/FileSystem.java,v
  retrieving revision 1.17
  retrieving revision 1.18
  diff -u -r1.17 -r1.18
  --- FileSystem.java	30 Jun 2004 19:06:38 -0000	1.17
  +++ FileSystem.java	9 Jul 2004 21:02:52 -0000	1.18
  @@ -31,6 +31,11 @@
       FileObject getRoot() throws FileSystemException;
   
       /**
  +     * Returns the name of the root file of this file system.
  +     */
  +    FileName getRootName();
  +
  +    /**
        * Determines if this file system has a particular capability.
        *
        * @param capability The capability to check for.
  
  
  
  1.3       +8 -2      jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/res/ResourceFileProvider.java
  
  Index: ResourceFileProvider.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/res/ResourceFileProvider.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- ResourceFileProvider.java	21 May 2004 20:54:37 -0000	1.2
  +++ ResourceFileProvider.java	9 Jul 2004 21:02:52 -0000	1.3
  @@ -17,6 +17,7 @@
   
   import org.apache.commons.vfs.Capability;
   import org.apache.commons.vfs.FileObject;
  +import org.apache.commons.vfs.FileSystem;
   import org.apache.commons.vfs.FileSystemConfigBuilder;
   import org.apache.commons.vfs.FileSystemException;
   import org.apache.commons.vfs.FileSystemOptions;
  @@ -30,7 +31,7 @@
   
   /**
    * Description
  - * 
  + *
    * @author <a href="mailto:imario@apache.org">Mario Ivankovits</a>
    * @version $Revision$ $Date$
    */
  @@ -77,6 +78,11 @@
       public FileSystemConfigBuilder getConfigBuilder()
       {
           return org.apache.commons.vfs.provider.res.ResourcelFileSystemConfigBuilder.getInstance();
  +    }
  +
  +    public void closeFileSystem(FileSystem filesystem)
  +    {
  +        // no filesystem created here - so nothing to do
       }
   
       public Collection getCapabilities()
  
  
  
  1.8       +21 -36    jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/cache/SoftRefFilesCache.java
  
  Index: SoftRefFilesCache.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/cache/SoftRefFilesCache.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- SoftRefFilesCache.java	7 Jul 2004 20:01:35 -0000	1.7
  +++ SoftRefFilesCache.java	9 Jul 2004 21:02:52 -0000	1.8
  @@ -21,7 +21,7 @@
   import org.apache.commons.vfs.FileObject;
   import org.apache.commons.vfs.FileSystem;
   import org.apache.commons.vfs.VfsLog;
  -import org.apache.commons.vfs.provider.AbstractFileSystem;
  +import org.apache.commons.vfs.impl.DefaultFileSystemManager;
   import org.apache.commons.vfs.util.Messages;
   
   import java.lang.ref.Reference;
  @@ -51,21 +51,25 @@
       private final Map refReverseMap = new HashMap(100);
       private final ReferenceQueue refqueue = new ReferenceQueue();
   
  -    private Thread softRefReleaseThread = null;
  +    private SoftRefReleaseThread softRefReleaseThread = null;
   
       /**
        * This thread will listen on the ReferenceQueue and remove the entry in the
        * filescache as soon as the vm removes the reference
        */
  -    private class SoftRefReleaseThread implements Runnable
  +    private class SoftRefReleaseThread extends Thread
       {
  +        private boolean requestEnd = false;
  +
           private SoftRefReleaseThread()
           {
  +            setName(SoftRefReleaseThread.class.getName());
  +            setDaemon(true);
           }
   
           public void run()
           {
  -            loop: while (!Thread.currentThread().isInterrupted())
  +            loop: while (!requestEnd && !Thread.currentThread().isInterrupted())
               {
                   try
                   {
  @@ -90,7 +94,10 @@
                   }
                   catch (InterruptedException e)
                   {
  -                    VfsLog.warn(getLogger(), log, Messages.getString("vfs.impl/SoftRefReleaseThread-interrupt.info"));
  +                    if (!requestEnd)
  +                    {
  +                        VfsLog.warn(getLogger(), log, Messages.getString("vfs.impl/SoftRefReleaseThread-interrupt.info"));
  +                    }
                       break loop;
                   }
               }
  @@ -108,8 +115,7 @@
               throw new IllegalStateException(Messages.getString("vfs.impl/SoftRefReleaseThread-already-running.warn"));
           }
   
  -        softRefReleaseThread = new Thread(new SoftRefReleaseThread());
  -        softRefReleaseThread.setDaemon(true);
  +        softRefReleaseThread = new SoftRefReleaseThread();
           softRefReleaseThread.start();
       }
   
  @@ -117,6 +123,7 @@
       {
           if (softRefReleaseThread != null)
           {
  +            softRefReleaseThread.requestEnd = true;
               softRefReleaseThread.interrupt();
               softRefReleaseThread = null;
           }
  @@ -127,16 +134,9 @@
           synchronized (this)
           {
               Map files = getOrCreateFilesystemCache(file.getFileSystem());
  -            /*
  -            if (files.size() < 1)
  -            {
  -                startThread();
  -            }
  -            */
   
               SoftReference ref = new SoftReference(file, refqueue);
               FileSystemAndNameKey key = new FileSystemAndNameKey(file.getFileSystem(), file.getName());
  -            // files.put(key, ref);
               files.put(file.getName(), ref);
               refReverseMap.put(ref, key);
           }
  @@ -147,9 +147,7 @@
           synchronized (this)
           {
               Map files = getOrCreateFilesystemCache(filesystem);
  -            // FileSystemAndNameKey key = new FileSystemAndNameKey(filesystem, name);
   
  -            // SoftReference ref = (SoftReference) files.get(key);
               SoftReference ref = (SoftReference) files.get(name);
               if (ref == null)
               {
  @@ -159,7 +157,6 @@
               FileObject fo = (FileObject) ref.get();
               if (fo == null)
               {
  -                // removeFile(key);
                   removeFile(filesystem, name);
               }
               return fo;
  @@ -173,16 +170,13 @@
               Map files = getOrCreateFilesystemCache(filesystem);
   
               Iterator iterKeys = refReverseMap.values().iterator();
  -            // Iterator iterKeys = files.keySet().iterator();
               while (iterKeys.hasNext())
               {
                   FileSystemAndNameKey key = (FileSystemAndNameKey) iterKeys.next();
                   if (key.getFileSystem() == filesystem)
                   {
  -                    // Object ref = files.get(key);
                       iterKeys.remove();
                       files.remove(key.getFileName());
  -                    // refReverseMap.remove(ref);
                   }
               }
   
  @@ -190,18 +184,17 @@
               {
                   filesystemClose(filesystem);
               }
  -            /*
  -            if (files.size() < 1)
  -            {
  -                endThread();
  -            }
  -            */
           }
       }
   
       private void filesystemClose(FileSystem filesystem)
       {
  -        ((AbstractFileSystem) filesystem).close();
  +        filesystemCache.remove(filesystem);
  +        if (filesystemCache.size() < 1)
  +        {
  +            endThread();
  +        }
  +        ((DefaultFileSystemManager) getContext().getFileSystemManager()).closeFileSystem(filesystem);
       }
   
       public void close()
  @@ -233,7 +226,6 @@
           {
               Map files = getOrCreateFilesystemCache(key.getFileSystem());
   
  -            // Object ref = files.remove(key);
               Object ref = files.remove(key.getFileName());
               if (ref != null)
               {
  @@ -244,13 +236,6 @@
               {
                   filesystemClose(key.getFileSystem());
               }
  -
  -            /*
  -            if (files.size() < 1)
  -            {
  -                endThread();
  -            }
  -            */
           }
       }
   
  
  
  

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


Mime
View raw message