980f864180a0b2c582015c3e5bfbb6fcbe3708ad
[org.ibex.jinetd.git] / src / org / ibex / jinetd / Watcher.java
1 // Copyright 2000-2005 the Contributors, as shown in the revision logs.
2 // Licensed under the Apache Public Source License 2.0 ("the License").
3 // You may not use this file except in compliance with the License.
4
5 package org.ibex.jinetd;
6 import org.ibex.util.*;
7 import java.io.*;
8 import java.util.*;
9
10 public abstract class Watcher extends File {
11
12     protected Watcher(String path) { super(path); }
13
14     public abstract void changed(File f) throws IOException;
15
16     private Vec watched = new Vec();
17
18     public void watch(File f) { watched.addElement(new Watched(f.getAbsolutePath())); }
19     public void watch(String s) { watch(getAbsolutePath() + File.separatorChar + s); }
20
21     public void scan() throws IOException {
22         for(int i=watched.size()-1; i>=0; i--)
23             ((Watched)(watched.elementAt(i))).scan();
24     }
25
26     private class Watched extends File {
27         long lastModifiedAtLastScan = -1;
28         public Watched(String s) { super(s); }
29         public void scan() throws IOException {
30             // FIXME
31             //if (!exists())                         { Watcher.this.changed(this); Watcher.this.watched.remove(this); return; }
32             if (lastModifiedAtLastScan != lastModified()) { Watcher.this.changed(this); lastModifiedAtLastScan = lastModified(); }
33         }
34     }
35 }