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 boolean equals(Object obj) {
61     if (obj instanceof ConnectionId) {
62       ConnectionId id = (ConnectionId) obj;
63       return address.equals(id.address) &&
64              ((ticket != null && ticket.equals(id.ticket)) ||
65               (ticket == id.ticket)) &&
66               this.serviceName == id.serviceName;
67     }
68     return false;
69    }
70  
71    @Override  // simply use the default Object#hashcode() ?
72    public int hashCode() {
73      return hashCode(ticket,serviceName,address);
74    }
75  
76    public static int hashCode(User ticket, String serviceName, InetSocketAddress address){
77      return (address.hashCode() +
78          PRIME * (PRIME * serviceName.hashCode() ^
79              (ticket == null ? 0 : ticket.hashCode())));
80    }
81  }