1 package com.quadcap.util; 2 3 40 41 import java.io.File ; 42 import java.io.FileInputStream ; 43 44 import java.util.Enumeration ; 45 import java.util.Hashtable ; 46 import java.util.Properties ; 47 48 53 public class Config { 54 private static final Hashtable vars = new Hashtable (); 55 private static final Properties vals = 56 new Properties (System.getProperties()); 57 private static final Object lock = new Object (); 58 59 static { 60 try { 61 String filename = System.getProperty("config.props", 62 "config.props"); 63 File f = new File (filename); 64 if (f.canRead()) { 65 FileInputStream fis = new FileInputStream (f); 66 vals.load(fis); 67 fis.close(); 68 } 69 } catch (Throwable e) { 70 Debug.print(e); 71 } 72 } 73 74 public static void reset() { 75 vars.clear(); 76 vals.clear(); 77 } 78 79 public static final ConfigVar find(Class c, String name, String dflt) { 80 synchronized (lock) { 81 ConfigVar var = (ConfigVar)vars.get(name); 82 if (var == null) { 83 String val = vals.getProperty(name); 84 if (val == null) val = dflt; 85 86 try { 87 var = (ConfigVar)c.newInstance(); 88 var.init(name, val); 89 } catch (Exception e) { 90 Debug.print(e); 91 var = null; 92 } 93 if (var != null) vars.put(name, var); 94 } 95 return var; 96 } 97 } 98 99 public static final ConfigVar find(String name) { 100 synchronized (lock) { 101 return (ConfigVar)vars.get(name); 102 } 103 } 104 105 public static final String getProperty(String name) { 106 ConfigVar f = find(name); 107 if (f == null) { 108 f = ConfigString.find(name, null); 109 } 110 return (f == null) ? null : f.getValue(); 111 } 112 113 public static final String getProperty(String name, String dflt) { 114 ConfigVar f = find(name); 115 if (f == null) { 116 f = ConfigString.find(name, dflt); 117 } 118 return (f == null) ? dflt : f.getValue(); 119 } 120 121 public static Enumeration getMatchingProps(String pattern) { 122 return getMatchingProps(vals, pattern); 123 } 124 125 134 public static String getMatch(String s, String p) { 135 int pre = p.indexOf('*'); 136 int post = (p.length() - pre) - 1; 137 s = s.substring(pre); 138 s = s.substring(0, s.length() - post); 139 return s; 140 } 141 142 public static Properties getPropSubset(Properties props, 143 String pattern) { 144 Properties np = new Properties (); 145 OctetString pat = new OctetString(pattern); 146 final OctetComparator cmp = OctetComparator.cmp; 147 148 Enumeration e = props.keys(); 149 while (e.hasMoreElements()) { 150 String name = e.nextElement().toString(); 151 OctetString s = new OctetString(name); 152 if (cmp.patternMatch(s, pat)) { 153 np.put(getMatch(name, pattern), props.get(name)); 154 } 155 } 156 return np; 157 } 158 159 public static Properties getPropSubset(String pattern) { 160 return getPropSubset(vals, pattern); 161 } 162 163 174 public static Enumeration getMatchingProps(Properties props, 175 final String pattern) { 176 final Enumeration e = props.keys(); 177 final OctetString p = new OctetString(pattern); 178 final OctetComparator cmp = OctetComparator.cmp; 179 180 return new Enumeration () { 181 String next = null; 182 public boolean hasMoreElements() { 183 while (next == null && e.hasMoreElements()) { 184 String s = e.nextElement().toString(); 185 OctetString os = new OctetString(s); 186 if (cmp.patternMatch(os, p)) { 187 next = getMatch(s, pattern); 188 } 189 } 190 return next != null; 191 } 192 193 public Object nextElement() { 194 Object ret = next; 195 next = null; 196 hasMoreElements(); 197 return ret; 198 } 199 }; 200 } 201 202 public static Properties getProperties() { 203 return vals; 204 } 205 } 206 | Popular Tags |