View Javadoc

1   /**
2    * Copyright The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  package org.apache.hadoop.hbase.client;
21  
22  import java.io.IOException;
23  import java.util.Collections;
24  import java.util.HashMap;
25  import java.util.Map;
26  
27  import org.apache.hadoop.conf.Configuration;
28  import org.apache.hadoop.hbase.HConstants;
29  import org.apache.hadoop.hbase.security.User;
30  
31  /**
32   * Denotes a unique key to an {@link HConnection} instance.
33   *
34   * In essence, this class captures the properties in {@link Configuration}
35   * that may be used in the process of establishing a connection. In light of
36   * that, if any new such properties are introduced into the mix, they must be
37   * added to the {@link HConnectionKey#properties} list.
38   *
39   */
40  class HConnectionKey {
41    final static String[] CONNECTION_PROPERTIES = new String[] {
42        HConstants.ZOOKEEPER_QUORUM, HConstants.ZOOKEEPER_ZNODE_PARENT,
43        HConstants.ZOOKEEPER_CLIENT_PORT,
44        HConstants.ZOOKEEPER_RECOVERABLE_WAITTIME,
45        HConstants.HBASE_CLIENT_PAUSE, HConstants.HBASE_CLIENT_RETRIES_NUMBER,
46        HConstants.HBASE_RPC_TIMEOUT_KEY,
47        HConstants.HBASE_CLIENT_PREFETCH_LIMIT,
48        HConstants.HBASE_META_SCANNER_CACHING,
49        HConstants.HBASE_CLIENT_INSTANCE_ID };
50  
51    private Map<String, String> properties;
52    private String username;
53  
54    HConnectionKey(Configuration conf) {
55      Map<String, String> m = new HashMap<String, String>();
56      if (conf != null) {
57        for (String property : CONNECTION_PROPERTIES) {
58          String value = conf.get(property);
59          if (value != null) {
60            m.put(property, value);
61          }
62        }
63      }
64      this.properties = Collections.unmodifiableMap(m);
65  
66      try {
67        User currentUser = User.getCurrent();
68        if (currentUser != null) {
69          username = currentUser.getName();
70        }
71      } catch (IOException ioe) {
72        HConnectionManager.LOG.warn("Error obtaining current user, skipping username in HConnectionKey", ioe);
73      }
74    }
75  
76    @Override
77    public int hashCode() {
78      final int prime = 31;
79      int result = 1;
80      if (username != null) {
81        result = username.hashCode();
82      }
83      for (String property : CONNECTION_PROPERTIES) {
84        String value = properties.get(property);
85        if (value != null) {
86          result = prime * result + value.hashCode();
87        }
88      }
89  
90      return result;
91    }
92  
93  
94    @edu.umd.cs.findbugs.annotations.SuppressWarnings (value="ES_COMPARING_STRINGS_WITH_EQ",
95        justification="Optimization")
96    @Override
97    public boolean equals(Object obj) {
98      if (this == obj)
99        return true;
100     if (obj == null)
101       return false;
102     if (getClass() != obj.getClass())
103       return false;
104     HConnectionKey that = (HConnectionKey) obj;
105     if (this.username != null && !this.username.equals(that.username)) {
106       return false;
107     } else if (this.username == null && that.username != null) {
108       return false;
109     }
110     if (this.properties == null) {
111       if (that.properties != null) {
112         return false;
113       }
114     } else {
115       if (that.properties == null) {
116         return false;
117       }
118       for (String property : CONNECTION_PROPERTIES) {
119         String thisValue = this.properties.get(property);
120         String thatValue = that.properties.get(property);
121         //noinspection StringEquality
122         if (thisValue == thatValue) {
123           continue;
124         }
125         if (thisValue == null || !thisValue.equals(thatValue)) {
126           return false;
127         }
128       }
129     }
130     return true;
131   }
132 
133   @Override
134   public String toString() {
135     return "HConnectionKey{" +
136       "properties=" + properties +
137       ", username='" + username + '\'' +
138       '}';
139   }
140 }