Path: blob/master/src/java.naming/share/classes/com/sun/jndi/ldap/ClientId.java
41161 views
/*1* Copyright (c) 2002, 2014, 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.Locale;28import java.util.Arrays; // JDK 1.229import java.io.OutputStream;30import javax.naming.ldap.Control;31import java.lang.reflect.Method;32import javax.net.SocketFactory;3334/**35* Represents identity information about an anonymous LDAP connection.36* This base class contains the following information:37* - protocol version number38* - server's hostname (case-insensitive)39* - server's port number40* - prototype type (plain or ssl)41* - controls to be sent with the LDAP bind request42*43* All other identity classes must be a subclass of ClientId.44* Identity subclasses would add more distinguishing information, depending45* on the type of authentication that the connection is to have.46*47* The equals() and hashCode() methods of this class and its subclasses are48* important because they are used to determine whether two requests for49* the same connection are identical, and thus whether the same connection50* may be shared. This is especially important for authenticated connections51* because a mistake would result in a serious security violation.52*53* @author Rosanna Lee54*/55class ClientId {56private final int version;57private final String hostname;58private final int port;59private final String protocol;60private final Control[] bindCtls;61private final OutputStream trace;62private final String socketFactory;63private final int myHash;64private final int ctlHash;6566private SocketFactory factory = null;67private Method sockComparator = null;68private boolean isDefaultSockFactory = false;69public static final boolean debug = false;7071ClientId(int version, String hostname, int port, String protocol,72Control[] bindCtls, OutputStream trace, String socketFactory) {73this.version = version;74this.hostname = hostname.toLowerCase(Locale.ENGLISH); // ignore case75this.port = port;76this.protocol = protocol;77this.bindCtls = (bindCtls != null ? bindCtls.clone() : null);78this.trace = trace;79//80// Needed for custom socket factory pooling81//82this.socketFactory = socketFactory;83if ((socketFactory != null) &&84!socketFactory.equals(LdapCtx.DEFAULT_SSL_FACTORY)) {85try {86Class<?> socketFactoryClass =87Obj.helper.loadClass(socketFactory);88this.sockComparator = socketFactoryClass.getMethod(89"compare", new Class<?>[]{Object.class, Object.class});90Method getDefault = socketFactoryClass.getMethod(91"getDefault", new Class<?>[]{});92this.factory =93(SocketFactory)getDefault.invoke(null, new Object[]{});94} catch (Exception e) {95// Ignore it here, the same exceptions are/will be handled by96// LdapPoolManager and Connection classes.97if (debug) {98System.out.println("ClientId received an exception");99e.printStackTrace();100}101}102} else {103isDefaultSockFactory = true;104}105106// The SocketFactory field is not used in the myHash107// computation as there is no right way to compute the hash code108// for this field. There is no harm in skipping it from the hash109// computation110myHash = version + port111+ (trace != null ? trace.hashCode() : 0)112+ (this.hostname != null ? this.hostname.hashCode() : 0)113+ (protocol != null ? protocol.hashCode() : 0)114+ (ctlHash=hashCodeControls(bindCtls));115}116117public boolean equals(Object obj) {118if (!(obj instanceof ClientId)) {119return false;120}121122ClientId other = (ClientId)obj;123124return myHash == other.myHash125&& version == other.version126&& port == other.port127&& trace == other.trace128&& (hostname == other.hostname // null OK129|| (hostname != null && hostname.equals(other.hostname)))130&& (protocol == other.protocol // null OK131|| (protocol != null && protocol.equals(other.protocol)))132&& ctlHash == other.ctlHash133&& (equalsControls(bindCtls, other.bindCtls))134&& (equalsSockFactory(other));135}136137public int hashCode() {138return myHash;139}140141private static int hashCodeControls(Control[] c) {142if (c == null) {143return 0;144}145146int code = 0;147for (int i = 0; i < c.length; i++) {148code = code * 31 + c[i].getID().hashCode();149}150return code;151}152153private static boolean equalsControls(Control[] a, Control[] b) {154if (a == b) {155return true; // both null or same156}157if (a == null || b == null) {158return false; // one is non-null159}160if (a.length != b.length) {161return false;162}163164for (int i = 0; i < a.length; i++) {165if (!a[i].getID().equals(b[i].getID())166|| a[i].isCritical() != b[i].isCritical()167|| !Arrays.equals(a[i].getEncodedValue(),168b[i].getEncodedValue())) {169return false;170}171}172return true;173}174175private boolean equalsSockFactory(ClientId other) {176if (this.isDefaultSockFactory && other.isDefaultSockFactory) {177return true;178}179else if (!other.isDefaultSockFactory) {180return invokeComparator(other, this);181} else {182return invokeComparator(this, other);183}184}185186// delegate the comparison work to the SocketFactory class187// as there is no enough information here, to do the comparison188private boolean invokeComparator(ClientId c1, ClientId c2) {189Object ret;190try {191ret = (c1.sockComparator).invoke(192c1.factory, c1.socketFactory, c2.socketFactory);193} catch(Exception e) {194if (debug) {195System.out.println("ClientId received an exception");196e.printStackTrace();197}198// Failed to invoke the comparator; flag inequality199return false;200}201if (((Integer) ret) == 0) {202return true;203}204return false;205}206207private static String toStringControls(Control[] ctls) {208if (ctls == null) {209return "";210}211StringBuilder str = new StringBuilder();212for (int i = 0; i < ctls.length; i++) {213str.append(ctls[i].getID());214str.append(' ');215}216return str.toString();217}218219public String toString() {220return (hostname + ":" + port + ":" +221(protocol != null ? protocol : "") + ":" +222toStringControls(bindCtls) + ":" +223socketFactory);224}225}226227228