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.ipc.RpcControllerFactory;
30 import org.apache.hadoop.hbase.security.User;
31 import org.apache.hadoop.hbase.security.UserProvider;
32
33
34
35
36
37
38
39
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 RpcControllerFactory.CUSTOM_CONTROLLER_CONF_KEY};
53
54 private Map<String, String> properties;
55 private String username;
56
57 HConnectionKey(Configuration conf) {
58 Map<String, String> m = new HashMap<String, String>();
59 if (conf != null) {
60 for (String property : CONNECTION_PROPERTIES) {
61 String value = conf.get(property);
62 if (value != null) {
63 m.put(property, value);
64 }
65 }
66 }
67 this.properties = Collections.unmodifiableMap(m);
68
69 try {
70 UserProvider provider = UserProvider.instantiate(conf);
71 User currentUser = provider.getCurrent();
72 if (currentUser != null) {
73 username = currentUser.getName();
74 }
75 } catch (IOException ioe) {
76 ConnectionManager.LOG.warn(
77 "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
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 }