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 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
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 }