View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.ipc;
19  
20  import org.apache.hadoop.hbase.classification.InterfaceAudience;
21  import org.apache.hadoop.hbase.net.Address;
22  import org.apache.hadoop.hbase.security.User;
23  
24  /**
25   * This class holds the address and the user ticket, etc. The client connections
26   * to servers are uniquely identified by <remoteAddress, ticket, serviceName>
27   */
28  @InterfaceAudience.Private
29  public class ConnectionId {
30    private static final int PRIME = 16777619;
31    final User ticket;
32    final String serviceName;
33    final Address address;
34  
35    public ConnectionId(User ticket, String serviceName, Address address) {
36      this.address = address;
37      this.ticket = ticket;
38      this.serviceName = serviceName;
39    }
40  
41    public String getServiceName() {
42      return this.serviceName;
43    }
44  
45    public Address getAddress() {
46      return address;
47    }
48  
49    public User getTicket() {
50      return ticket;
51    }
52  
53    @Override
54    public String toString() {
55      return this.address + "/" + this.serviceName + "/" + this.ticket;
56    }
57  
58    @Override
59    public int hashCode() {
60      final int prime = 31;
61      int result = 1;
62      result = prime * result + ((address == null) ? 0 : address.hashCode());
63      result = prime * result + ((serviceName == null) ? 0 : serviceName.hashCode());
64      result = prime * result + ((ticket == null) ? 0 : ticket.hashCode());
65      return result;
66    }
67  
68    @Override
69    public boolean equals(Object obj) {
70      if (this == obj) {
71        return true;
72      }
73      if (obj == null) {
74        return false;
75      }
76      if (getClass() != obj.getClass()) {
77        return false;
78      }
79      ConnectionId other = (ConnectionId) obj;
80      if (address == null) {
81        if (other.address != null) {
82          return false;
83        }
84      } else if (!address.equals(other.address)) {
85        return false;
86      }
87      if (serviceName == null) {
88        if (other.serviceName != null) {
89          return false;
90        }
91      } else if (!serviceName.equals(other.serviceName)) {
92        return false;
93      }
94      if (ticket == null) {
95        if (other.ticket != null) {
96          return false;
97        }
98      } else if (!ticket.equals(other.ticket)) {
99        return false;
100     }
101     return true;
102   }
103 
104   public static int hashCode(User ticket, String serviceName, Address address){
105     return (address.hashCode() +
106         PRIME * (PRIME * serviceName.hashCode() ^
107             (ticket == null ? 0 : ticket.hashCode())));
108   }
109 }