001: /* JFox, the OpenSource J2EE Application Server
002: *
003: * Copyright (C) 2002 huihoo.org
004: * Distributable under GNU LGPL license
005: * See the GNU Lesser General Public License for more details.
006: */
007:
008: package org.huihoo.jfox.datasource;
009:
010: import java.sql.Connection;
011: import java.sql.SQLException;
012: import java.sql.DriverManager;
013: import java.io.PrintWriter;
014: import java.util.Hashtable;
015:
016: import javax.sql.DataSource;
017: import javax.naming.spi.ObjectFactory;
018: import javax.naming.Referenceable;
019: import javax.naming.Reference;
020: import javax.naming.NamingException;
021: import javax.naming.StringRefAddr;
022: import javax.naming.Name;
023: import javax.naming.Context;
024:
025: import org.huihoo.jfox.pool.connection.SimpleConnectionPool;
026: import org.huihoo.jfox.service.ComponentSupport;
027:
028: /**
029: *
030: * @author <a href="mailto:kelvin_wym@hotmail.com">Kelvin Wu</a>
031: */
032: public class PoolDataSource extends ComponentSupport implements
033: DataSource, Referenceable, ObjectFactory, PoolDataSourceMBean {
034: protected SimpleConnectionPool pool = null; // The connection pool used the get connections.
035: private String dbDriver; // database driver class name.
036: private String dbURL; // database url
037: private String user; // database user
038: private String password; //database password
039:
040: /**
041: * Default constructor used by JNDI to get a new Instance without any parameters.
042: */
043: public PoolDataSource() {
044: // System.out.println("constructing PoolDataSource...");
045: }
046:
047: /**
048: * Constructor specify database connection parameters to generate a valid and usable datasource.
049: * @param dbDriver database class name according your dabase
050: * @param dbURL database url used to access database
051: * @param user valid database user
052: * @param password // corresponding database password
053: * @throws Exception any exception who cares?
054: */
055: public PoolDataSource(String dbDriver, String dbURL, String user,
056: String password) throws Exception {
057: this .dbDriver = dbDriver;
058: this .dbURL = dbURL;
059: this .user = user;
060: this .password = password;
061: pool = new SimpleConnectionPool(dbDriver, dbURL, user, password);
062: }
063:
064: /**
065: * Get usable connection from datasource
066: * @return Connection the valid database connection can used by user
067: * @throws SQLException
068: */
069: public Connection getConnection() throws SQLException {
070: // since the pool.getConnection() throws out exception, so I have to catch it.
071: // Please Young consider about the excpetion handler meshiasm.
072: try {
073: return pool.getConnection();
074: } catch (SQLException sqle) {
075: throw new SQLException(sqle.getLocalizedMessage(), sqle
076: .getSQLState());
077: } catch (Exception e) {
078: throw new SQLException(e.getMessage());
079: }
080: }
081:
082: /**
083: * Get connection by specific user name and password, the connection is not pooled by
084: * Connection Pool
085: * @param username
086: * @param password
087: * @return
088: * @throws SQLException
089: */
090: public Connection getConnection(String username, String password)
091: throws SQLException {
092: return DriverManager.getConnection(dbURL, username, password);
093:
094: }
095:
096: /**
097: * Get logger writer
098: * @return
099: * @throws SQLException
100: */
101: public PrintWriter getLogWriter() throws SQLException {
102: return DriverManager.getLogWriter();
103: }
104:
105: /**
106: *
107: * @param out
108: * @throws SQLException
109: */
110: public void setLogWriter(PrintWriter out) throws SQLException {
111: DriverManager.setLogWriter(out);
112: }
113:
114: /**
115: *
116: * @param seconds
117: * @throws SQLException
118: */
119: public void setLoginTimeout(int seconds) throws SQLException {
120: DriverManager.setLoginTimeout(seconds);
121: }
122:
123: /**
124: *
125: * @return
126: * @throws SQLException
127: */
128: public int getLoginTimeout() throws SQLException {
129: return DriverManager.getLoginTimeout();
130: }
131:
132: /**
133: * Method that inherited from Referenceable.
134: * @return
135: * @throws javax.naming.NamingException
136: */
137: public Reference getReference() throws NamingException {
138: Reference ref = new Reference(getClass().getName(), getClass()
139: .getName(), null);
140: ref.add(new StringRefAddr("dbDriver", dbDriver));
141: ref.add(new StringRefAddr("dbURL", dbURL));
142: ref.add(new StringRefAddr("user", user));
143: ref.add(new StringRefAddr("password", password));
144: return ref;
145: }
146:
147: /**
148: * Method that inherited from ObjectFactory.
149: * @param obj
150: * @param name
151: * @param nameCtx
152: * @param environment
153: * @return
154: * @throws Exception
155: */
156: public Object getObjectInstance(Object obj, Name name,
157: Context nameCtx, Hashtable environment) throws Exception {
158: Object result = null;
159: Reference ref = (Reference) obj;
160: if (ref.getClassName().equals(getClass().getName())) {
161: String _dbDriver = (String) ref.get("dbDriver")
162: .getContent();
163: String _dbURL = (String) ref.get("dbURL").getContent();
164: String _user = (String) ref.get("user").getContent();
165: String _password = (String) ref.get("password")
166: .getContent();
167: PoolDataSource pds = new PoolDataSource(_dbDriver, _dbURL,
168: _user, _password);
169: result = pds;
170: }
171: return result;
172: }
173:
174: public String getDbDriver() {
175: return dbDriver;
176: }
177:
178: public void setDbDriver(String dbDriver) {
179: this .dbDriver = dbDriver;
180: pool.setDbDriver(dbDriver);
181: }
182:
183: public String getDbURL() {
184: return dbURL;
185: }
186:
187: public void setDbURL(String dbURL) {
188: this .dbURL = dbURL;
189: pool.setDbURL(dbURL);
190: }
191:
192: public String getUser() {
193: return user;
194: }
195:
196: public void setUser(String user) {
197: this .user = user;
198: pool.setUser(user);
199: }
200:
201: public String getPassword() {
202: return password;
203: }
204:
205: public void setPassword(String password) {
206: this .password = password;
207: pool.setPassword(password);
208: }
209:
210: protected void doInit() throws Exception {
211: pool.init();
212: }
213:
214: protected void doDestroy() throws Exception {
215: pool.destroy();
216: }
217: }
|