1 6 package org.logicalcobwebs.proxool.configuration; 7 8 import org.apache.commons.logging.Log; 9 import org.apache.commons.logging.LogFactory; 10 import org.logicalcobwebs.proxool.ProxoolConstants; 11 import org.logicalcobwebs.proxool.ProxoolException; 12 import org.logicalcobwebs.proxool.ProxoolFacade; 13 14 import java.io.FileInputStream ; 15 import java.io.IOException ; 16 import java.util.HashMap ; 17 import java.util.Iterator ; 18 import java.util.Map ; 19 import java.util.Properties ; 20 21 78 public class PropertyConfigurator { 79 private static final Log LOG = LogFactory.getLog(PropertyConfigurator.class); 80 81 protected static final String PREFIX = "jdbc"; 82 83 private static final String DOT = "."; 84 85 private static final String EXAMPLE_FORMAT = PREFIX + "*" + DOT + "*"; 86 87 92 public static void configure(String filename) throws ProxoolException { 93 Properties properties = new Properties (); 94 try { 95 properties.load(new FileInputStream (filename)); 96 } catch (IOException e) { 97 throw new ProxoolException("Couldn't load property file " + filename); 98 } 99 configure(properties); 100 } 101 102 107 public static void configure(Properties properties) throws ProxoolException { 108 final Map propertiesMap = new HashMap (); 109 final Iterator allPropertyKeysIterator = properties.keySet().iterator(); 110 Properties proxoolProperties = null; 111 112 while (allPropertyKeysIterator.hasNext()) { 113 String key = (String ) allPropertyKeysIterator.next(); 114 String value = properties.getProperty(key); 115 116 if (key.startsWith(PREFIX)) { 117 int a = key.indexOf(DOT); 118 if (a == -1) { 119 throw new ProxoolException("Property " + key + " must be of the format " + EXAMPLE_FORMAT); 120 } 121 final String tag = key.substring(0, a); 122 final String name = key.substring(a + 1); 123 proxoolProperties = (Properties ) propertiesMap.get(tag); 124 if (proxoolProperties == null) { 125 proxoolProperties = new Properties (); 126 propertiesMap.put(tag, proxoolProperties); 127 } 128 proxoolProperties.put(name, value); 129 } 130 } 131 132 final Iterator tags = propertiesMap.keySet().iterator(); 133 while (tags.hasNext()) { 134 proxoolProperties = (Properties ) propertiesMap.get(tags.next()); 135 final String driverClass = proxoolProperties.getProperty(ProxoolConstants.DRIVER_CLASS_PROPERTY); 139 final String driverUrl = proxoolProperties.getProperty(ProxoolConstants.DRIVER_URL_PROPERTY); 140 if (driverClass == null || driverUrl == null) { 141 throw new ProxoolException("You must define the " + ProxoolConstants.DRIVER_CLASS_PROPERTY + " and the " 142 + ProxoolConstants.DRIVER_URL_PROPERTY + "."); 143 } 144 final String alias = proxoolProperties.getProperty(ProxoolConstants.ALIAS_PROPERTY); 145 146 StringBuffer url = new StringBuffer (); 148 url.append("proxool"); 149 if (alias != null) { 150 url.append(ProxoolConstants.ALIAS_DELIMITER); 151 url.append(alias); 152 proxoolProperties.remove(ProxoolConstants.ALIAS_PROPERTY); 153 } 154 url.append(ProxoolConstants.URL_DELIMITER); 155 url.append(driverClass); 156 proxoolProperties.remove(ProxoolConstants.DRIVER_CLASS_PROPERTY); 157 url.append(ProxoolConstants.URL_DELIMITER); 158 url.append(driverUrl); 159 proxoolProperties.remove(ProxoolConstants.DRIVER_URL_PROPERTY); 160 if (LOG.isDebugEnabled()) { 161 LOG.debug("Created url: " + url); 162 } 163 164 ProxoolFacade.registerConnectionPool(url.toString(), proxoolProperties); 165 } 166 } 167 168 } 169 170 226 | Popular Tags |