PropertiesFile and WatchedFile
authoradam <adam@megacz.com>
Thu, 9 Mar 2006 08:30:00 +0000 (08:30 +0000)
committeradam <adam@megacz.com>
Thu, 9 Mar 2006 08:30:00 +0000 (08:30 +0000)
darcs-hash:20060309083000-5007d-9a3606142f5fdca017026b930618cd8b51f4b841.gz

src/org/ibex/io/PropertiesFile.java [new file with mode: 0644]
src/org/ibex/io/WatchedFile.java [new file with mode: 0644]

diff --git a/src/org/ibex/io/PropertiesFile.java b/src/org/ibex/io/PropertiesFile.java
new file mode 100644 (file)
index 0000000..af5073b
--- /dev/null
@@ -0,0 +1,46 @@
+// Copyright 2000-2006 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.io;
+
+import java.io.*;
+import java.net.*;
+import java.util.*;
+import java.util.zip.*;
+import org.ibex.util.*;
+
+public class PropertiesFile {
+
+    public static final int INTERVAL = 2000; /* ms */
+
+    private File f;
+    private long lastChecked = 0;
+    private long lastModified = 0;
+    private Properties props = null;
+
+    public PropertiesFile(File f) throws IOException {
+        this.f = f;
+        if (!f.exists()) throw new IOException("file " + f + " does not exist");
+        checkReload();
+    }
+
+    public String get(String key) {
+        checkReload();
+        return (String)props.get(key);
+    }
+
+    public void checkReload() {
+        long diff = System.currentTimeMillis() - lastChecked;
+        if (diff < INTERVAL) return;
+        lastChecked += diff;
+        try {
+            if (lastModified == f.lastModified()) return;
+            lastModified = f.lastModified();
+            Properties newProps = new Properties();
+            newProps.load(new FileInputStream(f));
+            props = newProps;
+        } catch (Exception e) { Log.warn(this, e); }
+    }
+
+}
diff --git a/src/org/ibex/io/WatchedFile.java b/src/org/ibex/io/WatchedFile.java
new file mode 100644 (file)
index 0000000..339700f
--- /dev/null
@@ -0,0 +1,67 @@
+// Copyright 2000-2006 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.io;
+
+import java.io.*;
+import java.net.*;
+import java.util.*;
+import java.util.zip.*;
+import org.ibex.util.*;
+
+//
+// FIXME: this needs to be robust against a getLastModified() call
+// that hangs forever... what we need is a "watcher watcher" thread
+// that spawns a new watcher and leaves a note for the old watcher
+// telling him he's been left behind.
+//
+
+/** a "watched" file */
+public class WatchedFile extends File {
+
+    private static Vec files = new Vec();
+
+    private long lastModified = 0;
+
+    public void deleted() { }
+    public void modified() { }
+
+    public WatchedFile(String s) throws IOException {
+        super(s);
+        if (!this.exists()) throw new IOException("file " + this + " does not exist");
+        lastModified = lastModified();
+        //lastSize     = length();
+        files.add(this);
+    }
+
+    private static Thread watcherThread = new ImmortalThread(2000) {
+            int i=0;
+            public void cycle() throws Exception {
+                while(i<files.size()) {
+                    final WatchedFile f = (WatchedFile)files.get(i++);
+                    if (!f.exists()) {
+                        files.remove(i);
+                        new ResponseThread() { public void run2() throws Exception { f.deleted(); } }.start();
+                        i--;
+                    } else if (f.lastModified()!=f.lastModified) {
+                        f.lastModified = f.lastModified();
+                        new ResponseThread() { public void run2() throws Exception { f.modified(); } }.start();
+                    }
+                }
+                i=0;
+            }
+        };
+    static { watcherThread.start(); }
+
+    // FIXME: throttle these somehow?
+    private abstract static class ResponseThread extends Thread {
+        public abstract void run2() throws Exception;
+        public void run() {
+            try {
+                run2();
+            } catch (Exception e) { Log.error(this, e); }
+        }
+    }
+
+}