DataSourceConnectionFactory.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.dbcp2;

  18. import java.sql.Connection;
  19. import java.sql.SQLException;

  20. import javax.sql.DataSource;

  21. /**
  22.  * A {@link DataSource}-based implementation of {@link ConnectionFactory}.
  23.  *
  24.  * @since 2.0
  25.  */
  26. public class DataSourceConnectionFactory implements ConnectionFactory {

  27.     private final DataSource dataSource;

  28.     private final String userName;

  29.     private final char[] userPassword;

  30.     /**
  31.      * Constructs an instance for the given DataSource.
  32.      *
  33.      * @param dataSource
  34.      *            The DataSource for this factory.
  35.      */
  36.     public DataSourceConnectionFactory(final DataSource dataSource) {
  37.         this(dataSource, null, (char[]) null);
  38.     }

  39.     /**
  40.      * Constructs an instance for the given DataSource.
  41.      *
  42.      * @param dataSource
  43.      *            The DataSource for this factory.
  44.      * @param userName
  45.      *            The user name.
  46.      * @param userPassword
  47.      *            The user password.
  48.      * @since 2.4.0
  49.      */
  50.     public DataSourceConnectionFactory(final DataSource dataSource, final String userName, final char[] userPassword) {
  51.         this.dataSource = dataSource;
  52.         this.userName = userName;
  53.         this.userPassword = Utils.clone(userPassword);
  54.     }

  55.     /**
  56.      * Constructs an instance for the given DataSource.
  57.      *
  58.      * @param dataSource
  59.      *            The DataSource for this factory.
  60.      * @param userName
  61.      *            The user name.
  62.      * @param password
  63.      *            The user password.
  64.      */
  65.     public DataSourceConnectionFactory(final DataSource dataSource, final String userName, final String password) {
  66.         this.dataSource = dataSource;
  67.         this.userName = userName;
  68.         this.userPassword = Utils.toCharArray(password);
  69.     }

  70.     @Override
  71.     public Connection createConnection() throws SQLException {
  72.         if (null == userName && null == userPassword) {
  73.             return dataSource.getConnection();
  74.         }
  75.         return dataSource.getConnection(userName, Utils.toString(userPassword));
  76.     }

  77.     /**
  78.      * @return The data source.
  79.      * @since 2.6.0
  80.      */
  81.     public DataSource getDataSource() {
  82.         return dataSource;
  83.     }

  84.     /**
  85.      * @return The user name.
  86.      * @since 2.6.0
  87.      */
  88.     public String getUserName() {
  89.         return userName;
  90.     }

  91.     /**
  92.      * @return The user password.
  93.      * @since 2.6.0
  94.      */
  95.     public char[] getUserPassword() {
  96.         return Utils.clone(userPassword);
  97.     }
  98. }