1 /* 2 * Copyright (c) 2002, 2005, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package com.sun.jndi.ldap; 27 28 import java.util.Arrays; // JDK1.2 29 import java.io.OutputStream; 30 import javax.naming.ldap.Control; 31 32 /** 33 * Represents the identity of a 'simple' authenticated LDAP connection. 34 * In addition to ClientId information, this class contains also the 35 * username and password. 36 * 37 * @author Rosanna Lee 38 */ 39 class SimpleClientId extends ClientId { 40 final private String username; 41 final private Object passwd; 42 final private int myHash; 43 44 SimpleClientId(int version, String hostname, int port, 45 String protocol, Control[] bindCtls, OutputStream trace, 46 String socketFactory, String username, Object passwd) { 47 48 super(version, hostname, port, protocol, bindCtls, trace, 49 socketFactory); 50 51 this.username = username; 52 if (passwd == null) { 53 this.passwd = null; 54 } else if (passwd instanceof String) { 55 this.passwd = passwd; 56 } else if (passwd instanceof byte[]) { 57 this.passwd = (byte[]) ((byte[])passwd).clone(); 58 } else if (passwd instanceof char[]) { 59 this.passwd = (char[]) ((char[])passwd).clone(); 60 } else { 61 this.passwd = passwd; 62 } 63 64 myHash = super.hashCode() 65 + (username != null ? username.hashCode() : 0) 66 + (passwd != null ? passwd.hashCode() : 0); 67 } 68 69 public boolean equals(Object obj) { 70 if (obj == null || !(obj instanceof SimpleClientId)) { 71 return false; 72 } 73 74 SimpleClientId other = (SimpleClientId)obj; 75 76 return super.equals(obj) 77 && (username == other.username // null OK 78 || (username != null && username.equals(other.username))) 79 && ((passwd == other.passwd) // null OK 80 || (passwd != null && other.passwd != null 81 && (((passwd instanceof String) && passwd.equals(other.passwd)) 82 || ((passwd instanceof byte[]) 83 && (other.passwd instanceof byte[]) 84 && Arrays.equals((byte[])passwd, (byte[])other.passwd)) 85 || ((passwd instanceof char[]) 86 && (other.passwd instanceof char[]) 87 && Arrays.equals((char[])passwd, (char[])other.passwd))))); 88 89 } 90 91 public int hashCode() { 92 return myHash; 93 } 94 95 public String toString() { 96 return super.toString() + ":" + username; // omit password for security 97 } 98 }