KickJava   Java API By Example, From Geeks To Geeks.

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


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
28 class CombinedMultiPropertiesConfig extends MultiPropertiesConfig
29 {
30     MultiPropertiesConfig[] configs;
31     String JavaDoc[] resourcePaths;
32
33     CombinedMultiPropertiesConfig( MultiPropertiesConfig[] configs )
34     {
35     this.configs = configs;
36
37     List allPaths = new LinkedList();
38     for (int i = configs.length - 1; i >= 0; --i)
39         {
40         String JavaDoc[] rps = configs[i].getPropertiesResourcePaths();
41         for (int j = rps.length - 1; j >= 0; --j)
42             {
43             String JavaDoc rp = rps[j];
44             if (! allPaths.contains( rp ) )
45                 allPaths.add(0, rp);
46             }
47         }
48     this.resourcePaths = (String JavaDoc[]) allPaths.toArray( new String JavaDoc[ allPaths.size() ] );
49     }
50
51     public String JavaDoc[] getPropertiesResourcePaths()
52     { return (String JavaDoc[]) resourcePaths.clone(); }
53     
54     public Properties getPropertiesByResourcePath(String JavaDoc path)
55     {
56     for (int i = configs.length - 1; i >= 0; --i)
57         {
58         MultiPropertiesConfig config = configs[i];
59         Properties check = config.getPropertiesByResourcePath(path);
60         if (check != null)
61             return check;
62         }
63     return null;
64     }
65     
66     public Properties getPropertiesByPrefix(String JavaDoc pfx)
67     {
68     List entries = new LinkedList();
69     for (int i = configs.length - 1; i >= 0; --i)
70         {
71         MultiPropertiesConfig config = configs[i];
72         Properties check = config.getPropertiesByPrefix(pfx);
73         if (check != null)
74             entries.addAll( 0, check.entrySet() );
75         }
76     if (entries.size() == 0)
77         return null;
78     else
79         {
80         Properties out = new Properties();
81         for (Iterator ii = entries.iterator(); ii.hasNext(); )
82             {
83             Map.Entry entry = (Map.Entry) ii.next();
84             out.put( entry.getKey(), entry.getValue() );
85             }
86         return out;
87         }
88     }
89     
90     public String JavaDoc getProperty( String JavaDoc key )
91     {
92     for (int i = configs.length - 1; i >= 0; --i)
93         {
94         MultiPropertiesConfig config = configs[i];
95         String JavaDoc check = config.getProperty(key);
96         if (check != null)
97             return check;
98         }
99     return null;
100     }
101 }
102
103
Popular Tags