1 22 23 24 package com.mchange.v2.c3p0.cfg; 25 26 import java.io.*; 27 import java.lang.reflect.*; 28 import java.util.*; 29 import com.mchange.v2.cfg.*; 30 import com.mchange.v2.log.*; 31 import com.mchange.v2.c3p0.impl.*; 32 33 public final class C3P0ConfigUtils 34 { 35 public final static String PROPS_FILE_RSRC_PATH = "/c3p0.properties"; 36 public final static String PROPS_FILE_PROP_PFX = "c3p0."; 37 public final static int PROPS_FILE_PROP_PFX_LEN = 5; 38 39 private final static String [] MISSPELL_PFXS = {"/c3pO", "/c3po", "/C3P0", "/C3PO"}; 40 41 final static MLogger logger = MLog.getLogger( C3P0ConfigUtils.class ); 42 43 static 44 { 45 if ( logger.isLoggable(MLevel.WARNING) && C3P0ConfigUtils.class.getResource( PROPS_FILE_RSRC_PATH ) == null ) 46 { 47 for (int i = 0; i < MISSPELL_PFXS.length; ++i) 49 { 50 String test = MISSPELL_PFXS[i] + ".properties"; 51 if (C3P0ConfigUtils.class.getResource( MISSPELL_PFXS[i] + ".properties" ) != null) 52 { 53 logger.warning("POSSIBLY MISSPELLED c3p0.properties CONFIG RESOURCE FOUND. " + 54 "Please ensure the file name is c3p0.properties, all lower case, " + 55 "with the digit 0 (NOT the letter O) in c3p0. It should be placed " + 56 " in the top level of c3p0's effective classpath."); 57 break; 58 } 59 } 60 } 61 } 62 63 public static HashMap extractHardcodedC3P0Defaults(boolean stringify) 64 { 65 HashMap out = new HashMap(); 66 67 try 68 { 69 Method[] methods = C3P0Defaults.class.getMethods(); 70 for (int i = 0, len = methods.length; i < len; ++i) 71 { 72 Method m = methods[i]; 73 int mods = m.getModifiers(); 74 if ((mods & Modifier.PUBLIC) != 0 && (mods & Modifier.STATIC) != 0 && m.getParameterTypes().length == 0) 75 { 76 if (stringify) 77 { 78 Object val = m.invoke( null, null ); 79 if ( val != null ) 80 out.put( m.getName(), String.valueOf( val ) ); 81 } 82 else 83 out.put( m.getName(), m.invoke( null, null ) ); 84 } 85 } 86 } 87 catch (Exception e) 88 { 89 logger.log( MLevel.WARNING, "Failed to extract hardcoded default config!?", e ); 90 } 91 92 return out; 93 } 94 95 public static HashMap extractHardcodedC3P0Defaults() 96 { return extractHardcodedC3P0Defaults( true ); } 97 98 public static HashMap extractC3P0PropertiesResources() 99 { 100 HashMap out = new HashMap(); 101 102 105 Properties props = findAllC3P0Properties(); 106 for (Iterator ii = props.keySet().iterator(); ii.hasNext(); ) 107 { 108 String key = (String ) ii.next(); 109 String val = (String ) props.get(key); 110 if ( key.startsWith(PROPS_FILE_PROP_PFX) ) 111 out.put( key.substring(PROPS_FILE_PROP_PFX_LEN).trim(), val.trim() ); 112 } 113 114 return out; 115 } 116 117 public static C3P0Config configFromFlatDefaults(HashMap flatDefaults) 118 { 119 NamedScope defaults = new NamedScope(); 120 defaults.props.putAll( flatDefaults ); 121 122 HashMap configNamesToNamedScopes = new HashMap(); 123 124 return new C3P0Config( defaults, configNamesToNamedScopes ); 125 } 126 127 public static String getPropFileConfigProperty( String prop ) 128 { return MultiPropertiesConfig.readVmConfig().getProperty( prop ); } 129 130 private static Properties findResourceProperties() 131 { return MultiPropertiesConfig.readVmConfig().getPropertiesByResourcePath(PROPS_FILE_RSRC_PATH); } 132 133 private static Properties findAllC3P0Properties() 134 { return MultiPropertiesConfig.readVmConfig().getPropertiesByPrefix("c3p0"); } 135 136 static Properties findAllC3P0SystemProperties() 137 { 138 Properties out = new Properties(); 139 140 SecurityException sampleExc = null; 141 try 142 { 143 for (Iterator ii = C3P0Defaults.getKnownProperties().iterator(); ii.hasNext(); ) 144 { 145 String key = (String ) ii.next(); 146 String prefixedKey = "c3p0." + key; 147 String value = System.getProperty( prefixedKey ); 148 if (value != null && value.trim().length() > 0) 149 out.put( key, value ); 150 } 151 } 152 catch (SecurityException e) 153 { sampleExc = e; } 154 155 return out; 156 } 157 158 private C3P0ConfigUtils() 159 {} 160 } | Popular Tags |