001: /*
002: * Copyright (c) JForum Team
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms,
006: * with or without modification, are permitted provided
007: * that the following conditions are met:
008: *
009: * 1) Redistributions of source code must retain the above
010: * copyright notice, this list of conditions and the
011: * following disclaimer.
012: * 2) Redistributions in binary form must reproduce the
013: * above copyright notice, this list of conditions and
014: * the following disclaimer in the documentation and/or
015: * other materials provided with the distribution.
016: * 3) Neither the name of "Rafael Steil" nor
017: * the names of its contributors may be used to endorse
018: * or promote products derived from this software without
019: * specific prior written permission.
020: *
021: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
022: * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
023: * EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
024: * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
025: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR
026: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
027: * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
028: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
029: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES
030: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
031: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
032: * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
033: * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
034: * IN CONTRACT, STRICT LIABILITY, OR TORT
035: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
036: * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
037: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
038: *
039: * Created on 30/11/2005 17:07:51
040: * The JForum Project
041: * http://www.jforum.net
042: */
043: package net.jforum;
044:
045: import java.lang.reflect.Method;
046: import java.sql.Connection;
047:
048: import net.jforum.exceptions.DatabaseException;
049: import net.jforum.util.preferences.ConfigKeys;
050: import net.jforum.util.preferences.SystemGlobals;
051:
052: import com.mchange.v2.c3p0.ComboPooledDataSource;
053: import com.mchange.v2.c3p0.DataSources;
054:
055: /**
056: * @author Rafael Steil
057: * @version $Id: C3P0PooledConnection.java,v 1.7 2007/04/12 02:11:52 rafaelsteil Exp $
058: */
059: public class C3P0PooledConnection extends DBConnection {
060: private ComboPooledDataSource ds;
061:
062: /**
063: *
064: * @see net.jforum.DBConnection#init()
065: */
066: public void init() throws Exception {
067: this .ds = new ComboPooledDataSource();
068:
069: this .ds.setDriverClass(SystemGlobals
070: .getValue(ConfigKeys.DATABASE_CONNECTION_DRIVER));
071: this .ds.setJdbcUrl(SystemGlobals
072: .getValue(ConfigKeys.DATABASE_CONNECTION_STRING));
073: this .ds.setMinPoolSize(SystemGlobals
074: .getIntValue(ConfigKeys.DATABASE_POOL_MIN));
075: this .ds.setMaxPoolSize(SystemGlobals
076: .getIntValue(ConfigKeys.DATABASE_POOL_MAX));
077: this .ds.setIdleConnectionTestPeriod(SystemGlobals
078: .getIntValue(ConfigKeys.DATABASE_PING_DELAY));
079:
080: this .extraParams();
081: }
082:
083: private void extraParams() {
084: String extra = SystemGlobals
085: .getValue(ConfigKeys.C3P0_EXTRA_PARAMS);
086:
087: if (extra != null && extra.trim().length() > 0) {
088: String[] p = extra.split(";");
089:
090: for (int i = 0; i < p.length; i++) {
091: String[] kv = p[i].trim().split("=");
092:
093: if (kv.length == 2) {
094: this .invokeSetter(kv[0], kv[1]);
095: }
096: }
097: }
098: }
099:
100: /**
101: * Huge hack to invoke methods without the need of an external configuration file
102: * and whithout knowing the argument's type
103: */
104: private void invokeSetter(String propertyName, String value) {
105: try {
106: String setter = "set"
107: + propertyName.substring(0, 1).toUpperCase()
108: + propertyName.substring(1);
109:
110: Method[] methods = this .ds.getClass().getMethods();
111:
112: for (int i = 0; i < methods.length; i++) {
113: Method method = methods[i];
114:
115: if (method.getName().equals(setter)) {
116: Class[] paramTypes = method.getParameterTypes();
117:
118: if (paramTypes[0] == String.class) {
119: method.invoke(this .ds, new Object[] { value });
120: } else if (paramTypes[0] == int.class) {
121: method.invoke(this .ds,
122: new Object[] { new Integer(value) });
123: } else if (paramTypes[0] == boolean.class) {
124: method.invoke(this .ds, new Object[] { Boolean
125: .valueOf(value) });
126: }
127: }
128: }
129: } catch (Exception e) {
130: e.printStackTrace();
131: }
132: }
133:
134: /**
135: * @see net.jforum.DBConnection#getConnection()
136: */
137: public Connection getConnection() {
138: try {
139: return this .ds.getConnection();
140: } catch (Exception e) {
141: throw new DatabaseException(e);
142: }
143: }
144:
145: /**
146: * @see net.jforum.DBConnection#releaseConnection(java.sql.Connection)
147: */
148: public void releaseConnection(Connection conn) {
149: if (conn == null) {
150: return;
151: }
152:
153: try {
154: conn.close();
155: } catch (Exception e) {
156: e.printStackTrace();
157: }
158: }
159:
160: /**
161: * @see net.jforum.DBConnection#realReleaseAllConnections()
162: */
163: public void realReleaseAllConnections() throws Exception {
164: DataSources.destroy(this.ds);
165: }
166: }
|