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;
21
22 import java.io.DataInput;
23 import java.io.DataOutput;
24 import java.io.IOException;
25 import java.net.InetAddress;
26 import java.net.InetSocketAddress;
27
28 import org.apache.commons.logging.LogFactory;
29 import org.apache.hadoop.io.WritableComparable;
30
31
32
33
34
35
36
37
38
39
40
41
42 public class HServerAddress implements WritableComparable<HServerAddress> {
43
44
45
46 private InetSocketAddress address = null;
47 private String cachedToString = "";
48
49
50
51
52 public HServerAddress() {
53 super();
54 }
55
56
57
58
59
60 public HServerAddress(InetSocketAddress address) {
61 this.address = address;
62 checkBindAddressCanBeResolved();
63 this.cachedToString = createCachedToString();
64 }
65
66 private String createCachedToString() {
67 return this.address.toString();
68 }
69
70
71
72
73
74 public HServerAddress(final String hostname, final int port) {
75 this(getResolvedAddress(new InetSocketAddress(hostname, port)));
76 }
77
78
79
80
81
82 public HServerAddress(HServerAddress other) {
83 this(getResolvedAddress(new InetSocketAddress(other.getHostname(), other.getPort())));
84 }
85
86 private static InetSocketAddress getResolvedAddress(InetSocketAddress address) {
87 String bindAddress = getBindAddressInternal(address);
88 int port = address.getPort();
89 return new InetSocketAddress(bindAddress, port);
90 }
91
92
93
94
95 public String getBindAddress() {
96 return getBindAddressInternal(address);
97 }
98
99 private static String getBindAddressInternal(InetSocketAddress address) {
100 final InetAddress addr = address.getAddress();
101 if (addr != null) {
102 return addr.getHostAddress();
103 } else {
104 LogFactory.getLog(HServerAddress.class).error("Could not resolve the"
105 + " DNS name of " + address.getHostName());
106 return null;
107 }
108 }
109
110 private void checkBindAddressCanBeResolved() {
111 if (getBindAddress() == null) {
112 throw new IllegalArgumentException("Could not resolve the"
113 + " DNS name of " + this.address.toString());
114 }
115 }
116
117
118 public int getPort() {
119 return this.address.getPort();
120 }
121
122
123 public String getHostname() {
124
125
126
127 return this.address.getHostName().toLowerCase();
128 }
129
130
131
132
133 public String getHostnameAndPort() {
134 return getHostname() + ":" + getPort();
135 }
136
137
138 public InetSocketAddress getInetSocketAddress() {
139 return this.address;
140 }
141
142
143
144
145 @Override
146 public String toString() {
147 return this.cachedToString;
148 }
149
150 @Override
151 public boolean equals(Object o) {
152 if (this == o) return true;
153 if (o == null) return false;
154 if (getClass() != o.getClass()) return false;
155 return compareTo((HServerAddress)o) == 0;
156 }
157
158 @Override
159 public int hashCode() {
160 int result = address == null? 0: address.hashCode();
161 result ^= toString().hashCode();
162 return result;
163 }
164
165
166
167
168
169 public void readFields(DataInput in) throws IOException {
170 String hostname = in.readUTF();
171 int port = in.readInt();
172 if (hostname != null && hostname.length() > 0) {
173 this.address = getResolvedAddress(new InetSocketAddress(hostname, port));
174 checkBindAddressCanBeResolved();
175 createCachedToString();
176 }
177 }
178
179 public void write(DataOutput out) throws IOException {
180 if (this.address == null) {
181 out.writeUTF("");
182 out.writeInt(0);
183 } else {
184 out.writeUTF(this.address.getAddress().getHostName());
185 out.writeInt(this.address.getPort());
186 }
187 }
188
189
190
191
192
193 public int compareTo(HServerAddress o) {
194
195
196
197 if (this.address.equals(o.address)) return 0;
198 return toString().compareTo(o.toString());
199 }
200 }