PropertiesFile and WatchedFile
[org.ibex.io.git] / src / org / ibex / io / PropertiesFile.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 public class PropertiesFile {
14
15     public static final int INTERVAL = 2000; /* ms */
16
17     private File f;
18     private long lastChecked = 0;
19     private long lastModified = 0;
20     private Properties props = null;
21
22     public PropertiesFile(File f) throws IOException {
23         this.f = f;
24         if (!f.exists()) throw new IOException("file " + f + " does not exist");
25         checkReload();
26     }
27
28     public String get(String key) {
29         checkReload();
30         return (String)props.get(key);
31     }
32
33     public void checkReload() {
34         long diff = System.currentTimeMillis() - lastChecked;
35         if (diff < INTERVAL) return;
36         lastChecked += diff;
37         try {
38             if (lastModified == f.lastModified()) return;
39             lastModified = f.lastModified();
40             Properties newProps = new Properties();
41             newProps.load(new FileInputStream(f));
42             props = newProps;
43         } catch (Exception e) { Log.warn(this, e); }
44     }
45
46 }