Clover coverage report - Ledge LGPL - SNAPSHOT
Coverage timestamp: Fri Nov 17 2006 05:23:51 CET
file stats: LOC: 250   Methods: 6
NCLOC: 160   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
XaPoolDataSource.java 92.9% 95.7% 66.7% 93.3%
coverage coverage
 1    //
 2    // Copyright (c) 2003, Caltha - Gajda, Krzewski, Mach, Potempski Sp.J.
 3    // All rights reserved.
 4    //
 5    // Redistribution and use in source and binary forms, with or without modification,
 6    // are permitted provided that the following conditions are met:
 7    //
 8    // * Redistributions of source code must retain the above copyright notice,
 9    // this list of conditions and the following disclaimer.
 10    // * Redistributions in binary form must reproduce the above copyright notice,
 11    // this list of conditions and the following disclaimer in the documentation
 12    // and/or other materials provided with the distribution.
 13    // * Neither the name of the Caltha - Gajda, Krzewski, Mach, Potempski Sp.J.
 14    // nor the names of its contributors may be used to endorse or promote products
 15    // derived from this software without specific prior written permission.
 16    //
 17    // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 18    // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 19    // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 20    // IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 21    // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 22    // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
 23    // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 24    // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 25    // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 26    // POSSIBILITY OF SUCH DAMAGE.
 27    //
 28    package org.objectledge.database;
 29   
 30    import java.sql.SQLException;
 31   
 32    import javax.sql.DataSource;
 33    import javax.sql.XAConnection;
 34    import javax.sql.XADataSource;
 35   
 36    import org.enhydra.jdbc.core.CoreDataSource;
 37    import org.enhydra.jdbc.pool.StandardXAPoolDataSource;
 38    import org.enhydra.jdbc.standard.StandardXADataSource;
 39    import org.jcontainer.dna.Configuration;
 40    import org.jcontainer.dna.ConfigurationException;
 41    import org.objectledge.database.impl.DelegatingDataSource;
 42    import org.objectledge.logging.LoggingConfigurator;
 43    import org.picocontainer.Startable;
 44   
 45    /**
 46    * An implementation of DataSource interface using HSQLDB.
 47    *
 48    * @author <a href="mailto:rafal@caltha.pl">Rafal Krzewski</a>
 49    * @version $Id: XaPoolDataSource.java,v 1.3 2005/02/10 17:46:08 rafal Exp $
 50    */
 51    public class XaPoolDataSource extends DelegatingDataSource
 52    implements XADataSource, Startable
 53    {
 54    private Transaction transaction;
 55    private Configuration config;
 56   
 57    /**
 58    * Constructs a DataSource instance.
 59    *
 60    * @param transaction transaction manager wrapper.
 61    * @param config the data source configuration.
 62    * @param loggingConfigurator enforces instantiation order on Pico, may be null.
 63    * @throws ConfigurationException if the configuration is invalid.
 64    * @throws SQLException if the pool could not be initialized.
 65    */
 66  688 public XaPoolDataSource(Transaction transaction, Configuration config, LoggingConfigurator
 67    loggingConfigurator)
 68    throws ConfigurationException, SQLException
 69    {
 70  688 super(getDataSource(transaction, config));
 71  688 this.transaction = transaction;
 72  688 this.config = config;
 73    }
 74   
 75    /**
 76    * {@inheritDoc}
 77    */
 78  0 public void start()
 79    {
 80    // I wish Startable interface was split
 81    }
 82   
 83    /**
 84    * Forcibly terminates all connections in the pool.
 85    */
 86  688 public void stop()
 87    {
 88  688 ((CoreDataSource)getDelegate()).shutdown(true);
 89    }
 90   
 91    /**
 92    * Initializes the pool.
 93    *
 94    * @param transaction transaction manager wrapper.
 95    * @param config the data source configuration.
 96    * @return configured deleate data source.
 97    * @throws ConfigurationException if the configuration is invalid.
 98    * @throws SQLException if the pool could not be initialized.
 99    */
 100  688 private static DataSource getDataSource(Transaction transaction, Configuration config)
 101    throws ConfigurationException, SQLException
 102    {
 103  688 StandardXADataSource xaDataSource = new StandardXADataSource();
 104  688 xaDataSource.setTransactionManager(transaction.getTransactionManager());
 105   
 106  688 Configuration connectionConfig = config.getChild("connection");
 107  688 String driverName = connectionConfig.getChild("driver").getValue();
 108  688 String url = connectionConfig.getChild("url").getValue();
 109  688 String user = connectionConfig.getChild("user").getValue(null);
 110  688 String password = connectionConfig.getChild("password").getValue(null);
 111   
 112  688 int loginTimeout = connectionConfig.getChild("login-timeout").getValueAsInteger(-1);
 113  688 int transactionIsolation = connectionConfig.
 114    getChild("transaction-isolation").getValueAsInteger(-1);
 115  688 int preparedStmtCacheSize = connectionConfig.
 116    getChild("prepared-statement-cache-size").getValueAsInteger(-1);
 117   
 118  688 Configuration deadlockConfig = config.getChild("deadlock");
 119  688 long deadlockMaxWait = deadlockConfig.getChild("max-wait").getValueAsInteger(-1);
 120  688 long deadlockRetryWait = deadlockConfig.getChild("retry-wait").getValueAsInteger(-1);
 121   
 122    // CoreDataSource
 123  688 if(user != null)
 124    {
 125  688 xaDataSource.setUser(user);
 126    }
 127  688 if(password != null)
 128    {
 129  86 xaDataSource.setPassword(password);
 130    }
 131  688 if(loginTimeout != -1)
 132    {
 133  86 xaDataSource.setLoginTimeout(loginTimeout);
 134    }
 135    // StandardDataSource
 136  688 xaDataSource.setDriverName(driverName);
 137  688 xaDataSource.setUrl(url);
 138  688 if(transactionIsolation != -1)
 139    {
 140  86 xaDataSource.setTransactionIsolation(transactionIsolation);
 141    }
 142    // StandardConnectionPoolDataSource
 143  688 if(preparedStmtCacheSize != -1)
 144    {
 145  86 xaDataSource.setPreparedStmtCacheSize(preparedStmtCacheSize);
 146    }
 147    // StandardXADataSource
 148  688 if(deadlockMaxWait != -1)
 149    {
 150  86 xaDataSource.setDeadLockMaxWait(deadlockMaxWait);
 151    }
 152  688 if(deadlockRetryWait != -1)
 153    {
 154  86 xaDataSource.setDeadLockRetryWait(deadlockRetryWait);
 155    }
 156  688 xaDataSource.setTransactionManager(transaction.getTransactionManager());
 157   
 158    // pooling //////////////////////////////////////////////////////////////////////////////
 159   
 160  688 Configuration poolConfig = config.getChild("pool");
 161   
 162  688 Configuration minSizeConfig = poolConfig.getChild("capacity").getChild("min");
 163  688 int minSize = minSizeConfig.getValueAsInteger(-1);
 164  688 Configuration maxSizeConfig = poolConfig.getChild("capacity")
 165    .getChild("max");
 166  688 int maxSize = maxSizeConfig.getValueAsInteger(-1);
 167  688 boolean gc = poolConfig.getChild("cleanup").
 168    getChild("gc").getValueAsBoolean(false);
 169  688 int lifeTime = poolConfig.getChild("cleanup").
 170    getChild("unused-life-time").getValueAsInteger(-1);
 171  688 int sleepTime = poolConfig.getChild("cleanup").
 172    getChild("interval").getValueAsInteger(-1);
 173  688 int checkLevel = poolConfig.getChild("checking").
 174    getChild("level").getValueAsInteger(0);
 175  688 String jdbcTestStatement = poolConfig.getChild("checking").
 176    getChild("statement").getValue(null);
 177   
 178  688 StandardXAPoolDataSource xaPoolDataSource = new StandardXAPoolDataSource(xaDataSource);
 179  688 xaPoolDataSource.setTransactionManager(transaction.getTransactionManager());
 180  688 xaPoolDataSource.setDataSource(xaDataSource);
 181   
 182    // CoreDataSource
 183  688 if(user != null)
 184    {
 185  688 xaPoolDataSource.setUser(user);
 186    }
 187  688 if(password != null)
 188    {
 189  86 xaPoolDataSource.setPassword(password);
 190    }
 191    // StandardPoolDataSource
 192  688 if(minSize != -1)
 193    {
 194  86 try
 195    {
 196  86 xaPoolDataSource.setMinSize(minSize);
 197    }
 198    catch(Exception e)
 199    {
 200  0 throw new ConfigurationException("illegal minimum pool size",
 201    minSizeConfig.getPath(), minSizeConfig.getLocation(), e);
 202    }
 203    }
 204  688 if(maxSize != -1)
 205    {
 206  86 try
 207    {
 208  86 xaPoolDataSource.setMaxSize(maxSize);
 209    }
 210    catch(Exception e)
 211    {
 212  0 throw new ConfigurationException("illegal maximum pool size",
 213    maxSizeConfig.getPath(), maxSizeConfig.getLocation(), e);
 214    }
 215    }
 216  688 xaPoolDataSource.setGC(gc);
 217  688 if(lifeTime != -1)
 218    {
 219  86 xaPoolDataSource.setLifeTime(lifeTime);
 220    }
 221  688 if(sleepTime != -1)
 222    {
 223  86 xaPoolDataSource.setLifeTime(sleepTime);
 224    }
 225  688 xaPoolDataSource.setCheckLevelObject(checkLevel);
 226  688 if(jdbcTestStatement != null)
 227    {
 228  86 xaPoolDataSource.setJdbcTestStmt(jdbcTestStatement);
 229    }
 230  688 return xaPoolDataSource;
 231    }
 232   
 233    /**
 234    * {@inheritDoc}
 235    */
 236  86 public XAConnection getXAConnection()
 237    throws SQLException
 238    {
 239  86 return ((XADataSource)getDelegate()).getXAConnection();
 240    }
 241   
 242    /**
 243    * {@inheritDoc}
 244    */
 245  0 public XAConnection getXAConnection(String user, String password)
 246    throws SQLException
 247    {
 248  0 return ((XADataSource)getDelegate()).getXAConnection(user, password);
 249    }
 250    }