1 6 package org.logicalcobwebs.proxool.configuration; 7 8 import org.apache.avalon.framework.activity.Disposable; 9 import org.apache.avalon.framework.component.Component; 10 import org.apache.avalon.framework.configuration.Configurable; 11 import org.apache.avalon.framework.configuration.Configuration; 12 import org.apache.avalon.framework.configuration.ConfigurationException; 13 import org.apache.avalon.framework.thread.ThreadSafe; 14 import org.apache.commons.logging.Log; 15 import org.apache.commons.logging.LogFactory; 16 import org.logicalcobwebs.proxool.ProxoolConstants; 17 import org.logicalcobwebs.proxool.ProxoolException; 18 import org.logicalcobwebs.proxool.ProxoolFacade; 19 import org.xml.sax.Attributes ; 20 import org.xml.sax.SAXException ; 21 import org.xml.sax.helpers.AttributesImpl ; 22 23 import java.util.ArrayList ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 27 50 public class AvalonConfigurator implements Component, Configurable, ThreadSafe, Disposable { 51 private static final Log LOG = LogFactory.getLog(AvalonConfigurator.class); 52 53 56 public static final String ROLE = AvalonConfigurator.class.getName(); 57 58 64 public static final String CLOSE_ON_DISPOSE_ATTRIBUTE = "close-on-dispose"; 65 66 private boolean closeOnDispose = true; 67 private final List configuredPools = new ArrayList (3); 68 69 75 public void configure(Configuration configuration) throws ConfigurationException { 76 final XMLConfigurator xmlConfigurator = new XMLConfigurator(); 77 this.closeOnDispose = configuration.getAttributeAsBoolean(CLOSE_ON_DISPOSE_ATTRIBUTE, true); 78 final Configuration[] children = configuration.getChildren(); 79 for (int i = 0; i < children.length; ++i) { 80 if (!children[i].getName().equals(ProxoolConstants.PROXOOL)) { 81 throw new ConfigurationException("Found element named " + children[i].getName() + ". Only " 82 + ProxoolConstants.PROXOOL + " top level elements are alowed."); 83 } 84 } 85 try { 86 xmlConfigurator.startDocument(); 87 reportProperties(xmlConfigurator, configuration.getChildren()); 88 xmlConfigurator.endDocument(); 89 } catch (SAXException e) { 90 throw new ConfigurationException("", e); 91 } 92 } 93 94 98 public void dispose() { 99 LOG.info("Disposing."); 100 if (this.closeOnDispose) { 101 Iterator configuredPools = this.configuredPools.iterator(); 102 String alias = null; 103 while (configuredPools.hasNext()) { 104 alias = (String ) configuredPools.next(); 105 LOG.info("Closing connection pool '" + alias + "'."); 106 try { 107 ProxoolFacade.removeConnectionPool(alias); 108 } catch (ProxoolException e) { 109 LOG.error("Closing of connection pool '" + alias + "' failed.", e); 110 } 111 } 112 } else { 113 LOG.info(CLOSE_ON_DISPOSE_ATTRIBUTE + " attribute is not set, so configured pools will not be closed."); 114 } 115 LOG.info("Disposed."); 116 } 117 118 private void reportProperties(XMLConfigurator xmlConfigurator, Configuration[] properties) 120 throws ConfigurationException, SAXException { 121 Configuration[] children = null; 122 String value = null; 123 String namespace = null; 124 for (int i = 0; i < properties.length; ++i) { 125 Configuration configuration = properties[i]; 126 namespace = configuration.getNamespace(); 127 if (namespace == null) { 128 namespace = ""; 129 } 130 if (LOG.isDebugEnabled()) { 131 LOG.debug("Reporting element start for " + configuration.getName()); 132 } 133 final String lName = namespace.length() == 0 ? "" : configuration.getName(); 134 final String qName = namespace.length() == 0 ? configuration.getName() : ""; 135 136 xmlConfigurator.startElement(namespace, lName, qName, getAttributes(configuration)); 137 children = configuration.getChildren(); 138 if (children == null || children.length < 1) { 141 value = configuration.getValue(null); 142 if (value != null) { 143 xmlConfigurator.characters(value.toCharArray(), 0, value.length()); 144 } 145 } else { 146 reportProperties(xmlConfigurator, children); 147 } 148 xmlConfigurator.endElement(namespace, lName, qName); 149 if (lName.equals(ProxoolConstants.PROXOOL) || qName.equals(ProxoolConstants.PROXOOL)) { 150 Configuration conf = configuration.getChild(ProxoolConstants.ALIAS, false); 151 if (conf != null) { 152 if (LOG.isDebugEnabled()) { 153 LOG.debug("Adding to configured pools: " + conf.getValue()); 154 } 155 this.configuredPools.add(conf.getValue()); 156 } else { 157 LOG.error("proxool element was missing required element 'alias'"); 158 } 159 } 160 } 161 } 162 163 private Attributes getAttributes(Configuration configuration) throws ConfigurationException { 166 final AttributesImpl attributes = new AttributesImpl (); 167 final String [] avalonAttributeNames = configuration.getAttributeNames(); 168 if (avalonAttributeNames != null && avalonAttributeNames.length > 0) { 169 for (int i = 0; i < avalonAttributeNames.length; ++i) { 170 if (LOG.isDebugEnabled()) { 171 LOG.debug("Adding attribute " + avalonAttributeNames[i] + " with value " 172 + configuration.getAttribute(avalonAttributeNames[i])); 173 } 174 attributes.addAttribute("", avalonAttributeNames[i], avalonAttributeNames[i], "CDATA", 175 configuration.getAttribute(avalonAttributeNames[i])); 176 LOG.debug("In attributes: " + avalonAttributeNames[i] + " with value " 177 + attributes.getValue(avalonAttributeNames[i])); 178 } 179 } 180 return attributes; 181 } 182 } 183 184 286 | Popular Tags |