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 52 public abstract class MultiPropertiesConfig 53 { 54 final static MultiPropertiesConfig EMPTY = new BasicMultiPropertiesConfig( new String [0] ); 55 56 final static String VM_CONFIG_RSRC_PATHS = "/com/mchange/v2/cfg/vmConfigResourcePaths.txt"; 57 58 static MultiPropertiesConfig vmConfig = null; 59 60 public static MultiPropertiesConfig read(String [] resourcePath, MLogger logger) 61 { return new BasicMultiPropertiesConfig( resourcePath, logger ); } 62 63 public static MultiPropertiesConfig read(String [] 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 [] defaultResources, String [] 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 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 []) rps.toArray( new String [ 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 [] getPropertiesResourcePaths(); 126 127 public abstract Properties getPropertiesByResourcePath(String path); 128 129 public abstract Properties getPropertiesByPrefix(String pfx); 130 131 public abstract String getProperty( String key ); 132 } 133 | Popular Tags |