Clover coverage report - JFox Service-Oriented Application Framework - 1.0-RC1 Coverage timestamp: 星期五 八月 19 2005 13:21:55 CST FRAMES NO FRAMES |
file stats: | LOC: | 232 |  | Methods: | 5
| NCLOC: | 121 |  | Classes: | 1
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
Source file | Conditionals | Statements | Methods | TOTAL
C3P0DataSourceFactory.java | 43.3% | 62.3% | 80% | 57.3% |

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.6 $ $Date: 2005/05/22 06:49:48 $
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 |  |
         //  The configuration attributes for this {@link DataSourceFactory}.
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 | 4 |
         public DataSource getInstance() throws DataSourceConfigurationException,
69 |  |
                 SQLException {
70 | 4 |
             String jdbcDriverClass = (String) getAttribute(DatabaseConstant.JDBC_DRIVER_CLASS_NAME);
71 | 4 |
             if (StringUtils.isEmpty(jdbcDriverClass)) {
72 | 0 |
                 logger
73 |  |
                         .warn("No JDBC Driver class was specified by database.properties : "
74 |  |
                                 + DatabaseConstant.JDBC_DRIVER_CLASS_NAME);
75 |  |
             } else {
76 | 4 |
                 try {
77 | 4 |
                     Class.forName(jdbcDriverClass);
78 |  |
                 } catch (ClassNotFoundException e) {
79 | 0 |
                     String errorMsg = "JDBC Driver class not found: "
80 |  |
                             + jdbcDriverClass;
81 | 0 |
                     logger.fatal(errorMsg);
82 | 0 |
                     throw new DataSourceConfigurationException(errorMsg, e);
83 |  |
                 }
84 |  |
             }
85 |  |
86 |  |
             // Set pool configuration
87 | 4 |
             PoolConfig poolConfig = new PoolConfig();
88 | 4 |
             String initPoolSize = (String) getAttribute(DatabaseConstant.JDBC_INIT_POOL_SIZE);
89 |  |
90 | 4 |
             if (!NumberUtils.isNumber(initPoolSize)) {
91 | 0 |
                 poolConfig
92 |  |
                         .setInitialPoolSize(NumberUtils.stringToInt(initPoolSize));
93 |  |
             }
94 |  |
95 | 4 |
             String minPoolSize = (String) getAttribute(DatabaseConstant.JDBC_MIN_POOL_SIZE);
96 |  |
97 | 4 |
             if (!NumberUtils.isNumber(minPoolSize)) {
98 | 0 |
                 poolConfig.setMinPoolSize(NumberUtils.stringToInt(minPoolSize));
99 |  |
             }
100 |  |
101 | 4 |
             String maxPoolSize = (String) getAttribute(DatabaseConstant.JDBC_MAX_POOL_SIZE);
102 |  |
103 | 4 |
             if (!!NumberUtils.isNumber(maxPoolSize)) {
104 | 4 |
                 poolConfig.setMaxPoolSize(NumberUtils.stringToInt(maxPoolSize));
105 |  |
             }
106 |  |
107 | 4 |
             String maxIdleTime = (String) getAttribute(DatabaseConstant.JDBC_MAX_IDLE_TIME);
108 |  |
109 | 4 |
             if (!NumberUtils.isNumber(maxIdleTime)) {
110 | 0 |
                 poolConfig.setMaxIdleTime(NumberUtils.stringToInt(maxIdleTime));
111 |  |
             }
112 |  |
113 | 4 |
             String idleConnTestPeriod = (String) getAttribute(DatabaseConstant.C3P0_JDBC_IDLE_CONN_TEST_PERIOD);
114 |  |
115 | 4 |
             if (!NumberUtils.isNumber(maxIdleTime)) {
116 | 0 |
                 poolConfig.setIdleConnectionTestPeriod(NumberUtils
117 |  |
                         .stringToInt(idleConnTestPeriod));
118 |  |
             }
119 |  |
120 | 4 |
             String maxStatements = (String) getAttribute(DatabaseConstant.C3P0_JDBC_MAX_STATEMENTS);
121 |  |
122 | 4 |
             if (!NumberUtils.isNumber(maxStatements)) {
123 | 0 |
                 poolConfig.setMaxStatements(NumberUtils.stringToInt(maxStatements));
124 |  |
             }
125 |  |
126 | 4 |
             String propertyCycle = (String) getAttribute(DatabaseConstant.C3P0_JDBC_PROPERTY_CYCLE);
127 |  |
128 | 4 |
             if (!NumberUtils.isNumber(propertyCycle)) {
129 | 0 |
                 poolConfig.setPropertyCycle(NumberUtils.stringToInt(propertyCycle));
130 |  |
             }
131 |  |
132 | 4 |
             String acquireIncrement = (String) getAttribute(DatabaseConstant.C3P0_JDBC_ACQUIRE_INCREMENT);
133 |  |
134 | 4 |
             if (!NumberUtils.isNumber(acquireIncrement)) {
135 | 0 |
                 poolConfig.setAcquireIncrement(NumberUtils
136 |  |
                         .stringToInt(acquireIncrement));
137 |  |
             }
138 |  |
139 |  |
             // c3p0 version is below 0.8.5 , does't support acquireRetryAttempts,
140 |  |
             // acquireRetryDelay, breakAfterAcquireFailure,
141 |  |
             // usesTraditionalReflectiveProxies
142 |  |
143 | 4 |
             String forceIgnoreUnresolvedTransactions = (String) getAttribute(DatabaseConstant.C3P0_JDBC_FORCE_IGNORE_UNRESOLVED_TRANS);
144 |  |
145 | 4 |
             if (!StringUtils.isEmpty(forceIgnoreUnresolvedTransactions)) {
146 | 4 |
                 poolConfig.setForceIgnoreUnresolvedTransactions(BooleanUtils
147 |  |
                         .toBoolean(forceIgnoreUnresolvedTransactions));
148 |  |
             }
149 |  |
150 | 4 |
             String numHelperThreads = (String) getAttribute(DatabaseConstant.C3P0_JDBC_NUM_HELPER_THREADS);
151 |  |
152 | 4 |
             if (!NumberUtils.isNumber(numHelperThreads)) {
153 | 0 |
                 poolConfig.setNumHelperThreads(NumberUtils
154 |  |
                         .stringToInt(numHelperThreads));
155 |  |
             }
156 |  |
157 | 4 |
             String jdbcURL = (String) getAttribute(DatabaseConstant.JDBC_URL);
158 | 4 |
             if (StringUtils.isEmpty(jdbcURL)) {
159 | 0 |
                 logger.error("No JDBC url was specified by database.properties");
160 | 0 |
                 throw new DataSourceConfigurationException(
161 |  |
                         "No JDBC url was specified");
162 |  |
             } else {
163 | 4 |
                 String jdbcUsername = (String) getAttribute(DatabaseConstant.JDBC_USERNAME);
164 | 4 |
                 String jdbcPassword = (String) getAttribute(DatabaseConstant.JDBC_PASSWORD);
165 |  |
166 | 4 |
                 DataSource unpooledDataSource = DataSources.unpooledDataSource(
167 |  |
                         jdbcURL, jdbcUsername, jdbcPassword);
168 | 4 |
                 dataSource = DataSources.pooledDataSource(unpooledDataSource,
169 |  |
                         poolConfig);
170 |  |
             }
171 | 4 |
             return dataSource;
172 |  |
         }
