00001
package com.quadcap.util;
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
import java.io.File;
00042
import java.io.FileInputStream;
00043
00044
import java.util.Enumeration;
00045
import java.util.Hashtable;
00046
import java.util.Properties;
00047
00048
00049
00050
00051
00052
00053 public class Config {
00054 private static final Hashtable
vars =
new Hashtable();
00055 private static final Properties
vals =
00056
new Properties(System.getProperties());
00057 private static final Object
lock =
new Object();
00058
00059
static {
00060
try {
00061 String filename = System.getProperty(
"config.props",
00062
"config.props");
00063 File f =
new File(filename);
00064
if (f.canRead()) {
00065 FileInputStream fis =
new FileInputStream(f);
00066
vals.load(fis);
00067 fis.close();
00068 }
00069 }
catch (Throwable e) {
00070 Debug.print(e);
00071 }
00072 }
00073
00074 public static void reset() {
00075
vars.clear();
00076
vals.clear();
00077 }
00078
00079 public static final ConfigVar find(Class c, String name, String dflt) {
00080
synchronized (
lock) {
00081
ConfigVar var = (
ConfigVar)
vars.get(name);
00082
if (var == null) {
00083 String val =
vals.getProperty(name);
00084
if (val == null) val = dflt;
00085
00086
try {
00087 var = (
ConfigVar)c.newInstance();
00088 var.
init(name, val);
00089 }
catch (Exception e) {
00090
Debug.print(e);
00091 var = null;
00092 }
00093
if (var != null)
vars.put(name, var);
00094 }
00095
return var;
00096 }
00097 }
00098
00099 public static final ConfigVar find(String name) {
00100
synchronized (
lock) {
00101
return (
ConfigVar)
vars.get(name);
00102 }
00103 }
00104
00105 public static final String
getProperty(String name) {
00106
ConfigVar f = find(name);
00107
if (f == null) {
00108 f =
ConfigString.find(name, null);
00109 }
00110
return (f == null) ? null : f.
getValue();
00111 }
00112
00113 public static final String getProperty(String name, String dflt) {
00114
ConfigVar f = find(name);
00115
if (f == null) {
00116 f =
ConfigString.find(name, dflt);
00117 }
00118
return (f == null) ? dflt : f.
getValue();
00119 }
00120
00121 public static Enumeration
getMatchingProps(String pattern) {
00122
return getMatchingProps(
vals, pattern);
00123 }
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134 public static String
getMatch(String s, String p) {
00135
int pre = p.indexOf(
'*');
00136
int post = (p.length() - pre) - 1;
00137 s = s.substring(pre);
00138 s = s.substring(0, s.length() - post);
00139
return s;
00140 }
00141
00142 public static Properties
getPropSubset(Properties props,
00143 String pattern) {
00144 Properties np =
new Properties();
00145
OctetString pat =
new OctetString(pattern);
00146
final OctetComparator cmp =
OctetComparator.cmp;
00147
00148 Enumeration e = props.keys();
00149
while (e.hasMoreElements()) {
00150 String name = e.nextElement().toString();
00151 OctetString s =
new OctetString(name);
00152
if (cmp.
patternMatch(s, pat)) {
00153 np.put(getMatch(name, pattern), props.get(name));
00154 }
00155 }
00156
return np;
00157 }
00158
00159 public static Properties getPropSubset(String pattern) {
00160
return getPropSubset(
vals, pattern);
00161 }
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174 public static Enumeration getMatchingProps(Properties props,
00175
final String pattern) {
00176
final Enumeration e = props.keys();
00177
final OctetString p =
new OctetString(pattern);
00178
final OctetComparator cmp =
OctetComparator.cmp;
00179
00180
return new Enumeration() {
00181 String next = null;
00182
public boolean hasMoreElements() {
00183
while (next == null && e.hasMoreElements()) {
00184 String s = e.nextElement().toString();
00185 OctetString os =
new OctetString(s);
00186
if (cmp.
patternMatch(os, p)) {
00187 next = getMatch(s, pattern);
00188 }
00189 }
00190
return next != null;
00191 }
00192
00193
public Object nextElement() {
00194 Object ret = next;
00195 next = null;
00196 hasMoreElements();
00197
return ret;
00198 }
00199 };
00200 }
00201
00202 public static Properties
getProperties() {
00203
return vals;
00204 }
00205 }