KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > core > db > SimpleDataSource


1 /* ====================================================================
2  * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
3  *
4  * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by Jcorporate Ltd.
21  * (http://www.jcorporate.com/)."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. "Jcorporate" and product names such as "Expresso" must
26  * not be used to endorse or promote products derived from this
27  * software without prior written permission. For written permission,
28  * please contact info@jcorporate.com.
29  *
30  * 5. Products derived from this software may not be called "Expresso",
31  * or other Jcorporate product names; nor may "Expresso" or other
32  * Jcorporate product names appear in their name, without prior
33  * written permission of Jcorporate Ltd.
34  *
35  * 6. No product derived from this software may compete in the same
36  * market space, i.e. framework, without prior written permission
37  * of Jcorporate Ltd. For written permission, please contact
38  * partners@jcorporate.com.
39  *
40  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
41  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43  * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
44  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
46  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
47  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This software consists of voluntary contributions made by many
55  * individuals on behalf of the Jcorporate Ltd. Contributions back
56  * to the project(s) are encouraged when you make modifications.
57  * Please send them to support@jcorporate.com. For more information
58  * on Jcorporate Ltd. and its products, please see
59  * <http://www.jcorporate.com/>.
60  *
61  * Portions of this software are based upon other open source
62  * products and are subject to their respective licenses.
63  */

64
65 package com.jcorporate.expresso.core.db;
66
67 import org.apache.log4j.Logger;
68
69 import javax.sql.DataSource JavaDoc;
70 import java.io.PrintWriter JavaDoc;
71 import java.sql.Connection JavaDoc;
72 import java.sql.SQLException JavaDoc;
73
74 /**
75  * DataConnectionPool is an implementation of the <code>javax.sql.DataSource</code>.
76  * Normally you would get an instance of the DataConnectionPool using
77  * <code>DBConnectionPool.getDataSource(String)</code>
78  * <p/>
79  * The reason for this class is that some libraries that can access a database
80  * often take a DataSource as their way of connecting to the database. Unfortunately,
81  * Expresso's DBConnectionPool's API is incompatible with the javax.sql.DataSource
82  * API. So this class wraps the DBConnectionPool with the DataSource interface
83  * so that libraries that require DataSource and Connection interfaces can operate
84  * within Expresso's database connection management capabilities.
85  * </p>
86  * <p>Typical Usage:<br/>
87  * <code><blockquote>
88  * javax.sql.DataSource dataSource = DBConnectionPool.getDataSource(&quot;default&quot;);<br/>
89  * java.sql.Connection connection = dataSource.getConnection();<br/>
90  * ...<br/>
91  * connection.close();<br/>
92  * </blockquote></code>
93  *
94  * @author Michael Rimov
95  * @see com.jcorporate.expresso.core.db.DBConnectionPool
96  * @see com.jcorporate.expresso.core.db.DBConnection
97  */

98 public class SimpleDataSource implements DataSource JavaDoc {
99     private java.io.PrintWriter JavaDoc logWriter = null;
100
101     /**
102      * The log4j Logger
103      */

104     private static final Logger log = Logger.getLogger(SimpleDataSource.class);
105
106     /**
107      * The pool instance
108      */

109     private DBConnectionPool poolInstance;
110
111
112     /**
113      * Wrapping constructor. Takes a DBConnectionPool instance and wraps it
114      * for the DataSource implementation
115      *
116      * @param newPoolInstance the instance of the DBConnectionPool
117      */

118     public SimpleDataSource(DBConnectionPool newPoolInstance) {
119         poolInstance = newPoolInstance;
120     }
121
122     /**
123      * Helper method to retrieve the instance of the connectionPool we're attached
124      * to
125      *
126      * @return DBConnectionPool instance
127      */

128     protected DBConnectionPool getConnectionPool() {
129         return poolInstance;
130     }
131
132     /**
133      * Retreive a java.sql.Connection for this data source. It does not attempt
134      * to retrieve pooled connections.
135      *
136      * @return java.sql.Connection
137      * @throws SQLException upon error getting the connection
138      */

139     public Connection JavaDoc getConnection() throws SQLException JavaDoc {
140         if (logWriter != null) {
141             logWriter.println("DataConnectionPool: Retrieving new Connection");
142         }
143
144         try {
145             return poolInstance.buildNewConnection().getConnection();
146         } catch (DBException ex) {
147             log.error("Error getting connection", ex);
148             throw new SQLException JavaDoc(ex.getMessage());
149         }
150     }
151
152     /**
153      * [Unused]
154      * <p>Normally would retrieve a connection with the given username and password. However,
155      * that is not possible with the Expresso's DBConnectionPool which manages the default
156      * username and password as given in the configuration file. So this implementation
157      * logs a warning about this method and falls back to <code>getConnection()</code>
158      *
159      * @param username the user name for the connection
160      * @param password the password for the connection
161      * @return java.sql.Connection implementation
162      * @throws SQLException upon connection error
163      * @see #getConnection
164      */

165     public Connection JavaDoc getConnection(String JavaDoc username, String JavaDoc password) throws SQLException JavaDoc {
166         log.warn("getConnection(username,password) not directly implemented. falling back to getConnection()");
167
168         return getConnection();
169     }
170
171     /**
172      * Retrieve the current logWriter. Default is null
173      *
174      * @return java.io.PrintWriter for the current log writer
175      * @throws SQLException upon error
176      */

177     public PrintWriter JavaDoc getLogWriter() throws SQLException JavaDoc {
178         return logWriter;
179     }
180
181     /**
182      * Sets the log writer and thus turns on logging
183      *
184      * @param out the new print writer to log getConnection() calls to.
185      * @throws SQLException upon error
186      */

187     public void setLogWriter(PrintWriter JavaDoc out) throws SQLException JavaDoc {
188         logWriter = out;
189     }
190
191     /**
192      * Set the login timeout. [unimplemented]
193      *
194      * @param seconds the number of seconds to wait before logins timeout
195      * @throws SQLException upon error
196      */

197     public void setLoginTimeout(int seconds) throws SQLException JavaDoc {
198         log.warn("setLoginTimeout not yet implemented");
199     }
200
201     /**
202      * Retrieve the login timeout [unimplemented]
203      *
204      * @return number of seconds before login timeout occurs (zero)
205      * @throws SQLException upon error
206      */

207     public int getLoginTimeout() throws SQLException JavaDoc {
208         log.warn("getLoginTimeout not yet implemented");
209         return 0;
210     }
211 }
Popular Tags