1 package org.smartlib.pool.core; 2 3 4 import java.io.File ; 5 import java.io.IOException ; 6 import java.util.List ; 7 import java.util.ArrayList ; 8 import java.util.HashMap ; 9 10 import org.apache.log4j.Logger; 11 import org.apache.xerces.parsers.DOMParser; 12 import org.xml.sax.*; 13 import org.w3c.dom.Document ; 14 import org.w3c.dom.Node ; 15 import org.w3c.dom.NodeList ; 16 import org.w3c.dom.NamedNodeMap ; 17 18 19 26 public class ConfigFileProcessor { 27 28 Logger logger = Logger.getLogger(ConfigFileProcessor.class); 29 30 PoolConfig[] getPoolConfig(String fileName) throws SAXException, SAXNotRecognizedException, IOException , ConnectionPoolException { 31 32 if (fileName == null) 33 throw new IllegalArgumentException ("Filename cannot be null"); 34 35 File file = new File (fileName); 36 if (!file.exists()) { 37 throw new IllegalArgumentException ("File: " + file + " does not exist"); 38 } 39 40 return process(file); 41 42 43 } 44 45 PoolConfig[] getPoolConfig() throws SAXException, SAXNotRecognizedException, IOException , ConnectionPoolException { 46 47 String fileName = System.getProperty(PoolConstants.CONFIG_FILE_SYSTEM_PROPERTY); 48 if (logger.isDebugEnabled()) { 49 logger.debug("Config File System Property:" + fileName); 50 } 51 return getPoolConfig(fileName); 52 53 } 54 55 56 private PoolConfig[] process(File file) throws SAXException, SAXNotRecognizedException, IOException , ConnectionPoolException { 57 58 DOMParser xmlParser = new DOMParser(); 59 xmlParser.setFeature("http://apache.org/xml/features/validation/schema", true); 61 66 67 68 xmlParser.setErrorHandler(new MyErrorHandler()); 69 if (logger.isDebugEnabled()) { 70 logger.debug("Reading file: " + file.getAbsolutePath()); 71 } 72 xmlParser.parse(file.getAbsolutePath()); 73 Document dom = xmlParser.getDocument(); 74 75 NodeList nodeList = dom.getElementsByTagName("pool"); 76 PoolConfig[] configs = new PoolConfig[nodeList.getLength()]; 77 for (int i=0; i<nodeList.getLength(); i++) { 78 Node node = nodeList.item(i); 79 configs[i] = getPoolConfig(node); 80 } 81 validatePoolConfigs(configs); 82 return configs; 83 84 } 85 86 private PoolConfig getPoolConfig(Node node) throws ConnectionPoolException { 87 PoolConfig config = new PoolConfig(); 88 NodeList list = node.getChildNodes(); 89 for (int i=0; i<list.getLength(); i++) { 90 Node childNode = list.item(i); 91 String nodeName = childNode.getNodeName(); 92 if (nodeName != null && nodeName.equals("basic")) { 93 populateBasic(config, childNode); 94 } 95 if (nodeName != null && nodeName.equals("connect-strings")) { 96 populateConnectString(config, childNode); 97 } 98 if (nodeName != null && nodeName.equals("pool-sizing")) { 99 populatePoolSizing(config, childNode); 100 } 101 if (nodeName != null && nodeName.equals("leak-detection")) { 102 populateLeakDetection(config, childNode); 103 } 104 if (nodeName != null && nodeName.equals("external-pooling")) { 105 populateExternalPooling(config, childNode); 106 } 107 } 108 return config; 109 110 } 111 112 private void populateExternalPooling(PoolConfig config, Node node) throws ConnectionPoolException { 113 114 NodeList list = node.getChildNodes(); 115 List connList = new ArrayList (); 116 for (int i=0; i<list.getLength(); i++) { 117 Node childNode = list.item(i); 118 String nodeName = childNode.getNodeName(); 119 if (nodeName != null) { 120 if (nodeName != null) { 121 if (nodeName.equals("connection-loader-class")) { 122 PoolConfig.ConnectionLoaderClass connectLoaderClasses = new PoolConfig.ConnectionLoaderClass(); 123 connectLoaderClasses.setConnectionLoaderClass(getTagValue(childNode)); 124 connectLoaderClasses.setName(config.getMultiPoolName() + "." + getAttributeValue(childNode, "name")); 125 connList.add(connectLoaderClasses); 126 } 127 if (nodeName.equals("thread-stickiness")) { 128 config.setThreadStickiness(Boolean.valueOf(getTagValue(childNode))); 129 } 130 } 131 132 } 133 } 134 config.setConnectionLoaderClass((PoolConfig.ConnectionLoaderClass[])connList.toArray(new PoolConfig.ConnectionLoaderClass[connList.size()])); 135 } 136 137 private void populateConnectString(PoolConfig config, Node node) throws ConnectionPoolException { 138 139 NodeList list = node.getChildNodes(); 140 List connList = new ArrayList (); 141 142 for (int i=0; i<list.getLength(); i++) { 143 Node childNode = list.item(i); 144 String nodeName = childNode.getNodeName(); 145 if (nodeName != null) { 146 if (nodeName.equals("connect-string")) { 147 PoolConfig.ConnectionString connectString = new PoolConfig.ConnectionString(); 148 connectString.setConnectString(getTagValue(childNode)); 149 connectString.setName(config.getMultiPoolName() + "." + getAttributeValue(childNode, "name")); 150 connList.add(connectString); 151 } 152 if (nodeName.equals("thread-stickiness")) { 153 config.setThreadStickiness(Boolean.valueOf(getTagValue(childNode))); 154 } 155 } 156 } 157 config.setConnectionString((PoolConfig.ConnectionString[])connList.toArray(new PoolConfig.ConnectionString[connList.size()])); 158 159 } 160 161 private void populatePoolSizing(PoolConfig config, Node node) { 162 163 NodeList list = node.getChildNodes(); 164 for (int i=0; i<list.getLength(); i++) { 165 Node childNode = list.item(i); 166 String nodeName = childNode.getNodeName(); 167 if (nodeName != null) { 168 if (nodeName.equals("min-connections")) { 169 config.setMinConnections(Integer.valueOf(getTagValue(childNode))); 170 } 171 if (nodeName.equals("max-connections")) { 172 config.setMaxConnections(Integer.valueOf(getTagValue(childNode))); 173 } 174 if (nodeName.equals("increment-by")) { 175 config.setIncrement(Integer.valueOf(getTagValue(childNode))); 176 } 177 if (nodeName.equals("increment-by")) { 178 config.setIncrement(Integer.valueOf(getTagValue(childNode))); 179 } 180 if (nodeName.equals("max-free-connections-for-release")) { 181 config.setMaxConnectionsForRelease(Integer.valueOf(getTagValue(childNode))); 182 } 183 if (nodeName.equals("connection-wait-time-out")) { 184 config.setConnectionWaitTimeOut(Integer.valueOf(getTagValue(childNode))); 185 } 186 if (nodeName.equals("max-connection-idle-time")) { 187 config.setMaxConnectionIdleTime(Integer.valueOf(getTagValue(childNode))); 188 } 189 } 190 } 191 } 192 193 private void populateLeakDetection(PoolConfig config, Node node) { 194 195 NodeList list = node.getChildNodes(); 196 for (int i=0; i<list.getLength(); i++) { 197 Node childNode = list.item(i); 198 String nodeName = childNode.getNodeName(); 199 if (nodeName != null) { 200 if (nodeName.equals("detect-leaks")) { 201 config.setDetectLeaks(Boolean.valueOf(getTagValue(childNode))); 202 } 203 if (nodeName.equals("leak-time-out")) { 204 config.setLeakTimeOut(Integer.valueOf(getTagValue(childNode))); 205 } 206 if (nodeName.equals("poll-thread-time")) { 207 config.setPollThreadTime(Integer.valueOf(getTagValue(childNode))); 208 } 209 if (nodeName.equals("default-listener")) { 210 config.setDefaultListener(getTagValue(childNode)); 211 } 212 } 213 214 } 215 216 217 } 218 219 private void populateBasic(PoolConfig config, Node node) { 220 221 222 NodeList list = node.getChildNodes(); 223 for (int i=0; i<list.getLength(); i++) { 224 Node childNode = list.item(i); 225 String nodeName = childNode.getNodeName(); 226 if (nodeName != null) { 227 if (nodeName.equals("pool-name")) { 228 config.setMultiPoolName(getTagValue(childNode)); 229 } 230 if (nodeName.equals("default-pool")) { 231 config.setDefaultPool(Boolean.valueOf(getTagValue(childNode))); 232 } 233 if (nodeName.equals("connection-driver")) { 234 config.setDriver(getTagValue(childNode)); 235 } 236 if (nodeName.equals("username")) { 237 config.setUserName(getTagValue(childNode)); 238 } 239 if (nodeName.equals("password")) { 240 config.setPassword(getTagValue(childNode)); 241 } 242 if (nodeName.equals("auto-close")) { 243 config.setAutoClose(Boolean.valueOf(getTagValue(childNode))); 244 } 245 if (nodeName.equals("allow-anonymous-connections")) { 246 config.setAllowAnonymousConnections(Boolean.valueOf(getTagValue(childNode))); 247 } 248 if (nodeName.equals("validator-query")) { 249 config.setValidatorQuery(getTagValue(childNode)); 250 } 251 } 252 } 253 254 } 255 256 public class MyErrorHandler implements ErrorHandler { 257 258 259 public void warning(SAXParseException exception) throws SAXException { 260 logger.fatal("Warning:", exception); 261 } 262 263 public void error(SAXParseException exception) throws SAXException { 264 logger.fatal("Warning:", exception); 265 } 266 267 public void fatalError(SAXParseException exception) throws SAXException { 268 logger.fatal("Warning:", exception); 269 } 270 271 } 272 273 274 private String getTagValue(Node nod) { 275 276 if (nod.getChildNodes() != null && nod.getChildNodes().getLength() != 0) 278 return nod.getChildNodes().item(0).getNodeValue(); 279 else 280 return nod.getNodeValue(); 281 282 } 283 284 private String getAttributeValue(Node nod, String attrName) throws ConnectionPoolException { 285 286 NamedNodeMap map = nod.getAttributes(); 288 Node attrNode = map.getNamedItem(attrName); 289 if (attrNode == null) 290 throw new ConnectionPoolException("Attribute '" + attrName + "' of node '" + nod.getNodeName() + "' is null"); 291 return attrNode.getNodeValue(); 292 293 } 294 295 private void validatePoolConfigs(PoolConfig[] configs) throws ConnectionPoolException { 296 for (int i=0; i<configs.length; i++) { 297 PoolConfig config = configs[i]; 298 if (config.getConnectionLoaderClass() != null && config.getConnectionLoaderClass().length == 0) 299 throw new ConnectionPoolException("Please provide a valid connection provider for pool:" + config.getMultiPoolName()); 300 if (config.getConnectionString() !=null && config.getConnectionString().length == 0) 301 throw new ConnectionPoolException("Please provide a valid connection string for pool:" + config.getMultiPoolName()); 302 if (config.getConnectionString() != null && config.getConnectionLoaderClass() != null) 303 throw new ConnectionPoolException("You can specify either external-pooling or connect-strings but not both."); 304 305 HashMap distinctMap = new HashMap (); 306 PoolConfig.ConnectionString[] connectStrings = config.getConnectionString(); 307 if (connectStrings != null) { 308 for (int j=0; j <connectStrings.length; j++ ) { 309 if (connectStrings[j].getName() == null || connectStrings[j].getName().trim().length() == 0) 310 throw new ConnectionPoolException("Connect String name attribute cannot be null"); 311 if (connectStrings[j].getConnectString() == null || connectStrings[j].getConnectString().trim().length() == 0) 312 throw new ConnectionPoolException("Connect String cannot be null"); 313 if (distinctMap.get(connectStrings[j].getName()) != null) 314 throw new ConnectionPoolException("Connection string name is not unique: " + connectStrings[j].getName()); 315 distinctMap.put(connectStrings[j].getName(), "hello"); 316 } 317 } 318 319 distinctMap.clear(); 320 PoolConfig.ConnectionLoaderClass[] connectionLoaderClasses = config.getConnectionLoaderClass(); 321 if (connectionLoaderClasses != null) { 322 for (int j=0; j <connectionLoaderClasses.length; j++ ) { 323 if (connectionLoaderClasses[j].getName() == null || connectionLoaderClasses[j].getName().trim().length() == 0) 324 throw new ConnectionPoolException("Connect String name attribute cannot be null"); 325 if (connectionLoaderClasses[j].getConnectionLoaderClass() == null || connectionLoaderClasses[j].getConnectionLoaderClass().trim().length() == 0) 326 throw new ConnectionPoolException("Connect String cannot be null"); 327 if (distinctMap.get(connectionLoaderClasses[j].getName()) != null) 328 throw new ConnectionPoolException("Connection provider name is not unique: " + connectionLoaderClasses[j].getName()); 329 distinctMap.put(connectionLoaderClasses[j].getName(), "hello"); 330 } 331 } 332 333 334 } 335 } 336 337 338 } 339 | Popular Tags |