Unnamed patch
[org.ibex.jinetd.git] / src / org / ibex / jinetd / Watcher.java
index 86b1958..265073c 100644 (file)
@@ -1,10 +1,35 @@
+// Copyright 2000-2005 the Contributors, as shown in the revision logs.
+// Licensed under the Apache Public Source License 2.0 ("the License").
+// You may not use this file except in compliance with the License.
+
 package org.ibex.jinetd;
 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 abstract void changed(Watched w);
-    public Watcher watcher() { return this; }
+
+    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(new File(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(); }
+        }
+    }
 }