From 1594d72289e3da167db29dc470b9a3b3080bd676 Mon Sep 17 00:00:00 2001 From: adam Date: Thu, 9 Mar 2006 08:30:00 +0000 Subject: [PATCH] PropertiesFile and WatchedFile darcs-hash:20060309083000-5007d-9a3606142f5fdca017026b930618cd8b51f4b841.gz --- src/org/ibex/io/PropertiesFile.java | 46 ++++++++++++++++++++++++ src/org/ibex/io/WatchedFile.java | 67 +++++++++++++++++++++++++++++++++++ 2 files changed, 113 insertions(+) create mode 100644 src/org/ibex/io/PropertiesFile.java create mode 100644 src/org/ibex/io/WatchedFile.java diff --git a/src/org/ibex/io/PropertiesFile.java b/src/org/ibex/io/PropertiesFile.java new file mode 100644 index 0000000..af5073b --- /dev/null +++ b/src/org/ibex/io/PropertiesFile.java @@ -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 index 0000000..339700f --- /dev/null +++ b/src/org/ibex/io/WatchedFile.java @@ -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