KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbforms > conprovider > ConnectionFactory


1 /*
2  * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/conprovider/ConnectionFactory.java,v 1.7 2004/08/18 12:25:56 hkollmann Exp $
3  * $Revision: 1.7 $
4  * $Date: 2004/08/18 12:25:56 $
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.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28
29 import java.sql.Connection JavaDoc;
30 import java.sql.SQLException JavaDoc;
31
32
33
34 /**
35  * ConnectionFactory class. <br>
36  * Provides SQL Connection objects using the underlying ConnectionProvider
37  * instance.
38  *
39  * @author Luca Fossato
40  */

41 public class ConnectionFactory {
42    /** an handle to the unique ConnectionFactory instance. */
43    private static ConnectionFactory instance = null;
44
45    /** default ConnectionProvider instance */
46    private ConnectionProvider provider = null;
47
48    /** Log4j category */
49    private Log cat = LogFactory.getLog(this.getClass());
50
51    /**
52     * Constructor for the ConnectionFactory object
53     */

54    private ConnectionFactory() {
55    }
56
57    /**
58     * Get the unique instance of ConnectionFactory class.
59     *
60     * @return the instance of ConnectionFactory class
61     */

62    public static synchronized ConnectionFactory instance() {
63       if (instance == null) {
64          instance = new ConnectionFactory();
65       }
66
67       return instance;
68    }
69
70
71    /**
72     * Get a connection object from the underlying ConnectionProvider object.
73     *
74     * @return the connection object from the underlying ConnectionProvider
75     * object
76     *
77     * @throws SQLException if any error occurs
78     */

79    public Connection JavaDoc getConnection() throws SQLException JavaDoc {
80       return provider.getConnection();
81    }
82
83
84    /**
85     * Get a "transactional" JDBC connection from the underlying default
86     * ConnectionProvider.
87     *
88     * @param isolationLevel the isolation level to set the connection to
89     *
90     * @return the new "transactional" connection object from the underlying
91     * default ConnectionProvider
92     *
93     * @throws SQLException if any error occurs
94     */

95    public Connection JavaDoc getConnection(int isolationLevel)
96                             throws SQLException JavaDoc {
97       return provider.getConnection(isolationLevel);
98    }
99
100
101    /**
102     * Set the ConnectionProvider object
103     *
104     * @param prefs the connection provider preferences object
105     *
106     * @throws Exception if any error occurs
107     */

108    public void setProvider(ConnectionProviderPrefs prefs)
109                     throws Exception JavaDoc {
110       String JavaDoc providerClass = prefs.getConnectionProviderClass();
111       provider = (ConnectionProvider) Class.forName(providerClass)
112                                            .newInstance();
113       provider.setPrefs(prefs);
114       provider.init();
115
116       cat.info("::setProvider - ConnectionProvider [" + providerClass
117                + "] successfully set and initialized");
118    }
119 }
120
Popular Tags