Author: tcurdt
Date: Sun Mar 27 05:58:25 2005
New Revision: 159148

URL: http://svn.apache.org/viewcvs?view=rev&rev=159148
Log:
support monitoring multiple repositories


Added:
    
jakarta/commons/sandbox/jci/trunk/src/java/org/apache/commons/jci/monitor/FilesystemAlterationListener.java
      - copied, changed from r156520, 
jakarta/commons/sandbox/jci/trunk/src/java/org/apache/commons/jci/monitor/AlterationListener.java
    
jakarta/commons/sandbox/jci/trunk/src/java/org/apache/commons/jci/monitor/FilesystemAlterationMonitor.java
      - copied, changed from r156520, 
jakarta/commons/sandbox/jci/trunk/src/java/org/apache/commons/jci/monitor/AlterationMonitor.java
    jakarta/commons/sandbox/jci/trunk/src/test/
    jakarta/commons/sandbox/jci/trunk/src/test/org/
    jakarta/commons/sandbox/jci/trunk/src/test/org/apache/
    jakarta/commons/sandbox/jci/trunk/src/test/org/apache/commons/
    jakarta/commons/sandbox/jci/trunk/src/test/org/apache/commons/jci/
    jakarta/commons/sandbox/jci/trunk/src/test/org/apache/commons/jci/monitor/
    
jakarta/commons/sandbox/jci/trunk/src/test/org/apache/commons/jci/monitor/CommandlineTest.java
Removed:
    
jakarta/commons/sandbox/jci/trunk/src/java/org/apache/commons/jci/monitor/AlterationListener.java
    
jakarta/commons/sandbox/jci/trunk/src/java/org/apache/commons/jci/monitor/AlterationMonitor.java
Modified:
    jakarta/commons/sandbox/jci/trunk/.classpath
    jakarta/commons/sandbox/jci/trunk/project.xml
    
jakarta/commons/sandbox/jci/trunk/src/java/org/apache/commons/jci/CompilingClassLoader.java
    
jakarta/commons/sandbox/jci/trunk/src/java/org/apache/commons/jci/ReloadingClassLoader.java

Modified: jakarta/commons/sandbox/jci/trunk/.classpath
URL: 
http://svn.apache.org/viewcvs/jakarta/commons/sandbox/jci/trunk/.classpath?view=diff&r1=159147&r2=159148
==============================================================================
--- jakarta/commons/sandbox/jci/trunk/.classpath (original)
+++ jakarta/commons/sandbox/jci/trunk/.classpath Sun Mar 27 05:58:25 2005
@@ -1,6 +1,7 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <classpath>
        <classpathentry kind="src" path="src/java"/>
+       <classpathentry kind="src" path="src/test"/>
        <classpathentry kind="con" 
path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
        <classpathentry sourcepath="/jakarta-bcel-trunk/src/java" kind="lib" 
path="lib/jakarta-bcel-20040329.jar"/>
        <classpathentry kind="var" 
path="MAVEN_REPO/commons-io/jars/commons-io-1.0.jar"/>
@@ -8,5 +9,6 @@
        <classpathentry kind="var" 
path="MAVEN_REPO/eclipse/jars/jdtcore-3.0.1.jar"/>
        <classpathentry kind="var" 
path="MAVEN_REPO/commons-logging/jars/commons-logging-1.0.4.jar"/>
        <classpathentry kind="var" 
path="MAVEN_REPO/commons-logging/jars/commons-logging-api-1.0.4.jar"/>
-       <classpathentry kind="output" path="bin"/>
+       <classpathentry kind="var" 
path="MAVEN_REPO/commons-collections/jars/commons-collections-3.1.jar"/>
+       <classpathentry kind="output" path="eclipse"/>
 </classpath>

