View Javadoc
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  
19  import java.sql.Connection;
20  import java.sql.SQLException;
21  
22  import javax.sql.DataSource;
23  
24  /**
25   * A {@link DataSource}-based implementation of {@link ConnectionFactory}.
26   *
27   * @since 2.0
28   */
29  public class DataSourceConnectionFactory implements ConnectionFactory {
30  
31      private final DataSource dataSource;
32  
33      private final String userName;
34  
35      private final char[] userPassword;
36  
37      /**
38       * Constructs an instance for the given DataSource.
39       *
40       * @param dataSource
41       *            The DataSource for this factory.
42       */
43      public DataSourceConnectionFactory(final DataSource dataSource) {
44          this(dataSource, null, (char[]) null);
45      }
46  
47      /**
48       * Constructs an instance for the given DataSource.
49       *
50       * @param dataSource
51       *            The DataSource for this factory.
52       * @param userName
53       *            The user name.
54       * @param userPassword
55       *            The user password.
56       * @since 2.4.0
57       */
58      public DataSourceConnectionFactory(final DataSource dataSource, final String userName, final char[] userPassword) {
59          this.dataSource = dataSource;
60          this.userName = userName;
61          this.userPassword = Utils.clone(userPassword);
62      }
63  
64      /**
65       * Constructs an instance for the given DataSource.
66       *
67       * @param dataSource
68       *            The DataSource for this factory.
69       * @param userName
70       *            The user name.
71       * @param password
72       *            The user password.
73       */
74      public DataSourceConnectionFactory(final DataSource dataSource, final String userName, final String password) {
75          this.dataSource = dataSource;
76          this.userName = userName;
77          this.userPassword = Utils.toCharArray(password);
78      }
79  
80      @Override
81      public Connection createConnection() throws SQLException {
82          if (null == userName && null == userPassword) {
83              return dataSource.getConnection();
84          }
85          return dataSource.getConnection(userName, Utils.toString(userPassword));
86      }
87  
88      /**
89       * @return The data source.
90       * @since 2.6.0
91       */
92      public DataSource getDataSource() {
93          return dataSource;
94      }
95  
96      /**
97       * @return The user name.
98       * @since 2.6.0
99       */
100     public String getUserName() {
101         return userName;
102     }
103 
104     /**
105      * @return The user password.
106      * @since 2.6.0
107      */
108     public char[] getUserPassword() {
109         return Utils.clone(userPassword);
110     }
111 }