-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

For simplicity, here's the patch

Giacomo Pati wrote:
> 
> 
> Torsten Curdt wrote:
>>> Hi all (but probably mainly to Thorsten :-)
>> ;)
> 
>>> I'd like to reuse the FilesystemAlterationMonitor but without the
>>> Runnable logic in it.
>>>
>>> Couldn't we separate the monitoring process from the looping part, so
>>> that the filesystem observation logic could be used lets say with Quartz
>>> to trigger it?
>> Sure ...sounds good
> 
>>> I'd would happily donate a patch for it :-)
>> I'd be happy to accept it :)
> 
> Do you want a Jira entry for the patch?
> 
> I've called the splitted class FilesystemAlterationObserver (the one
> containing the observation code, and yes, any better naming suggestion
> welcome) and left the the FilesystemAlterationMonitor just with the
> Runnable logic.
> 
> All test run through as before (there was one which failed, and still
> failes after refactoring, so I guess it still does what was expected :-).
> 
> Is there any reason to use commons-collection 3.1 over 3.2? 3.1 has
> deprecated MultiHashMap, so I've upgraded to 3.2 and replaced it with
> the suggested MultiValueMap.
> 
> Ciao
> 
>> cheers
>> -- 
>> Torsten
> 
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [EMAIL PROTECTED]
>> For additional commands, e-mail: [EMAIL PROTECTED]
> 
> 

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


- --
Giacomo Pati
Otego AG, Switzerland - http://www.otego.com
Orixo, the XML business alliance - http://www.orixo.com

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (GNU/Linux)

iD8DBQFFU0JRLNdJvZjjVZARAuahAKDDvzTmYW6K9IU/jgRfTBtbJp+ZIACfTs9c
+Dl6SKOb75ipijP5eMfOeS8=
=rgqL
-----END PGP SIGNATURE-----
Index: 
src/main/java/org/apache/commons/jci/monitor/FilesystemAlterationMonitor.java
===================================================================
--- 
src/main/java/org/apache/commons/jci/monitor/FilesystemAlterationMonitor.java   
    (revision 472915)
+++ 
src/main/java/org/apache/commons/jci/monitor/FilesystemAlterationMonitor.java   
    (working copy)
@@ -9,14 +9,7 @@
 package org.apache.commons.jci.monitor;
 
 import java.io.File;
-import java.io.FileFilter;
 import java.util.Collection;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.Iterator;
