001: /* JFox, the OpenSource J2EE Application Server
002: *
003: * Copyright (C) 2002 huihoo.com
004: * Distributable under GNU LGPL license
005: * See the GNU Lesser General Public License for more details.
006: */
007:
008: package org.huihoo.jfox.pool.connection;
009:
010: import java.lang.reflect.InvocationHandler;
011: import java.lang.reflect.Proxy;
012: import java.sql.Connection;
013: import java.sql.DriverManager;
014:
015: import org.huihoo.jfox.pool.ProxyObjectFactory;
016: import org.huihoo.jfox.pool.PoolableObject;
017:
018: /**
019: *
020: * @author <a href="mailto:kelvin_wym@hotmail.com">Kelvin Wu</a>
021: */
022:
023: public class ConnectionFactory extends ProxyObjectFactory {
024: private String dbDriver = null; // database dirver class name
025: private String dbURL = null; // database url
026: private String user = null; // database user name.
027: private String password = null; // database password
028:
029: public ConnectionFactory(Class classType, Class invokerClass)
030: throws Exception {
031: super (classType, invokerClass);
032: }
033:
034: public PoolableObject makeObject() throws Exception {
035: // 建立实际的连接
036: Class.forName(dbDriver);
037: // System.out.println("dbURL: " + dbURL);
038: Connection conn = DriverManager.getConnection(dbURL, user,
039: password);
040:
041: // 生成 PoolableConnection
042: PoolableConnection pconn = (PoolableConnection) classType
043: .getConstructor(new Class[] { Connection.class })
044: .newInstance(new Object[] { conn });
045:
046: // 生成 PoolableConnectionInvocationHandler
047: InvocationHandler handler = (InvocationHandler) invokerClass
048: .getConstructor(new Class[] { PoolableObject.class })
049: .newInstance(new Object[] { pconn });
050:
051: return (PoolableObject) Proxy.newProxyInstance(Thread
052: .currentThread().getContextClassLoader(), new Class[] {
053: Connection.class, PoolableConnection.class }, handler);
054:
055: }
056:
057: /**
058: * destroy the poolConnection in pool.
059: * @param object
060: * @throws Exception
061: */
062: public void destroyObject(PoolableObject object) throws Exception {
063: // System.out.println("ConnectionFactory.destroyObject");
064: ((org.huihoo.jfox.pool.connection.PoolableConnection) object)
065: .reallyClose();
066: object.passivate();
067: super .destroyObject(object);
068: }
069:
070: /**
071: * Set database driver name.
072: * @param dbDriver
073: */
074: void setDbDriver(String dbDriver) {
075: this .dbDriver = dbDriver;
076: }
077:
078: /**
079: * Set database url
080: * @param dbURL
081: */
082: void setDbURL(String dbURL) {
083: this .dbURL = dbURL;
084: }
085:
086: /**
087: * Set user name.
088: * @param user
089: */
090: void setUser(String user) {
091: this .user = user;
092: }
093:
094: /**
095: * Set password.
096: * @param password
097: */
098: void setPassword(String password) {
099: this .password = password;
100: }
101:
102: String getDbDriver() {
103: return dbDriver;
104: }
105:
106: String getDbURL() {
107: return dbURL;
108: }
109:
110: String getUser() {
111: return user;
112: }
113:
114: String getPassword() {
115: return password;
116: }
117: }
|