001: /*
002: * $Header: /export/home/cvsroot/MyPersonalizerRepository/MyPersonalizer/Subsystems/Kernel/Sources/es/udc/mypersonalizer/kernel/model/repository/sql/plain/ConnectionPoolAdapterSingleton.java,v 1.1.1.1 2004/03/25 12:08:36 fbellas Exp $
003: * $Revision: 1.1.1.1 $
004: * $Date: 2004/03/25 12:08:36 $
005: *
006: * =============================================================================
007: *
008: * Copyright (c) 2003, The MyPersonalizer Development Group
009: * (http://www.tic.udc.es/~fbellas/mypersonalizer/index.html) at
010: * University Of A Coruna
011: * All rights reserved.
012: *
013: * Redistribution and use in source and binary forms, with or without
014: * modification, are permitted provided that the following conditions are met:
015: *
016: * - Redistributions of source code must retain the above copyright notice,
017: * this list of conditions and the following disclaimer.
018: *
019: * - Redistributions in binary form must reproduce the above copyright notice,
020: * this list of conditions and the following disclaimer in the documentation
021: * and/or other materials provided with the distribution.
022: *
023: * - Neither the name of the University Of A Coruna nor the names of its
024: * contributors may be used to endorse or promote products derived from
025: * this software without specific prior written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
028: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
029: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
030: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
031: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
032: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
033: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
034: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
035: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
036: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
037: * POSSIBILITY OF SUCH DAMAGE.
038: *
039: */
040:
041: package es.udc.mypersonalizer.kernel.model.repository.sql.plain;
042:
043: import java.sql.Connection;
044:
045: import es.udc.mypersonalizer.kernel.config.ConnectionPoolAdapterSingletonConfig;
046: import es.udc.mypersonalizer.kernel.log.Log;
047: import es.udc.mypersonalizer.kernel.log.LogManager;
048: import es.udc.mypersonalizer.kernel.log.LogNamingConventions;
049: import es.udc.mypersonalizer.kernel.model.repository.sql.config.DatabaseSpecificsConfig;
050: import es.udc.mypersonalizer.kernel.model.repository.sql.config.DatabaseSpecificsConfigManager;
051: import es.udc.mypersonalizer.kernel.util.exceptions.InternalErrorException;
052:
053: /**
054: * This singleton abstract class provides an implementation of the
055: * Adapter design pattern which provides a way to use any database
056: * connection pool you want. A default implementation is provided.
057: * <p>
058: * This singleton class creates the concrete singleton on run-time the first
059: * time is needed. The name of such a concrete class is obtained by using the
060: * standard "MyPersonalizer" configuration mechanism
061: * {@link es.udc.mypersonalizer.kernel.config.ConnectionPoolAdapterSingletonConfig}).
062: * There is a configuration parameter that specifies the name of the concrete
063: * singleton class.
064: *
065: * @author Fernando Bellas
066: * @author Daniel Fernandez
067: * @author Abel Muinho
068: * @since 1.0
069: */
070: public abstract class ConnectionPoolAdapterSingleton {
071:
072: /**
073: * The driver for the database, where to connect.
074: */
075: private static String connectionDriver = null;
076:
077: /**
078: * The URL for the database, where to connect.
079: */
080: private static String connectionUrl = null;
081:
082: /**
083: * The user name for the database, where to connect.
084: */
085: private static String connectionUser = "";
086:
087: /**
088: * The password of the user for the database, where to connect.
089: */
090: private static String connectionPassword = "";
091:
092: /**
093: * The timeout for the process of getting a Connection from the pool.
094: */
095: private static long connectionWaitTimeOutSeconds;
096:
097: /**
098: * The maximum number of Connections for the pool.
099: */
100: private static int connectionMaximumNumber;
101:
102: /**
103: * A SQL query that will be used to validate connections from this pool
104: * before returning them to the caller. If specified, this query
105: * <b>MUST</b> be an SQL SELECT statement that returns at least one row.
106: */
107: private static String connectionPingQuery = null;
108:
109: /**
110: * The singleton instance.
111: */
112: private static ConnectionPoolAdapterSingleton singleton = null;
113:
114: /*
115: * It uses the standard "MyPersonalizer" configuration mechanism for
116: * getting the name of the concrete singleton class, and creates the
117: * instance.
118: */
119: static {
120:
121: try {
122: DatabaseSpecificsConfig databaseConfig = DatabaseSpecificsConfigManager
123: .getConfig();
124:
125: ConnectionPoolAdapterSingletonConfig connectionPoolAdapterSingletonConfig = databaseConfig
126: .getConnectionPoolAdapterSingletonConfig();
127:
128: String className = connectionPoolAdapterSingletonConfig
129: .getClassName();
130: connectionDriver = connectionPoolAdapterSingletonConfig
131: .getConnectionDriver();
132: connectionUrl = connectionPoolAdapterSingletonConfig
133: .getConnectionURL();
134: connectionUser = connectionPoolAdapterSingletonConfig
135: .getConnectionUser();
136: connectionPassword = connectionPoolAdapterSingletonConfig
137: .getConnectionPassword();
138: String connectionWaitTimeOutSecondsStr = connectionPoolAdapterSingletonConfig
139: .getConnectionWaitTimeOutSeconds();
140: String connectionMaximumNumberStr = connectionPoolAdapterSingletonConfig
141: .getConnectionMaximumNumber();
142: connectionWaitTimeOutSeconds = (new Long(
143: connectionWaitTimeOutSecondsStr)).longValue();
144: connectionMaximumNumber = (new Integer(
145: connectionMaximumNumberStr)).intValue();
146: connectionPingQuery = connectionPoolAdapterSingletonConfig
147: .getConnectionPingQuery();
148:
149: Class singletonClassName = Class.forName(className);
150: singleton = (ConnectionPoolAdapterSingleton) singletonClassName
151: .newInstance();
152:
153: Log mypersonalizerLog = LogManager
154: .getLog(LogNamingConventions.MYPERSONALIZER);
155:
156: mypersonalizerLog.write("ConnectionPool started (Driver: "
157: + connectionDriver + ", URL: " + connectionUrl
158: + ", Timeout: " + connectionWaitTimeOutSeconds
159: + ", Maximum pooled connections: "
160: + connectionMaximumNumber + ").", null, // no exception
161: ConnectionPoolAdapterSingleton.class);
162:
163: } catch (Exception e) {
164:
165: Log mypersonalizerLog = LogManager
166: .getLog(LogNamingConventions.MYPERSONALIZER);
167: mypersonalizerLog.write(
168: "Could not initialize configuration for "
169: + "ConnectionPoolAdapterSingleton", e,
170: ConnectionPoolAdapterSingleton.class);
171:
172: }
173:
174: }
175:
176: /**
177: * Creates an instance of this class
178: */
179: protected ConnectionPoolAdapterSingleton() {
180: }
181:
182: /**
183: * Retuns the singleton.
184: *
185: * @return the singleton
186: */
187: public static ConnectionPoolAdapterSingleton getInstance() {
188: return singleton;
189: }
190:
191: /**
192: * Gets a <code>Connection</code> from the pool.
193: *
194: * @return the connection from the pool
195: */
196: public abstract Connection getConnection()
197: throws InternalErrorException;
198:
199: /**
200: * Gets the connection driver.
201: *
202: * @return the connection driver
203: */
204: final String getConnectionDriver() {
205: return connectionDriver;
206: }
207:
208: /**
209: * Gets the connection URL.
210: *
211: * @return the connection URL
212: */
213: final String getConnectionUrl() {
214: return connectionUrl;
215: }
216:
217: /**
218: * Gets the connection user.
219: *
220: * @return the connection user
221: */
222: final String getConnectionUser() {
223: return connectionUser;
224: }
225:
226: /**
227: * Gets the connection password.
228: *
229: * @return the connection password
230: */
231: final String getConnectionPassword() {
232: return connectionPassword;
233: }
234:
235: /**
236: * Gets the time out time for getting a connection, in seconds.
237: *
238: * @return the time out time for getting a connection
239: */
240: final long getConnectionWaitTimeOutSeconds() {
241: return connectionWaitTimeOutSeconds;
242: }
243:
244: /**
245: * Gets the maximum number of connections for the pool.
246: *
247: * @return the maximum number of connections for the pool
248: */
249: final int getConnectionMaximumNumber() {
250: return connectionMaximumNumber;
251: }
252:
253: /**
254: * Gets the ping query to be executed before returning a connection. A
255: * ping query is a SQL command (i.e. a <code>SELECT</code>) that will be
256: * executed before a connection is returned. Such a query should
257: * execute very quickly. It returns <code>null</code> if no ping query will
258: * be used.
259: *
260: * @return the ping query or <code>null</code>
261: */
262: final String getConnectionPingQuery() {
263: return connectionPingQuery;
264: }
265:
266: }
|