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