KickJava   Java API By Example, From Geeks To Geeks.

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


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 public class BasicMultiPropertiesConfig extends MultiPropertiesConfig
31 {
32     String JavaDoc[] rps;
33     Map propsByResourcePaths = new HashMap();
34     Map propsByPrefixes;
35
36     Properties propsByKey;
37
38     public BasicMultiPropertiesConfig(String JavaDoc[] resourcePaths)
39     { this( resourcePaths, null ); }
40
41     public BasicMultiPropertiesConfig(String JavaDoc[] resourcePaths, MLogger logger)
42     {
43     List goodPaths = new ArrayList();
44     for( int i = 0, len = resourcePaths.length; i < len; ++i )
45         {
46         String JavaDoc rp = resourcePaths[i];
47         if ("/".equals(rp))
48             {
49             try
50                 {
51                 propsByResourcePaths.put( rp, System.getProperties() );
52                 goodPaths.add( rp );
53                 }
54             catch (SecurityException JavaDoc e)
55                 {
56                 if (logger != null)
57                     {
58                     if ( logger.isLoggable( MLevel.WARNING ) )
59                         logger.log( MLevel.WARNING,
60                             "Read of system Properties blocked -- " +
61                             "ignoring any configuration via System properties, and using Empty Properties! " +
62                             "(But any configuration via a resource properties files is still okay!)",
63                             e );
64                     }
65                 else
66                     {
67                     System.err.println("Read of system Properties blocked -- " +
68                                "ignoring any configuration via System properties, and using Empty Properties! " +
69                                "(But any configuration via a resource properties files is still okay!)");
70                     e.printStackTrace();
71                     }
72                 }
73             }
74         else
75             {
76             Properties p = new Properties();
77             InputStream pis = MultiPropertiesConfig.class.getResourceAsStream( rp );
78             if ( pis != null )
79                 {
80                 try
81                     {
82                     p.load( pis );
83                     propsByResourcePaths.put( rp, p );
84                     goodPaths.add( rp );
85                     }
86                 catch (IOException e)
87                     {
88                     if (logger != null)
89                         {
90                         if ( logger.isLoggable( MLevel.WARNING ) )
91                             logger.log( MLevel.WARNING,
92                                 "An IOException occurred while loading configuration properties from resource path '" + rp + "'.",
93                                 e );
94                         }
95                     else
96                         e.printStackTrace();
97                     }
98                 finally
99                     {
100                     try { if ( pis != null ) pis.close(); }
101                     catch (IOException e)
102                         {
103                         if (logger != null)
104                             {
105                             if ( logger.isLoggable( MLevel.WARNING ) )
106                                 logger.log( MLevel.WARNING,
107                                     "An IOException occurred while closing InputStream from resource path '" + rp + "'.",
108                                     e );
109                             }
110                         else
111                             e.printStackTrace();
112                         }
113                     }
114                 }
115             else
116                 {
117                 if (logger != null)
118                     {
119                     if ( logger.isLoggable( MLevel.FINE ) )
120                         logger.fine( "Configuration properties not found at ResourcePath '" + rp + "'. [logger name: " + logger.getName() + ']' );
121                     }
122 // else if (Debug.DEBUG && Debug.TRACE == Debug.TRACE_MAX)
123
// System.err.println("Configuration properties not found at ResourcePath '" + rp + "'." );
124
}
125             }
126         }
127     
128     this.rps = (String JavaDoc[]) goodPaths.toArray( new String JavaDoc[ goodPaths.size() ] );
129     this.propsByPrefixes = Collections.unmodifiableMap( extractPrefixMapFromRsrcPathMap(rps, propsByResourcePaths) );
130     this.propsByResourcePaths = Collections.unmodifiableMap( propsByResourcePaths );
131     this.propsByKey = extractPropsByKey(rps, propsByResourcePaths);
132     }
133
134
135     private static String JavaDoc extractPrefix( String JavaDoc s )
136     {
137     int lastdot = s.lastIndexOf('.');
138     if ( lastdot < 0 )
139         return null;
140     else
141         return s.substring(0, lastdot);
142     }
143
144     private static Properties findProps(String JavaDoc rp, Map pbrp)
145     {
146     //System.err.println("findProps( " + rp + ", ... )");
147
Properties p;
148     
149     // MOVED THIS LOGIC INTO CONSTRUCTOR ABOVE, TO TREAT SYSTEM PROPS UNIFORMLY
150
// WITH THE REST, AND TO AVOID UNINTENTIONAL ATTEMPTS TO READ RESOURCE "/"
151
// AS STREAM -- swaldman, 2006-01-19
152

153 // if ( "/".equals( rp ) )
154
// {
155
// try { p = System.getProperties(); }
156
// catch ( SecurityException e )
157
// {
158
// System.err.println(BasicMultiPropertiesConfig.class.getName() +
159
// " Read of system Properties blocked -- ignoring any configuration via System properties, and using Empty Properties! " +
160
// "(But any configuration via a resource properties files is still okay!)");
161
// p = new Properties();
162
// }
163
// }
164
// else
165
p = (Properties) pbrp.get( rp );
166     
167 // System.err.println( p );
168

169     return p;
170     }
171
172     private static Properties extractPropsByKey( String JavaDoc[] resourcePaths, Map pbrp )
173     {
174     Properties out = new Properties();
175     for (int i = 0, len = resourcePaths.length; i < len; ++i)
176         {
177         String JavaDoc rp = resourcePaths[i];
178         Properties p = findProps( rp, pbrp );
179         if (p == null)
180             {
181             System.err.println("Could not find loaded properties for resource path: " + rp);
182             continue;
183             }
184         for (Iterator ii = p.keySet().iterator(); ii.hasNext(); )
185             {
186             Object JavaDoc kObj = ii.next();
187             if (!(kObj instanceof String JavaDoc))
188                 {
189                 // note that we can not use the MLog library here, because initialization
190
// of that library depends on this function.
191
System.err.println( BasicMultiPropertiesConfig.class.getName() + ": " +
192                             "Properties object found at resource path " +
193                             ("/".equals(rp) ? "[system properties]" : "'" + rp + "'") +
194                             "' contains a key that is not a String: " +
195                             kObj);
196                 System.err.println("Skipping...");
197                 continue;
198                 }
199             Object JavaDoc vObj = p.get( kObj );
200             if (vObj != null && !(vObj instanceof String JavaDoc))
201                 {
202                 // note that we can not use the MLog library here, because initialization
203
// of that library depends on this function.
204
System.err.println( BasicMultiPropertiesConfig.class.getName() + ": " +
205                             "Properties object found at resource path " +
206                             ("/".equals(rp) ? "[system properties]" : "'" + rp + "'") +
207                             " contains a value that is not a String: " +
208                             vObj);
209                 System.err.println("Skipping...");
210                 continue;
211                 }
212
213             String JavaDoc key = (String JavaDoc) kObj;
214             String JavaDoc val = (String JavaDoc) vObj;
215             out.put( key, val );
216             }
217         }
218     return out;
219     }
220
221     private static Map extractPrefixMapFromRsrcPathMap(String JavaDoc[] resourcePaths, Map pbrp)
222     {
223     Map out = new HashMap();
224     //for( Iterator ii = pbrp.values().iterator(); ii.hasNext(); )
225
for (int i = 0, len = resourcePaths.length; i < len; ++i)
226         {
227         String JavaDoc rp = resourcePaths[i];
228         Properties p = findProps( rp, pbrp );
229         if (p == null)
230             {
231             System.err.println(BasicMultiPropertiesConfig.class.getName() + " -- Could not find loaded properties for resource path: " + rp);
232             continue;
233             }
234         for (Iterator jj = p.keySet().iterator(); jj.hasNext(); )
235             {
236             Object JavaDoc kObj = jj.next();
237             if (! (kObj instanceof String JavaDoc))
238                 {
239                 // note that we can not use the MLog library here, because initialization
240
// of that library depends on this function.
241
System.err.println( BasicMultiPropertiesConfig.class.getName() + ": " +
242                             "Properties object found at resource path " +
243                             ("/".equals(rp) ? "[system properties]" : "'" + rp + "'") +
244                             "' contains a key that is not a String: " +
245                             kObj);
246                 System.err.println("Skipping...");
247                 continue;
248                 }
249
250             String JavaDoc key = (String JavaDoc) kObj;
251             String JavaDoc prefix = extractPrefix( key );
252             while (prefix != null)
253                 {
254                 Properties byPfx = (Properties) out.get( prefix );
255                 if (byPfx == null)
256                     {
257                     byPfx = new Properties();
258                     out.put( prefix, byPfx );
259                     }
260                 byPfx.put( key, p.get( key ) );
261
262                 prefix=extractPrefix( prefix );
263                 }
264             }
265         }
266     return out;
267     }
268
269     public String JavaDoc[] getPropertiesResourcePaths()
270     { return (String JavaDoc[]) rps.clone(); }
271
272     public Properties getPropertiesByResourcePath(String JavaDoc path)
273     {
274     Properties out = ((Properties) propsByResourcePaths.get( path ));
275     return (out == null ? new Properties() : out);
276     }
277
278     public Properties getPropertiesByPrefix(String JavaDoc pfx)
279     {
280     Properties out = ((Properties) propsByPrefixes.get( pfx ));
281     return (out == null ? new Properties() : out);
282     }
283
284     public String JavaDoc getProperty( String JavaDoc key )
285     { return propsByKey.getProperty( key ); }
286 }
287
Popular Tags