1 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 [] rps; 33 Map propsByResourcePaths = new HashMap(); 34 Map propsByPrefixes; 35 36 Properties propsByKey; 37 38 public BasicMultiPropertiesConfig(String [] resourcePaths) 39 { this( resourcePaths, null ); } 40 41 public BasicMultiPropertiesConfig(String [] resourcePaths, MLogger logger) 42 { 43 List goodPaths = new ArrayList(); 44 for( int i = 0, len = resourcePaths.length; i < len; ++i ) 45 { 46 String 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 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 } 125 } 126 } 127 128 this.rps = (String []) goodPaths.toArray( new String [ 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 extractPrefix( String 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 rp, Map pbrp) 145 { 146 Properties p; 148 149 153 p = (Properties) pbrp.get( rp ); 166 167 169 return p; 170 } 171 172 private static Properties extractPropsByKey( String [] resourcePaths, Map pbrp ) 173 { 174 Properties out = new Properties(); 175 for (int i = 0, len = resourcePaths.length; i < len; ++i) 176 { 177 String 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 kObj = ii.next(); 187 if (!(kObj instanceof String )) 188 { 189 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 vObj = p.get( kObj ); 200 if (vObj != null && !(vObj instanceof String )) 201 { 202 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 key = (String ) kObj; 214 String val = (String ) vObj; 215 out.put( key, val ); 216 } 217 } 218 return out; 219 } 220 221 private static Map extractPrefixMapFromRsrcPathMap(String [] resourcePaths, Map pbrp) 222 { 223 Map out = new HashMap(); 224 for (int i = 0, len = resourcePaths.length; i < len; ++i) 226 { 227 String 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 kObj = jj.next(); 237 if (! (kObj instanceof String )) 238 { 239 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 key = (String ) kObj; 251 String 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 [] getPropertiesResourcePaths() 270 { return (String []) rps.clone(); } 271 272 public Properties getPropertiesByResourcePath(String path) 273 { 274 Properties out = ((Properties) propsByResourcePaths.get( path )); 275 return (out == null ? new Properties() : out); 276 } 277 278 public Properties getPropertiesByPrefix(String pfx) 279 { 280 Properties out = ((Properties) propsByPrefixes.get( pfx )); 281 return (out == null ? new Properties() : out); 282 } 283 284 public String getProperty( String key ) 285 { return propsByKey.getProperty( key ); } 286 } 287 | Popular Tags |