173 |  |
174 |  |
         /**
175 |  |
          * Return the configuration attribute with the specified name (if any), or
176 |  |
          * <code>null</code> if there is no such attribute.
177 |  |
          *
178 |  |
          * @param name Name of the attribute to return
179 |  |
          * @return attribute
180 |  |
          */
181 | 59 |
         public Object getAttribute(String name) {
182 | 59 |
             return attributes.get(name);
183 |  |
         }
184 |  |
185 |  |
         /**
186 |  |
          * Return an array containing the names of all currently defined
187 |  |
          * configuration attributes. If there are no such attributes, a zero length
188 |  |
          * array is returned.
189 |  |
          *
190 |  |
          * @return attribute names
191 |  |
          */
192 | 0 |
         public String[] getAttributeNames() {
193 | 0 |
             Vector names = new Vector();
194 | 0 |
             Enumeration keys = attributes.keys();
195 | 0 |
             while (keys.hasMoreElements()) {
196 | 0 |
                 names.addElement(keys.nextElement());
197 |  |
             }
198 | 0 |
             String results[] = new String[names.size()];
199 | 0 |
             for (int i = 0; i < results.length; i++) {
200 | 0 |
                 results[i] = (String) names.elementAt(i);
201 |  |
             }
202 | 0 |
             return (results);
203 |  |
         }
204 |  |
205 |  |
         /**
206 |  |
          * Set the configuration attribute with the specified name. Calling this
207 |  |
          * with a <code>null</code> value is equivalent to calling
208 |  |
          * <code>removeAttribute(name)</code>.
209 |  |
          *
210 |  |
          * @param name Name of the attribute to set
211 |  |
          * @param value Value of the attribute to set, or <code>null</code> to
212 |  |
          *            remove any setting for this attribute
213 |  |
          */
214 | 62 |
         public void setAttribute(String name, Object value) {
215 | 62 |
             if (value == null) {
216 | 0 |
                 attributes.remove(name);
217 |  |
             } else {
218 | 62 |
                 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 Name of the attribute to remove
227 |  |
          */
228 | 1 |
         public void removeAttribute(String name) {
229 | 1 |
             attributes.remove(name);
230 |  |
         }
231 |  |
232 |  |
     }