KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quadcap > util > Config


1 package com.quadcap.util;
2
3 /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
4  *
5  * This software is distributed under the Quadcap Free Software License.
6  * This software may be used or modified for any purpose, personal or
7  * commercial. Open Source redistributions are permitted. Commercial
8  * redistribution of larger works derived from, or works which bundle
9  * this software requires a "Commercial Redistribution License"; see
10  * http://www.quadcap.com/purchase.
11  *
12  * Redistributions qualify as "Open Source" under one of the following terms:
13  *
14  * Redistributions are made at no charge beyond the reasonable cost of
15  * materials and delivery.
16  *
17  * Redistributions are accompanied by a copy of the Source Code or by an
18  * irrevocable offer to provide a copy of the Source Code for up to three
19  * years at the cost of materials and delivery. Such redistributions
20  * must allow further use, modification, and redistribution of the Source
21  * Code under substantially the same terms as this license.
22  *
23  * Redistributions of source code must retain the copyright notices as they
24  * appear in each source code file, these license terms, and the
25  * disclaimer/limitation of liability set forth as paragraph 6 below.
26  *
27  * Redistributions in binary form must reproduce this Copyright Notice,
28  * these license terms, and the disclaimer/limitation of liability set
29  * forth as paragraph 6 below, in the documentation and/or other materials
30  * provided with the distribution.
31  *
32  * The Software is provided on an "AS IS" basis. No warranty is
33  * provided that the Software is free of defects, or fit for a
34  * particular purpose.
35  *
36  * Limitation of Liability. Quadcap Software shall not be liable
37  * for any damages suffered by the Licensee or any third party resulting
38  * from use of the Software.
39  */

40
41 import java.io.File JavaDoc;
42 import java.io.FileInputStream JavaDoc;
43
44 import java.util.Enumeration JavaDoc;
45 import java.util.Hashtable JavaDoc;
46 import java.util.Properties JavaDoc;
47
48 /**
49  * Central repository for configuration information.
50  *
51  * @author Stan Bailes
52  */

53 public class Config {
54     private static final Hashtable JavaDoc vars = new Hashtable JavaDoc();
55     private static final Properties JavaDoc vals =
56     new Properties JavaDoc(System.getProperties());
57     private static final Object JavaDoc lock = new Object JavaDoc();
58
59     static {
60     try {
61         String JavaDoc filename = System.getProperty("config.props",
62                          "config.props");
63         File JavaDoc f = new File JavaDoc(filename);
64         if (f.canRead()) {
65         FileInputStream JavaDoc fis = new FileInputStream JavaDoc(f);
66         vals.load(fis);
67         fis.close();
68         }
69     } catch (Throwable JavaDoc 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 JavaDoc c, String JavaDoc name, String JavaDoc dflt) {
80     synchronized (lock) {
81         ConfigVar var = (ConfigVar)vars.get(name);
82         if (var == null) {
83         String JavaDoc 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 JavaDoc 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 JavaDoc name) {
100     synchronized (lock) {
101         return (ConfigVar)vars.get(name);
102     }
103     }
104
105     public static final String JavaDoc getProperty(String JavaDoc 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 JavaDoc getProperty(String JavaDoc name, String JavaDoc 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 JavaDoc getMatchingProps(String JavaDoc pattern) {
122         return getMatchingProps(vals, pattern);
123     }
124     
125     /**
126      * Assuming that <code>str</code> matches pattern <code>pat</code>,
127      * with a wildcard character in the pattern, return the portion of the
128      * string matching the wildcard.
129      *
130      * @param str the matched string
131      * @param pat the pattern
132      * @return the wildcard portion of the match.
133      */

134     public static String JavaDoc getMatch(String JavaDoc s, String JavaDoc 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 JavaDoc getPropSubset(Properties JavaDoc props,
143                                            String JavaDoc pattern) {
144         Properties JavaDoc np = new Properties JavaDoc();
145         OctetString pat = new OctetString(pattern);
146         final OctetComparator cmp = OctetComparator.cmp;
147
148         Enumeration JavaDoc e = props.keys();
149         while (e.hasMoreElements()) {
150             String JavaDoc 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 JavaDoc getPropSubset(String JavaDoc pattern) {
160         return getPropSubset(vals, pattern);
161     }
162
163     /**
164      * Return an enumeration of property names which match a glob-style
165      * pattern. This is useful when properties are used to specify a
166      * set, e.g., servlet.*.class=<i>name</i> or class.*.preload=true.
167      *
168      * @param props the property set to search
169      * @param pattern the glob style pattern used by
170      * OctetComparator.patternMatch().
171      * @return an Enumeration of the glob-matching part of each of the
172      * names matching the pattern
173      */

174     public static Enumeration JavaDoc getMatchingProps(Properties JavaDoc props,
175                                                final String JavaDoc pattern) {
176         final Enumeration JavaDoc e = props.keys();
177         final OctetString p = new OctetString(pattern);
178         final OctetComparator cmp = OctetComparator.cmp;
179             
180         return new Enumeration JavaDoc() {
181             String JavaDoc next = null;
182             public boolean hasMoreElements() {
183                 while (next == null && e.hasMoreElements()) {
184                     String JavaDoc 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 JavaDoc nextElement() {
194                 Object JavaDoc ret = next;
195                 next = null;
196                 hasMoreElements();
197                 return ret;
198             }
199         };
200     }
201
202     public static Properties JavaDoc getProperties() {
203         return vals;
204     }
205 }
206
Popular Tags