Clover coverage report -
Coverage timestamp: Thu Jul 5 2007 20:02:32 EDT
file stats: LOC: 140   Methods: 10
NCLOC: 94   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
ManagedConnectionFactory.java 50% 69.6% 80% 68.3%
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 org.apache.commons.logging.Log;
 10    import org.apache.commons.logging.LogFactory;
 11   
 12    import javax.naming.InitialContext;
 13    import javax.naming.NamingException;
 14    import javax.sql.DataSource;
 15    import java.sql.Connection;
 16    import java.sql.SQLException;
 17   
 18    /**
 19    * ManagedConnectionFactory for Application Server managed environments
 20    *
 21    * @author <a href="mailto:hmesha@novell.com">Hany Mesha </a>
 22    * @author <a href="mailto:galder.zamarreno@jboss.com">Galder Zamarreno</a>
 23    */
 24    public class ManagedConnectionFactory implements ConnectionFactory
 25    {
 26    private static final Log log = LogFactory.getLog(ManagedConnectionFactory.class);
 27   
 28    private DataSource dataSource;
 29    private String datasourceName;
 30   
 31  63 public void setConfig(AdjListJDBCCacheLoaderConfig config)
 32    {
 33  63 datasourceName = config.getDatasourceName();
 34    }
 35   
 36  63 public void start() throws Exception
 37    {
 38    // A datasource will be registered in JNDI in the start portion of
 39    // its lifecycle, so now that we are in start() we can look it up
 40  63 InitialContext ctx = null;
 41  63 try
 42    {
 43  63 ctx = new InitialContext();
 44  63 dataSource = (DataSource) ctx.lookup(datasourceName);
 45  63 if (log.isTraceEnabled())
 46    {
 47  0 log.trace("Datasource lookup for " + datasourceName + " succeded: " + dataSource);
 48    }
 49    }
 50    catch (NamingException e)
 51    {
 52  0 reportAndRethrowError("Failed to lookup datasource " + datasourceName, e);
 53    }
 54    finally
 55    {
 56  63 if (ctx != null)
 57    {
 58  63 try
 59    {
 60  63 ctx.close();
 61    }
 62    catch (NamingException e)
 63    {
 64  0 log.warn("Failed to close naming context.", e);
 65    }
 66    }
 67    }
 68    }
 69   
 70  19889 public Connection getConnection()
 71    throws SQLException
 72    {
 73  19889 Connection connection = dataSource.getConnection();
 74  19889 if (log.isTraceEnabled())
 75    {
 76  0 log.trace("Connection checked out: " + connection);
 77    }
 78  19889 return connection;
 79    }
 80   
 81  4 public void prepare(Object txId)
 82    {
 83    /* This implementation should be left empty. In a managed environment, we retrieve the datasource from
 84    the application server, and it's down to the configuration of the datasource whether it will participate
 85    in the current transaction, i.e. local-tx-datasource, no-tx-datasource,...etc. The application server will
 86    make sure that if configured, the datasource will participate in the transaction and will commit/rollback
 87    changes if required. Therefore, we don't have to do anything for these transactional methods but leave them
 88    empty. */
 89    }
 90   
 91  4 public void commit(Object txId)
 92    {
 93    /* This implementation should be left empty. In a managed environment, we retrieve the datasource from
 94    the application server, and it's down to the configuration of the datasource whether it will participate
 95    in the current transaction, i.e. local-tx-datasource, no-tx-datasource,...etc. The application server will
 96    make sure that if configured, the datasource will participate in the transaction and will commit/rollback
 97    changes if required. Therefore, we don't have to do anything for these transactional methods but leave them
 98    empty. */
 99    }
 100   
 101  0 public void rollback(Object txId)
 102    {
 103    /* This implementation should be left empty. In a managed environment, we retrieve the datasource from
 104    the application server, and it's down to the configuration of the datasource whether it will participate
 105    in the current transaction, i.e. local-tx-datasource, no-tx-datasource,...etc. The application server will
 106    make sure that if configured, the datasource will participate in the transaction and will commit/rollback
 107    changes if required. Therefore, we don't have to do anything for these transactional methods but leave them
 108    empty. */
 109    }
 110   
 111  19889 public void close(Connection con)
 112    {
 113  19889 safeClose(con);
 114    }
 115   
 116  63 public void stop()
 117    {
 118    }
 119   
 120  19889 private void safeClose(Connection con)
 121    {
 122  19889 if (con != null)
 123    {
 124  19889 try
 125    {
 126  19889 con.close();
 127    }
 128    catch (SQLException e)
 129    {
 130  0 log.warn("Failed to close connection", e);
 131    }
 132    }
 133    }
 134   
 135  0 private void reportAndRethrowError(String message, Exception cause) throws IllegalStateException
 136    {
 137  0 log.error(message, cause);
 138  0 throw new IllegalStateException(message, cause);
 139    }
 140    }