PropertiesFile and WatchedFile
[org.ibex.io.git] / src / org / ibex / io / PropertiesFile.java
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); }
+    }
+
+}