Path: blob/master/src/java.naming/share/classes/com/sun/jndi/ldap/SimpleClientId.java
41161 views
/*1* Copyright (c) 2002, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.sun.jndi.ldap;2627import java.util.Arrays; // JDK1.228import java.io.OutputStream;29import javax.naming.ldap.Control;3031/**32* Represents the identity of a 'simple' authenticated LDAP connection.33* In addition to ClientId information, this class contains also the34* username and password.35*36* @author Rosanna Lee37*/38class SimpleClientId extends ClientId {39private final String username;40private final Object passwd;41private final int myHash;4243SimpleClientId(int version, String hostname, int port,44String protocol, Control[] bindCtls, OutputStream trace,45String socketFactory, String username, Object passwd) {4647super(version, hostname, port, protocol, bindCtls, trace,48socketFactory);4950this.username = username;51int pwdHashCode = 0;52if (passwd == null) {53this.passwd = null;54} else if (passwd instanceof byte[]) {55this.passwd = ((byte[])passwd).clone();56pwdHashCode = Arrays.hashCode((byte[])passwd);57} else if (passwd instanceof char[]) {58this.passwd = ((char[])passwd).clone();59pwdHashCode = Arrays.hashCode((char[])passwd);60} else {61this.passwd = passwd;62pwdHashCode = passwd.hashCode();63}6465myHash = super.hashCode()66^ (username != null ? username.hashCode() : 0)67^ pwdHashCode;68}6970public boolean equals(Object obj) {71if (obj == null || !(obj instanceof SimpleClientId)) {72return false;73}7475SimpleClientId other = (SimpleClientId)obj;7677return super.equals(obj)78&& (username == other.username // null OK79|| (username != null && username.equals(other.username)))80&& ((passwd == other.passwd) // null OK81|| (passwd != null && other.passwd != null82&& (((passwd instanceof String) && passwd.equals(other.passwd))83|| ((passwd instanceof byte[])84&& (other.passwd instanceof byte[])85&& Arrays.equals((byte[])passwd, (byte[])other.passwd))86|| ((passwd instanceof char[])87&& (other.passwd instanceof char[])88&& Arrays.equals((char[])passwd, (char[])other.passwd)))));8990}9192public int hashCode() {93return myHash;94}9596public String toString() {97return super.toString() + ":" + username; // omit password for security98}99}100101102