001: /*
002: * Distributed as part of c3p0 v.0.9.1.2
003: *
004: * Copyright (C) 2005 Machinery For Change, Inc.
005: *
006: * Author: Steve Waldman <swaldman@mchange.com>
007: *
008: * This library is free software; you can redistribute it and/or modify
009: * it under the terms of the GNU Lesser General Public License version 2.1, as
010: * published by the Free Software Foundation.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public License
018: * along with this software; see the file LICENSE. If not, write to the
019: * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020: * Boston, MA 02111-1307, USA.
021: */
022:
023: package com.mchange.v2.cfg;
024:
025: import java.util.*;
026: import java.io.*;
027: import com.mchange.v2.log.*;
028:
029: public class BasicMultiPropertiesConfig extends MultiPropertiesConfig {
030: String[] rps;
031: Map propsByResourcePaths = new HashMap();
032: Map propsByPrefixes;
033:
034: Properties propsByKey;
035:
036: public BasicMultiPropertiesConfig(String[] resourcePaths) {
037: this (resourcePaths, null);
038: }
039:
040: public BasicMultiPropertiesConfig(String[] resourcePaths,
041: MLogger logger) {
042: List goodPaths = new ArrayList();
043: for (int i = 0, len = resourcePaths.length; i < len; ++i) {
044: String rp = resourcePaths[i];
045: if ("/".equals(rp)) {
046: try {
047: propsByResourcePaths
048: .put(rp, System.getProperties());
049: goodPaths.add(rp);
050: } catch (SecurityException e) {
051: if (logger != null) {
052: if (logger.isLoggable(MLevel.WARNING))
053: logger
054: .log(
055: MLevel.WARNING,
056: "Read of system Properties blocked -- "
057: + "ignoring any configuration via System properties, and using Empty Properties! "
058: + "(But any configuration via a resource properties files is still okay!)",
059: e);
060: } else {
061: System.err
062: .println("Read of system Properties blocked -- "
063: + "ignoring any configuration via System properties, and using Empty Properties! "
064: + "(But any configuration via a resource properties files is still okay!)");
065: e.printStackTrace();
066: }
067: }
068: } else {
069: Properties p = new Properties();
070: InputStream pis = MultiPropertiesConfig.class
071: .getResourceAsStream(rp);
072: if (pis != null) {
073: try {
074: p.load(pis);
075: propsByResourcePaths.put(rp, p);
076: goodPaths.add(rp);
077: } catch (IOException e) {
078: if (logger != null) {
079: if (logger.isLoggable(MLevel.WARNING))
080: logger
081: .log(
082: MLevel.WARNING,
083: "An IOException occurred while loading configuration properties from resource path '"
084: + rp + "'.", e);
085: } else
086: e.printStackTrace();
087: } finally {
088: try {
089: if (pis != null)
090: pis.close();
091: } catch (IOException e) {
092: if (logger != null) {
093: if (logger.isLoggable(MLevel.WARNING))
094: logger.log(MLevel.WARNING,
095: "An IOException occurred while closing InputStream from resource path '"
096: + rp + "'.", e);
097: } else
098: e.printStackTrace();
099: }
100: }
101: } else {
102: if (logger != null) {
103: if (logger.isLoggable(MLevel.FINE))
104: logger
105: .fine("Configuration properties not found at ResourcePath '"
106: + rp
107: + "'. [logger name: "
108: + logger.getName() + ']');
109: }
110: // else if (Debug.DEBUG && Debug.TRACE == Debug.TRACE_MAX)
111: // System.err.println("Configuration properties not found at ResourcePath '" + rp + "'." );
112: }
113: }
114: }
115:
116: this .rps = (String[]) goodPaths.toArray(new String[goodPaths
117: .size()]);
118: this .propsByPrefixes = Collections
119: .unmodifiableMap(extractPrefixMapFromRsrcPathMap(rps,
120: propsByResourcePaths));
121: this .propsByResourcePaths = Collections
122: .unmodifiableMap(propsByResourcePaths);
123: this .propsByKey = extractPropsByKey(rps, propsByResourcePaths);
124: }
125:
126: private static String extractPrefix(String s) {
127: int lastdot = s.lastIndexOf('.');
128: if (lastdot < 0)
129: return null;
130: else
131: return s.substring(0, lastdot);
132: }
133:
134: private static Properties findProps(String rp, Map pbrp) {
135: //System.err.println("findProps( " + rp + ", ... )");
136: Properties p;
137:
138: // MOVED THIS LOGIC INTO CONSTRUCTOR ABOVE, TO TREAT SYSTEM PROPS UNIFORMLY
139: // WITH THE REST, AND TO AVOID UNINTENTIONAL ATTEMPTS TO READ RESOURCE "/"
140: // AS STREAM -- swaldman, 2006-01-19
141:
142: // if ( "/".equals( rp ) )
143: // {
144: // try { p = System.getProperties(); }
145: // catch ( SecurityException e )
146: // {
147: // System.err.println(BasicMultiPropertiesConfig.class.getName() +
148: // " Read of system Properties blocked -- ignoring any configuration via System properties, and using Empty Properties! " +
149: // "(But any configuration via a resource properties files is still okay!)");
150: // p = new Properties();
151: // }
152: // }
153: // else
154: p = (Properties) pbrp.get(rp);
155:
156: // System.err.println( p );
157:
158: return p;
159: }
160:
161: private static Properties extractPropsByKey(String[] resourcePaths,
162: Map pbrp) {
163: Properties out = new Properties();
164: for (int i = 0, len = resourcePaths.length; i < len; ++i) {
165: String rp = resourcePaths[i];
166: Properties p = findProps(rp, pbrp);
167: if (p == null) {
168: System.err
169: .println("Could not find loaded properties for resource path: "
170: + rp);
171: continue;
172: }
173: for (Iterator ii = p.keySet().iterator(); ii.hasNext();) {
174: Object kObj = ii.next();
175: if (!(kObj instanceof String)) {
176: // note that we can not use the MLog library here, because initialization
177: // of that library depends on this function.
178: System.err
179: .println(BasicMultiPropertiesConfig.class
180: .getName()
181: + ": "
182: + "Properties object found at resource path "
183: + ("/".equals(rp) ? "[system properties]"
184: : "'" + rp + "'")
185: + "' contains a key that is not a String: "
186: + kObj);
187: System.err.println("Skipping...");
188: continue;
189: }
190: Object vObj = p.get(kObj);
191: if (vObj != null && !(vObj instanceof String)) {
192: // note that we can not use the MLog library here, because initialization
193: // of that library depends on this function.
194: System.err
195: .println(BasicMultiPropertiesConfig.class
196: .getName()
197: + ": "
198: + "Properties object found at resource path "
199: + ("/".equals(rp) ? "[system properties]"
200: : "'" + rp + "'")
201: + " contains a value that is not a String: "
202: + vObj);
203: System.err.println("Skipping...");
204: continue;
205: }
206:
207: String key = (String) kObj;
208: String val = (String) vObj;
209: out.put(key, val);
210: }
211: }
212: return out;
213: }
214:
215: private static Map extractPrefixMapFromRsrcPathMap(
216: String[] resourcePaths, Map pbrp) {
217: Map out = new HashMap();
218: //for( Iterator ii = pbrp.values().iterator(); ii.hasNext(); )
219: for (int i = 0, len = resourcePaths.length; i < len; ++i) {
220: String rp = resourcePaths[i];
221: Properties p = findProps(rp, pbrp);
222: if (p == null) {
223: System.err
224: .println(BasicMultiPropertiesConfig.class
225: .getName()
226: + " -- Could not find loaded properties for resource path: "
227: + rp);
228: continue;
229: }
230: for (Iterator jj = p.keySet().iterator(); jj.hasNext();) {
231: Object kObj = jj.next();
232: if (!(kObj instanceof String)) {
233: // note that we can not use the MLog library here, because initialization
234: // of that library depends on this function.
235: System.err
236: .println(BasicMultiPropertiesConfig.class
237: .getName()
238: + ": "
239: + "Properties object found at resource path "
240: + ("/".equals(rp) ? "[system properties]"
241: : "'" + rp + "'")
242: + "' contains a key that is not a String: "
243: + kObj);
244: System.err.println("Skipping...");
245: continue;
246: }
247:
248: String key = (String) kObj;
249: String prefix = extractPrefix(key);
250: while (prefix != null) {
251: Properties byPfx = (Properties) out.get(prefix);
252: if (byPfx == null) {
253: byPfx = new Properties();
254: out.put(prefix, byPfx);
255: }
256: byPfx.put(key, p.get(key));
257:
258: prefix = extractPrefix(prefix);
259: }
260: }
261: }
262: return out;
263: }
264:
265: public String[] getPropertiesResourcePaths() {
266: return (String[]) rps.clone();
267: }
268:
269: public Properties getPropertiesByResourcePath(String path) {
270: Properties out = ((Properties) propsByResourcePaths.get(path));
271: return (out == null ? new Properties() : out);
272: }
273:
274: public Properties getPropertiesByPrefix(String pfx) {
275: Properties out = ((Properties) propsByPrefixes.get(pfx));
276: return (out == null ? new Properties() : out);
277: }
278:
279: public String getProperty(String key) {
280: return propsByKey.getProperty(key);
281: }
282: }
|