001: /*
002: * Copyright (C) 2003 <a href="mailto:jochen.hiller@bauer-partner.com">Jochen Hiller</a>
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018: package org.mandarax.sql;
019:
020: import java.io.PrintWriter;
021: import java.sql.Connection;
022: import java.sql.SQLException;
023:
024: import javax.sql.DataSource;
025:
026: import org.mandarax.util.logging.LogCategories;
027:
028: /**
029: * The DefaultConnectionManager implements the
030: * interface of the SQLConnectionManager.
031: *
032: * It supports specification of a plain SQL connection,
033: * or usage of the J2EE compliant data sources.
034: * If a plain SQL connection will be given during initialize,
035: * a data source wrapper will be used for this connection.
036: *
037: * @see SQLConnectionManager
038: * @see java.sql.Connection
039: * @see javax.sql.DataSource
040: *
041: * @author <a href="mailto:jochen.hiller@bauer-partner.com">Jochen Hiller</a>
042: * @version 3.4 <7 March 05>
043: * @since 2.2
044: */
045: public class DefaultConnectionManager implements SQLConnectionManager {
046:
047: // attributes
048:
049: /** a data source */
050: private DataSource dataSource = null;
051:
052: // constructors
053:
054: /**
055: * Instantiates a new connection manager for a given SQL connection.
056: * The connection will be wrapped through our data source
057: * connection wrapper.
058: *
059: * @param con the plain sql connection
060: */
061: public DefaultConnectionManager(Connection con) {
062: if (con == null) {
063: throw new IllegalArgumentException("Connection is null");
064: }
065: this .dataSource = new ConnectionWrapperDataSource(con);
066: }
067:
068: /**
069: * Instantiates a new connection manager for a given data source.
070: *
071: * @param ds the given data source
072: */
073: public DefaultConnectionManager(DataSource ds) {
074: if (ds == null) {
075: throw new IllegalArgumentException("DataSource is null");
076: }
077: this .dataSource = ds;
078: }
079:
080: // public methods
081:
082: /**
083: * Gets a data source from the connection manager.
084: *
085: * @return a data source
086: */
087: public DataSource getDataSource() {
088: LogCategories.LOG_SQL.debug("getDataSource()");
089: return this .dataSource;
090: }
091:
092: /**
093: * Releases a connection, give it back to connection manager.
094: *
095: * @todo close the connection, dependent on a bool setting
096: * @param con the connection to release
097: */
098: public void releaseConnection(Connection con) throws SQLException {
099: LogCategories.LOG_SQL.debug("releaseConnection ()");
100: // nothing to do for the moment
101: }
102:
103: // inner classes
104:
105: /**
106: * A data source, which wraps a simple SQL connection.
107: *
108: * Only in private scope.
109: *
110: * @author <a href="mailto:jochen.hiller@bauer-partner.com">Jochen Hiller</a>
111: * @version 3.4 <7 March 05>
112: * @since 2.2
113: */
114: private class ConnectionWrapperDataSource implements DataSource {
115:
116: /** the connection to wrap */
117: private Connection wrappedConnection;
118:
119: /** the login timeout */
120: private int loginTimeout;
121:
122: /** the log print writer */
123: private PrintWriter logPrintWriter;
124:
125: /**
126: * Default constructor.
127: */
128: public ConnectionWrapperDataSource(Connection con) {
129: super ();
130: wrappedConnection = con;
131: }
132:
133: /**
134: * Get a database connection.
135: * @return a database connection
136: */
137: public Connection getConnection() throws SQLException {
138: return wrappedConnection;
139: }
140:
141: /**
142: * Get a database connection.
143: *
144: * @todo support user/password
145: * @param userName a user name
146: * @param password a password
147: * @return a database connection
148: */
149: public Connection getConnection(String userName, String password)
150: throws SQLException {
151: return getConnection();
152: }
153:
154: /**
155: * Get the login timeout.
156: * @return the login timeout (an integer).
157: */
158: public int getLoginTimeout() throws SQLException {
159: return loginTimeout;
160: }
161:
162: /**
163: * Get a log writer.
164: * @return a log writer.
165: */
166: public PrintWriter getLogWriter() throws SQLException {
167: return logPrintWriter;
168: }
169:
170: /**
171: * Set the login timeout.
172: * @param millis the time in milli seconds
173: */
174: public void setLoginTimeout(int millis) throws SQLException {
175: loginTimeout = millis;
176: }
177:
178: /**
179: * Set the log writer.
180: * @param writer a writer
181: */
182: public void setLogWriter(PrintWriter writer)
183: throws SQLException {
184: logPrintWriter = writer;
185: }
186: }
187: }
|