View Javadoc

1   /***
2    *  The contents of this file are subject to the Mozilla Public
3    *  License Version 1.1 (the "License"); you may not use this file
4    *  except in compliance with the License. You may obtain a copy of
5    *  the License at http://www.mozilla.org/MPL/
6    *
7    *  Software distributed under the License is distributed on an "AS
8    *  IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
9    *  implied. See the License for the specific language governing
10   *  rights and limitations under the License.
11   *
12   *  The Original Code is pow2toolkit library.
13   *
14   *  The Initial Owner of the Original Code is
15   *  Power Of Two S.R.L. (www.pow2.com)
16   *
17   *  Portions created by Power Of Two S.R.L. are
18   *  Copyright (C) Power Of Two S.R.L.
19   *  All Rights Reserved.
20   *
21   * Contributor(s):
22   *
23   *  Henner Kollman <Henner.Kollmann@aucos.de>
24   *    adds the support for custom JDBC dataSource properties
25   */
26  
27  package com.pow2.dao;
28  
29  
30  import java.util.*;
31  import java.sql.*;
32  
33  import org.apache.commons.dbcp.BasicDataSource;
34  
35  /***
36   *  Connection provider for Apache Jakarta commons-dbcp.
37   *  <br>
38   *  See <code>http://jakarta.apache.org/commons/components.html</code>
39   *  for further informations.
40   *
41   * @author  Eric Pugh
42   * @author  Luca Fossato <fossato@pow2.com>
43   * @created  25 giugno 2002
44   */
45  public class JakartaConnectionProvider extends ConnectionProvider
46  {
47    /*** Commons-dbcp dataSource * */
48    private BasicDataSource dataSource  = null;
49  
50  
51    /***
52     *  Default constructor.
53     *
54     * @exception  Exception Description of the Exception
55     * @throws  Exception because of the <code>throws Exception</code> clause
56     *                   of the  <code>init</code> method.
57     */
58    public JakartaConnectionProvider() throws Exception
59    {
60      super();
61    }
62  
63  
64    /***
65     *  Get a JDBC Connection
66     *
67     * @return  a JDBC Connection
68     * @exception  SQLException Description of the Exception
69     */
70    protected Connection getConnection() throws SQLException
71    {
72      return dataSource.getConnection();
73    }
74  
75  
76    /***
77     *  Initialize the Jakarta Commons connection pool.
78     *
79     * @throws  Exception if any error occurs
80     */
81    protected void init() throws Exception
82    {
83      Properties props = null;
84  
85      dataSource = new BasicDataSource();
86      dataSource.setDriverClassName(prefs.getJdbcDriver());
87      dataSource.setUrl(prefs.getJdbcURL());
88      dataSource.setUsername(prefs.getUser());
89      dataSource.setPassword(prefs.getPassword());
90  
91      // set the dataSource properties;
92      if ((props = prefs.getProperties()) != null)
93      {
94        for (Enumeration e = props.propertyNames(); e.hasMoreElements(); )
95        {
96          String key = (String) e.nextElement();
97          dataSource.addConnectionProperty(key, props.getProperty(key));
98          cat.info("::init - dataSource property [" + key + "] = [" + props.getProperty(key) + "]");
99        }
100     }
101 
102     dataSource.setValidationQuery(null);
103     dataSource.setMaxActive(20);
104     dataSource.setMaxIdle(5);
105     dataSource.setMaxWait(-1);
106   }
107 }