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.datasources;
18  
19  import java.io.Serializable;
20  import java.util.Objects;
21  
22  import org.apache.commons.pool2.KeyedObjectPool;
23  
24  /**
25   * <p>
26   * Holds a user name and password pair. Serves as a poolable object key for the {@link KeyedObjectPool} backing a
27   * {@link SharedPoolDataSource}. Two instances with the same user name are considered equal. This ensures that there
28   * will be only one keyed pool for each user in the pool. The password is used (along with the user name) by the
29   * {@code KeyedCPDSConnectionFactory} when creating new connections.
30   * </p>
31   *
32   * <p>
33   * {@link InstanceKeyDataSource#getConnection(String, String)} validates that the password used to create a connection
34   * matches the password provided by the client.
35   * </p>
36   *
37   * @since 2.0
38   */
39  final class UserPassKey implements Serializable {
40      private static final long serialVersionUID = 5142970911626584817L;
41  
42      private final CharArray name;
43      private final CharArray password;
44  
45      UserPassKey(final char[] userName, final char[] password) {
46          this(new CharArray(userName), new CharArray(password));
47      }
48  
49      UserPassKey(final CharArray userName, final CharArray userPassword) {
50          this.name = userName;
51          this.password = userPassword;
52      }
53  
54      UserPassKey(final String userName) {
55          this(new CharArray(userName), CharArray.NULL);
56      }
57  
58      UserPassKey(final String userName, final char[] password) {
59          this(new CharArray(userName), new CharArray(password));
60      }
61  
62      UserPassKey(final String userName, final String userPassword) {
63          this(new CharArray(userName), new CharArray(userPassword));
64      }
65  
66      /**
67       * Only takes the user name into account.
68       */
69      @Override
70      public boolean equals(final Object obj) {
71          if (this == obj) {
72              return true;
73          }
74          if (obj == null) {
75              return false;
76          }
77          if (getClass() != obj.getClass()) {
78              return false;
79          }
80          final UserPassKey other = (UserPassKey) obj;
81          return Objects.equals(name, other.name);
82      }
83  
84      /**
85       * Gets the value of password.
86       *
87       * @return value of password.
88       */
89      String getPassword() {
90          return password.asString();
91      }
92  
93      /**
94       * Gets the value of password.
95       *
96       * @return value of password.
97       */
98      char[] getPasswordCharArray() {
99          return password.get();
100     }
101 
102     /**
103      * Gets the value of user name.
104      *
105      * @return value of user name.
106      */
107     String getUserName() {
108         return name.asString();
109     }
110 
111     /**
112      * Only takes the user name into account.
113      */
114     @Override
115     public int hashCode() {
116         return Objects.hash(name);
117     }
118 
119 }