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