KickJava   Java API By Example, From Geeks To Geeks.

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


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.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 JavaDoc;
15 import java.io.IOException JavaDoc;
16 import java.util.HashMap JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.Map JavaDoc;
19 import java.util.Properties JavaDoc;
20
21 /**
22  * Uses a standard Java properties file to configure Proxool. For example:
23  *
24  * <pre>
25  * jdbc-0.proxool.alias=property-test
26  * jdbc-0.proxool.driver-url=jdbc:hsqldb:.
27  * jdbc-0.proxool.driver-class=org.hsqldb.jdbcDriver
28  * jdbc-0.user=foo
29  * jdbc-0.password=bar
30  * jdbc-0.proxool.house-keeping-sleep-time=40000
31  * jdbc-0.proxool.house-keeping-test-sql=select CURRENT_DATE
32  * jdbc-0.proxool.maximum-connection-count=10
33  * jdbc-0.proxool.minimum-connection-count=3
34  * jdbc-0.proxool.maximum-connection-lifetime=18000000
35  * jdbc-0.proxool.simultaneous-build-throttle=5
36  * jdbc-0.proxool.recently-started-threshold=40000
37  * jdbc-0.proxool.overload-without-refusal-lifetime=50000
38  * jdbc-0.proxool.maximum-active-time=60000
39  * jdbc-0.proxool.verbose=true
40  * jdbc-0.proxool.trace=true
41  * jdbc-0.proxool.fatal-sql-exception=Fatal error
42  * jdbc-0.proxool.prototype-count=2
43  *
44  * jdbc-1.proxool.alias=property-test-2
45  * jdbc-1.proxool.driver-url=jdbc:hsqldb:.
46  * jdbc-1.proxool.driver-class=org.hsqldb.jdbcDriver
47  * jdbc-1.user=scott
48  * jdbc-1.password=tiger
49  * jdbc-1.proxool.house-keeping-sleep-time=40000
50  * jdbc-1.proxool.house-keeping-test-sql=select CURRENT_DATE
51  * jdbc-1.proxool.maximum-connection-count=10
52  * jdbc-1.proxool.minimum-connection-count=3
53  * jdbc-1.proxool.maximum-connection-lifetime=18000000
54  * jdbc-1.proxool.simultaneous-build-throttle=5
55  * jdbc-1.proxool.recently-started-threshold=40000
56  * jdbc-1.proxool.overload-without-refusal-lifetime=50000
57  * jdbc-1.proxool.maximum-active-time=60000
58  * jdbc-1.proxool.verbose=true
59  * jdbc-1.proxool.trace=true
60  * jdbc-1.proxool.fatal-sql-exception=Fatal error
61  * jdbc-1.proxool.prototype-count=2
62  * </pre>
63  *
64  * <p>The first word (up to the first dot) must start with "jdbc", but it can
65  * be anything you like. Use unique names to identify each pool. Any property
66  * not starting with "jdbc" will be ignored.</p>
67  * <p>
68  * The properties prefixed with "proxool." will be used by Proxool while
69  * the properties that are not prefixed will be passed on to the
70  * delegate JDBC driver.
71  * </p>
72  *
73  * @version $Revision: 1.11 $, $Date: 2006/01/18 14:39:58 $
74  * @author Bill Horsman (bill@logicalcobwebs.co.uk)
75  * @author $Author: billhorsman $ (current maintainer)
76  * @since Proxool 0.5
77  */

