KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > logicalcobwebs > proxool > configuration > AvalonConfigurator


1 /*
2  * This software is released under a licence similar to the Apache Software Licence.
3  * See org.logicalcobwebs.proxool.package.html for details.
4  * The latest version is available at http://proxool.sourceforge.net
5  */

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 JavaDoc;
20 import org.xml.sax.SAXException JavaDoc;
21 import org.xml.sax.helpers.AttributesImpl JavaDoc;
22
23 import java.util.ArrayList JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26
27 /**
28  * Configurator for the <a HREF="http://jakarta.apache.org/avalon/framework/" target="_new">Avalon Framework</a>.
29  * The configuration can contain any number of &lt;proxool&gt; elements. The &lt;proxool&gt; elements
30  * are delegated to {@link XMLConfigurator},
31  * and have exactly the same format as is documented in that class.
32  * <p>
33  * This is a "faceless" Avalon component. This means that it does not present an operational interface, it
34  * simply configures Proxool when Avalon calls its <code>configure</code> method. You need to lookup this
35  * component in your bootstrap code to make this happen.
36  * </p>
37  * <p>
38  * The configuration takes one attribute: <code>close-on-dispose</code>
39  * <br>
40  * You can use this to let this configurator know
41  * wether or not to close the pools it has created
42  * when it is disposed.
43  * <br>Legal values are <code>true</code> or <code>false</code>. Default: <code>true</code>.
44  * </p>
45  *
46  * @version $Revision: 1.14 $, $Date: 2006/01/18 14:39:58 $
47  * @author billhorsman
48  * @author $Author: billhorsman $ (current maintainer)
49  */

50 public class AvalonConfigurator implements Component, Configurable, ThreadSafe, Disposable {
51     private static final Log LOG = LogFactory.getLog(AvalonConfigurator.class);
52
53     /**
54      * Avalon ROLE id for this component.
55      */

56     public static final String JavaDoc ROLE = AvalonConfigurator.class.getName();
57
58     /**
59      * Constant for the boolean "close-on-dispose" attribute that signifies
60      * wether or not this configurator shall close the pools it has created
61      * when it is disposed. Legal values are "true" or "false". Default: true.
62      *
63      */

64     public static final String JavaDoc CLOSE_ON_DISPOSE_ATTRIBUTE = "close-on-dispose";
65
66     private boolean closeOnDispose = true;
67     private final List JavaDoc configuredPools = new ArrayList JavaDoc(3);
68
69     /**
70      * Check that all top level elements are named proxool and hand them to
71      * {@link XMLConfigurator}.
72      * @param configuration the configuration handed over by the Avalon Framework.
73      * @throws ConfigurationException if the configuration fails.
74      */

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 JavaDoc e) {
90             throw new ConfigurationException("", e);
91         }
92     }
93
94     /**
95      * If {@link #CLOSE_ON_DISPOSE_ATTRIBUTE} is set: Close all connection pools that this configurator has configured.
96      * <br>...else: do nothing.
97      */

