001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.ipc;
019
020import java.util.Objects;
021import org.apache.hadoop.hbase.net.Address;
022import org.apache.hadoop.hbase.security.User;
023import org.apache.yetus.audience.InterfaceAudience;
024
025/**
026 * This class holds the address and the user ticket, etc. The client connections to servers are
027 * uniquely identified by <remoteAddress, ticket, serviceName>
028 */
029@InterfaceAudience.Private
030class ConnectionId {
031  private static final int PRIME = 16777619;
032  final User ticket;
033  final String serviceName;
034  final Address address;
035
036  public ConnectionId(User ticket, String serviceName, Address address) {
037    this.address = address;
038    this.ticket = ticket;
039    this.serviceName = serviceName;
040  }
041
042  public String getServiceName() {
043    return this.serviceName;
044  }
045
046  public Address getAddress() {
047    return address;
048  }
049
050  public User getTicket() {
051    return ticket;
052  }
053
054  @Override
055  public String toString() {
056    return this.address.toString() + "/" + this.serviceName + "/" + this.ticket;
057  }
058
059  @Override
060  @SuppressWarnings("ReferenceEquality")
061  public boolean equals(Object obj) {
062    if (obj == this) {
063      return true;
064    }
065    if (!(obj instanceof ConnectionId)) {
066      return false;
067    }
068    ConnectionId id = (ConnectionId) obj;
069    return address.equals(id.address)
070      && ((ticket != null && ticket.equals(id.ticket)) || (ticket == id.ticket))
071      && Objects.equals(this.serviceName, id.serviceName);
072  }
073
074  @Override // simply use the default Object#hashcode() ?
075  public int hashCode() {
076    return hashCode(ticket, serviceName, address);
077  }
078
079  public static int hashCode(User ticket, String serviceName, Address address) {
080    return (address.hashCode()
081      + PRIME * (PRIME * serviceName.hashCode() ^ (ticket == null ? 0 : ticket.hashCode())));
082  }
083}