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.security.User;
22  
23  import java.net.InetSocketAddress;
24  
25  /**
26   * This class holds the address and the user ticket, etc. The client connections
27   * to servers are uniquely identified by <remoteAddress, ticket, serviceName>
28   */
29  @InterfaceAudience.Private
30  public class ConnectionId {
31    private static final int PRIME = 16777619;
32    final User ticket;
33    final String serviceName;
34    final InetSocketAddress address;
35  
36    public ConnectionId(User ticket, String serviceName, InetSocketAddress address) {
37      this.address = address;
38      this.ticket = ticket;
39      this.serviceName = serviceName;
40    }
41  
42    public String getServiceName() {
43      return this.serviceName;
44    }
45  
46    public InetSocketAddress getAddress() {
47      return address;
48    }
49  
50    public User getTicket() {
51      return ticket;
52    }
53  
54    @Override
55    public String toString() {
56      return this.address.toString() + "/" + this.serviceName + "/" + this.ticket;
57    }
58  
59    @Override
60    public int hashCode() {
61      final int prime = 31;
62      int result = 1;
63      result = prime * result + ((address == null) ? 0 : address.hashCode());
64      result = prime * result + ((serviceName == null) ? 0 : serviceName.hashCode());
65      result = prime * result + ((ticket == null) ? 0 : ticket.hashCode());
66      return result;
67    }
68  
69    @Override
70    public boolean equals(Object obj) {
71      if (this == obj) {
72        return true;
73      }
74      if (obj == null) {
75        return false;
76      }
77      if (getClass() != obj.getClass()) {
78        return false;
79      }
80      ConnectionId other = (ConnectionId) obj;
81      if (address == null) {
82        if (other.address != null) {
83          return false;
84        }
85      } else if (!address.equals(other.address)) {
86        return false;
87      }
88      if (serviceName == null) {
89        if (other.serviceName != null) {
90          return false;
91        }
92      } else if (!serviceName.equals(other.serviceName)) {
93        return false;
94      }
95      if (ticket == null) {
96        if (other.ticket != null) {
97          return false;
98        }
99      } else if (!ticket.equals(other.ticket)) {
100       return false;
101     }
102     return true;
103   }
104 
105   public static int hashCode(User ticket, String serviceName, InetSocketAddress address){
106     return (address.hashCode() +
107         PRIME * (PRIME * serviceName.hashCode() ^
108             (ticket == null ? 0 : ticket.hashCode())));
109   }
110 }