View Javadoc

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