98     public void dispose() {
99         LOG.info("Disposing.");
100         if (this.closeOnDispose) {
101             Iterator JavaDoc configuredPools = this.configuredPools.iterator();
102             String JavaDoc alias = null;
103             while (configuredPools.hasNext()) {
104                 alias = (String JavaDoc) 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     // Parse the properties recursively, and report found properties to the given XMLConfigurator
119
private void reportProperties(XMLConfigurator xmlConfigurator, Configuration[] properties)
120             throws ConfigurationException, SAXException JavaDoc {
121         Configuration[] children = null;
122         String JavaDoc value = null;
123         String JavaDoc 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 JavaDoc lName = namespace.length() == 0 ? "" : configuration.getName();
134             final String JavaDoc qName = namespace.length() == 0 ? configuration.getName() : "";
135
136             xmlConfigurator.startElement(namespace, lName, qName, getAttributes(configuration));
137             children = configuration.getChildren();
138             // If this is a leaf node, report the value,
139
// else recurse.
140
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     // create a SAX attributes instance from
164
// Avalon configuration attributes
165
private Attributes JavaDoc getAttributes(Configuration configuration) throws ConfigurationException {
166         final AttributesImpl JavaDoc attributes = new AttributesImpl JavaDoc();
167         final String JavaDoc[] 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 /*
185  Revision history:
186  $Log: AvalonConfigurator.java,v $
187  Revision 1.14 2006/01/18 14:39:58 billhorsman
188  Unbundled Jakarta's Commons Logging.
189
190  Revision 1.13 2003/03/10 15:26:53 billhorsman
191  refactoringn of concurrency stuff (and some import
192  optimisation)
193
194  Revision 1.12 2003/03/03 11:12:00 billhorsman
195  fixed licence
196
197  Revision 1.11 2003/02/19 16:52:39 chr32
198  Added support for close-on-dispose attribute.
199
200  Revision 1.10 2003/02/06 17:41:05 billhorsman
201  now uses imported logging
202
203  Revision 1.9 2003/01/27 18:26:42 billhorsman
204  refactoring of ProxyConnection and ProxyStatement to
205  make it easier to write JDK 1.2 patch
206
207  Revision 1.8 2002/12/23 02:59:38 chr32
208  Tiny doc fix.
209
210  Revision 1.7 2002/12/23 02:58:16 chr32
211  Improved doc.
212
213  Revision 1.6 2002/12/23 02:44:44 chr32
214  Added ROLE id and started implementing Component.
215  Improved namespace support.
216
217  Revision 1.5 2002/12/18 23:31:57 chr32
218  Expanded doc.
219
220  Revision 1.4 2002/12/16 11:47:00 billhorsman
221  checkstyle
222
223  Revision 1.3 2002/12/16 02:37:14 chr32
224  Updated to new driver-properties xml format.
225
226  Revision 1.2 2002/12/15 19:42:18 chr32
227  Rewrite. Now delegates to XMLConfigurator.
228
229  Revision 1.1 2002/12/15 18:48:33 chr32
230  Movied in from 'ext' source tree.
231
232  Revision 1.5 2002/10/27 13:05:01 billhorsman
233  checkstyle
234
235  Revision 1.4 2002/10/27 12:00:14 billhorsman
236  moved classes from ext sub-package which is now obsolete - let's keep everything together in one place
237
238  Revision 1.2 2002/10/23 21:04:37 billhorsman
239  checkstyle fixes (reduced max line width and lenient naming convention
240
241  Revision 1.1 2002/09/19 08:53:41 billhorsman
242  created new ext package
243
244  Revision 1.2 2002/09/18 13:48:56 billhorsman
245  checkstyle and doc
246
247  Revision 1.1.1.1 2002/09/13 08:14:16 billhorsman
248  new
249
250  Revision 1.5 2002/08/24 19:57:15 billhorsman
251  checkstyle changes
252
253  Revision 1.4 2002/07/10 16:14:47 billhorsman
254  widespread layout changes and move constants into ProxoolConstants
255
256  Revision 1.3 2002/07/10 10:52:51 billhorsman
257  doc fix
258
259  Revision 1.2 2002/07/02 23:08:55 billhorsman
260  new config package to allow us to introduce other ways
261
262  Revision 1.1 2002/07/02 23:05:31 billhorsman
263  New config package so that we can introduce other ways of configuring pools.
264
265  Revision 1.6 2002/07/02 11:19:08 billhorsman
266  layout code and imports
267
268  Revision 1.5 2002/07/02 11:14:26 billhorsman
269  added test (andbug fixes) for FileLogger
270
271  Revision 1.4 2002/06/28 11:19:47 billhorsman
272  improved doc
273
274  Revision 1.3 2002/06/05 09:01:31 billhorsman
275  removed ConnectionFacadeIF interface in preparation for new, cleaner ProxoolFacade class. _And_ did a code layout update. Why, of
276  why did I mix that up with one commit? It makes it unclear where the cosmetic changes and code changes were made. I won't do it again.
277
278  Revision 1.2 2002/06/04 22:23:00 billhorsman
279  added class header comments
280
281  Revision 1.1.1.1 2002/06/04 14:24:01 billhorsman
282  start
283
284
285 */

286
Popular Tags