001: package com.quadcap.util;
002:
003: /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
004: *
005: * This software is distributed under the Quadcap Free Software License.
006: * This software may be used or modified for any purpose, personal or
007: * commercial. Open Source redistributions are permitted. Commercial
008: * redistribution of larger works derived from, or works which bundle
009: * this software requires a "Commercial Redistribution License"; see
010: * http://www.quadcap.com/purchase.
011: *
012: * Redistributions qualify as "Open Source" under one of the following terms:
013: *
014: * Redistributions are made at no charge beyond the reasonable cost of
015: * materials and delivery.
016: *
017: * Redistributions are accompanied by a copy of the Source Code or by an
018: * irrevocable offer to provide a copy of the Source Code for up to three
019: * years at the cost of materials and delivery. Such redistributions
020: * must allow further use, modification, and redistribution of the Source
021: * Code under substantially the same terms as this license.
022: *
023: * Redistributions of source code must retain the copyright notices as they
024: * appear in each source code file, these license terms, and the
025: * disclaimer/limitation of liability set forth as paragraph 6 below.
026: *
027: * Redistributions in binary form must reproduce this Copyright Notice,
028: * these license terms, and the disclaimer/limitation of liability set
029: * forth as paragraph 6 below, in the documentation and/or other materials
030: * provided with the distribution.
031: *
032: * The Software is provided on an "AS IS" basis. No warranty is
033: * provided that the Software is free of defects, or fit for a
034: * particular purpose.
035: *
036: * Limitation of Liability. Quadcap Software shall not be liable
037: * for any damages suffered by the Licensee or any third party resulting
038: * from use of the Software.
039: */
040:
041: import java.io.File;
042: import java.io.FileInputStream;
043:
044: import java.util.Enumeration;
045: import java.util.Hashtable;
046: import java.util.Properties;
047:
048: /**
049: * Central repository for configuration information.
050: *
051: * @author Stan Bailes
052: */
053: public class Config {
054: private static final Hashtable vars = new Hashtable();
055: private static final Properties vals = new Properties(System
056: .getProperties());
057: private static final Object lock = new Object();
058:
059: static {
060: try {
061: String filename = System.getProperty("config.props",
062: "config.props");
063: File f = new File(filename);
064: if (f.canRead()) {
065: FileInputStream fis = new FileInputStream(f);
066: vals.load(fis);
067: fis.close();
068: }
069: } catch (Throwable e) {
070: Debug.print(e);
071: }
072: }
073:
074: public static void reset() {
075: vars.clear();
076: vals.clear();
077: }
078:
079: public static final ConfigVar find(Class c, String name, String dflt) {
080: synchronized (lock) {
081: ConfigVar var = (ConfigVar) vars.get(name);
082: if (var == null) {
083: String val = vals.getProperty(name);
084: if (val == null)
085: val = dflt;
086:
087: try {
088: var = (ConfigVar) c.newInstance();
089: var.init(name, val);
090: } catch (Exception e) {
091: Debug.print(e);
092: var = null;
093: }
094: if (var != null)
095: vars.put(name, var);
096: }
097: return var;
098: }
099: }
100:
101: public static final ConfigVar find(String name) {
102: synchronized (lock) {
103: return (ConfigVar) vars.get(name);
104: }
105: }
106:
107: public static final String getProperty(String name) {
108: ConfigVar f = find(name);
109: if (f == null) {
110: f = ConfigString.find(name, null);
111: }
112: return (f == null) ? null : f.getValue();
113: }
114:
115: public static final String getProperty(String name, String dflt) {
116: ConfigVar f = find(name);
117: if (f == null) {
118: f = ConfigString.find(name, dflt);
119: }
120: return (f == null) ? dflt : f.getValue();
121: }
122:
123: public static Enumeration getMatchingProps(String pattern) {
124: return getMatchingProps(vals, pattern);
125: }
126:
127: /**
128: * Assuming that <code>str</code> matches pattern <code>pat</code>,
129: * with a wildcard character in the pattern, return the portion of the
130: * string matching the wildcard.
131: *
132: * @param str the matched string
133: * @param pat the pattern
134: * @return the wildcard portion of the match.
135: */
136: public static String getMatch(String s, String p) {
137: int pre = p.indexOf('*');
138: int post = (p.length() - pre) - 1;
139: s = s.substring(pre);
140: s = s.substring(0, s.length() - post);
141: return s;
142: }
143:
144: public static Properties getPropSubset(Properties props,
145: String pattern) {
146: Properties np = new Properties();
147: OctetString pat = new OctetString(pattern);
148: final OctetComparator cmp = OctetComparator.cmp;
149:
150: Enumeration e = props.keys();
151: while (e.hasMoreElements()) {
152: String name = e.nextElement().toString();
153: OctetString s = new OctetString(name);
154: if (cmp.patternMatch(s, pat)) {
155: np.put(getMatch(name, pattern), props.get(name));
156: }
157: }
158: return np;
159: }
160:
161: public static Properties getPropSubset(String pattern) {
162: return getPropSubset(vals, pattern);
163: }
164:
165: /**
166: * Return an enumeration of property names which match a glob-style
167: * pattern. This is useful when properties are used to specify a
168: * set, e.g., servlet.*.class=<i>name</i> or class.*.preload=true.
169: *
170: * @param props the property set to search
171: * @param pattern the glob style pattern used by
172: * OctetComparator.patternMatch().
173: * @return an Enumeration of the glob-matching part of each of the
174: * names matching the pattern
175: */
176: public static Enumeration getMatchingProps(Properties props,
177: final String pattern) {
178: final Enumeration e = props.keys();
179: final OctetString p = new OctetString(pattern);
180: final OctetComparator cmp = OctetComparator.cmp;
181:
182: return new Enumeration() {
183: String next = null;
184:
185: public boolean hasMoreElements() {
186: while (next == null && e.hasMoreElements()) {
187: String s = e.nextElement().toString();
188: OctetString os = new OctetString(s);
189: if (cmp.patternMatch(os, p)) {
190: next = getMatch(s, pattern);
191: }
192: }
193: return next != null;
194: }
195:
196: public Object nextElement() {
197: Object ret = next;
198: next = null;
199: hasMoreElements();
200: return ret;
201: }
202: };
203: }
204:
205: public static Properties getProperties() {
206: return vals;
207: }
208: }
|