Modified: jakarta/commons/sandbox/jci/trunk/project.xml
URL: 
http://svn.apache.org/viewcvs/jakarta/commons/sandbox/jci/trunk/project.xml?view=diff&r1=159147&r2=159148
==============================================================================
--- jakarta/commons/sandbox/jci/trunk/project.xml (original)
+++ jakarta/commons/sandbox/jci/trunk/project.xml Sun Mar 27 05:58:25 2005
@@ -3,32 +3,25 @@
   <!--
   -->
   <extend>../commons-build/sandbox-project.xml</extend>
-
+  <pomVersion>1</pomVersion>
+  <artifactId>commons-jci</artifactId>
   <name>Commons JCI</name>
+  <groupId>commons-jci</groupId>
   <id>commons-jci</id>
   <logo>/images/jci-logo-white.png</logo>
   <url>http://jakarta.apache.org/commons/sandbox/jci/</url>
   <inceptionYear>2004</inceptionYear>
   <shortDescription>Commons JCI</shortDescription>
   <description>Commons JCI (java compiler interface)</description>
-
   <currentVersion>0.1-dev</currentVersion>
-  <versions></versions>
-  <branches></branches>
-
   <package>org.apache.commons.jci.*</package>
-
-
   <developers>
-
     <developer>
       <name>Torsten Curdt</name>
       <id>tcurdt</id>
       <email>tcurdt at apache.org</email>
     </developer>
-
   </developers>
-
   <build>
     <sourceDirectory>src/java</sourceDirectory>
     <unitTestSourceDirectory>src/test</unitTestSourceDirectory>
@@ -38,40 +31,44 @@
       </includes>
     </unitTest>
   </build>
-
   <dependencies>
-
-   <dependency>
-     <groupId>bcel</groupId>
-     <artifactId>bcel</artifactId>
-     <version>20040329</version>
-   </dependency>
-
-   <dependency>
-     <groupId>commons-io</groupId>
-     <artifactId>commons-io</artifactId>
-     <version>1.0</version>
-   </dependency>
-
-   <dependency>
-     <groupId>commons-lang</groupId>
-     <artifactId>commons-lang</artifactId>
-     <version>2.0</version>
-   </dependency>
-
-   <dependency>
-     <groupId>commons-logging</groupId>
-     <artifactId>commons-logging</artifactId>
-     <version>1.0.4</version>
-   </dependency>
-
-   <dependency>
-     <groupId>eclipse</groupId>
-     <artifactId>jdtcore</artifactId>
-     <version>3.0.1</version>
-   </dependency>
-
+    <dependency>
+      <groupId>bcel</groupId>
+      <artifactId>bcel</artifactId>
+      <version>20040329</version>
+      <jar>${basedir}/lib/jakarta-bcel-20040329.jar</jar>
+      <type>jar</type>
+    </dependency>
+    <dependency>
+      <groupId>commons-io</groupId>
+      <artifactId>commons-io</artifactId>
+      <version>1.0</version>
+      <type>jar</type>
+    </dependency>
+    <dependency>
+      <groupId>commons-lang</groupId>
+      <artifactId>commons-lang</artifactId>
+      <version>2.0</version>
+      <type>jar</type>
+    </dependency>
+    <dependency>
+      <groupId>commons-logging</groupId>
+      <artifactId>commons-logging</artifactId>
+      <version>1.0.4</version>
+      <type>jar</type>
+    </dependency>
+    <dependency>
+      <groupId>commons-collections</groupId>
+      <artifactId>commons-collections</artifactId>
+      <version>3.1</version>
+      <type>jar</type>
+    </dependency>
+    <dependency>
+      <groupId>eclipse</groupId>
+      <artifactId>jdtcore</artifactId>
+      <version>3.0.1</version>
+      <type>jar</type>
+    </dependency>
   </dependencies>
-
 </project>
 

