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 import org.xml.sax.Attributes ; 14 import org.xml.sax.SAXException ; 15 import org.xml.sax.SAXParseException ; 16 import org.xml.sax.helpers.DefaultHandler ; 17 18 import java.util.Properties ; 19 20 92 public class XMLConfigurator extends DefaultHandler { 93 private static final Log LOG = LogFactory.getLog(XMLConfigurator.class); 94 95 private StringBuffer content = new StringBuffer (); 96 97 private String poolName; 98 99 private String driverClass; 100 101 private String driverUrl; 102 103 private Properties properties = new Properties (); 104 105 private static final String PROXOOL = "proxool"; 106 107 private static final String DRIVER_PROPERTIES = "driver-properties"; 108 109 private static final String PROPERTY = "property"; 110 111 private static final String NAME = "name"; 112 113 private static final String VALUE = "value"; 114 115 private boolean insideDelegateProperties; 116 117 private boolean insideProxool; 118 119 122 public void startElement(String uri, String lname, String qname, Attributes attributes) throws SAXException { 123 content.setLength(0); 124 125 if (!namespaceOk(uri)) { 126 return; 127 } 128 129 final String elementName = getElementName(uri, lname, qname); 130 131 if (elementName.equals(PROXOOL)) { 132 if (insideProxool) { 133 throw new SAXException ("A <" + PROXOOL + "> element can't contain another <" + PROXOOL + "> element."); 134 } 135 insideProxool = true; 136 properties.clear(); 137 driverClass = null; 138 driverUrl = null; 139 } 140 141 if (insideProxool) { 142 if (elementName.equals(DRIVER_PROPERTIES)) { 143 insideDelegateProperties = true; 144 } else if (insideDelegateProperties) { 145 if (elementName.equals(PROPERTY)) { 146 setDriverProperty(attributes); 147 } 148 } 149 } 150 } 151 152 155 public void characters(char[] chars, int start, int length) throws SAXException { 156 if (insideProxool) { 157 content.append(chars, start, length); 158 } 159 } 160 161 164 public void endElement(String uri, String lname, String qname) throws SAXException { 165 if (!namespaceOk(uri)) { 166 return; 167 } 168 169 final String elementName = getElementName(uri, lname, qname); 170 171 if (elementName.equals(PROXOOL)) { 173 174 if (driverClass == null || driverUrl == null) { 176 throw new SAXException ("You must define the " + ProxoolConstants.DRIVER_CLASS + " and the " + ProxoolConstants.DRIVER_URL + "."); 177 } 178 179 StringBuffer url = new StringBuffer (); 181 url.append("proxool"); 182 if (poolName != null) { 183 url.append(ProxoolConstants.ALIAS_DELIMITER); 184 url.append(poolName); 185 } 186 url.append(ProxoolConstants.URL_DELIMITER); 187 url.append(driverClass); 188 url.append(ProxoolConstants.URL_DELIMITER); 189 url.append(driverUrl); 190 if (LOG.isDebugEnabled()) { 191 LOG.debug("Created url: " + url); 192 } 193 194 try { 196 ProxoolFacade.registerConnectionPool(url.toString(), properties); 197 } catch (ProxoolException e) { 198 throw new SAXException (e); 199 } 200 201 insideProxool = false; 204 } 205 206 if (insideProxool && !elementName.equals(PROXOOL)) { 207 if (elementName.equals(DRIVER_PROPERTIES)) { 208 insideDelegateProperties = false; 209 } else if (!insideDelegateProperties) { 210 setProxoolProperty(elementName, content.toString().trim()); 211 } 212 } 213 } 214 215 private void setProxoolProperty(String localName, String value) { 216 if (localName.equals(ProxoolConstants.ALIAS)) { 217 poolName = value; 218 } else if (localName.equals(ProxoolConstants.DRIVER_CLASS)) { 219 driverClass = value; 220 } else if (localName.equals(ProxoolConstants.DRIVER_URL)) { 221 driverUrl = value; 222 } else { 223 if (LOG.isDebugEnabled()) { 224 LOG.debug("Setting property '" + ProxoolConstants.PROPERTY_PREFIX + localName + "' to value '" + value + "'."); 225 } 226 properties.put(ProxoolConstants.PROPERTY_PREFIX + localName, value); 227 } 228 } 229 230 private void setDriverProperty(Attributes attributes) throws SAXException { 231 final String name = attributes.getValue(NAME); 232 final String value = attributes.getValue(VALUE); 233 if (name == null || name.length() < 1 || value == null) { 234 throw new SAXException ("Name or value attribute missing from property element." 235 + "Name: '" + name + "' Value: '" + value + "'."); 236 } 237 if (LOG.isDebugEnabled()) { 238 if (name.toLowerCase().indexOf("password") > -1) { 239 LOG.debug("Adding driver property: " + name + "=" + "*******"); 240 } else { 241 LOG.debug("Adding driver property: " + name + "=" + value); 242 } 243 } 244 properties.put(name, value); 245 } 246 247 250 public void warning(SAXParseException e) throws SAXException { 251 LOG.debug("The saxparser reported a warning.", e); 253 } 254 255 258 public void error(SAXParseException e) throws SAXException { 259 throw e; 262 } 263 264 267 public void fatalError(SAXParseException e) throws SAXException { 268 throw e; 271 } 272 273 private String getElementName(String uri, String lname, String qname) { 275 if (uri == null || "".equals(uri)) { 276 return qname; 277 } else { 278 return lname; 279 } 280 } 281 282 private boolean namespaceOk(String uri) { 283 return uri == null || uri.length() == 0 || uri.equals(ProxoolConstants.PROXOOL_XML_NAMESPACE_URI); 284 } 285 } 286 | Popular Tags |