Clover coverage report -
Coverage timestamp: Thu Jul 5 2007 20:02:32 EDT
file stats: LOC: 109   Methods: 5
NCLOC: 75   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
C3p0ConnectionFactory.java 68.8% 79.3% 100% 78%
coverage coverage
 1    /*
 2    * JBoss, the OpenSource J2EE webOS
 3    *
 4    * Distributable under LGPL license.
 5    * See terms of license at gnu.org.
 6    */
 7    package org.jboss.cache.loader;
 8   
 9    import com.mchange.v2.c3p0.DataSources;
 10    import org.apache.commons.logging.Log;
 11    import org.apache.commons.logging.LogFactory;
 12   
 13    import javax.sql.DataSource;
 14    import java.sql.Connection;
 15    import java.sql.SQLException;
 16    import java.util.Enumeration;
 17    import java.util.Properties;
 18   
 19    /**
 20    * Standalone connection factory based on c3p0 connection pooling library
 21    *
 22    * @author <a href="mailto:galder.zamarreno@jboss.com">Galder Zamarreno</a>
 23    */
 24    public class C3p0ConnectionFactory extends NonManagedConnectionFactory
 25    {
 26    private static final Log log = LogFactory.getLog(C3p0ConnectionFactory.class);
 27   
 28    private DataSource ds;
 29   
 30  65 public void setConfig(AdjListJDBCCacheLoaderConfig config)
 31    {
 32  65 super.setConfig(config);
 33   
 34  65 Properties properties = config.getProperties();
 35  65 Enumeration e = properties.propertyNames();
 36  65 while (e.hasMoreElements())
 37    {
 38  471 String property = (String) e.nextElement();
 39  471 if (property.startsWith("c3p0."))
 40    {
 41    /* System properties should come before settings from XML configuration.
 42   
 43    For simplicity (c3p0 manual says overrides should not carry c3p0. start whereas system properties yes)
 44    and to avoid parsing, it's easier to set the values from XML configuration that should be c3p0. as
 45    system properties.
 46   
 47    So, this check allows us to determine if the System property was not set, in which case, we take the
 48    value from the XML configuration and set it to be a System property. If the value from the XML config
 49    was already set as System property, we do nothing, original System property should stand. */
 50   
 51  2 String xmlPropertyValue = properties.getProperty(property);
 52  2 String sysPropertyValue = System.getProperty(property);
 53  2 if (System.getProperty(property) == null)
 54    {
 55  1 System.setProperty(property, xmlPropertyValue);
 56  1 if (log.isDebugEnabled())
 57    {
 58  0 log.debug("c3p0 property defined in XML: " + property + "=" + xmlPropertyValue);
 59    }
 60    }
 61    else
 62    {
 63  1 if (log.isDebugEnabled())
 64    {
 65  0 log.debug(property + "=" + sysPropertyValue + " defined as system property. It will override the value defined in XML which was: " + xmlPropertyValue);
 66    };
 67    }
 68    }
 69    }
 70    }
 71   
 72  65 public void start() throws Exception
 73    {
 74    /* We need to call super so that the driver is loaded. This is required by the C3P0 manual. */
 75  65 super.start();
 76   
 77  65 DataSource unpooled = DataSources.unpooledDataSource(getUrl(), getUsr(), getPwd());
 78  65 ds = DataSources.pooledDataSource(unpooled);
 79   
 80  65 if (log.isDebugEnabled())
 81    {
 82  0 log.debug("Pooled datasource(url=" + getUrl() + ",usr=" + getUsr() + ",pwd=" + getPwd() + ") started.");
 83    }
 84    }
 85   
 86  19957 public Connection checkoutConnection() throws SQLException
 87    {
 88  19957 Connection connection = ds.getConnection();
 89  0 if (log.isTraceEnabled()) { log.trace("Connection checked out: " + connection); }
 90  19956 return connection;
 91    }
 92   
 93  65 public void stop()
 94    {
 95  65 try
 96    {
 97  65 DataSources.destroy(ds);
 98  0 if (log.isDebugEnabled()) { log.debug("Pooled datasource destroyed."); }
 99    } catch (SQLException sqle)
 100    {
 101  0 log.warn("Could not destroy C3P0 connection pool: " + ds, sqle);
 102    }
 103    }
 104   
 105  1 protected DataSource getDataSource()
 106    {
 107  1 return ds;
 108    }
 109    }