cleanup
[org.ibex.jinetd.git] / src / org / ibex / jinetd / Watcher.java
index 19a33d6..980f864 100644 (file)
@@ -7,9 +7,29 @@ import org.ibex.util.*;
 import java.io.*;
 import java.util.*;
 
-public abstract class Watcher extends Watched {
+public abstract class Watcher extends File {
+
     protected Watcher(String path) { super(path); }
-    public Watcher watcher() { return this; }
-    public abstract void changed(Watched w);
-    public synchronized void scan() throws IOException { super.scan(); }
+
+    public abstract void changed(File f) throws IOException;
+
+    private Vec watched = new Vec();
+
+    public void watch(File f) { watched.addElement(new Watched(f.getAbsolutePath())); }
+    public void watch(String s) { watch(getAbsolutePath() + File.separatorChar + s); }
+
+    public void scan() throws IOException {
+        for(int i=watched.size()-1; i>=0; i--)
+            ((Watched)(watched.elementAt(i))).scan();
+    }
+
+    private class Watched extends File {
+        long lastModifiedAtLastScan = -1;
+        public Watched(String s) { super(s); }
+        public void scan() throws IOException {
+            // FIXME
+            //if (!exists())                         { Watcher.this.changed(this); Watcher.this.watched.remove(this); return; }
+            if (lastModifiedAtLastScan != lastModified()) { Watcher.this.changed(this); lastModifiedAtLastScan = lastModified(); }
+        }
+    }
 }