001: // DaemonProperties.java
002: // $Id: DaemonProperties.java,v 1.12 2000/08/16 21:37:34 ylafon Exp $
003: // (c) COPYRIGHT MIT and INRIA, 1996.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005:
006: package org.w3c.jigsaw.daemon;
007:
008: import java.util.Hashtable;
009: import java.util.Properties;
010:
011: import java.io.BufferedInputStream;
012: import java.io.File;
013: import java.io.FileInputStream;
014: import java.io.FileNotFoundException;
015: import java.io.IOException;
016: import java.io.InputStream;
017: import java.io.PrintStream;
018:
019: import org.w3c.util.ObservableProperties;
020:
021: /**
022: * Subclass of Properties, to deal with daemon specific usage of them.
023: * This class refines the basic Properties class, in order to tune them for
024: * ServerHandler specific needs:
025: * <ul>
026: * <li>Properties can be observed through the PropertyMonitoring interface,
027: * <li>Properties are multiplexed according to the server identifier. Two
028: * servers can rely on the same set of properties, but each of them can
029: * have its own property value. Eg the org.w3c.jigsaw.http package host
030: * property can be set fro server1 to host1 and for server2 to host2.
031: * This is done by defining server1.org.w3c.jigsaw.http.host
032: * and server2.org.w3c.jigsaw.http.host
033: * </ul>
034: * <p>Each property can be monitored, to allow for dynamic reconfiguration of
035: * the server.
036: * @see org.w3c.util.PropertyMonitoring
037: */
038:
039: public class DaemonProperties {
040: /**
041: * The set of loaded properties set.
042: */
043: protected Hashtable propspace = new Hashtable(5);
044: /**
045: * The global set of properties (inherited by all spaces).
046: */
047: protected Properties globprops = null;
048: /**
049: * Our base config directory.
050: */
051: protected File configdir = null;
052:
053: /**
054: * Extend a property space.
055: * @param id The identifier of the property set to extend.
056: * @param in The input stream containing Java properties to add.
057: * @exception IOException If the input stream couldn't be read.
058: */
059:
060: public ObservableProperties loadPropertySpace(String id,
061: InputStream in) throws IOException {
062: ObservableProperties p = (ObservableProperties) propspace
063: .get(id);
064: if (p == null) {
065: p = new ObservableProperties(globprops);
066: propspace.put(id, p);
067: }
068: p.load(in);
069: return p;
070: }
071:
072: /**
073: * Load in the default properties for the given space.
074: * The file from which properties are loaded is kept itself as the
075: * <code>org.w3c.jigsaw.propfile</code> property.
076: * @param id The identifier of the property set to load.
077: * @exception FileNotFoundException If the default property file wasn't
078: * found.
079: * @exception IOException If default property file couldn't be read.
080: */
081:
082: public ObservableProperties loadPropertySpace(String id)
083: throws IOException, FileNotFoundException {
084: File file = new File(configdir, id + ".props");
085: ObservableProperties p = null;
086: p = loadPropertySpace(id, (new BufferedInputStream(
087: new FileInputStream(file))));
088: p
089: .put(org.w3c.jigsaw.http.httpd.PROPS_P, file
090: .getAbsolutePath());
091: return p;
092: }
093:
094: /**
095: * Get the properties for the given space.
096: * @param id The identifier for a property set space.
097: * @return An ObservableProperties instance, or <strong>null</strong>.
098: */
099:
100: public ObservableProperties getPropertySpace(String id) {
101: return (ObservableProperties) propspace.get(id);
102: }
103:
104: // FIXME
105: public void save() {
106: System.out.println("DaemonProperties.save: not implemented !");
107: }
108:
109: // FIXME
110: public void savePropertySpace(String id) {
111: System.out.println("DaemonProperties.save [" + id
112: + "]; not implemented!");
113: }
114:
115: // FIXME doc
116: public void load(InputStream in) throws IOException {
117: if (globprops == null)
118: globprops = new Properties();
119: globprops.load(in);
120: }
121:
122: // FIXME doc
123: public String getProperty(String name) {
124: return (globprops != null) ? globprops.getProperty(name) : null;
125: }
126:
127: public String getString(String name, String def) {
128: return (globprops != null) ? globprops.getProperty(name, def)
129: : def;
130: }
131:
132: /**
133: * @param props The global properties to use in all spaces.
134: */
135:
136: public DaemonProperties(File configdir, Properties props) {
137: this.configdir = configdir;
138: this.globprops = props;
139: }
140:
141: }
|