78 public class PropertyConfigurator {
79     private static final Log LOG = LogFactory.getLog(PropertyConfigurator.class);
80
81     protected static final String JavaDoc PREFIX = "jdbc";
82
83     private static final String JavaDoc DOT = ".";
84
85     private static final String JavaDoc EXAMPLE_FORMAT = PREFIX + "*" + DOT + "*";
86
87     /**
88      * Configure proxool with the given properties file.
89      * @param filename the filename of the properties file.
90      * @throws ProxoolException if the configuration fails.
91      */

92     public static void configure(String JavaDoc filename) throws ProxoolException {
93         Properties JavaDoc properties = new Properties JavaDoc();
94         try {
95             properties.load(new FileInputStream JavaDoc(filename));
96         } catch (IOException JavaDoc e) {
97             throw new ProxoolException("Couldn't load property file " + filename);
98         }
99         configure(properties);
100     }
101
102     /**
103      * Configure proxool with the given properties.
104      * @param properties the properties instance to use.
105      * @throws ProxoolException if the configuration fails.
106      */

107     public static void configure(Properties JavaDoc properties) throws ProxoolException {
108         final Map JavaDoc propertiesMap = new HashMap JavaDoc();
109         final Iterator JavaDoc allPropertyKeysIterator = properties.keySet().iterator();
110         Properties JavaDoc proxoolProperties = null;
111
112         while (allPropertyKeysIterator.hasNext()) {
113             String JavaDoc key = (String JavaDoc) allPropertyKeysIterator.next();
114             String JavaDoc 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 JavaDoc tag = key.substring(0, a);
122                 final String JavaDoc name = key.substring(a + 1);
123                 proxoolProperties = (Properties JavaDoc) propertiesMap.get(tag);
124                 if (proxoolProperties == null) {
125                     proxoolProperties = new Properties JavaDoc();
126                     propertiesMap.put(tag, proxoolProperties);
127                 }
128                 proxoolProperties.put(name, value);
129             }
130         }
131
132         final Iterator JavaDoc tags = propertiesMap.keySet().iterator();
133         while (tags.hasNext()) {
134             proxoolProperties = (Properties JavaDoc) propertiesMap.get(tags.next());
135             // make sure that required propeties are defined
136
// and build the url
137
// Check that we have defined the minimum information
138
final String JavaDoc driverClass = proxoolProperties.getProperty(ProxoolConstants.DRIVER_CLASS_PROPERTY);
139             final String JavaDoc 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 JavaDoc alias = proxoolProperties.getProperty(ProxoolConstants.ALIAS_PROPERTY);
145
146             // Build the URL; optionally defining a name
147
StringBuffer JavaDoc url = new StringBuffer JavaDoc();
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 /*
171  Revision history:
172  $Log: PropertyConfigurator.java,v $
173  Revision 1.11 2006/01/18 14:39:58 billhorsman
174  Unbundled Jakarta's Commons Logging.
175
176  Revision 1.10 2003/03/05 23:28:56 billhorsman
177  deprecated maximum-new-connections property in favour of
178  more descriptive simultaneous-build-throttle
179
180  Revision 1.9 2003/03/03 11:12:00 billhorsman
181  fixed licence
182
183  Revision 1.8 2003/02/06 17:41:05 billhorsman
184  now uses imported logging
185
186  Revision 1.7 2003/02/05 14:46:31 billhorsman
187  fixed copyright and made PREFIX protected for
188  use by ServletConfigurator
189
190  Revision 1.6 2003/01/27 18:26:43 billhorsman
191  refactoring of ProxyConnection and ProxyStatement to
192  make it easier to write JDK 1.2 patch
193
194  Revision 1.5 2003/01/23 10:41:05 billhorsman
195  changed use of pool-name to alias for consistency
196
197  Revision 1.4 2003/01/22 17:35:01 billhorsman
198  checkstyle
199
200  Revision 1.3 2003/01/18 15:13:12 billhorsman
201  Signature changes (new ProxoolException
202  thrown) on the ProxoolFacade API.
203
204  Revision 1.2 2002/12/26 11:32:59 billhorsman
205  Rewrote to support new format.
206
207  Revision 1.1 2002/12/15 18:48:33 chr32
208  Movied in from 'ext' source tree.
209
210  Revision 1.4 2002/11/09 15:57:57 billhorsman
211  fix doc
212
213  Revision 1.3 2002/11/02 14:22:16 billhorsman
214  Documentation
215
216  Revision 1.2 2002/10/27 13:05:01 billhorsman
217  checkstyle
218
219  Revision 1.1 2002/10/27 12:00:16 billhorsman
220  moved classes from ext sub-package which is now obsolete - let's keep everything together in one place
221
222  Revision 1.1 2002/10/25 10:40:27 billhorsman
223  draft
224
225 */

226
Popular Tags