一定時間ごとに変更されたファイルを読み込む設定クラス

Log4JのFileWatchdogクラスのパクリ。


import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

public class DaemonConfig extends Thread {

private long delay = 60000;

private File file = null;

private long lastModified = 0;

private Properties props = new Properties();

public DaemonConfig(String filename) {
file = new File(filename);
configure();
setDaemon(true);
}

public void setDelay(long delay) {
this.delay = delay;
}

public void run() {
while (true) {
try {
sleep(delay);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}

check();
}
}

private void check() {
if (file.lastModified() > lastModified) {
lastModified = file.lastModified();
configure();
}
}

private void configure() {
try {
FileInputStream fin = new FileInputStream(file);
props.load(fin);
fin.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}

public String get(String key) {
return props.getProperty(key);
}

}