commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tcu...@apache.org
Subject svn commit: r498463 - in /jakarta/commons/sandbox/jci/trunk/fam/src/main/java/org/apache/commons/jci/monitor: FilesystemAlterationMonitor.java FilesystemAlterationObserver.java FilesystemAlterationObserverImpl.java
Date Sun, 21 Jan 2007 21:55:32 GMT
Author: tcurdt
Date: Sun Jan 21 13:55:31 2007
New Revision: 498463

URL: http://svn.apache.org/viewvc?view=rev&rev=498463
Log:
https://issues.apache.org/jira/browse/SANDBOX-179


Added:
    jakarta/commons/sandbox/jci/trunk/fam/src/main/java/org/apache/commons/jci/monitor/FilesystemAlterationObserver.java
  (with props)
    jakarta/commons/sandbox/jci/trunk/fam/src/main/java/org/apache/commons/jci/monitor/FilesystemAlterationObserverImpl.java
  (with props)
Modified:
    jakarta/commons/sandbox/jci/trunk/fam/src/main/java/org/apache/commons/jci/monitor/FilesystemAlterationMonitor.java

Modified: jakarta/commons/sandbox/jci/trunk/fam/src/main/java/org/apache/commons/jci/monitor/FilesystemAlterationMonitor.java
URL: http://svn.apache.org/viewvc/jakarta/commons/sandbox/jci/trunk/fam/src/main/java/org/apache/commons/jci/monitor/FilesystemAlterationMonitor.java?view=diff&rev=498463&r1=498462&r2=498463
==============================================================================
--- jakarta/commons/sandbox/jci/trunk/fam/src/main/java/org/apache/commons/jci/monitor/FilesystemAlterationMonitor.java
(original)
+++ jakarta/commons/sandbox/jci/trunk/fam/src/main/java/org/apache/commons/jci/monitor/FilesystemAlterationMonitor.java
Sun Jan 21 13:55:31 2007
@@ -17,14 +17,8 @@
 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;
 
@@ -35,139 +29,14 @@
 
     private final 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;
-        }
-
+    private final FilesystemAlterationObserver observer;
 
-        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 volatile boolean running = true;
     private Thread thread;
 
-
     public FilesystemAlterationMonitor() {
+    	observer = new FilesystemAlterationObserverImpl();
     }
 
 
