1 /***
2 * @(#)IbatisService.java
3 *
4 * JFoxSOAF, Service-Oriented Application Framework
5 *
6 * Copyright(c) JFoxSOAF Team
7 *
8 * Licensed under the GNU LGPL, Version 2.1 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.gnu.org/copyleft/lesser.html
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 *
20 * For more information, please visit:
21 * http://www.jfox.cn/confluence/display/JFoxSOAF/Home
22 * http://www.huihoo.org/jfox/jfoxsoaf
23 */
24
25 package org.huihoo.jfox.soaf.services.jdbc;
26
27 import java.sql.SQLException;
28 import java.util.Enumeration;
29 import java.util.Hashtable;
30 import java.util.Vector;
31
32 import javax.sql.DataSource;
33
34 import org.apache.commons.lang.BooleanUtils;
35 import org.apache.commons.lang.StringUtils;
36 import org.apache.commons.lang.math.NumberUtils;
37 import org.apache.commons.logging.Log;
38 import org.apache.commons.logging.LogFactory;
39 import org.huihoo.jfox.soaf.exception.DataSourceConfigurationException;
40
41 import com.mchange.v2.c3p0.DataSources;
42 import com.mchange.v2.c3p0.PoolConfig;
43
44 /***
45 * <p>
46 * C3P0 DataSource Factory
47 * </p>
48 *
49 * @author <a href="mailto:founder_chen@yahoo.com.cn">Peter Cheng </a>
50 * @version $Revision: 1.7 $ $Date: 2006/02/15 08:45:45 $
51 * @version Revision: 1.0
52 */
53
54 public class C3P0DataSourceFactory extends DataSourceFactory {
55
56 private final Log logger = LogFactory.getLog(getClass());
57
58 private DataSource dataSource;
59
60
61 private Hashtable attributes = new Hashtable();
62
63 /***
64 * @see org.huihoo.jfox.soaf.services.jdbc.DataSourceFactory#getInstance()
65 * @return DataSource instance
66 * @throws DataSourceConfigurationException
67 */
68 public DataSource getInstance() throws DataSourceConfigurationException,
69 SQLException {
70 String jdbcDriverClass = (String) getAttribute(DatabaseConstant.JDBC_DRIVER_CLASS_NAME);
71 if (StringUtils.isEmpty(jdbcDriverClass)) {
72 logger
73 .warn("No JDBC Driver class was specified by jfoxsoaf-database.properties : "
74 + DatabaseConstant.JDBC_DRIVER_CLASS_NAME);
75 } else {
76 try {
77 Class.forName(jdbcDriverClass);
78 } catch (ClassNotFoundException e) {
79 String errorMsg = "JDBC Driver class not found: "
80 + jdbcDriverClass;
81 logger.fatal(errorMsg);
82 throw new DataSourceConfigurationException(errorMsg, e);
83 }
84 }
85
86
87 PoolConfig poolConfig = new PoolConfig();
88 String initPoolSize = (String) getAttribute(DatabaseConstant.JDBC_INIT_POOL_SIZE);
89
90 if (!NumberUtils.isNumber(initPoolSize)) {
91 poolConfig.setInitialPoolSize(NumberUtils.toInt(initPoolSize));
92 }
93
94 String minPoolSize = (String) getAttribute(DatabaseConstant.JDBC_MIN_POOL_SIZE);
95
96 if (!NumberUtils.isNumber(minPoolSize)) {
97 poolConfig.setMinPoolSize(NumberUtils.toInt(minPoolSize));
98 }
99
100 String maxPoolSize = (String) getAttribute(DatabaseConstant.JDBC_MAX_POOL_SIZE);
101
102 if (!!NumberUtils.isNumber(maxPoolSize)) {
103 poolConfig.setMaxPoolSize(NumberUtils.toInt(maxPoolSize));
104 }
105
106 String maxIdleTime = (String) getAttribute(DatabaseConstant.JDBC_MAX_IDLE_TIME);
107
108 if (!NumberUtils.isNumber(maxIdleTime)) {
109 poolConfig.setMaxIdleTime(NumberUtils.toInt(maxIdleTime));
110 }
111
112 String idleConnTestPeriod = (String) getAttribute(DatabaseConstant.C3P0_JDBC_IDLE_CONN_TEST_PERIOD);
113
114 if (!NumberUtils.isNumber(maxIdleTime)) {
115 poolConfig.setIdleConnectionTestPeriod(NumberUtils
116 .toInt(idleConnTestPeriod));
117 }
118
119 String maxStatements = (String) getAttribute(DatabaseConstant.C3P0_JDBC_MAX_STATEMENTS);
120
121 if (!NumberUtils.isNumber(maxStatements)) {
122 poolConfig.setMaxStatements(NumberUtils.toInt(maxStatements));
123 }
124
125 String propertyCycle = (String) getAttribute(DatabaseConstant.C3P0_JDBC_PROPERTY_CYCLE);
126
127 if (!NumberUtils.isNumber(propertyCycle)) {
128 poolConfig.setPropertyCycle(NumberUtils.toInt(propertyCycle));
129 }
130
131 String acquireIncrement = (String) getAttribute(DatabaseConstant.C3P0_JDBC_ACQUIRE_INCREMENT);
132
133 if (!NumberUtils.isNumber(acquireIncrement)) {
134 poolConfig.setAcquireIncrement(NumberUtils.toInt(acquireIncrement));
135 }
136
137
138
139
140
141 String forceIgnoreUnresolvedTransactions = (String) getAttribute(DatabaseConstant.C3P0_JDBC_FORCE_IGNORE_UNRESOLVED_TRANS);
142
143 if (!StringUtils.isEmpty(forceIgnoreUnresolvedTransactions)) {
144 poolConfig.setForceIgnoreUnresolvedTransactions(BooleanUtils
145 .toBoolean(forceIgnoreUnresolvedTransactions));
146 }
147
148 String numHelperThreads = (String) getAttribute(DatabaseConstant.C3P0_JDBC_NUM_HELPER_THREADS);
149
150 if (!NumberUtils.isNumber(numHelperThreads)) {
151 poolConfig.setNumHelperThreads(NumberUtils.toInt(numHelperThreads));
152 }
153
154 String jdbcURL = (String) getAttribute(DatabaseConstant.JDBC_URL);
155 if (StringUtils.isEmpty(jdbcURL)) {
156 logger.error("No JDBC url was specified by database.properties");
157 throw new DataSourceConfigurationException(
158 "No JDBC url was specified");
159 } else {
160 String jdbcUsername = (String) getAttribute(DatabaseConstant.JDBC_USERNAME);
161 String jdbcPassword = (String) getAttribute(DatabaseConstant.JDBC_PASSWORD);
162
163 DataSource unpooledDataSource = DataSources.unpooledDataSource(
164 jdbcURL, jdbcUsername, jdbcPassword);
165 dataSource = DataSources.pooledDataSource(unpooledDataSource,
166 poolConfig);
167 }
168 return dataSource;
169 }
170
171 /***
172 * Return the configuration attribute with the specified name (if any), or
173 * <code>null</code> if there is no such attribute.
174 *
175 * @param name
176 * Name of the attribute to return
177 * @return attribute
178 */
179 public Object getAttribute(String name) {
180 return attributes.get(name);
181 }
182
183 /***
184 * Return an array containing the names of all currently defined
185 * configuration attributes. If there are no such attributes, a zero length
186 * array is returned.
187 *
188 * @return attribute names
189 */
190 public String[] getAttributeNames() {
191 Vector names = new Vector();
192 Enumeration keys = attributes.keys();
193 while (keys.hasMoreElements()) {
194 names.addElement(keys.nextElement());
195 }
196 String results[] = new String[names.size()];
197 for (int i = 0; i < results.length; i++) {
198 results[i] = (String) names.elementAt(i);
199 }
200 return (results);
201 }
202
203 /***
204 * Set the configuration attribute with the specified name. Calling this
205 * with a <code>null</code> value is equivalent to calling
206 * <code>removeAttribute(name)</code>.
207 *
208 * @param name
209 * Name of the attribute to set
210 * @param value
211 * Value of the attribute to set, or <code>null</code> to
212 * remove any setting for this attribute
213 */
214 public void setAttribute(String name, Object value) {
215 if (value == null) {
216 attributes.remove(name);
217 } else {
218 attributes.put(name, value);
219 }
220 }
221
222 /***
223 * Remove any configuration attribute associated with the specified name. If
224 * there is no such attribute, no action is taken.
225 *
226 * @param name
227 * Name of the attribute to remove
228 */
229 public void removeAttribute(String name) {
230 attributes.remove(name);
231 }
232
233 }