-import java.util.Map;
-import java.util.Set;
-import org.apache.commons.collections.MultiHashMap;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
@@ -26,140 +19,15 @@
 public final class FilesystemAlterationMonitor implements Runnable {
 
     private final static Log log = 
LogFactory.getLog(FilesystemAlterationMonitor.class);
-
-    public class Entry {
-
-        private final File root;
-        private final File file;
-        private long lastModified;
-        private Set paths = new HashSet();
-        private Set childs = new HashSet();
-        private final boolean isDirectory;
-
-
-        public Entry(final File pRoot, final File pFile) {
-            root = pRoot;
-            file = pFile;
-            lastModified = -1;
-            isDirectory = file.isDirectory();
-        }
-
-
-        public boolean hasChanged() {
-            final long modified = file.lastModified();
-            return modified != lastModified;
-        }
-
-
-        public boolean isDelected() {
-            return !file.exists();
-        }
-
-
-        public boolean isDirectory() {
-            return isDirectory;
-        }
-
-
-        public Entry[] getChilds() {
-            final Entry[] r = new Entry[childs.size()];
-            childs.toArray(r);
-            return r;
-        }
-
-
-        private FileFilter getFileFilter() {
-            return new FileFilter() {
-
-                public boolean accept( final File pathname ) {
-                    final String p = pathname.getAbsolutePath();
-                    return !paths.contains(p);
-                }
-            };
-        }
-
-
-        public Entry[] getNonChilds() {
-            final File[] newFiles = file.listFiles(getFileFilter());
-            final Entry[] r = new Entry[newFiles.length];
-            for (int i = 0; i < newFiles.length; i++) {
-                r[i] = new Entry(root, newFiles[i]);
-            }
-            return r;
-        }
-
-
-        public void add( final Entry entry ) {
-            childs.add(entry);
-            paths.add(entry.toString());
-            onCreate(root, entry);
-        }
-
-
-        private void deleteChilds() {
-            final Entry[] childs = this.getChilds();
-            for (int i = 0; i < childs.length; i++) {
-                final Entry child = childs[i];
-                delete(child);
-            }
-        }
-
-
-        public void delete( final Entry entry ) {
-            childs.remove(entry);
-            paths.remove(entry.toString());
-            entry.deleteChilds();
-            onDelete(root, entry);
-        }
-
-
-        public File getFile() {
-            return file;
-        }
-
-
-        public void markNotChanged() {
-            lastModified = file.lastModified();
-        }
-
-
-        public String toString() {
-            return file.getAbsolutePath();
-        }
-    }
-
-    public static class UniqueMultiHashMap extends MultiHashMap {
-
-               private static final long serialVersionUID = 1L;
-
-               public UniqueMultiHashMap() {
-            super();
-        }
-
-        public UniqueMultiHashMap(Map copy) {
-            super(copy);
-        }
-
-        protected Collection createCollection( Collection copy ) {
-            if (copy != null) {
-                return new HashSet(copy);
-            }
-            return new HashSet();
-        }
-        
-    }
-    
-    private Map listeners = new UniqueMultiHashMap();
-    private Map directories = new UniqueMultiHashMap();
-    private Map entries = new HashMap();
-    private final Object mutexListeners = new Object();
     private final Object mutexRunning = new Object();
     private long delay = 3000;
     private boolean running = true;
     private Thread thread;
+    private FilesystemAlterationObserver filesystemAlterationObserver;
 
 
     public FilesystemAlterationMonitor() {
+        filesystemAlterationObserver = new FilesystemAlterationObserver();
     }
 
 
@@ -186,212 +54,22 @@
 
 
     public void addListener( final FilesystemAlterationListener pListener ) {
-        final File directory = pListener.getRepository();
-        synchronized (mutexListeners) {
-            // listerner -> dir1, dir2, dir3
-            final UniqueMultiHashMap newListeners = new 
UniqueMultiHashMap(listeners);
-            newListeners.put(pListener, directory);
-            listeners = newListeners;
-            // directory -> listener1, listener2, listener3
-            final UniqueMultiHashMap newDirectories = new 
UniqueMultiHashMap(directories);
-            newDirectories.put(directory, pListener);
-            directories = newDirectories;
-        }
+        filesystemAlterationObserver.addListener( pListener );
     }
 
     public Collection getListeners() {
-        synchronized (mutexListeners) {
-            return listeners.keySet();
-        }
+        return filesystemAlterationObserver.getListeners();
     }
 
     public Collection getListenersFor( final File pRepository ) {
-        synchronized (mutexListeners) {
-            return (Collection) directories.get(pRepository);
-        }
+        return filesystemAlterationObserver.getListenersFor( pRepository );
     }
 
     public void removeListener( final FilesystemAlterationListener listener ) {
-        synchronized (mutexListeners) {
-            // listerner -> dir1, dir2, dir3
-            final UniqueMultiHashMap newListeners = new 
UniqueMultiHashMap(listeners);
-            Collection d = (Collection) newListeners.remove(listener);
-            listeners = newListeners;
-            if (d != null) {
-                // directory -> listener1, listener2, listener3
-                final UniqueMultiHashMap newDirectories = new 
UniqueMultiHashMap(directories);
-                for (Iterator it = d.iterator(); it.hasNext();) {
-                    newDirectories.remove(it.next());
-                    entries.remove(d);
-                }
-                directories = newDirectories;
-            }
-        }
+        filesystemAlterationObserver.removeListener( listener );
     }
 
 
-    private void onStart( final File root ) {
-        log.debug("start checking " + root);
-        Map directories;
-        synchronized (mutexListeners) {
-            directories = this.directories;
-        }
-        final Collection l = (Collection) directories.get(root);
-        if (l != null) {
-            for (Iterator it = l.iterator(); it.hasNext();) {
-                final FilesystemAlterationListener listener = 
(FilesystemAlterationListener) it
-                        .next();
-                listener.onStart();
-            }
-        }
-    }
-
-
-    private void onStop( final File root ) {
-        log.debug("stop checking " + root);
-        Map directories;
-        synchronized (mutexListeners) {
-            directories = this.directories;
-        }
-        final Collection l = (Collection) directories.get(root);
-        if (l != null) {
-            for (Iterator it = l.iterator(); it.hasNext();) {
-                final FilesystemAlterationListener listener = 
(FilesystemAlterationListener) it
-                        .next();
-                listener.onStop();
-            }
-        }
-    }
-
-
-    private void onCreate( final File root, final Entry entry ) {
-        log.debug("created " + ((entry.isDirectory()) ? "dir " : "file ") + 
entry);
-        Map directories;
-        synchronized (mutexListeners) {
-            directories = this.directories;
-        }
-        final Collection l = (Collection) directories.get(root);
-        if (l != null) {
-            if (entry.isDirectory()) {
-                for (Iterator it = l.iterator(); it.hasNext();) {
-                    final FilesystemAlterationListener listener = 
(FilesystemAlterationListener) it
-                            .next();
-                    listener.onCreateDirectory(entry.getFile());
-                }
-            } else {
-                for (Iterator it = l.iterator(); it.hasNext();) {
-                    final FilesystemAlterationListener listener = 
(FilesystemAlterationListener) it
-                            .next();
-                    listener.onCreateFile(entry.getFile());
-                }
-            }
-        }
-        entry.markNotChanged();
-    }
-
-
-    private void onChange( final File root, final Entry entry ) {
-        log.debug("changed " + ((entry.isDirectory()) ? "dir " : "file ") + 
entry);
-        Map directories;
-        synchronized (mutexListeners) {
-            directories = this.directories;
-        }
-        final Collection l = (Collection) directories.get(root);
-        if (l != null) {
-            if (entry.isDirectory()) {
-                for (Iterator it = l.iterator(); it.hasNext();) {
-                    final FilesystemAlterationListener listener = 
(FilesystemAlterationListener) it
-                            .next();
-                    listener.onChangeDirectory(entry.getFile());
-                }
-            } else {
-                for (Iterator it = l.iterator(); it.hasNext();) {
-                    final FilesystemAlterationListener listener = 
(FilesystemAlterationListener) it
-                            .next();
-                    listener.onChangeFile(entry.getFile());
-                }
-            }
-        }
-        entry.markNotChanged();
-    }
-
-
-    private void onDelete( final File root, final Entry entry ) {
-        log.debug("deleted " + ((entry.isDirectory()) ? "dir " : "file ") + 
entry);
-        Map directories;
-        synchronized (mutexListeners) {
-            directories = this.directories;
-        }
-        final Collection l = (Collection) directories.get(root);
-        if (l != null) {
-            if (entry.isDirectory()) {
-                for (Iterator it = l.iterator(); it.hasNext();) {
-                    final FilesystemAlterationListener listener = 
(FilesystemAlterationListener) it
-                            .next();
-                    listener.onDeleteDirectory(entry.getFile());
-                }
-            } else {
-                for (Iterator it = l.iterator(); it.hasNext();) {
-                    final FilesystemAlterationListener listener = 
(FilesystemAlterationListener) it
-                            .next();
-                    listener.onDeleteFile(entry.getFile());
-                }
-            }
-        }
-        entry.markNotChanged();
-    }
-
-
-    private void check( final File root, final Entry entry, final boolean 
create ) {
-        // log.debug("checking " + entry);
-        if (entry.isDirectory()) {
-            final Entry[] currentChilds = entry.getChilds();
-            if (entry.hasChanged() || create) {
-                // log.debug(entry + " has changed");
-                if (!create) {
-                    onChange(root, entry);
-                    for (int i = 0; i < currentChilds.length; i++) {
-                        final Entry child = currentChilds[i];
-                        if (child.isDelected()) {
-                            entry.delete(child);
-                            currentChilds[i] = null;
-                        }
-                    }
-                }
-                final Entry[] newChilds = entry.getNonChilds();
-                for (int i = 0; i < newChilds.length; i++) {
-                    final Entry child = newChilds[i];
-                    entry.add(child);
-                }
-                if (!create) {
-                    for (int i = 0; i < currentChilds.length; i++) {
-                        final Entry child = currentChilds[i];
-                        if (child != null) {
-                            check(root, child, false);
-                        }
-                    }
-                }
-                for (int i = 0; i < newChilds.length; i++) {
-                    final Entry child = newChilds[i];
-                    check(root, child, true);
-                }
-            } else {
-                // log.debug(entry + " has not changed");
-                for (int i = 0; i < currentChilds.length; i++) {
-                    final Entry child = currentChilds[i];
-                    check(root, child, false);
-                }
-            }
-        } else {
-            if (entry.isDelected()) {
-                onDelete(root, entry);
-            } else if (entry.hasChanged()) {
-                onChange(root, entry);
-            }
-        }
-    }
-
-
     public void run() {
         log.debug("fam running");
         while (true) {
@@ -400,26 +78,7 @@
                     break;
                 }
             }
-            Map directories;
-            synchronized (mutexListeners) {
-                directories = this.directories;
-            }
-            for (Iterator it = directories.keySet().iterator(); it.hasNext();) 
{
-                final File directory = (File) it.next();
-                if (directory.exists()) {
-                    onStart(directory);
-                    Entry root;
-                    synchronized (mutexListeners) {
-                        root = (Entry) entries.get(directory);
-                        if (root == null) {
-                            root = new Entry(directory, directory);
-                            entries.put(directory, root);
-                        }
-                    }
-                    check(directory, root, false);
-                    onStop(directory);
-                }
-            }
+            filesystemAlterationObserver.check();
             try {
                 Thread.sleep(delay);
             } catch (final InterruptedException e) {
@@ -431,7 +90,7 @@
 
 
     public String toString() {
-        return listeners.toString() + directories.toString();
+        return filesystemAlterationObserver.toString();
     }
 
     
Index: pom.xml
===================================================================
--- pom.xml     (revision 472012)
+++ pom.xml     (working copy)
@@ -42,7 +42,7 @@
     <dependency>
       <groupId>commons-collections</groupId>
       <artifactId>commons-collections</artifactId>
-      <version>3.1</version>
+      <version>3.2</version>
     </dependency>
 
   </dependencies>

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

Reply via email to