Modified: 
jakarta/commons/sandbox/jci/trunk/src/java/org/apache/commons/jci/CompilingClassLoader.java
URL: 
http://svn.apache.org/viewcvs/jakarta/commons/sandbox/jci/trunk/src/java/org/apache/commons/jci/CompilingClassLoader.java?view=diff&r1=159147&r2=159148
==============================================================================
--- 
jakarta/commons/sandbox/jci/trunk/src/java/org/apache/commons/jci/CompilingClassLoader.java
 (original)
+++ 
jakarta/commons/sandbox/jci/trunk/src/java/org/apache/commons/jci/CompilingClassLoader.java
 Sun Mar 27 05:58:25 2005
@@ -26,8 +26,8 @@
 import org.apache.commons.logging.LogFactory;
 import org.apache.commons.jci.compilers.JavaCompiler;
 import org.apache.commons.jci.compilers.eclipse.EclipseJavaCompiler;
-import org.apache.commons.jci.monitor.AlterationListener;
-import org.apache.commons.jci.monitor.AlterationMonitor;
+import org.apache.commons.jci.monitor.FilesystemAlterationListener;
+import org.apache.commons.jci.monitor.FilesystemAlterationMonitor;
 import org.apache.commons.jci.problems.ConsoleCompilationProblemHandler;
 import org.apache.commons.jci.readers.FileResourceReader;
 import org.apache.commons.jci.readers.ResourceReader;
@@ -48,7 +48,7 @@
     private final TransactionalResourceStore store;
     private final ResourceReader reader;
     private final JavaCompiler compiler; 
