KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mchange > v2 > cfg > MultiPropertiesConfig


1 /*
2  * Distributed as part of c3p0 v.0.9.1
3  *
4  * Copyright (C) 2005 Machinery For Change, Inc.
5  *
6  * Author: Steve Waldman <swaldman@mchange.com>
7  *
8  * This library is free software; you can redistribute it and/or modify
9  * 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
24 package com.mchange.v2.cfg;
25
26 import java.util.*;
27 import java.io.*;
28 import com.mchange.v2.log.*;
29
30 /**
31  * MultiPropertiesConfig allows applications to accept configuration data
32  * from a more than one property file (each of which is to be loaded from
33  * a unique path using this class' ClassLoader's resource-loading mechanism),
34  * and permits access to property data via the resource path from which the
35  * properties were loaded, via the prefix of the property (where hierarchical
36  * property names are presumed to be '.'-separated), and simply by key.
37  * In the by-key and by-prefix indices, when two definitions conflict, the
38  * key value pairing specified in the MOST RECENT properties file shadows
39  * earlier definitions, and files are loaded in the order of the list of
40  * resource paths provided a constructor.
41  *
42  * The rescource path "/" is a special case that always refers to System
43  * properties. No actual resource will be loaded.
44
45  * The class manages a special instance called "vmConfig" which is accessable
46  * via a static method. It's resource path is list specified by a text-file,
47  * itself a ClassLoader managed resource, which must be located at
48  * <tt>/com/mchange/v2/cfg/vmConfigResourcePaths.txt</tt>. This file should
49  * be one resource path per line, with blank lines ignored and lines beginning
50  * with '#' treated as comments.
51  */

52 public abstract class MultiPropertiesConfig
53 {
54     final static MultiPropertiesConfig EMPTY = new BasicMultiPropertiesConfig( new String JavaDoc[0] );
55
56     final static String JavaDoc VM_CONFIG_RSRC_PATHS = "/com/mchange/v2/cfg/vmConfigResourcePaths.txt";
57
58     static MultiPropertiesConfig vmConfig = null;
59
60     public static MultiPropertiesConfig read(String JavaDoc[] resourcePath, MLogger logger)
61     { return new BasicMultiPropertiesConfig( resourcePath, logger ); }
62
63     public static MultiPropertiesConfig read(String JavaDoc[] resourcePath)
64     { return new BasicMultiPropertiesConfig( resourcePath ); }
65
66     public static MultiPropertiesConfig combine( MultiPropertiesConfig[] configs )
67     { return new CombinedMultiPropertiesConfig( configs ); }
68
69     public static MultiPropertiesConfig readVmConfig(String JavaDoc[] defaultResources, String JavaDoc[] preemptingResources)
70     {
71     List l = new LinkedList();
72     if (defaultResources != null)
73         l.add( read( defaultResources ) );
74     l.add( readVmConfig() );
75     if (preemptingResources != null)
76         l.add( read( preemptingResources ) );
77     return combine( (MultiPropertiesConfig[]) l.toArray( new MultiPropertiesConfig[ l.size() ] ) );
78     }
79
80     public static MultiPropertiesConfig readVmConfig()
81     {
82     if ( vmConfig == null )
83         {
84         List rps = new ArrayList();
85
86         BufferedReader br = null;
87         try
88             {
89             InputStream is = MultiPropertiesConfig.class.getResourceAsStream( VM_CONFIG_RSRC_PATHS );
90             if ( is != null )
91                 {
92                 br = new BufferedReader( new InputStreamReader( is, "8859_1" ) );
93                 String JavaDoc rp;
94                 while ((rp = br.readLine()) != null)
95                     {
96                     rp = rp.trim();
97                     if ("".equals( rp ) || rp.startsWith("#"))
98                         continue;
99                     
100                     rps.add( rp );
101                     }
102                 vmConfig = new BasicMultiPropertiesConfig( (String JavaDoc[]) rps.toArray( new String JavaDoc[ rps.size() ] ) );
103                 }
104             else
105                 {
106                 System.err.println("com.mchange.v2.cfg.MultiPropertiesConfig: Resource path list could not be found at resource path: " + VM_CONFIG_RSRC_PATHS);
107                 System.err.println("com.mchange.v2.cfg.MultiPropertiesConfig: Using empty vmconfig.");
108                 vmConfig = EMPTY;
109                 }
110             }
111         catch (IOException e)
112             { e.printStackTrace(); }
113         finally
114             {
115             try { if ( br != null ) br.close(); }
116             catch (IOException e) { e.printStackTrace(); }
117             }
118         }
119     return vmConfig;
120     }
121
122     public boolean foundVmConfig()
123     { return vmConfig != EMPTY; }
124
125     public abstract String JavaDoc[] getPropertiesResourcePaths();
126
127     public abstract Properties getPropertiesByResourcePath(String JavaDoc path);
128
129     public abstract Properties getPropertiesByPrefix(String JavaDoc pfx);
130
131     public abstract String JavaDoc getProperty( String JavaDoc key );
132 }
133
Popular Tags