01: /*
02: * Distributed as part of c3p0 v.0.9.1.2
03: *
04: * Copyright (C) 2005 Machinery For Change, Inc.
05: *
06: * Author: Steve Waldman <swaldman@mchange.com>
07: *
08: * This library is free software; you can redistribute it and/or modify
09: * it under the terms of the GNU Lesser General Public License version 2.1, as
10: * published by the Free Software Foundation.
11: *
12: * This software is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: * GNU Lesser General Public License for more details.
16: *
17: * You should have received a copy of the GNU Lesser General Public License
18: * along with this software; see the file LICENSE. If not, write to the
19: * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20: * Boston, MA 02111-1307, USA.
21: */
22:
23: package com.mchange.v2.cfg;
24:
25: import java.util.*;
26:
27: class CombinedMultiPropertiesConfig extends MultiPropertiesConfig {
28: MultiPropertiesConfig[] configs;
29: String[] resourcePaths;
30:
31: CombinedMultiPropertiesConfig(MultiPropertiesConfig[] configs) {
32: this .configs = configs;
33:
34: List allPaths = new LinkedList();
35: for (int i = configs.length - 1; i >= 0; --i) {
36: String[] rps = configs[i].getPropertiesResourcePaths();
37: for (int j = rps.length - 1; j >= 0; --j) {
38: String rp = rps[j];
39: if (!allPaths.contains(rp))
40: allPaths.add(0, rp);
41: }
42: }
43: this .resourcePaths = (String[]) allPaths
44: .toArray(new String[allPaths.size()]);
45: }
46:
47: public String[] getPropertiesResourcePaths() {
48: return (String[]) resourcePaths.clone();
49: }
50:
51: public Properties getPropertiesByResourcePath(String path) {
52: for (int i = configs.length - 1; i >= 0; --i) {
53: MultiPropertiesConfig config = configs[i];
54: Properties check = config.getPropertiesByResourcePath(path);
55: if (check != null)
56: return check;
57: }
58: return null;
59: }
60:
61: public Properties getPropertiesByPrefix(String pfx) {
62: List entries = new LinkedList();
63: for (int i = configs.length - 1; i >= 0; --i) {
64: MultiPropertiesConfig config = configs[i];
65: Properties check = config.getPropertiesByPrefix(pfx);
66: if (check != null)
67: entries.addAll(0, check.entrySet());
68: }
69: if (entries.size() == 0)
70: return null;
71: else {
72: Properties out = new Properties();
73: for (Iterator ii = entries.iterator(); ii.hasNext();) {
74: Map.Entry entry = (Map.Entry) ii.next();
75: out.put(entry.getKey(), entry.getValue());
76: }
77: return out;
78: }
79: }
80:
81: public String getProperty(String key) {
82: for (int i = configs.length - 1; i >= 0; --i) {
83: MultiPropertiesConfig config = configs[i];
84: String check = config.getProperty(key);
85: if (check != null)
86: return check;
87: }
88: return null;
89: }
90: }
|