bogus patch
[org.ibex.mail.git] / src / org / ibex / jinetd / Watched.java
1 package org.ibex.jinetd;
2 import org.ibex.util.*;
3 import java.io.*;
4 import java.util.*;
5
6 public class Watched extends File {
7
8     // Instance //////////////////////////////////////////////////////////////////////////////
9     
10     private Hashtable cache = new Hashtable();
11     long lastModifiedAtLastScan = -1;
12     public final String path;
13     public final String part;
14
15     public Watcher watcher() { return ((Watched)all.get(getParent())).watcher(); }
16     public Watched slash(String part) { return get(this.path + File.separatorChar + part); }
17     public void scan() throws IOException {
18         if (!exists())                                { return; }
19         if (lastModifiedAtLastScan != lastModified()) { watcher().changed(this); lastModifiedAtLastScan = lastModified(); }
20         if (!isDirectory())                           { return; }
21         Vec removals = new Vec();
22         for(Iterator i = cache.values().iterator(); i.hasNext();) {
23             Watched w = ((Watched)i.next());
24             if (w.exists()) w.scan();
25             else { watcher().changed(w); removals.addElement(w.path.substring(this.path.length() + 1)); }
26         }
27         for(int i=0; i<removals.size(); i++) cache.remove(removals.elementAt(i));
28         String[] kids = list();        
29         for(int i=0; i<kids.length; i++) {
30             if (cache.get(kids[i]) != null) continue;
31             Watched kid = slash(kids[i]);
32             if (kid == null) continue;
33             cache.put(kids[i], kid);
34             watcher().changed(kid);
35         }
36     }
37
38
39     // Pooling //////////////////////////////////////////////////////////////////////////////
40
41     private static WeakHashMap all = new WeakHashMap();
42     protected Watched(String path) {
43         super(path);
44         this.path = path;
45         this.part = path.substring(path.lastIndexOf(File.separatorChar) + 1);
46         all.put(path, this);
47     }
48     private static Watched get(String path) {
49         Watched ret = (Watched)all.get(path);
50         if (ret == null) ret = new Watched(path);
51         return ret;
52     }
53 }