View Javadoc

1   /*
2    * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/conprovider/JakartaConnectionProvider.java,v 1.16 2005/11/30 20:31:17 hkollmann Exp $
3    * $Revision: 1.16 $
4    * $Date: 2005/11/30 20:31:17 $
5    *
6    * DbForms - a Rapid Application Development Framework
7    * Copyright (C) 2001 Joachim Peer <joepeer@excite.com>
8    *
9    * This library is free software; you can redistribute it and/or
10   * modify it under the terms of the GNU Lesser General Public
11   * License as published by the Free Software Foundation; either
12   * version 2.1 of the License, or (at your option) any later version.
13   *
14   * This library is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17   * Lesser General Public License for more details.
18   *
19   * You should have received a copy of the GNU Lesser General Public
20   * License along with this library; if not, write to the Free Software
21   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
22   */
23  
24  package org.dbforms.conprovider;
25  
26  import org.apache.commons.dbcp.BasicDataSource;
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  
30  import org.dbforms.util.Util;
31  
32  import java.io.PrintWriter;
33  
34  import java.sql.Connection;
35  import java.sql.SQLException;
36  
37  import java.util.Enumeration;
38  import java.util.Properties;
39  
40  
41  
42  /***
43   * Connection provider for Apache Jakarta commons-dbcp. <br>
44   * See <code>http://jakarta.apache.org/commons/dbcp.html</code> for further
45   * informations.
46   *
47   * @author Luca Fossato
48   */
49  public class JakartaConnectionProvider extends AbstractConnectionProvider {
50     /***
51      * The SQL query that will be used to validate connections from this pool
52      * before returning them to the caller.
53      */
54     protected static final String CP_PROPS_VALIDATION_QUERY = "validationQuery";
55  
56     /***
57      * the maximum number of active connections that can be allocated from this
58      * pool at the same time, or zero for no limit.
59      */
60     protected static final String CP_PROPS_MAX_ACTIVE = "maxActive";
61  
62     /***
63      * The maximum number of active connections that can remain idle in the
64      * pool, without extra ones being released, or zero for no limit.
65      */
66     protected static final String CP_PROPS_MAX_IDLE = "maxIdle";
67  
68     /***
69      * The maximum number of milliseconds that the pool will wait (when there
70      * are no available connections) for a connection to be returned before
71      * throwing an exception, or -1 to wait indefinitely.
72      */
73     protected static final String CP_PROPS_MAX_WAIT = "maxWait";
74  
75     /*** log the dataSource object statements using log4j category class */
76     protected static final String CP_PROPS_USE_LOG = "useLog";
77     private static Log            logCat = LogFactory.getLog(JakartaConnectionProvider.class);
78  
79     /*** Commons-dbcp dataSource */
80     private BasicDataSource dataSource = null;
81  
82     /***
83      * Default constructor.
84      *
85      * @exception Exception Description of the Exception
86      * @throws Exception because of the <code>throws Exception</code> clause of
87      *         the  <code>init</code> method.
88      */
89     public JakartaConnectionProvider() throws Exception {
90        super();
91     }
92  
93     /***
94      * Get a JDBC Connection
95      *
96      * @return a JDBC Connection
97      *
98      * @exception SQLException Description of the Exception
99      */
100    protected Connection getConnection() throws SQLException {
101       logCat.debug("::getConnection - MaxActive = " + dataSource.getMaxActive());
102       logCat.debug("::getConnection - NumActive = " + dataSource.getNumActive());
103       logCat.debug("::getConnection - NumIdle   = " + dataSource.getNumIdle());
104 
105       return dataSource.getConnection();
106    }
107 
108 
109    /***
110     * Initialize the Jakarta Commons connection pool.
111     *
112     * @throws Exception if any error occurs
113     */
114    protected void init() throws Exception {
115       Properties props = null;
116 
117       dataSource = new BasicDataSource();
118       dataSource.setDriverClassName(getPrefs().getJdbcDriver());
119       dataSource.setUrl(getPrefs().getJdbcURL());
120       dataSource.setUsername(getPrefs().getUser());
121       dataSource.setPassword(getPrefs().getPassword());
122 
123       // set the dataSource properties;
124       if ((props = getPrefs()
125                             .getProperties()) != null) {
126          for (Enumeration e = props.propertyNames(); e.hasMoreElements();) {
127             String key = (String) e.nextElement();
128             dataSource.addConnectionProperty(key, props.getProperty(key));
129             logCat.info("::init - dataSource property [" + key + "] = ["
130                         + props.getProperty(key) + "]");
131          }
132       }
133 
134       // now set the connection pool custom properties;
135       // if the connectionPool properties object is null,
136       // instance a new properties object anyway, to use default values;
137       if ((props = getPrefs()
138                             .getPoolProperties()) == null) {
139          props = new Properties();
140       }
141 
142       String validationQuery = props.getProperty(CP_PROPS_VALIDATION_QUERY, null);
143 
144       if (!Util.isNull(validationQuery)) {
145          dataSource.setValidationQuery(validationQuery.trim());
146       }
147 
148       dataSource.setMaxActive(Integer.parseInt(props.getProperty(CP_PROPS_MAX_ACTIVE,
149                                                                  "20")));
150       dataSource.setMaxIdle(Integer.parseInt(props.getProperty(CP_PROPS_MAX_IDLE,
151                                                                "5")));
152       dataSource.setMaxWait(Long.parseLong(props.getProperty(CP_PROPS_MAX_WAIT,
153                                                              "-1")));
154 
155       // if PROPS_LOG == true, use log4j category to log the datasource info;
156       String useLog = props.getProperty(CP_PROPS_USE_LOG, "false");
157 
158       if (!Util.isNull(useLog) && "true".equals(useLog.trim())) {
159          logCat.info("::init - dataSource log activated");
160          dataSource.setLogWriter(new Log4jPrintWriter());
161       }
162    }
163 
164    private static class Log4jPrintWriter extends PrintWriter {
165       private StringBuffer text = new StringBuffer("");
166 
167       /***
168        * Creates a new Log4jPrintWriter object.
169        */
170       public Log4jPrintWriter() {
171          super(System.err); // PrintWriter doesn't have default constructor.
172       }
173 
174       /***
175        * overrides all the print and println methods for 'print' it to the
176        * constructor's Category
177        */
178       public void close() {
179          flush();
180       }
181 
182 
183       /***
184        * DOCUMENT ME!
185        */
186       public void flush() {
187          if (!text.toString()
188                         .equals("")) {
189             logCat.info(text.toString());
190             text.setLength(0);
191          }
192       }
193 
194 
195       /***
196        * DOCUMENT ME!
197        *
198        * @param b DOCUMENT ME!
199        */
200       public void print(boolean b) {
201          text.append(b);
202       }
203 
204 
205       /***
206        * DOCUMENT ME!
207        *
208        * @param c DOCUMENT ME!
209        */
210       public void print(char c) {
211          text.append(c);
212       }
213 
214 
215       /***
216        * DOCUMENT ME!
217        *
218        * @param s DOCUMENT ME!
219        */
220       public void print(char[] s) {
221          text.append(s);
222       }
223 
224 
225       /***
226        * DOCUMENT ME!
227        *
228        * @param d DOCUMENT ME!
229        */
230       public void print(double d) {
231          text.append(d);
232       }
233 
234 
235       /***
236        * DOCUMENT ME!
237        *
238        * @param f DOCUMENT ME!
239        */
240       public void print(float f) {
241          text.append(f);
242       }
243 
244 
245       /***
246        * DOCUMENT ME!
247        *
248        * @param i DOCUMENT ME!
249        */
250       public void print(int i) {
251          text.append(i);
252       }
253 
254 
255       /***
256        * DOCUMENT ME!
257        *
258        * @param l DOCUMENT ME!
259        */
260       public void print(long l) {
261          text.append(l);
262       }
263 
264 
265       /***
266        * DOCUMENT ME!
267        *
268        * @param obj DOCUMENT ME!
269        */
270       public void print(Object obj) {
271          text.append(obj);
272       }
273 
274 
275       /***
276        * DOCUMENT ME!
277        *
278        * @param s DOCUMENT ME!
279        */
280       public void print(String s) {
281          text.append(s);
282       }
283 
284 
285       /***
286        * DOCUMENT ME!
287        */
288       public void println() {
289          flush();
290       }
291 
292 
293       /***
294        * DOCUMENT ME!
295        *
296        * @param x DOCUMENT ME!
297        */
298       public void println(boolean x) {
299          text.append(x);
300          flush();
301       }
302 
303 
304       /***
305        * DOCUMENT ME!
306        *
307        * @param x DOCUMENT ME!
308        */
309       public void println(char x) {
310          text.append(x);
311          flush();
312       }
313 
314 
315       /***
316        * DOCUMENT ME!
317        *
318        * @param x DOCUMENT ME!
319        */
320       public void println(char[] x) {
321          text.append(x);
322          flush();
323       }
324 
325 
326       /***
327        * DOCUMENT ME!
328        *
329        * @param x DOCUMENT ME!
330        */
331       public void println(double x) {
332          text.append(x);
333          flush();
334       }
335 
336 
337       /***
338        * DOCUMENT ME!
339        *
340        * @param x DOCUMENT ME!
341        */
342       public void println(float x) {
343          text.append(x);
344          flush();
345       }
346 
347 
348       /***
349        * DOCUMENT ME!
350        *
351        * @param x DOCUMENT ME!
352        */
353       public void println(int x) {
354          text.append(x);
355          flush();
356       }
357 
358 
359       /***
360        * DOCUMENT ME!
361        *
362        * @param x DOCUMENT ME!
363        */
364       public void println(long x) {
365          text.append(x);
366          flush();
367       }
368 
369 
370       /***
371        * DOCUMENT ME!
372        *
373        * @param x DOCUMENT ME!
374        */
375       public void println(Object x) {
376          text.append(x);
377          flush();
378       }
379 
380 
381       /***
382        * DOCUMENT ME!
383        *
384        * @param x DOCUMENT ME!
385        */
386       public void println(String x) {
387          text.append(x);
388          flush();
389       }
390    }
391 }