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