1 /**
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19 package org.apache.hadoop.hbase.util;
20
21 import java.net.InetSocketAddress;
22
23 import org.apache.hadoop.classification.InterfaceAudience;
24 import org.apache.hadoop.classification.InterfaceStability;
25
26 /**
27 * Utility for network addresses, resolving and naming.
28 */
29 @InterfaceAudience.Public
30 @InterfaceStability.Evolving
31 public class Addressing {
32 public static final String VALID_PORT_REGEX = "[\\d]+";
33 public static final String HOSTNAME_PORT_SEPARATOR = ":";
34
35 /**
36 * @param hostAndPort Formatted as <code><hostname> ':' <port></code>
37 * @return An InetSocketInstance
38 */
39 public static InetSocketAddress createInetSocketAddressFromHostAndPortStr(
40 final String hostAndPort) {
41 return new InetSocketAddress(parseHostname(hostAndPort), parsePort(hostAndPort));
42 }
43
44 /**
45 * @param hostname Server hostname
46 * @param port Server port
47 * @return Returns a concatenation of <code>hostname</code> and
48 * <code>port</code> in following
49 * form: <code><hostname> ':' <port></code>. For example, if hostname
50 * is <code>example.org</code> and port is 1234, this method will return
51 * <code>example.org:1234</code>
52 */
53 public static String createHostAndPortStr(final String hostname, final int port) {
54 return hostname + HOSTNAME_PORT_SEPARATOR + port;
55 }
56
57 /**
58 * @param hostAndPort Formatted as <code><hostname> ':' <port></code>
59 * @return The hostname portion of <code>hostAndPort</code>
60 */
61 public static String parseHostname(final String hostAndPort) {
62 int colonIndex = hostAndPort.lastIndexOf(HOSTNAME_PORT_SEPARATOR);
63 if (colonIndex < 0) {
64 throw new IllegalArgumentException("Not a host:port pair: " + hostAndPort);
65 }
66 return hostAndPort.substring(0, colonIndex);
67 }
68
69 /**
70 * @param hostAndPort Formatted as <code><hostname> ':' <port></code>
71 * @return The port portion of <code>hostAndPort</code>
72 */
73 public static int parsePort(final String hostAndPort) {
74 int colonIndex = hostAndPort.lastIndexOf(HOSTNAME_PORT_SEPARATOR);
75 if (colonIndex < 0) {
76 throw new IllegalArgumentException("Not a host:port pair: " + hostAndPort);
77 }
78 return Integer.parseInt(hostAndPort.substring(colonIndex + 1));
79 }
80 }