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 properties
25   */
26  
27  package com.pow2.dao;
28  
29  
30  import java.util.*;
31  import java.sql.*;
32  
33  import com.protomatter.jdbc.pool.JdbcConnectionPool;
34  
35  /***
36   *  Connection provider for Protomatter Connection pool.
37   *  <br>
38   *  See <code>http://protomatter.sourceforge.net/</code> for further informations.
39   *
40   * @author  Luca Fossato <fossato@pow2.com>
41   * @created  25 giugno 2002
42   */
43  public class ProtomatterConnectionProvider extends ConnectionProvider
44  {
45    /*** connection pool driver class */
46    private static final String POOL_DRIVER = "com.protomatter.jdbc.pool.JdbcConnectionPoolDriver";
47  
48    /*** Protomatter connectionPool * */
49    private static JdbcConnectionPool connectionPool  = null;
50  
51  
52    /***
53     *  Default constructor.
54     *
55     * @exception  Exception Description of the Exception
56     * @throws  Exception because of the <code>throws Exception</code> clause
57     *                   of the  <code>init</code> method.
58     */
59    public ProtomatterConnectionProvider() throws Exception
60    {
61      super();
62    }
63  
64  
65    /***
66     *  Get a JDBC Connection
67     *
68     * @return  a JDBC Connection
69     * @exception  SQLException Description of the Exception
70     */
71    protected Connection getConnection() throws SQLException
72    {
73      return DriverManager.getConnection(prefs.getConnectionPoolURL(), prefs.getUser(), prefs.getPassword());
74    }
75  
76  
77    /***
78     *  Initialize the Protomatter connection pool.
79     *
80     * @throws  Exception if any error occurs
81     */
82    protected void init() throws Exception
83    {
84      Properties props = null;
85  
86      // initialization params are kept in a Hashtable
87      Hashtable args = new Hashtable();
88  
89      // the underlying driver
90      args.put("jdbc.driver", prefs.getJdbcDriver());
91  
92      // the URL to connect the underlyng driver with the server
93      args.put("jdbc.URL", prefs.getJdbcURL());
94  
95      // these are properties that get passed to DriverManager.getConnection(...)
96      Properties jdbcProperties = new Properties();
97  
98      jdbcProperties.put("user", prefs.getUser());
99      jdbcProperties.put("password", prefs.getPassword());
100 
101     // set the connection properties;
102     if ((props = prefs.getProperties()) != null)
103     {
104       for (Enumeration e = props.propertyNames(); e.hasMoreElements(); )
105       {
106         String key  = (String) e.nextElement();
107         jdbcProperties.put(key, props.getProperty(key));
108         cat.info("::init - JDBC property [" + key + "] = [" + props.getProperty(key) + "]");
109       }
110     }
111 
112     args.put("jdbc.properties", jdbcProperties);
113 
114     // the initial size of the pool.
115     args.put("pool.initialSize", new Integer(5));
116 
117     // the maximum size the pool can grow to.
118     args.put("pool.maxSize", new Integer(10));
119 
120     // each time the pool grows, it grows by this many connections
121     args.put("pool.growBlock", new Integer(2));
122 
123     // between successive connections, wait this many milliseconds.
124     args.put("pool.createWaitTime", new Integer(1000));
125 
126     // finally create the pool and we're ready to go!
127     Class.forName(POOL_DRIVER).newInstance();
128     connectionPool = new JdbcConnectionPool(getLastToken(prefs.getConnectionPoolURL(), ":"), args);
129   }
130 }