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  
24  package com.pow2.dao;
25  
26  
27  import java.sql.*;
28  import org.apache.log4j.Category;
29  
30  
31  /***
32   *  ConnectionProvider base class.
33   *  <br>
34   *  To create a ConnectionProvider for your preferred ConnectionPooler,
35   *  extend this calss and implement <code>initialize</code> and
36   *  <code>getConnection</code> methods.
37   *
38   * @author  Luca Fossato <fossato@pow2.com>
39   * @created  25 giugno 2002
40   */
41  public abstract class ConnectionProvider
42  {
43    /*** Log4j category */
44    protected Category cat = Category.getInstance(this.getClass());
45  
46    /*** ConnectionProvider preferences */
47    protected ConnectionProviderPrefs prefs = null;
48  
49  
50    /***
51     *  Constructor for the ConnectionProvider object.
52     *
53     * @exception  Exception Description of the Exception
54     */
55    public ConnectionProvider() throws Exception
56    {
57    }
58  
59  
60    /***
61     *  Gets the prefs attribute of the ConnectionProvider object
62     *
63     * @return  The prefs value
64     */
65    public ConnectionProviderPrefs getPrefs()
66    {
67      return prefs;
68    }
69  
70  
71    /***
72     *  Sets the prefs attribute of the ConnectionProvider object
73     *
74     * @param  prefs The new prefs value
75     */
76    public void setPrefs(ConnectionProviderPrefs prefs)
77    {
78      this.prefs = prefs;
79    }
80  
81  
82    /***
83     *  Initialize the connection pool provider.
84     *
85     * @exception  Exception Description of the Exception
86     */
87    protected abstract void init() throws Exception;
88  
89  
90    /***
91     *  Get a JDBC Connection.
92     *
93     * @return  a JDBC Connection
94     * @exception  SQLException Description of the Exception
95     */
96    protected abstract Connection getConnection() throws SQLException;
97  
98  
99    /***
100    *  Get a "transactional" JDBC connection.
101    *
102    * @param  isolationLevel the isolation level to set the connection to
103    * @return  the new "transactional" connection object
104    * @throws  SQLException if any error occurs
105    */
106   protected Connection getConnection(int isolationLevel) throws SQLException
107   {
108     Connection con = getConnection();
109     con.setTransactionIsolation(isolationLevel);
110     con.setAutoCommit(false);
111 
112     return con;
113   }
114 
115 
116   /***
117    *  Get the last token from the input string.
118    *
119    * @param  str the string containing the token
120    * @param  tokenSeparator the token separator string (i.e.: "'", ":", etc)
121    * @return  the last token from the input string
122    */
123   protected String getLastToken(String str, String tokenSeparator)
124   {
125     str.trim();
126     return str.substring(str.lastIndexOf(tokenSeparator) + 1, str.length());
127   }
128 }