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 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 * Only takes the user name into account. 64 */ 65 @Override 66 public boolean equals(final Object obj) { 67 if (this == obj) { 68 return true; 69 } 70 if (obj == null) { 71 return false; 72 } 73 if (getClass() != obj.getClass()) { 74 return false; 75 } 76 final UserPassKey other = (UserPassKey) obj; 77 return Objects.equals(name, other.name); 78 } 79 80 /** 81 * Gets the value of password. 82 * 83 * @return value of password. 84 */ 85 String getPassword() { 86 return password.asString(); 87 } 88 89 /** 90 * Gets the value of password. 91 * 92 * @return value of password. 93 */ 94 char[] getPasswordCharArray() { 95 return password.get(); 96 } 97 98 /** 99 * Gets the value of user name. 100 * 101 * @return value of user name. 102 */ 103 String getUserName() { 104 return name.asString(); 105 } 106 107 /** 108 * Only takes the user name into account. 109 */ 110 @Override 111 public int hashCode() { 112 return Objects.hash(name); 113 } 114 115 }