1
- /**
2
- *
1
+ /*
3
2
* Licensed to the Apache Software Foundation (ASF) under one
4
3
* or more contributor license agreements. See the NOTICE file
5
4
* distributed with this work for additional information
23
22
import java .util .List ;
24
23
import java .util .Locale ;
25
24
import java .util .regex .Pattern ;
26
-
27
25
import org .apache .hadoop .hbase .net .Address ;
28
26
import org .apache .hadoop .hbase .util .Addressing ;
29
27
import org .apache .hadoop .hbase .util .Bytes ;
28
+ import org .apache .yetus .audience .InterfaceAudience ;
30
29
import org .apache .hbase .thirdparty .com .google .common .collect .Interner ;
31
30
import org .apache .hbase .thirdparty .com .google .common .collect .Interners ;
32
31
import org .apache .hbase .thirdparty .com .google .common .net .InetAddresses ;
33
- import org .apache .yetus .audience .InterfaceAudience ;
34
-
35
-
36
32
37
33
/**
38
34
* Name of a particular incarnation of an HBase Server.
@@ -120,63 +116,24 @@ private ServerName(final Address address, final long startcode) {
120
116
this .address .getPort (), startcode );
121
117
}
122
118
123
- private ServerName (final String serverName ) {
124
- this (parseHostname (serverName ), parsePort (serverName ),
125
- parseStartcode (serverName ));
126
- }
127
-
128
119
private ServerName (final String hostAndPort , final long startCode ) {
129
120
this (Address .fromString (hostAndPort ), startCode );
130
121
}
131
122
132
123
/**
133
- * @param hostname
124
+ * @param hostname the hostname string to get the actual hostname from
134
125
* @return hostname minus the domain, if there is one (will do pass-through on ip addresses)
135
- * @deprecated Since 2.0. This is for internal use only.
136
126
*/
137
- @ Deprecated
138
- // Make this private in hbase-3.0.
139
- static String getHostNameMinusDomain (final String hostname ) {
140
- if (InetAddresses .isInetAddress (hostname )) return hostname ;
141
- String [] parts = hostname .split ("\\ ." );
142
- if (parts == null || parts .length == 0 ) return hostname ;
143
- return parts [0 ];
144
- }
145
-
146
- /**
147
- * @deprecated Since 2.0. Use {@link #valueOf(String)}
148
- */
149
- @ Deprecated
150
- // This is unused. Get rid of it.
151
- public static String parseHostname (final String serverName ) {
152
- if (serverName == null || serverName .length () <= 0 ) {
153
- throw new IllegalArgumentException ("Passed hostname is null or empty" );
127
+ private static String getHostNameMinusDomain (final String hostname ) {
128
+ if (InetAddresses .isInetAddress (hostname )) {
129
+ return hostname ;
154
130
}
155
- if (!Character .isLetterOrDigit (serverName .charAt (0 ))) {
156
- throw new IllegalArgumentException ("Bad passed hostname, serverName=" + serverName );
131
+ String [] parts = hostname .split ("\\ ." );
132
+ if (parts .length == 0 ) {
133
+ return hostname ;
157
134
}
158
- int index = serverName .indexOf (SERVERNAME_SEPARATOR );
159
- return serverName .substring (0 , index );
160
- }
161
-
162
- /**
163
- * @deprecated Since 2.0. Use {@link #valueOf(String)}
164
- */
165
- @ Deprecated
166
- // This is unused. Get rid of it.
167
- public static int parsePort (final String serverName ) {
168
- String [] split = serverName .split (SERVERNAME_SEPARATOR );
169
- return Integer .parseInt (split [1 ]);
170
- }
171
135
172
- /**
173
- * @deprecated Since 2.0. Use {@link #valueOf(String)}
174
- */
175
- @ Deprecated
176
- // This is unused. Get rid of it.
177
- public static long parseStartcode (final String serverName ) {
178
- int index = serverName .lastIndexOf (SERVERNAME_SEPARATOR );
179
- return Long .parseLong (serverName .substring (index + 1 ));
136
+ return parts [0 ];
180
137
}
181
138
182
139
/**
@@ -194,7 +151,11 @@ public static ServerName valueOf(final String hostname, final int port, final lo
194
151
* a shared immutable object as an internal optimization.
195
152
*/
196
153
public static ServerName valueOf (final String serverName ) {
197
- return INTERN_POOL .intern (new ServerName (serverName ));
154
+ final String hostname = serverName .substring (0 , serverName .indexOf (SERVERNAME_SEPARATOR ));
155
+ final int port = Integer .parseInt (serverName .split (SERVERNAME_SEPARATOR )[1 ]);
156
+ final long statuscode =
157
+ Long .parseLong (serverName .substring (serverName .lastIndexOf (SERVERNAME_SEPARATOR ) + 1 ));
158
+ return INTERN_POOL .intern (new ServerName (hostname , port , statuscode ));
198
159
}
199
160
200
161
/**
@@ -206,26 +167,40 @@ public static ServerName valueOf(final String hostAndPort, final long startCode)
206
167
return INTERN_POOL .intern (new ServerName (hostAndPort , startCode ));
207
168
}
208
169
170
+ /**
171
+ * Retrieve an instance of {@link ServerName}. Callers should use the {@link #equals(Object)}
172
+ * method to compare returned instances, though we may return a shared immutable object as an
173
+ * internal optimization.
174
+ *
175
+ * @param address the {@link Address} to use for getting the {@link ServerName}
176
+ * @param startcode the startcode to use for getting the {@link ServerName}
177
+ * @return the constructed {@link ServerName}
178
+ * @see #valueOf(String, int, long)
179
+ */
180
+ public static ServerName valueOf (final Address address , final long startcode ) {
181
+ return valueOf (address .getHostname (), address .getPort (), startcode );
182
+ }
183
+
209
184
@ Override
210
185
public String toString () {
211
186
return getServerName ();
212
187
}
213
188
214
189
/**
215
- * @return Return a SHORT version of {@link ServerName #toString()}, one that has the host only,
216
- * minus the domain, and the port only -- no start code; the String is for us internally mostly
217
- * tying threads to their server. Not for external use. It is lossy and will not work in
218
- * in compares, etc.
190
+ * @return Return a SHORT version of {@link #toString()}, one that has the host only,
191
+ * minus the domain, and the port only -- no start code; the String is for us internally mostly
192
+ * tying threads to their server. Not for external use. It is lossy and will not work in
193
+ * in compares, etc.
219
194
*/
220
195
public String toShortString () {
221
196
return Addressing .createHostAndPortStr (
222
- getHostNameMinusDomain (this .address .getHostname ()),
223
- this .address .getPort ());
197
+ getHostNameMinusDomain (this .address .getHostname ()),
198
+ this .address .getPort ());
224
199
}
225
200
226
201
/**
227
202
* @return {@link #getServerName()} as bytes with a short-sized prefix with
228
- * the ServerName #VERSION of this class.
203
+ * the {@link #VERSION} of this class.
229
204
*/
230
205
public synchronized byte [] getVersionedBytes () {
231
206
if (this .bytes == null ) {
@@ -256,83 +231,21 @@ public long getStartcode() {
256
231
257
232
/**
258
233
* For internal use only.
259
- * @param hostName
260
- * @param port
261
- * @param startcode
234
+ * @param hostName the name of the host to use
235
+ * @param port the port on the host to use
236
+ * @param startcode the startcode to use for formatting
262
237
* @return Server name made of the concatenation of hostname, port and
263
- * startcode formatted as <code><hostname> ',' <port> ',' <startcode></code>
264
- * @deprecated Since 2.0. Use {@link ServerName#valueOf(String, int, long)} instead.
238
+ * startcode formatted as <code><hostname> ',' <port> ',' <startcode></code>
265
239
*/
266
- @ Deprecated
267
- // TODO: Make this private in hbase-3.0.
268
- static String getServerName (String hostName , int port , long startcode ) {
269
- final StringBuilder name = new StringBuilder (hostName .length () + 1 + 5 + 1 + 13 );
270
- name .append (hostName .toLowerCase (Locale .ROOT ));
271
- name .append (SERVERNAME_SEPARATOR );
272
- name .append (port );
273
- name .append (SERVERNAME_SEPARATOR );
274
- name .append (startcode );
275
- return name .toString ();
276
- }
277
-
278
- /**
279
- * @param hostAndPort String in form of <hostname> ':' <port>
280
- * @param startcode
281
- * @return Server name made of the concatenation of hostname, port and
282
- * startcode formatted as <code><hostname> ',' <port> ',' <startcode></code>
283
- * @deprecated Since 2.0. Use {@link ServerName#valueOf(String, long)} instead.
284
- */
285
- @ Deprecated
286
- public static String getServerName (final String hostAndPort ,
287
- final long startcode ) {
288
- int index = hostAndPort .indexOf (":" );
289
- if (index <= 0 ) throw new IllegalArgumentException ("Expected <hostname> ':' <port>" );
290
- return getServerName (hostAndPort .substring (0 , index ),
291
- Integer .parseInt (hostAndPort .substring (index + 1 )), startcode );
292
- }
293
-
294
- /**
295
- * @return Hostname and port formatted as described at
296
- * {@link Addressing#createHostAndPortStr(String, int)}
297
- * @deprecated Since 2.0. Use {@link #getAddress()} instead.
298
- */
299
- @ Deprecated
300
- public String getHostAndPort () {
301
- return this .address .toString ();
240
+ private static String getServerName (String hostName , int port , long startcode ) {
241
+ return hostName .toLowerCase (Locale .ROOT ) + SERVERNAME_SEPARATOR + port
242
+ + SERVERNAME_SEPARATOR + startcode ;
302
243
}
303
244
304
245
public Address getAddress () {
305
246
return this .address ;
306
247
}
307
248
308
- /**
309
- * @param serverName ServerName in form specified by {@link #getServerName()}
310
- * @return The server start code parsed from <code>servername</code>
311
- * @deprecated Since 2.0. Use instance of ServerName to pull out start code.
312
- */
313
- @ Deprecated
314
- public static long getServerStartcodeFromServerName (final String serverName ) {
315
- int index = serverName .lastIndexOf (SERVERNAME_SEPARATOR );
316
- return Long .parseLong (serverName .substring (index + 1 ));
317
- }
318
-
319
- /**
320
- * Utility method to excise the start code from a server name
321
- * @param inServerName full server name
322
- * @return server name less its start code
323
- * @deprecated Since 2.0. Use {@link #getAddress()}
324
- */
325
- @ Deprecated
326
- public static String getServerNameLessStartCode (String inServerName ) {
327
- if (inServerName != null && inServerName .length () > 0 ) {
328
- int index = inServerName .lastIndexOf (SERVERNAME_SEPARATOR );
329
- if (index > 0 ) {
330
- return inServerName .substring (0 , index );
331
- }
332
- }
333
- return inServerName ;
334
- }
335
-
336
249
@ Override
337
250
public int compareTo (ServerName other ) {
338
251
int compare ;
@@ -366,24 +279,25 @@ public int hashCode() {
366
279
367
280
@ Override
368
281
public boolean equals (Object o ) {
369
- if (this == o ) return true ;
370
- if (o == null ) return false ;
371
- if (!(o instanceof ServerName )) return false ;
282
+ if (this == o ) {
283
+ return true ;
284
+ }
285
+ if (o == null ) {
286
+ return false ;
287
+ }
288
+ if (!(o instanceof ServerName )) {
289
+ return false ;
290
+ }
372
291
return this .compareTo ((ServerName )o ) == 0 ;
373
292
}
374
293
375
294
/**
376
- * @param left
377
- * @param right
378
- * @return True if < code>other</ code> has same hostname and port.
295
+ * @param left the first server address to compare
296
+ * @param right the second server address to compare
297
+ * @return {@code true} if {@ code left} and {@ code right} have the same hostname and port.
379
298
*/
380
- public static boolean isSameAddress (final ServerName left ,
381
- final ServerName right ) {
382
- // TODO: Make this left.getAddress().equals(right.getAddress())
383
- if (left == null ) return false ;
384
- if (right == null ) return false ;
385
- return left .getHostname ().compareToIgnoreCase (right .getHostname ()) == 0 &&
386
- left .getPort () == right .getPort ();
299
+ public static boolean isSameAddress (final ServerName left , final ServerName right ) {
300
+ return left .getAddress ().equals (right .getAddress ());
387
301
}
388
302
389
303
/**
@@ -407,22 +321,23 @@ public static ServerName parseVersionedServerName(final byte [] versionedBytes)
407
321
}
408
322
409
323
/**
410
- * @param str Either an instance of {@link ServerName #toString()} or a
411
- * "'<hostname>' ':' '<port>'".
324
+ * @param str Either an instance of {@link #toString()} or a
325
+ * "'<hostname>' ':' '<port>'".
412
326
* @return A ServerName instance.
413
327
*/
414
328
public static ServerName parseServerName (final String str ) {
415
329
return SERVERNAME_PATTERN .matcher (str ).matches ()? valueOf (str ) :
416
330
valueOf (str , NON_STARTCODE );
417
331
}
418
332
419
-
420
333
/**
421
- * @return true if the String follows the pattern of {@link ServerName #toString()}, false
422
- * otherwise.
334
+ * @return true if the String follows the pattern of {@link #toString()}, false
335
+ * otherwise.
423
336
*/
424
337
public static boolean isFullServerName (final String str ){
425
- if (str == null ||str .isEmpty ()) return false ;
338
+ if (str == null ||str .isEmpty ()) {
339
+ return false ;
340
+ }
426
341
return SERVERNAME_PATTERN .matcher (str ).matches ();
427
342
}
428
343
}
0 commit comments