commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bugzi...@apache.org
Subject DO NOT REPLY [Bug 38686] - [VFS] Default VFS cache behavior
Date Tue, 21 Feb 2006 09:29:08 GMT
DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG·
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://issues.apache.org/bugzilla/show_bug.cgi?id=38686>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND·
INSERTED IN THE BUG DATABASE.

http://issues.apache.org/bugzilla/show_bug.cgi?id=38686





------- Additional Comments From troeger@alaun.de  2006-02-21 10:29 -------
Hi,

the tmp files are created by the FileReplicator and are deleted during shutdown
of the VFS-Manager.
You may set your own implementation of a FileReplicator (that does a better Job)
via:

((DefaultFileSystemManager) VFS.getManager()).setReplicator(...);

You may gracefully shutdown the default replicator
(VFS.getManager()).getReplicator()) before.

Be sure to do this at application start up!!!

After accessing your tar.gz you should shutdown the implicitly created file
system like so:

((DefaultFileSystemManager)
VFS.getManger()).closeFileSystem(someFileObjectInYourTarGz.getFilesSystem());

This will release the TarFileSystem instance that holds the reference to the tmp
File object.
As far as I know thereīs no event (or something like that) to keep track on file
system shutdowns. So you may use a weak reference to the file object (you
replicated) to cleanup the unused temp file:

1. create a class that extends java.lang.ref.WeakReference:
public class WeakFileRef extends WeakReference {
    private String absPath;
    public WeakFileRef(File file, ReferenceQueue queue) {
        super(file, queue);
        this.absPath = file.getAbsolutePath();
    }
    public String getPath() {
        return this.absPath;
    }
}

2. Do your own FileReplicator implementation. You may copy and slightly modify
org.apache.commons.vfs.impl.DefaultFileReplicator. Attention: donīt use any hard
reference on file objects here - use the WeakFileRef class.

3. In your FileReplicator implementation create a field of type ReferenceQueue
    private ReferenceQueue queue = new ReferenceQueue();

4. Change allocateFile(String):
public File allocateFile(final String baseName) throws FileSystemException
{
    WeakFileRef fileRef = (WeakFileRef) queue.poll(); // poll the refence queue
and delete finalized files
    while (fileRef != null) {
        File toDelete = new File(fileRef.getAbsPath());
        try {
            if (toDelete.exists()) {
                toDelete.delete();
            }
        } catch (IOException) {
            // do some exception handling or throw a FileSystemExeption
        }
        copies.remove(fileRef);
        fileRef = (WeakFileRef) queue.poll();
    }

    // Create a unique-ish file name
    final String basename = createFilename(baseName);
    filecount++;
    final File file = createFile(tempDir, basename);

    // Keep track to delete later
    copies.add(new WeakFileRef(file, queue)); // <- changed

    return file;
}

5. Change the close() method -> ArrayList 'copies' now holds WeakFileRef objects!

Notice that the files are not cleaned up immediately - it depends on garbage
collection.

Hope this helps/(works) ;-)

-- 
Configure bugmail: http://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug, or are watching the assignee.

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