Author: imario
Date: Mon May 14 11:17:52 2007
New Revision: 537930

URL: http://svn.apache.org/viewvc?view=rev&rev=537930
Log:
VFS-143: Fix memory leak in DelegateFileObject in it's handling of listeners - 
Thanks to Adam Heath for the patch!

Added:
    
jakarta/commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs/util/WeakRefFileListener.java
Modified:
    
jakarta/commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs/provider/DelegateFileObject.java

Modified: 
jakarta/commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs/provider/DelegateFileObject.java
URL: 
http://svn.apache.org/viewvc/jakarta/commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs/provider/DelegateFileObject.java?view=diff&rev=537930&r1=537929&r2=537930
==============================================================================
--- 
jakarta/commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs/provider/DelegateFileObject.java
 (original)
+++ 
jakarta/commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs/provider/DelegateFileObject.java
 Mon May 14 11:17:52 2007
@@ -22,6 +22,7 @@
 import org.apache.commons.vfs.FileObject;
 import org.apache.commons.vfs.FileSystemException;
 import org.apache.commons.vfs.FileType;
+import org.apache.commons.vfs.util.WeakRefFileListener;
 
 import java.io.InputStream;
 import java.io.OutputStream;
@@ -54,7 +55,7 @@
         this.file = file;
         if (file != null)
         {
-            file.getFileSystem().addListener(file, this);
+                       WeakRefFileListener.installListener(file, this);
         }
     }
 
@@ -80,7 +81,7 @@
 
         if (file != null)
         {
-            file.getFileSystem().addListener(file, this);
+                       WeakRefFileListener.installListener(file, this);
         }
         this.file = file;
         maybeTypeChanged(oldType);
@@ -296,6 +297,7 @@
      */
     public void fileCreated(final FileChangeEvent event) throws Exception
     {
+        if (event.getFile() != file) return;
         if (!ignoreEvent)
         {
             handleCreate(file.getType());
@@ -307,6 +309,7 @@
      */
     public void fileDeleted(final FileChangeEvent event) throws Exception
     {
+        if (event.getFile() != file) return;
         if (!ignoreEvent)
         {
             handleDelete();
@@ -320,6 +323,7 @@
      */
     public void fileChanged(FileChangeEvent event) throws Exception
     {
+        if (event.getFile() != file) return;
         if (!ignoreEvent)
         {
             handleChanged();

Added: 
jakarta/commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs/util/WeakRefFileListener.java
URL: 
http://svn.apache.org/viewvc/jakarta/commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs/util/WeakRefFileListener.java?view=auto&rev=537930
==============================================================================
--- 
jakarta/commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs/util/WeakRefFileListener.java
 (added)
+++ 
jakarta/commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs/util/WeakRefFileListener.java
 Mon May 14 11:17:52 2007
@@ -0,0 +1,111 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.vfs.util;
+
+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.FileSystem;
+
+import java.lang.ref.WeakReference;
+
+/**
+ * Wrap a listener with a WeakReference.
+ *
+ * @author <a href="mailto:[EMAIL PROTECTED]">Adam Heath</a>
+ * @version $Revision: 262 $ $Date: 2006-12-20T09:14:53.055649Z $
+ */
+public class WeakRefFileListener implements FileListener
+{
+       private final FileSystem fs;
+       private final FileName name;
+       private final WeakReference listener;
+
+       protected WeakRefFileListener(final FileObject file, final FileListener 
listener)
+       {
+               this.fs = file.getFileSystem();
+               this.name = file.getName();
+               this.listener = new WeakReference(listener);
+       }
+
+       /**
+        * This will install the <code>listener<code> at the given 
<code>file</code>
+        */
+       public static void installListener(final FileObject file, final 
FileListener listener)
+       {
+               WeakRefFileListener weakListener = new 
WeakRefFileListener(file, listener);
+               
+               file.getFileSystem().addListener(file, new 
WeakRefFileListener(file, weakListener));
+       }
+
+       /**
+        * returns the wrapped listener. If it is gone, the WeakRefFileListener 
wrapper will
+        * remove itself from the list of listeners.
+        */
+       protected FileListener getListener() throws Exception
+       {
+               FileListener listener = (FileListener) this.listener.get();
+               if (listener == null)
+               {
+                       FileObject file = fs.resolveFile(name);
+                       file.getFileSystem().removeListener(file, this);
+               }
+               return listener;
+       }
+
+       /**
+        * Called when a file is created.
+        */
+       public void fileCreated(final FileChangeEvent event) throws Exception
+       {
+               FileListener listener = getListener();
+               if (listener == null)
+               {
+                       return;
+               }
+               listener.fileCreated(event);
+       }
+
+       /**
+        * Called when a file is deleted.
+        */
+       public void fileDeleted(final FileChangeEvent event) throws Exception
+       {
+               FileListener listener = getListener();
+               if (listener == null)
+               {
+                       return;
+               }
+               listener.fileDeleted(event);
+       }
+
+       /**
+        * Called when a file is changed.
+        * <p/>
+        * This will only happen if you monitor the file using [EMAIL 
PROTECTED] org.apache.commons.vfs.FileMonitor}.
+        */
+       public void fileChanged(FileChangeEvent event) throws Exception
+       {
+               FileListener listener = getListener();
+               if (listener == null)
+               {
+                       return;
+               }
+               listener.fileChanged(event);
+       }
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to