Reworked class to actually use itself as propertie container instead of a delegate

properties object.
This commit is contained in:
hns 2002-11-13 16:35:13 +00:00
parent a41483168a
commit 09d0bf57b9

View file

@ -13,8 +13,6 @@ import java.io.*;
public final class SystemProperties extends Properties { public final class SystemProperties extends Properties {
private Properties props; // wrapped properties
private Properties newProps; // used while building up props
private SystemProperties defaultProps; // the default/fallback properties. private SystemProperties defaultProps; // the default/fallback properties.
private File file; // the underlying properties file from which we read. private File file; // the underlying properties file from which we read.
private long lastread, lastcheck, lastadd; // time we last read/checked the underlying properties file private long lastread, lastcheck, lastadd; // time we last read/checked the underlying properties file
@ -23,9 +21,10 @@ public final class SystemProperties extends Properties {
// did a check, in milliseconds. // did a check, in milliseconds.
final static long cacheTime = 1500l; final static long cacheTime = 1500l;
// map of additional properties
private HashMap additionalProps = null; private HashMap additionalProps = null;
/** /**
* Construct an empty properties object. * Construct an empty properties object.
*/ */
public SystemProperties () { public SystemProperties () {
@ -69,8 +68,8 @@ public final class SystemProperties extends Properties {
*/ */
public SystemProperties (String filename, SystemProperties defaultProps) { public SystemProperties (String filename, SystemProperties defaultProps) {
// System.err.println ("building sysprops with file "+filename+" and node "+node); // System.err.println ("building sysprops with file "+filename+" and node "+node);
super (defaultProps);
this.defaultProps = defaultProps; this.defaultProps = defaultProps;
props = defaultProps == null ? new Properties () : new Properties (defaultProps);
file = filename == null ? null : new File (filename); file = filename == null ? null : new File (filename);
lastcheck = lastread = lastadd = 0; lastcheck = lastread = lastadd = 0;
} }
@ -83,7 +82,7 @@ public final class SystemProperties extends Properties {
return lastadd; return lastadd;
return Math.max (file.lastModified (), lastadd); return Math.max (file.lastModified (), lastadd);
} }
/** /**
* Return a checksum that changes when something in the properties changes. * Return a checksum that changes when something in the properties changes.
*/ */
@ -123,16 +122,13 @@ public final class SystemProperties extends Properties {
public synchronized void load (InputStream in) throws IOException { public synchronized void load (InputStream in) throws IOException {
newProps = defaultProps == null ? clear ();
new Properties () : new Properties (defaultProps);
super.load (in); super.load (in);
lastread = System.currentTimeMillis ();
props = newProps;
newProps = null;
if (additionalProps != null) { if (additionalProps != null) {
for (Iterator i=additionalProps.values().iterator(); i.hasNext(); ) for (Iterator i=additionalProps.values().iterator(); i.hasNext(); )
props.putAll ((Properties) i.next()); putAll ((Properties) i.next());
} }
lastread = System.currentTimeMillis ();
} }
@ -146,13 +142,13 @@ public final class SystemProperties extends Properties {
if (additionalProps == null) if (additionalProps == null)
additionalProps = new HashMap (); additionalProps = new HashMap ();
additionalProps.put (key, p); additionalProps.put (key, p);
if (props != null) putAll (p);
props.putAll (p);
else
props = p;
lastadd = System.currentTimeMillis (); lastadd = System.currentTimeMillis ();
} }
/**
* Remove an additional properties dictionary.
*/
public synchronized void removeProps (String key) { public synchronized void removeProps (String key) {
if (additionalProps != null) { if (additionalProps != null) {
// remove added properties for this key. If we had // remove added properties for this key. If we had
@ -172,10 +168,7 @@ public final class SystemProperties extends Properties {
// cut off trailing whitespace // cut off trailing whitespace
if (value != null) if (value != null)
value = value.toString().trim(); value = value.toString().trim();
if (newProps == null) return super.put (key.toString().toLowerCase(), value);
return props.put (key.toString().toLowerCase(), value);
else
return newProps.put (key.toString().toLowerCase(), value);
} }
/** /**
@ -184,21 +177,14 @@ public final class SystemProperties extends Properties {
public Object get (Object key) { public Object get (Object key) {
if (System.currentTimeMillis () - lastcheck > cacheTime) if (System.currentTimeMillis () - lastcheck > cacheTime)
checkFile (); checkFile ();
return props.get (key.toString().toLowerCase()); return super.get (key.toString().toLowerCase());
} }
/** /**
* Overrides method to act on the wrapped properties object. * Overrides method to act on the wrapped properties object.
*/ */
public Object remove (Object key) { public Object remove (Object key) {
return props.remove (key.toString().toLowerCase()); return super.remove (key.toString().toLowerCase());
}
/**
* Overrides method to act on the wrapped properties object.
*/
public void clear () {
props.clear ();
} }
/** /**
@ -207,7 +193,7 @@ public final class SystemProperties extends Properties {
public boolean contains (Object obj) { public boolean contains (Object obj) {
if (System.currentTimeMillis () - lastcheck > cacheTime) if (System.currentTimeMillis () - lastcheck > cacheTime)
checkFile (); checkFile ();
return props.contains (obj); return super.contains (obj);
} }
/** /**
@ -216,7 +202,7 @@ public final class SystemProperties extends Properties {
public boolean containsKey (Object key) { public boolean containsKey (Object key) {
if (System.currentTimeMillis () - lastcheck > cacheTime) if (System.currentTimeMillis () - lastcheck > cacheTime)
checkFile (); checkFile ();
return props.containsKey (key.toString().toLowerCase()); return super.containsKey (key.toString().toLowerCase());
} }
/** /**
@ -225,7 +211,7 @@ public final class SystemProperties extends Properties {
public boolean isEmpty () { public boolean isEmpty () {
if (System.currentTimeMillis () - lastcheck > cacheTime) if (System.currentTimeMillis () - lastcheck > cacheTime)
checkFile (); checkFile ();
return props.isEmpty (); return super.isEmpty ();
} }
/** /**
@ -234,7 +220,7 @@ public final class SystemProperties extends Properties {
public String getProperty (String name) { public String getProperty (String name) {
if (System.currentTimeMillis () - lastcheck > cacheTime) if (System.currentTimeMillis () - lastcheck > cacheTime)
checkFile (); checkFile ();
return props.getProperty (name.toLowerCase()); return super.getProperty (name.toLowerCase());
} }
/** /**
@ -243,7 +229,7 @@ public final class SystemProperties extends Properties {
public String getProperty (String name, String defaultValue) { public String getProperty (String name, String defaultValue) {
if (System.currentTimeMillis () - lastcheck > cacheTime) if (System.currentTimeMillis () - lastcheck > cacheTime)
checkFile (); checkFile ();
return props.getProperty (name.toLowerCase(), defaultValue); return super.getProperty (name.toLowerCase(), defaultValue);
} }
/** /**
@ -252,7 +238,7 @@ public final class SystemProperties extends Properties {
public Enumeration keys () { public Enumeration keys () {
if (System.currentTimeMillis () - lastcheck > cacheTime) if (System.currentTimeMillis () - lastcheck > cacheTime)
checkFile (); checkFile ();
return props.keys(); return super.keys();
} }
/** /**
@ -261,7 +247,7 @@ public final class SystemProperties extends Properties {
public Set keySet () { public Set keySet () {
if (System.currentTimeMillis () - lastcheck > cacheTime) if (System.currentTimeMillis () - lastcheck > cacheTime)
checkFile (); checkFile ();
return props.keySet(); return super.keySet();
} }
/** /**
@ -270,7 +256,7 @@ public final class SystemProperties extends Properties {
public Enumeration elements () { public Enumeration elements () {
if (System.currentTimeMillis () - lastcheck > cacheTime) if (System.currentTimeMillis () - lastcheck > cacheTime)
checkFile (); checkFile ();
return props.elements(); return super.elements();
} }
/** /**
@ -279,14 +265,14 @@ public final class SystemProperties extends Properties {
public int size() { public int size() {
if (System.currentTimeMillis () - lastcheck > cacheTime) if (System.currentTimeMillis () - lastcheck > cacheTime)
checkFile (); checkFile ();
return props.size(); return super.size();
} }
/** /**
* Overrides method to act on the wrapped properties object. * Overrides method to act on the wrapped properties object.
*/ */
public String toString () { public String toString () {
return props.toString (); return super.toString ();
} }
} }