1 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 [] 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 [] rps = configs[i].getPropertiesResourcePaths(); 41 for (int j = rps.length - 1; j >= 0; --j) 42 { 43 String rp = rps[j]; 44 if (! allPaths.contains( rp ) ) 45 allPaths.add(0, rp); 46 } 47 } 48 this.resourcePaths = (String []) allPaths.toArray( new String [ allPaths.size() ] ); 49 } 50 51 public String [] getPropertiesResourcePaths() 52 { return (String []) resourcePaths.clone(); } 53 54 public Properties getPropertiesByResourcePath(String 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 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 getProperty( String key ) 91 { 92 for (int i = configs.length - 1; i >= 0; --i) 93 { 94 MultiPropertiesConfig config = configs[i]; 95 String check = config.getProperty(key); 96 if (check != null) 97 return check; 98 } 99 return null; 100 } 101 } 102 103 | Popular Tags |