1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
26
27
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
39
40
41
42
43 public DataSourceConnectionFactory(final DataSource dataSource) {
44 this(dataSource, null, (char[]) null);
45 }
46
47
48
49
50
51
52
53
54
55
56
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
66
67
68
69
70
71
72
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
90
91
92 public DataSource getDataSource() {
93 return dataSource;
94 }
95
96
97
98
99
100 public String getUserName() {
101 return userName;
102 }
103
104
105
106
107
108 public char[] getUserPassword() {
109 return Utils.clone(userPassword);
110 }
111 }