1 19 package org.apache.cayenne.conf; 20 21 import java.io.IOException ; 22 import java.io.InputStream ; 23 import java.sql.Connection ; 24 import java.util.Properties ; 25 26 import org.apache.cayenne.ConfigurationException; 27 import org.apache.cayenne.util.ResourceLocator; 28 import org.apache.commons.pool.impl.GenericObjectPool; 29 30 36 class DBCPDataSourceProperties { 37 38 private static final String PROPERTY_PREFIX = "cayenne.dbcp."; 39 40 private Properties properties; 41 42 45 static Properties loadProperties(ResourceLocator resourceLocator, String location) 46 throws IOException { 47 48 InputStream in = resourceLocator.findResourceStream(location); 49 50 if (in == null && !location.endsWith(".properties")) { 52 in = resourceLocator.findResourceStream(location + ".properties"); 53 } 54 55 if (in == null) { 56 throw new ConfigurationException("DBCP properties file not found: " 57 + location); 58 } 59 60 Properties properties = new Properties (); 61 try { 62 properties.load(in); 63 } 64 finally { 65 try { 66 in.close(); 67 } 68 catch (IOException e) { 69 } 70 } 71 72 return properties; 73 } 74 75 DBCPDataSourceProperties(ResourceLocator resourceLocator, String location) 76 throws IOException { 77 this(loadProperties(resourceLocator, location)); 78 } 79 80 DBCPDataSourceProperties(Properties properties) { 81 this.properties = properties; 82 } 83 84 Properties getProperties() { 85 return properties; 86 } 87 88 String getString(String property, boolean required) { 89 String value = properties.getProperty(PROPERTY_PREFIX + property); 90 91 if (required && value == null) { 92 throw new ConfigurationException("No value for required property: " 93 + PROPERTY_PREFIX 94 + property); 95 } 96 97 return value; 98 } 99 100 String getString(String property) { 101 return getString(property, false); 102 } 103 104 boolean getBoolean(String property, boolean defaultValue) { 105 String value = getString(property); 106 return (value != null) 107 ? "true".equalsIgnoreCase(getString(property)) 108 : defaultValue; 109 } 110 111 int getInt(String property, int defaultValue) { 112 String value = getString(property); 113 114 try { 115 return (value != null) ? Integer.parseInt(value) : defaultValue; 116 } 117 catch (NumberFormatException nfex) { 118 return defaultValue; 119 } 120 } 121 122 long getLong(String property, long defaultValue) { 123 String value = getString(property); 124 try { 125 return (value != null) ? Long.parseLong(value) : defaultValue; 126 } 127 catch (NumberFormatException nfex) { 128 return defaultValue; 129 } 130 } 131 132 byte getByte(String property, byte defaultValue) { 133 String value = getString(property); 134 try { 135 return (value != null) ? Byte.parseByte(value) : defaultValue; 136 } 137 catch (NumberFormatException nfex) { 138 return defaultValue; 139 } 140 } 141 142 byte getWhenExhaustedAction(String property, byte defaultValue) 143 throws ConfigurationException { 144 145 String value = getString(property); 146 147 if (value == null) { 148 return defaultValue; 149 } 150 151 try { 153 return Byte.parseByte(value); 154 } 155 catch (NumberFormatException nfex) { 156 try { 158 return GenericObjectPool.class.getField(value).getByte(null); 159 } 160 catch (Throwable th) { 161 throw new ConfigurationException("Invalid 'whenExhaustedAction': " 162 + value); 163 } 164 } 165 } 166 167 int getTransactionIsolation(String property, int defaultValue) { 168 169 String value = getString(property); 170 171 if (value == null) { 172 return defaultValue; 173 } 174 175 try { 177 return Integer.parseInt(value); 178 } 179 catch (NumberFormatException nfex) { 180 try { 182 return Connection .class.getField(value).getInt(null); 183 } 184 catch (Throwable th) { 185 throw new ConfigurationException( 186 "Invalid 'defaultTransactionIsolation': " + value); 187 } 188 } 189 } 190 191 } 192 | Popular Tags |