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_META_SCANNER_CACHING,
50        HConstants.HBASE_CLIENT_INSTANCE_ID,
51        HConstants.RPC_CODEC_CONF_KEY,
52        HConstants.USE_META_REPLICAS,
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        ConnectionManager.LOG.warn(
78            "Error obtaining current user, skipping username in HConnectionKey", ioe);
79      }
80    }
81  
82    @Override
83    public int hashCode() {
84      final int prime = 31;
85      int result = 1;
86      if (username != null) {
87        result = username.hashCode();
88      }
89      for (String property : CONNECTION_PROPERTIES) {
90        String value = properties.get(property);
91        if (value != null) {
92          result = prime * result + value.hashCode();
93        }
94      }
95  
96      return result;
97    }
98  
99  
100   @edu.umd.cs.findbugs.annotations.SuppressWarnings (value="ES_COMPARING_STRINGS_WITH_EQ",
101       justification="Optimization")
102   @Override
103   public boolean equals(Object obj) {
104     if (this == obj)
105       return true;
106     if (obj == null)
107       return false;
108     if (getClass() != obj.getClass())
109       return false;
110     HConnectionKey that = (HConnectionKey) obj;
111     if (this.username != null && !this.username.equals(that.username)) {
112       return false;
113     } else if (this.username == null && that.username != null) {
114       return false;
115     }
116     if (this.properties == null) {
117       if (that.properties != null) {
118         return false;
119       }
120     } else {
121       if (that.properties == null) {
122         return false;
123       }
124       for (String property : CONNECTION_PROPERTIES) {
125         String thisValue = this.properties.get(property);
126         String thatValue = that.properties.get(property);
127         //noinspection StringEquality
128         if (thisValue == thatValue) {
129           continue;
130         }
131         if (thisValue == null || !thisValue.equals(thatValue)) {
132           return false;
133         }
134       }
135     }
136     return true;
137   }
138 
139   @Override
140   public String toString() {
141     return "HConnectionKey{" +
142       "properties=" + properties +
143       ", username='" + username + '\'' +
144       '}';
145   }
146 }