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  
52    private Map<String, String> properties;
53    private String username;
54  
55    HConnectionKey(Configuration conf) {
56      Map<String, String> m = new HashMap<String, String>();
57      if (conf != null) {
58        for (String property : CONNECTION_PROPERTIES) {
59          String value = conf.get(property);
60          if (value != null) {
61            m.put(property, value);
62          }
63        }
64      }
65      this.properties = Collections.unmodifiableMap(m);
66  
67      try {
68        UserProvider provider = UserProvider.instantiate(conf);
69        User currentUser = provider.getCurrent();
70        if (currentUser != null) {
71          username = currentUser.getName();
72        }
73      } catch (IOException ioe) {
74        HConnectionManager.LOG.warn("Error obtaining current user, skipping username in HConnectionKey", ioe);
75      }
76    }
77  
78    @Override
79    public int hashCode() {
80      final int prime = 31;
81      int result = 1;
82      if (username != null) {
83        result = username.hashCode();
84      }
85      for (String property : CONNECTION_PROPERTIES) {
86        String value = properties.get(property);
87        if (value != null) {
88          result = prime * result + value.hashCode();
89        }
90      }
91  
92      return result;
93    }
94  
95  
96    @edu.umd.cs.findbugs.annotations.SuppressWarnings (value="ES_COMPARING_STRINGS_WITH_EQ",
97        justification="Optimization")
98    @Override
99    public boolean equals(Object obj) {
100     if (this == obj)
101       return true;
102     if (obj == null)
103       return false;
104     if (getClass() != obj.getClass())
105       return false;
106     HConnectionKey that = (HConnectionKey) obj;
107     if (this.username != null && !this.username.equals(that.username)) {
108       return false;
109     } else if (this.username == null && that.username != null) {
110       return false;
111     }
112     if (this.properties == null) {
113       if (that.properties != null) {
114         return false;
115       }
116     } else {
117       if (that.properties == null) {
118         return false;
119       }
120       for (String property : CONNECTION_PROPERTIES) {
121         String thisValue = this.properties.get(property);
122         String thatValue = that.properties.get(property);
123         //noinspection StringEquality
124         if (thisValue == thatValue) {
125           continue;
126         }
127         if (thisValue == null || !thisValue.equals(thatValue)) {
128           return false;
129         }
130       }
131     }
132     return true;
133   }
134 
135   @Override
136   public String toString() {
137     return "HConnectionKey{" +
138       "properties=" + properties +
139       ", username='" + username + '\'' +
140       '}';
141   }
142 }