add a FIXME in WatchedFile
[org.ibex.io.git] / src / org / ibex / io / WatchedFile.java
1 // Copyright 2000-2006 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.io;
6
7 import java.io.*;
8 import java.net.*;
9 import java.util.*;
10 import java.util.zip.*;
11 import org.ibex.util.*;
12
13 //
14 // FIXME: this needs to be robust against a getLastModified() call
15 // that hangs forever... what we need is a "watcher watcher" thread
16 // that spawns a new watcher and leaves a note for the old watcher
17 // telling him he's been left behind.
18 //
19
20 // FIXME: use cronjob api
21
22 /** a "watched" file */
23 public class WatchedFile extends File {
24
25     private static Vec files = new Vec();
26
27     private long lastModified = 0;
28
29     public void deleted() { }
30     public void modified() { }
31
32     public WatchedFile(String s) throws IOException {
33         super(s);
34         if (!this.exists()) throw new IOException("file " + this + " does not exist");
35         lastModified = lastModified();
36         //lastSize     = length();
37         files.add(this);
38     }
39
40     private static Thread watcherThread = new ImmortalThread(2000) {
41             int i=0;
42             public void cycle() throws Exception {
43                 while(i<files.size()) {
44                     final WatchedFile f = (WatchedFile)files.get(i++);
45                     if (!f.exists()) {
46                         files.remove(i);
47                         new ResponseThread() { public void run2() throws Exception { f.deleted(); } }.start();
48                         i--;
49                     } else if (f.lastModified()!=f.lastModified) {
50                         f.lastModified = f.lastModified();
51                         new ResponseThread() { public void run2() throws Exception { f.modified(); } }.start();
52                     }
53                 }
54                 i=0;
55             }
56         };
57     static { watcherThread.start(); }
58
59     // FIXME: throttle these somehow?
60     private abstract static class ResponseThread extends Thread {
61         public abstract void run2() throws Exception;
62         public void run() {
63             try {
64                 run2();
65             } catch (Exception e) { Log.error(this, e); }
66         }
67     }
68
69 }