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