Clover coverage report - JFox Service-Oriented Application Framework - 1.0-RC2
Coverage timestamp: 星期四 十二月 15 2005 11:58:01 CST
file stats: LOC: 233   Methods: 5
NCLOC: 118   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%
coverage coverage
 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  86
     public DataSource getInstance() throws DataSourceConfigurationException,
 69   
             SQLException {
 70  86
         String jdbcDriverClass = (String) getAttribute(DatabaseConstant.JDBC_DRIVER_CLASS_NAME);
 71  86
         if (StringUtils.isEmpty(jdbcDriverClass)) {
 72  0
             logger
 73   
                     .warn("No JDBC Driver class was specified by jfoxsoaf-database.properties : "
 74   
                             + DatabaseConstant.JDBC_DRIVER_CLASS_NAME);
 75   
         } else {
 76  86
             try {
 77  86
                 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  86
         PoolConfig poolConfig = new PoolConfig();
 88  86
         String initPoolSize = (String) getAttribute(DatabaseConstant.JDBC_INIT_POOL_SIZE);
 89   
 
 90  86
         if (!NumberUtils.isNumber(initPoolSize)) {
 91  0
             poolConfig.setInitialPoolSize(NumberUtils.toInt(initPoolSize));
 92   
         }
 93   
 
 94  86
         String minPoolSize = (String) getAttribute(DatabaseConstant.JDBC_MIN_POOL_SIZE);
 95   
 
 96  86
         if (!NumberUtils.isNumber(minPoolSize)) {
 97  0
             poolConfig.setMinPoolSize(NumberUtils.toInt(minPoolSize));
 98   
         }
 99   
 
 100  86
         String maxPoolSize = (String) getAttribute(DatabaseConstant.JDBC_MAX_POOL_SIZE);
 101   
 
 102  86
         if (!!NumberUtils.isNumber(maxPoolSize)) {
 103  86
             poolConfig.setMaxPoolSize(NumberUtils.toInt(maxPoolSize));
 104   
         }
 105   
 
 106  86
         String maxIdleTime = (String) getAttribute(DatabaseConstant.JDBC_MAX_IDLE_TIME);
 107   
 
 108  86
         if (!NumberUtils.isNumber(maxIdleTime)) {
 109  0
             poolConfig.setMaxIdleTime(NumberUtils.toInt(maxIdleTime));
 110   
         }
 111   
 
 112  86
         String idleConnTestPeriod = (String) getAttribute(DatabaseConstant.C3P0_JDBC_IDLE_CONN_TEST_PERIOD);
 113   
 
 114  86
         if (!NumberUtils.isNumber(maxIdleTime)) {
 115  0
             poolConfig.setIdleConnectionTestPeriod(NumberUtils
 116   
                     .toInt(idleConnTestPeriod));
 117   
         }
 118   
 
 119  86
         String maxStatements = (String) getAttribute(DatabaseConstant.C3P0_JDBC_MAX_STATEMENTS);
 120   
 
 121  86
         if (!NumberUtils.isNumber(maxStatements)) {
 122  0
             poolConfig.setMaxStatements(NumberUtils.toInt(maxStatements));
 123   
         }
 124   
 
 125  86
         String propertyCycle = (String) getAttribute(DatabaseConstant.C3P0_JDBC_PROPERTY_CYCLE);
 126   
 
 127  86
         if (!NumberUtils.isNumber(propertyCycle)) {
 128  0
             poolConfig.setPropertyCycle(NumberUtils.toInt(propertyCycle));
 129   
         }
 130   
 
 131  86
         String acquireIncrement = (String) getAttribute(DatabaseConstant.C3P0_JDBC_ACQUIRE_INCREMENT);
 132   
 
 133  86
         if (!NumberUtils.isNumber(acquireIncrement)) {
 134  0
             poolConfig.setAcquireIncrement(NumberUtils.toInt(acquireIncrement));
 135   
         }
 136   
 
 137   
         // c3p0 version is below 0.8.5 , does't support acquireRetryAttempts,
 138   
         // acquireRetryDelay, breakAfterAcquireFailure,
 139   
         // usesTraditionalReflectiveProxies
 140   
 
 141  86
         String forceIgnoreUnresolvedTransactions = (String) getAttribute(DatabaseConstant.C3P0_JDBC_FORCE_IGNORE_UNRESOLVED_TRANS);
 142   
 
 143  86
         if (!StringUtils.isEmpty(forceIgnoreUnresolvedTransactions)) {
 144  86
             poolConfig.setForceIgnoreUnresolvedTransactions(BooleanUtils
 145   
                     .toBoolean(forceIgnoreUnresolvedTransactions));
 146   
         }
 147   
 
 148  86
         String numHelperThreads = (String) getAttribute(DatabaseConstant.C3P0_JDBC_NUM_HELPER_THREADS);
 149   
 
 150  86
         if (!NumberUtils.isNumber(numHelperThreads)) {
 151  0
             poolConfig.setNumHelperThreads(NumberUtils.toInt(numHelperThreads));
 152   
         }
 153   
 
 154  86
         String jdbcURL = (String) getAttribute(DatabaseConstant.JDBC_URL);
 155  86
         if (StringUtils.isEmpty(jdbcURL)) {
 156  0
             logger.error("No JDBC url was specified by database.properties");
 157  0
             throw new DataSourceConfigurationException(
 158   
                     "No JDBC url was specified");
 159   
         } else {
 160  86
             String jdbcUsername = (String) getAttribute(DatabaseConstant.JDBC_USERNAME);
 161  86
             String jdbcPassword = (String) getAttribute(DatabaseConstant.JDBC_PASSWORD);
 162   
 
 163  86
             DataSource unpooledDataSource = DataSources.unpooledDataSource(
 164   
                     jdbcURL, jdbcUsername, jdbcPassword);
 165  86
             dataSource = DataSources.pooledDataSource(unpooledDataSource,
 166   
                     poolConfig);
 167   
         }
 168  86
         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  1207
     public Object getAttribute(String name) {
 180  1207
         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  0
     public String[] getAttributeNames() {
 191  0
         Vector names = new Vector();
 192  0
         Enumeration keys = attributes.keys();
 193  0
         while (keys.hasMoreElements()) {
 194  0
             names.addElement(keys.nextElement());
 195   
         }
 196  0
         String results[] = new String[names.size()];
 197  0
         for (int i = 0; i < results.length; i++) {
 198  0
             results[i] = (String) names.elementAt(i);
 199   
         }
 200  0
         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  82
     public void setAttribute(String name, Object value) {
 215  82
         if (value == null) {
 216  0
             attributes.remove(name);
 217   
         } else {
 218  82
             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  1
     public void removeAttribute(String name) {
 230  1
         attributes.remove(name);
 231   
     }
 232   
 
 233   
 }