-    private final AlterationMonitor fam;
+    private final FilesystemAlterationMonitor fam;
 
     public CompilingClassLoader(final ClassLoader pParent, final File 
pRepository) {
         this(pParent, pRepository,
@@ -70,9 +70,9 @@
         store = pStore;
         compiler = new EclipseJavaCompiler();
                 
-        fam = new AlterationMonitor(repository); 
+        fam = new FilesystemAlterationMonitor(); 
 
-        fam.addListener(new AlterationListener() {
+        fam.addListener(new FilesystemAlterationListener() {
 
             private Collection created = new ArrayList();
             private Collection changed = new ArrayList();
@@ -97,7 +97,7 @@
                 if (deleted.size() > 0) {
                     for (Iterator it = deleted.iterator(); it.hasNext();) {
                         final File file = (File) it.next();
-                        store.remove(clazzName(fam.getRoot(), file));
+                        store.remove(clazzName(repository, file));
                     }
                     reload = true;
                 }
@@ -113,7 +113,7 @@
                     int i = 0;
                     for (Iterator it = compileables.iterator(); it.hasNext();) 
{
                         final File file = (File) it.next();
-                        clazzes[i] = clazzName(fam.getRoot(),file);
+                        clazzes[i] = clazzName(repository,file);
                         //log.debug(clazzes[i]);
                         i++;
                     }
@@ -174,7 +174,7 @@
             }
             public void onDeleteDirectory( final File file ) {
             }
-            });
+            }, repository);
         
         delegate = new ResourceStoreClassLoader(parent, store);
 

Modified: 
jakarta/commons/sandbox/jci/trunk/src/java/org/apache/commons/jci/ReloadingClassLoader.java
URL: 
http://svn.apache.org/viewcvs/jakarta/commons/sandbox/jci/trunk/src/java/org/apache/commons/jci/ReloadingClassLoader.java?view=diff&r1=159147&r2=159148
==============================================================================
--- 
jakarta/commons/sandbox/jci/trunk/src/java/org/apache/commons/jci/ReloadingClassLoader.java
 (original)
+++ 
jakarta/commons/sandbox/jci/trunk/src/java/org/apache/commons/jci/ReloadingClassLoader.java
 Sun Mar 27 05:58:25 2005
@@ -24,8 +24,8 @@
 import java.util.Iterator;
 
 import org.apache.commons.io.IOUtils;
-import org.apache.commons.jci.monitor.AlterationListener;
-import org.apache.commons.jci.monitor.AlterationMonitor;
+import org.apache.commons.jci.monitor.FilesystemAlterationListener;
+import org.apache.commons.jci.monitor.FilesystemAlterationMonitor;
 import org.apache.commons.jci.readers.FileResourceReader;
 import org.apache.commons.jci.readers.ResourceReader;
 import org.apache.commons.jci.stores.MemoryResourceStore;
@@ -46,7 +46,7 @@
     private ClassLoader delegate;
     private final ResourceStore store;
     private final ResourceReader reader;
-    private final AlterationMonitor fam;
+    private final FilesystemAlterationMonitor fam;
 
     public ReloadingClassLoader(final ClassLoader pParent, final File 
pRepository) {
         this(pParent, pRepository, new MemoryResourceStore());
@@ -59,9 +59,9 @@
         reader = new FileResourceReader(repository);
         store = pStore;
                 
-        fam = new AlterationMonitor(repository); 
+        fam = new FilesystemAlterationMonitor(); 
 
-        fam.addListener(new AlterationListener() {
+        fam.addListener(new FilesystemAlterationListener() {
 
             private Collection created = new ArrayList();
             private Collection changed = new ArrayList();
@@ -79,7 +79,7 @@
                 if (deleted.size() > 0) {
                     for (Iterator it = deleted.iterator(); it.hasNext();) {
                         final File file = (File) it.next();
-                        store.remove(clazzName(fam.getRoot(), file));
+                        store.remove(clazzName(repository, file));
                     }
                     reload = true;
                 }
@@ -89,7 +89,7 @@
                         final File file = (File) it.next();
                         try {
                             final byte[] bytes = IOUtils.toByteArray(new 
FileReader(file));
-                            store.write(clazzName(fam.getRoot(), file), bytes);
+                            store.write(clazzName(repository, file), bytes);
                         } catch(final Exception e) {
                             log.error("could not load " + file, e);
                         }
@@ -127,7 +127,7 @@
             }
             public void onDeleteDirectory( final File file ) {
             }
-            });
+            }, repository);
         
         delegate = new ResourceStoreClassLoader(parent, store);
 

Copied: 
jakarta/commons/sandbox/jci/trunk/src/java/org/apache/commons/jci/monitor/FilesystemAlterationListener.java
 (from r156520, 
jakarta/commons/sandbox/jci/trunk/src/java/org/apache/commons/jci/monitor/AlterationListener.java)
URL: 
http://svn.apache.org/viewcvs/jakarta/commons/sandbox/jci/trunk/src/java/org/apache/commons/jci/monitor/FilesystemAlterationListener.java?view=diff&rev=159148&p1=jakarta/commons/sandbox/jci/trunk/src/java/org/apache/commons/jci/monitor/AlterationListener.java&r1=156520&p2=jakarta/commons/sandbox/jci/trunk/src/java/org/apache/commons/jci/monitor/FilesystemAlterationListener.java&r2=159148
==============================================================================
--- 
jakarta/commons/sandbox/jci/trunk/src/java/org/apache/commons/jci/monitor/AlterationListener.java
 (original)
+++ 
jakarta/commons/sandbox/jci/trunk/src/java/org/apache/commons/jci/monitor/FilesystemAlterationListener.java
 Sun Mar 27 05:58:25 2005
@@ -21,7 +21,7 @@
  * @author tcurdt
  *
  */
-public interface AlterationListener {
+public interface FilesystemAlterationListener {
     void onStart();
     void onCreateFile( final File file );
     void onChangeFile( final File file );

Copied: 
jakarta/commons/sandbox/jci/trunk/src/java/org/apache/commons/jci/monitor/FilesystemAlterationMonitor.java
 (from r156520, 
jakarta/commons/sandbox/jci/trunk/src/java/org/apache/commons/jci/monitor/AlterationMonitor.java)
URL: 
http://svn.apache.org/viewcvs/jakarta/commons/sandbox/jci/trunk/src/java/org/apache/commons/jci/monitor/FilesystemAlterationMonitor.java?view=diff&rev=159148&p1=jakarta/commons/sandbox/jci/trunk/src/java/org/apache/commons/jci/monitor/AlterationMonitor.java&r1=156520&p2=jakarta/commons/sandbox/jci/trunk/src/java/org/apache/commons/jci/monitor/FilesystemAlterationMonitor.java&r2=159148
==============================================================================
--- 
jakarta/commons/sandbox/jci/trunk/src/java/org/apache/commons/jci/monitor/AlterationMonitor.java
 (original)
+++ 
jakarta/commons/sandbox/jci/trunk/src/java/org/apache/commons/jci/monitor/FilesystemAlterationMonitor.java
 Sun Mar 27 05:58:25 2005
@@ -17,275 +17,378 @@
 
 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 final class AlterationMonitor implements Runnable {
-    
-    private final static Log log = LogFactory.getLog(AlterationMonitor.class);
-    
+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 pFile ) {
+
+
+        public Entry(final File pRoot, final File pFile) {
+            root = pRoot;
             file = pFile;
             lastModified = -1;
             isDirectory = file.isDirectory();
         }
-        
+
+
         public boolean hasChanged() {
             return file.lastModified() != 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();               
     
+
+                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(newFiles[i]);
+                r[i] = new Entry(root, newFiles[i]);
             }
             return r;
         }
-        
+
+
         public void add( final Entry entry ) {
             childs.add(entry);
             paths.add(entry.toString());
-            onCreate(entry);
+            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);                
+                delete(child);
             }
         }
-        
+
+
         public void delete( final Entry entry ) {
             childs.remove(entry);
             paths.remove(entry.toString());
             entry.deleteChilds();
-            onDelete(entry);
+            onDelete(root, entry);
         }
-        
+
+
         public File getFile() {
             return file;
         }
-        
+
+
+        public File getRoot() {
+            return root;
+        }
+
+
         public void markNotChanged() {
             lastModified = file.lastModified();
         }
-        
+
+
         public String toString() {
             return file.getAbsolutePath();
         }
     }
