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    *      https://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   * {@link 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 CharArray userName, final CharArray userPassword) {
46          this.name = userName;
47          this.password = userPassword;
48      }
49  
50      UserPassKey(final String userName) {
51          this(new CharArray(userName), CharArray.NULL);
52      }
53  
54      UserPassKey(final String userName, final char[] password) {
55          this(new CharArray(userName), new CharArray(password));
56      }
57  
58      UserPassKey(final String userName, final String userPassword) {
59          this(new CharArray(userName), new CharArray(userPassword));
60      }
61  
62      /**
63       * Clears the content of the name and password.
64       *
65       * @return {@code this} instance.
66       */
67      UserPassKey clear() {
68          name.clear();
69          password.clear();
70          return this;
71      }
72  
73      /**
74       * Only takes the user name into account.
75       */
76      @Override
77      public boolean equals(final Object obj) {
78          if (this == obj) {
79              return true;
80          }
81          if (obj == null || getClass() != obj.getClass()) {
82              return false;
83          }
84          final UserPassKey other = (UserPassKey) obj;
85          return Objects.equals(name, other.name);
86      }
87  
88      /**
89       * Gets the value of password.
90       *
91       * @return value of password.
92       */
93      String getPassword() {
94          return password.asString();
95      }
96  
97      /**
98       * Gets the value of password.
99       *
100      * @return value of password.
101      */
102     char[] getPasswordCharArray() {
103         return password.get();
104     }
105 
106     /**
107      * Gets the value of user name.
108      *
109      * @return value of user name.
110      */
111     String getUserName() {
112         return name.asString();
113     }
114 
115     /**
116      * Only takes the user name into account.
117      */
118     @Override
119     public int hashCode() {
120         return Objects.hash(name);
121     }
122 
123 }