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