Quadcap Embeddable Server

com/quadcap/util/Config.java

Go to the documentation of this file.
00001 package com.quadcap.util; 00002 00003 /* Copyright 1999 - 2003 Quadcap Software. All rights reserved. 00004 * 00005 * This software is distributed under the Quadcap Free Software License. 00006 * This software may be used or modified for any purpose, personal or 00007 * commercial. Open Source redistributions are permitted. Commercial 00008 * redistribution of larger works derived from, or works which bundle 00009 * this software requires a "Commercial Redistribution License"; see 00010 * http://www.quadcap.com/purchase. 00011 * 00012 * Redistributions qualify as "Open Source" under one of the following terms: 00013 * 00014 * Redistributions are made at no charge beyond the reasonable cost of 00015 * materials and delivery. 00016 * 00017 * Redistributions are accompanied by a copy of the Source Code or by an 00018 * irrevocable offer to provide a copy of the Source Code for up to three 00019 * years at the cost of materials and delivery. Such redistributions 00020 * must allow further use, modification, and redistribution of the Source 00021 * Code under substantially the same terms as this license. 00022 * 00023 * Redistributions of source code must retain the copyright notices as they 00024 * appear in each source code file, these license terms, and the 00025 * disclaimer/limitation of liability set forth as paragraph 6 below. 00026 * 00027 * Redistributions in binary form must reproduce this Copyright Notice, 00028 * these license terms, and the disclaimer/limitation of liability set 00029 * forth as paragraph 6 below, in the documentation and/or other materials 00030 * provided with the distribution. 00031 * 00032 * The Software is provided on an "AS IS" basis. No warranty is 00033 * provided that the Software is free of defects, or fit for a 00034 * particular purpose. 00035 * 00036 * Limitation of Liability. Quadcap Software shall not be liable 00037 * for any damages suffered by the Licensee or any third party resulting 00038 * from use of the Software. 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 * Central repository for configuration information. 00050 * 00051 * @author Stan Bailes 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 * Assuming that <code>str</code> matches pattern <code>pat</code>, 00127 * with a wildcard character in the pattern, return the portion of the 00128 * string matching the wildcard. 00129 * 00130 * @param str the matched string 00131 * @param pat the pattern 00132 * @return the wildcard portion of the match. 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 * Return an enumeration of property names which match a glob-style 00165 * pattern. This is useful when properties are used to specify a 00166 * set, e.g., servlet.*.class=<i>name</i> or class.*.preload=true. 00167 * 00168 * @param props the property set to search 00169 * @param pattern the glob style pattern used by 00170 * OctetComparator.patternMatch(). 00171 * @return an Enumeration of the glob-matching part of each of the 00172 * names matching the pattern 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 }