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: /**
030: * MultiPropertiesConfig allows applications to accept configuration data
031: * from a more than one property file (each of which is to be loaded from
032: * a unique path using this class' ClassLoader's resource-loading mechanism),
033: * and permits access to property data via the resource path from which the
034: * properties were loaded, via the prefix of the property (where hierarchical
035: * property names are presumed to be '.'-separated), and simply by key.
036: * In the by-key and by-prefix indices, when two definitions conflict, the
037: * key value pairing specified in the MOST RECENT properties file shadows
038: * earlier definitions, and files are loaded in the order of the list of
039: * resource paths provided a constructor.
040: *
041: * The rescource path "/" is a special case that always refers to System
042: * properties. No actual resource will be loaded.
043:
044: * The class manages a special instance called "vmConfig" which is accessable
045: * via a static method. It's resource path is list specified by a text-file,
046: * itself a ClassLoader managed resource, which must be located at
047: * <tt>/com/mchange/v2/cfg/vmConfigResourcePaths.txt</tt>. This file should
048: * be one resource path per line, with blank lines ignored and lines beginning
049: * with '#' treated as comments.
050: */
051: public abstract class MultiPropertiesConfig {
052: final static MultiPropertiesConfig EMPTY = new BasicMultiPropertiesConfig(
053: new String[0]);
054:
055: final static String VM_CONFIG_RSRC_PATHS = "/com/mchange/v2/cfg/vmConfigResourcePaths.txt";
056:
057: static MultiPropertiesConfig vmConfig = null;
058:
059: public static MultiPropertiesConfig read(String[] resourcePath,
060: MLogger logger) {
061: return new BasicMultiPropertiesConfig(resourcePath, logger);
062: }
063:
064: public static MultiPropertiesConfig read(String[] resourcePath) {
065: return new BasicMultiPropertiesConfig(resourcePath);
066: }
067:
068: public static MultiPropertiesConfig combine(
069: MultiPropertiesConfig[] configs) {
070: return new CombinedMultiPropertiesConfig(configs);
071: }
072:
073: public static MultiPropertiesConfig readVmConfig(
074: String[] defaultResources, String[] preemptingResources) {
075: List l = new LinkedList();
076: if (defaultResources != null)
077: l.add(read(defaultResources));
078: l.add(readVmConfig());
079: if (preemptingResources != null)
080: l.add(read(preemptingResources));
081: return combine((MultiPropertiesConfig[]) l
082: .toArray(new MultiPropertiesConfig[l.size()]));
083: }
084:
085: public static MultiPropertiesConfig readVmConfig() {
086: if (vmConfig == null) {
087: List rps = new ArrayList();
088:
089: BufferedReader br = null;
090: try {
091: InputStream is = MultiPropertiesConfig.class
092: .getResourceAsStream(VM_CONFIG_RSRC_PATHS);
093: if (is != null) {
094: br = new BufferedReader(new InputStreamReader(is,
095: "8859_1"));
096: String rp;
097: while ((rp = br.readLine()) != null) {
098: rp = rp.trim();
099: if ("".equals(rp) || rp.startsWith("#"))
100: continue;
101:
102: rps.add(rp);
103: }
104: vmConfig = new BasicMultiPropertiesConfig(
105: (String[]) rps.toArray(new String[rps
106: .size()]));
107: } else {
108: System.err
109: .println("com.mchange.v2.cfg.MultiPropertiesConfig: Resource path list could not be found at resource path: "
110: + VM_CONFIG_RSRC_PATHS);
111: System.err
112: .println("com.mchange.v2.cfg.MultiPropertiesConfig: Using empty vmconfig.");
113: vmConfig = EMPTY;
114: }
115: } catch (IOException e) {
116: e.printStackTrace();
117: } finally {
118: try {
119: if (br != null)
120: br.close();
121: } catch (IOException e) {
122: e.printStackTrace();
123: }
124: }
125: }
126: return vmConfig;
127: }
128:
129: public boolean foundVmConfig() {
130: return vmConfig != EMPTY;
131: }
132:
133: public abstract String[] getPropertiesResourcePaths();
134:
135: public abstract Properties getPropertiesByResourcePath(String path);
136:
137: public abstract Properties getPropertiesByPrefix(String pfx);
138:
139: public abstract String getProperty(String key);
140: }
|