@@ -178,9 +47,8 @@
 
 
     public void stop() {
-        synchronized (mutexRunning) {
-            running = false;
-        }
+        running = false;
+
         try {
             thread.join(delay);
         } catch (InterruptedException e) {
@@ -194,240 +62,31 @@
 
 
     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;
-        }
+    	observer.addListener( pListener );
     }
 
     public Collection getListeners() {
-        synchronized (mutexListeners) {
-            return listeners.keySet();
-        }
+        return observer.getListeners();
     }
 
     public Collection getListenersFor( final File pRepository ) {
-        synchronized (mutexListeners) {
-            return (Collection) directories.get(pRepository);
-        }
+        return observer.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;
-            }
-        }
-    }
-
-
-    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);
-            }
-        }
+    	observer.removeListener( listener );
     }
 
 
     public void run() {
         log.debug("fam running");
         while (true) {
-            synchronized (mutexRunning) {
-                if (!running) {
-                    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);
-                }
+            if (!running) {
+                break;
             }
+
+            observer.check();
+
             try {
                 Thread.sleep(delay);
             } catch (final InterruptedException e) {
@@ -439,8 +98,6 @@
 
 
     public String toString() {
-        return listeners.toString() + directories.toString();
-    }
-
-    
+        return observer.toString();
+    }   
 }

Added: jakarta/commons/sandbox/jci/trunk/fam/src/main/java/org/apache/commons/jci/monitor/FilesystemAlterationObserver.java
URL: http://svn.apache.org/viewvc/jakarta/commons/sandbox/jci/trunk/fam/src/main/java/org/apache/commons/jci/monitor/FilesystemAlterationObserver.java?view=auto&rev=498463
==============================================================================
--- jakarta/commons/sandbox/jci/trunk/fam/src/main/java/org/apache/commons/jci/monitor/FilesystemAlterationObserver.java
(added)
+++ jakarta/commons/sandbox/jci/trunk/fam/src/main/java/org/apache/commons/jci/monitor/FilesystemAlterationObserver.java
Sun Jan 21 13:55:31 2007
@@ -0,0 +1,20 @@
+package org.apache.commons.jci.monitor;
+
+import java.io.File;
+import java.util.Collection;
+
+public interface FilesystemAlterationObserver {
+
+	public abstract void addListener(
+			final FilesystemAlterationListener pListener);
+
+	public abstract Collection getListeners();
+
+	public abstract Collection getListenersFor(final File pRepository);
+
+	public abstract void removeListener(
+			final FilesystemAlterationListener listener);
+
+	public abstract void check();
+
+}
\ No newline at end of file

Propchange: jakarta/commons/sandbox/jci/trunk/fam/src/main/java/org/apache/commons/jci/monitor/FilesystemAlterationObserver.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/commons/sandbox/jci/trunk/fam/src/main/java/org/apache/commons/jci/monitor/FilesystemAlterationObserver.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision Author HeadURL Id

Propchange: jakarta/commons/sandbox/jci/trunk/fam/src/main/java/org/apache/commons/jci/monitor/FilesystemAlterationObserver.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: jakarta/commons/sandbox/jci/trunk/fam/src/main/java/org/apache/commons/jci/monitor/FilesystemAlterationObserverImpl.java
URL: http://svn.apache.org/viewvc/jakarta/commons/sandbox/jci/trunk/fam/src/main/java/org/apache/commons/jci/monitor/FilesystemAlterationObserverImpl.java?view=auto&rev=498463
==============================================================================
--- jakarta/commons/sandbox/jci/trunk/fam/src/main/java/org/apache/commons/jci/monitor/FilesystemAlterationObserverImpl.java
(added)
+++ jakarta/commons/sandbox/jci/trunk/fam/src/main/java/org/apache/commons/jci/monitor/FilesystemAlterationObserverImpl.java
Sun Jan 21 13:55:31 2007
@@ -0,0 +1,401 @@
+/*
+ * Copyright 1999-2004 The Apache Software Foundation. Licensed 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.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;
+
+/**
+ * @author tcurdt
+ */
+public class FilesystemAlterationObserverImpl implements FilesystemAlterationObserver {
+
+    private final Log log = LogFactory.getLog(FilesystemAlterationObserverImpl.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 UniqueMultiValueMap extends MultiHashMap {
+
+		private static final long serialVersionUID = 1L;
+
+		public UniqueMultiValueMap() {
+            super( );
+        }
+
+        public UniqueMultiValueMap(Map copy) {
+            super( copy );
+        }
+
+        protected Collection createCollection( Collection copy ) {
+            if (copy != null) {
+                return new HashSet(copy);
+            }
+            return new HashSet();
+        }
+        
+    }
+    
+    private Map listeners = new UniqueMultiValueMap();
+    private Map directories = new UniqueMultiValueMap();
+    private Map entries = new HashMap();
+    private final Object mutexListeners = new Object();
+    
+
+    public FilesystemAlterationObserverImpl() {
+    }
+
+
+    public void addListener( final FilesystemAlterationListener pListener ) {
+        final File directory = pListener.getRepository();
+        synchronized (mutexListeners) {
+            // listerner -> dir1, dir2, dir3
+            final UniqueMultiValueMap newListeners = new UniqueMultiValueMap(listeners);
+            newListeners.put(pListener, directory);
+            listeners = newListeners;
+            // directory -> listener1, listener2, listener3
+            final UniqueMultiValueMap newDirectories = new UniqueMultiValueMap(directories);
+            newDirectories.put(directory, pListener);
+            directories = newDirectories;
+        }
+    }
+
+    public Collection getListeners() {
+        synchronized (mutexListeners) {
+            return listeners.keySet();
+        }
+    }
+
+    public Collection getListenersFor( final File pRepository ) {
+        synchronized (mutexListeners) {
+            return (Collection) directories.get(pRepository);
+        }
+    }
+
+    public void removeListener( final FilesystemAlterationListener listener ) {
+        synchronized (mutexListeners) {
+            // listerner -> dir1, dir2, dir3
+            final UniqueMultiValueMap newListeners = new UniqueMultiValueMap(listeners);
+            Collection d = (Collection) newListeners.remove(listener);
+            listeners = newListeners;
+            if (d != null) {
+                // directory -> listener1, listener2, listener3
+                final UniqueMultiValueMap newDirectories = new UniqueMultiValueMap(directories);
+                for (Iterator it = d.iterator(); it.hasNext();) {
+                    newDirectories.remove(it.next());
+                    entries.remove(d);
+                }
+                directories = newDirectories;
+            }
+        }
+    }
+
+
+    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 check() {
+        log.debug("observation running");
+        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);
+            }
+        }
+        log.debug("observation exiting");
+    }
+
+    public String toString() {
+        return listeners.toString() + directories.toString();
+    }
+
+    
+}

Propchange: jakarta/commons/sandbox/jci/trunk/fam/src/main/java/org/apache/commons/jci/monitor/FilesystemAlterationObserverImpl.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/commons/sandbox/jci/trunk/fam/src/main/java/org/apache/commons/jci/monitor/FilesystemAlterationObserverImpl.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision Author HeadURL Id

Propchange: jakarta/commons/sandbox/jci/trunk/fam/src/main/java/org/apache/commons/jci/monitor/FilesystemAlterationObserverImpl.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



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