1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
34
35
36
37
38
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
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 }