Coverage Report - org.apache.commons.dbcp.managed.DataSourceXAConnectionFactory
 
Classes in this File Line Coverage Branch Coverage Complexity
DataSourceXAConnectionFactory
0%
0/23
0%
0/6
1.714
 
 1  
 /**
 2  
  *
 3  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 4  
  * contributor license agreements.  See the NOTICE file distributed with
 5  
  * this work for additional information regarding copyright ownership.
 6  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 7  
  * (the "License"); you may not use this file except in compliance with
 8  
  * the License.  You may obtain a copy of the License at
 9  
  *
 10  
  *     http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  *  Unless required by applicable law or agreed to in writing, software
 13  
  *  distributed under the License is distributed on an "AS IS" BASIS,
 14  
  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15  
  *  See the License for the specific language governing permissions and
 16  
  *  limitations under the License.
 17  
  */
 18  
 package org.apache.commons.dbcp.managed;
 19  
 
 20  
 import javax.sql.XAConnection;
 21  
 import javax.sql.XADataSource;
 22  
 import javax.transaction.TransactionManager;
 23  
 import javax.transaction.xa.XAResource;
 24  
 import java.sql.Connection;
 25  
 import java.sql.SQLException;
 26  
 
 27  
 /**
 28  
  * An implementation of XAConnectionFactory which uses a real XADataSource to obtain connections and XAResources.
 29  
  *
 30  
  * @author Dain Sundstrom
 31  
  * @version $Revision$
 32  
  */
 33  
 public class DataSourceXAConnectionFactory implements XAConnectionFactory {
 34  
     protected TransactionRegistry transactionRegistry;
 35  
     protected XADataSource xaDataSource;
 36  
     protected String username;
 37  
     protected String password;
 38  
 
 39  
     /**
 40  
      * Creates an DataSourceXAConnectionFactory which uses the specified XADataSource to create database
 41  
      * connections.  The connections are enlisted into transactions using the specified transaction manager.
 42  
      *
 43  
      * @param transactionManager the transaction manager in which connections will be enlisted
 44  
      * @param xaDataSource the data source from which connections will be retrieved
 45  
      */
 46  
     public DataSourceXAConnectionFactory(TransactionManager transactionManager, XADataSource xaDataSource) {
 47  0
         this(transactionManager, xaDataSource, null, null);
 48  0
     }
 49  
 
 50  
     /**
 51  
      * Creates an DataSourceXAConnectionFactory which uses the specified XADataSource to create database
 52  
      * connections.  The connections are enlisted into transactions using the specified transaction manager.
 53  
      *
 54  
      * @param transactionManager the transaction manager in which connections will be enlisted
 55  
      * @param xaDataSource the data source from which connections will be retrieved
 56  
      * @param username the username used for authenticating new connections or null for unauthenticated
 57  
      * @param password the password used for authenticating new connections
 58  
      */
 59  0
     public DataSourceXAConnectionFactory(TransactionManager transactionManager, XADataSource xaDataSource, String username, String password) {
 60  0
         if (transactionManager == null) throw new NullPointerException("transactionManager is null");
 61  0
         if (xaDataSource == null) throw new NullPointerException("xaDataSource is null");
 62  
 
 63  0
         this.transactionRegistry = new TransactionRegistry(transactionManager);
 64  0
         this.xaDataSource = xaDataSource;
 65  0
         this.username = username;
 66  0
         this.password = password;
 67  0
     }
 68  
 
 69  
     /**
 70  
      * Gets the username used to authenticate new connections.
 71  
      * @return the user name or null if unauthenticated connections are used
 72  
      */
 73  
     public String getUsername() {
 74  0
         return username;
 75  
     }
 76  
 
 77  
     /**
 78  
      * Sets the username used to authenticate new connections.
 79  
      * @param username the username used for authenticating the connection or null for unauthenticated
 80  
      */
 81  
     public void setUsername(String username) {
 82  0
         this.username = username;
 83  0
     }
 84  
 
 85  
     /**
 86  
      * Sets the password used to authenticate new connections.
 87  
      * @param password the password used for authenticating the connection or null for unauthenticated
 88  
      */
 89  
     public void setPassword(String password) {
 90  0
         this.password = password;
 91  0
     }
 92  
 
 93  
     public TransactionRegistry getTransactionRegistry() {
 94  0
         return transactionRegistry;
 95  
     }
 96  
 
 97  
     public Connection createConnection() throws SQLException {
 98  
         // create a new XAConection
 99  
         XAConnection xaConnection;
 100  0
         if (username == null) {
 101  0
             xaConnection = xaDataSource.getXAConnection();
 102  
         } else {
 103  0
             xaConnection = xaDataSource.getXAConnection(username, password);
 104  
         }
 105  
 
 106  
         // get the real connection and XAResource from the connection
 107  0
         Connection connection = xaConnection.getConnection();
 108  0
         XAResource xaResource = xaConnection.getXAResource();
 109  
 
 110  
         // register the xa resource for the connection
 111  0
         transactionRegistry.registerConnection(connection, xaResource);
 112  
 
 113  0
         return connection;
 114  
     }
 115  
 }