+
+    private Map listeners = new MultiHashMap();
+    private Map directories = new MultiHashMap();
+    private Map entries = new HashMap();
+
+    private final Object mutex = new Object();
     
-    
-    private final Entry root;
-    
-    private final Set listeners = new HashSet();
     private long delay = 3000;
     private boolean running = true;
 
-    public AlterationMonitor( final File pDirectory ) {
-        root = new Entry(pDirectory);
-    }
-    
-    public File getRoot() {
-        return root.getFile();
+    public FilesystemAlterationMonitor() {
     }
-    
+
+
     public void setInterval( final long pDelay ) {
         delay = pDelay;
     }
-    
-    public void addListener( final AlterationListener listener ) {
-        listeners.add(listener);
+
+
+    public void addListener( final FilesystemAlterationListener listener, 
final File directory ) {
+        synchronized (mutex) {
+            // listerner -> dir1, dir2, dir3
+
+            final MultiHashMap newListeners = new MultiHashMap(listeners);
+            newListeners.put(listener, directory);
+            listeners = newListeners;
+            
+            // directory -> listener1, listener2, listener3
+            final MultiHashMap newDirectories = new MultiHashMap(directories);
+            newDirectories.put(directory, listener);
+            directories = newDirectories;
+        }
     }
-    
-    public void removeListener( final AlterationListener listener ) {
-        listeners.remove(listener);
+
+
+    public void removeListener( final FilesystemAlterationListener listener ) {
+        synchronized (mutex) {
+            // listerner -> dir1, dir2, dir3
+            final MultiHashMap newListeners = new MultiHashMap(listeners);
+            Collection d = (Collection) newListeners.remove(listener);
+            listeners = newListeners;
+
+            if (d != null) {
+                // directory -> listener1, listener2, listener3
+                final MultiHashMap newDirectories = new 
MultiHashMap(directories);
+                for (Iterator it = d.iterator(); it.hasNext();) {
+                    newDirectories.remove(it.next());
+                    entries.remove(d);
+                }
+                directories = newDirectories;
+            }            
+        }
     }
 
-    private void onStart() {
-        for (Iterator it = listeners.iterator(); it.hasNext();) {
-            final AlterationListener listener = (AlterationListener) it.next();
-            listener.onStart();
+    private void onStart( final File root ) {
+        log.debug("* start checking " + root);
+
+        Map directories;
+        synchronized(mutex) {
+            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() {
-        for (Iterator it = listeners.iterator(); it.hasNext();) {
-            final AlterationListener listener = (AlterationListener) it.next();
-            listener.onStop();
+
+    private void onStop( final File root ) {
+        log.debug("* stop checking " + root);
+                
+        Map directories;
+        synchronized(mutex) {
+            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 Entry entry ) {
+
+
+    private void onCreate( final File root, final Entry entry ) {
         
-        if (entry.isDirectory()) {
-            log.debug("* created dir " + entry);
-            for (Iterator it = listeners.iterator(); it.hasNext();) {
-                final AlterationListener listener = (AlterationListener) 
it.next();
-                listener.onCreateDirectory(entry.getFile());
-            }
+        log.debug("* created " + ((entry.isDirectory())?"dir ":"file ") + 
entry);
+        
+        Map directories;
+        synchronized(mutex) {
+            directories = this.directories;
         }
-        else {
-            log.debug("* created file " + entry);
-            for (Iterator it = listeners.iterator(); it.hasNext();) {
-                final AlterationListener listener = (AlterationListener) 
it.next();
-                listener.onCreateFile(entry.getFile());
+
+        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 Entry entry ) {
-        if (entry.isDirectory()) {
-            log.debug("* changed dir " + entry);
-            for (Iterator it = listeners.iterator(); it.hasNext();) {
-                final AlterationListener listener = (AlterationListener) 
it.next();
-                listener.onChangeDirectory(entry.getFile());
-            }
+
+
+    private void onChange( final File root, final Entry entry ) {
+        
+        log.debug("* changed " + ((entry.isDirectory())?"dir ":"file ") + 
entry);
+        
+        Map directories;
+        synchronized(mutex) {
+            directories = this.directories;
         }
-        else {
-            log.debug("* changed file " + entry);            
-            for (Iterator it = listeners.iterator(); it.hasNext();) {
-                final AlterationListener listener = (AlterationListener) 
it.next();
-                listener.onChangeFile(entry.getFile());
+
+        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 Entry entry ) {
-        if (entry.isDirectory()) {
-            log.debug("* deleted dir " + entry);
-            for (Iterator it = listeners.iterator(); it.hasNext();) {
-                final AlterationListener listener = (AlterationListener) 
it.next();
-                listener.onDeleteDirectory(entry.getFile());
-            }
+
+
+    private void onDelete( final File root, final Entry entry ) {
+        
+        log.debug("* deleted " + ((entry.isDirectory())?"dir ":"file ") + 
entry);
+        
+        Map directories;
+        synchronized(mutex) {
+            directories = this.directories;
         }
-        else {
-            log.debug("* deleted file " + entry);            
-            for (Iterator it = listeners.iterator(); it.hasNext();) {
-                final AlterationListener listener = (AlterationListener) 
it.next();
-                listener.onDeleteFile(entry.getFile());
+
+        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 Entry entry, final boolean create ) {
-        if (entry.isDirectory()) {
 
-            final Entry[] currentChilds = entry.getChilds();
 
+    private void check( final File root, final Entry entry, final boolean 
create ) {
+        if (entry.isDirectory()) {
+            final Entry[] currentChilds = entry.getChilds();
             if (entry.hasChanged() || create) {
                 if (!create) {
-                    onChange(entry);
-                
-                       for (int i = 0; i < currentChilds.length; i++) {
-                           final Entry child = currentChilds[i];
-                           
-                           if (child.isDelected()) {
-                               entry.delete(child);
-                               currentChilds[i] = null;
-                           }
-                       }
+                    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(child, false);                        
-                           }
-                       }
+                    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(child, true);
+                    check(root, child, true);
                 }
-
-            }
-            else {
+            } else {
                 for (int i = 0; i < currentChilds.length; i++) {
                     final Entry child = currentChilds[i];
-                    check(child, false);
+                    check(root, child, false);
                 }
-                
             }
-
-        }
-        else {
+        } else {
             if (entry.isDelected()) {
-                throw new RuntimeException("should not get here");
+                onDelete(root, entry);
+            } else if (entry.hasChanged()) {
+                onChange(root, entry);
             }
-            else if (entry.hasChanged()) {
-                onChange(entry);
-            }            
         }
-
-            
     }
-    
+
+
     public void run() {
-        while(running) {
-               //log.debug("*");
+        
+        while (running) {
+            Map directories;
+            
+            synchronized (mutex) {
+                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 (mutex) {
+                        root = (Entry)entries.get(directory);
+                        if (root == null) {
+                            root = new Entry(directory, directory);
+                            entries.put(directory, root);
+                        }
+                    }
+                    
+                    check(directory, root, false);
+                    onStop(directory);
+                }
+            }
 
-               if (!root.isDelected()) {
-                       onStart();
-                   check(root, false);
-                       onStop();
-               }
-               
-               try {
-                   Thread.sleep(delay);
-               } catch (InterruptedException e) {
-               }
+            try {
+                Thread.sleep(delay);
+            } catch (InterruptedException e) {
+            }
         }
     }
-    
 }

Added: 
jakarta/commons/sandbox/jci/trunk/src/test/org/apache/commons/jci/monitor/CommandlineTest.java
URL: 
http://svn.apache.org/viewcvs/jakarta/commons/sandbox/jci/trunk/src/test/org/apache/commons/jci/monitor/CommandlineTest.java?view=auto&rev=159148
==============================================================================
--- 
jakarta/commons/sandbox/jci/trunk/src/test/org/apache/commons/jci/monitor/CommandlineTest.java
 (added)
+++ 
jakarta/commons/sandbox/jci/trunk/src/test/org/apache/commons/jci/monitor/CommandlineTest.java
 Sun Mar 27 05:58:25 2005
@@ -0,0 +1,54 @@
+package org.apache.commons.jci.monitor;
+
+import java.io.File;
+
+
+public final class CommandlineTest {
+
+    public static void main( String[] args ) {
+        
+        File repository = new File("/Users/tcurdt/dev/cocoon-trunk/eclipse");
+        //File repository = new File("/Users/tcurdt/dev/test");
+               
+        FilesystemAlterationMonitor fam = new FilesystemAlterationMonitor(); 
+
+        fam.addListener(new FilesystemAlterationListener() {
+            public void onStart() {
+                System.out.println("start");
+            }
+            public void onStop() {
+                System.out.println("stop");
+            }
+
+            public void onCreateFile( final File file ) {
+                System.out.println("create file");
+            }
+            public void onChangeFile( final File file ) {                
+                System.out.println("change file");
+            }
+            public void onDeleteFile( final File file ) {
+                System.out.println("delete file");
+            }
+
+            public void onCreateDirectory( final File file ) {                
+                System.out.println("create dir");
+            }
+            public void onChangeDirectory( final File file ) {                
+                System.out.println("change dir");
+            }
+            public void onDeleteDirectory( final File file ) {
+                System.out.println("delete dir");
+            }
+            }, repository);
+        
+        Thread myThread = new Thread(fam); 
+        myThread.start();
+        
+        while(true) {
+            try {
+                Thread.sleep(1000);
+            } catch (InterruptedException e) {
+            }
+        }
+    }
+}



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

Reply via email to