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.ipc.RpcControllerFactory;
30  import org.apache.hadoop.hbase.security.User;
31  import org.apache.hadoop.hbase.security.UserProvider;
32  
33  /**
34   * Denotes a unique key to an {@link HConnection} instance.
35   *
36   * In essence, this class captures the properties in {@link Configuration}
37   * that may be used in the process of establishing a connection. In light of
38   * that, if any new such properties are introduced into the mix, they must be
39   * added to the {@link HConnectionKey#properties} list.
40   *
41   */
42  class HConnectionKey {
43    final static String[] CONNECTION_PROPERTIES = new String[] {
44        HConstants.ZOOKEEPER_QUORUM, HConstants.ZOOKEEPER_ZNODE_PARENT,
45        HConstants.ZOOKEEPER_CLIENT_PORT,
46        HConstants.ZOOKEEPER_RECOVERABLE_WAITTIME,
47        HConstants.HBASE_CLIENT_PAUSE, HConstants.HBASE_CLIENT_RETRIES_NUMBER,
48        HConstants.HBASE_RPC_TIMEOUT_KEY,
49        HConstants.HBASE_CLIENT_PREFETCH_LIMIT,
50        HConstants.HBASE_META_SCANNER_CACHING,
51        HConstants.HBASE_CLIENT_INSTANCE_ID,
52        HConstants.RPC_CODEC_CONF_KEY,
53        RpcControllerFactory.CUSTOM_CONTROLLER_CONF_KEY};
54  
55    private Map<String, String> properties;
56    private String username;
57  
58    HConnectionKey(Configuration conf) {
59      Map<String, String> m = new HashMap<String, String>();
60      if (conf != null) {
61        for (String property : CONNECTION_PROPERTIES) {
62          String value = conf.get(property);
63          if (value != null) {
64            m.put(property, value);
65          }
66        }
67      }
68      this.properties = Collections.unmodifiableMap(m);
69  
70      try {
71        UserProvider provider = UserProvider.instantiate(conf);
72        User currentUser = provider.getCurrent();
73        if (currentUser != null) {
74          username = currentUser.getName();
75        }
76      } catch (IOException ioe) {
77        HConnectionManager.LOG.warn("Error obtaining current user, skipping username in HConnectionKey", ioe);
78      }
79    }
80  
81    @Override
82    public int hashCode() {
83      final int prime = 31;
84      int result = 1;
85      if (username != null) {
86        result = username.hashCode();
87      }
88      for (String property : CONNECTION_PROPERTIES) {
89        String value = properties.get(property);
90        if (value != null) {
91          result = prime * result + value.hashCode();
92        }
93      }
94  
95      return result;
96    }
97  
98  
99    @edu.umd.cs.findbugs.annotations.SuppressWarnings (value="ES_COMPARING_STRINGS_WITH_EQ",
100       justification="Optimization")
101   @Override
102   public boolean equals(Object obj) {
103     if (this == obj)
104       return true;
105     if (obj == null)
106       return false;
107     if (getClass() != obj.getClass())
108       return false;
109     HConnectionKey that = (HConnectionKey) obj;
110     if (this.username != null && !this.username.equals(that.username)) {
111       return false;
112     } else if (this.username == null && that.username != null) {
113       return false;
114     }
115     if (this.properties == null) {
116       if (that.properties != null) {
117         return false;
118       }
119     } else {
120       if (that.properties == null) {
121         return false;
122       }
123       for (String property : CONNECTION_PROPERTIES) {
124         String thisValue = this.properties.get(property);
125         String thatValue = that.properties.get(property);
126         //noinspection StringEquality
127         if (thisValue == thatValue) {
128           continue;
129         }
130         if (thisValue == null || !thisValue.equals(thatValue)) {
131           return false;
132         }
133       }
134     }
135     return true;
136   }
137 
138   @Override
139   public String toString() {
140     return "HConnectionKey{" +
141       "properties=" + properties +
142       ", username='" + username + '\'' +
143       '}';
144   }
145 }