001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.net;
019
020import java.net.InetSocketAddress;
021import org.apache.commons.lang3.StringUtils;
022import org.apache.yetus.audience.InterfaceAudience;
023
024import org.apache.hbase.thirdparty.com.google.common.net.HostAndPort;
025
026/**
027 * An immutable type to hold a hostname and port combo, like an Endpoint or
028 * java.net.InetSocketAddress (but without danger of our calling resolve -- we do NOT want a resolve
029 * happening every time we want to hold a hostname and port combo). This class is also
030 * {@link Comparable}
031 * <p>
032 * In implementation this class is a facade over Guava's {@link HostAndPort}. We cannot have Guava
033 * classes in our API hence this Type.
034 */
035@InterfaceAudience.Public
036public class Address implements Comparable<Address> {
037  private final HostAndPort hostAndPort;
038
039  private Address(HostAndPort hostAndPort) {
040    this.hostAndPort = hostAndPort;
041  }
042
043  public static Address fromParts(String hostname, int port) {
044    return new Address(HostAndPort.fromParts(hostname, port));
045  }
046
047  public static Address fromString(String hostnameAndPort) {
048    return new Address(HostAndPort.fromString(hostnameAndPort));
049  }
050
051  public static Address fromSocketAddress(InetSocketAddress addr) {
052    return Address.fromParts(addr.getHostString(), addr.getPort());
053  }
054
055  public static InetSocketAddress toSocketAddress(Address addr) {
056    return new InetSocketAddress(addr.getHostName(), addr.getPort());
057  }
058
059  public static InetSocketAddress[] toSocketAddress(Address[] addrs) {
060    if (addrs == null) {
061      return null;
062    }
063    InetSocketAddress[] result = new InetSocketAddress[addrs.length];
064    for (int i = 0; i < addrs.length; i++) {
065      result[i] = toSocketAddress(addrs[i]);
066    }
067    return result;
068  }
069
070  public String getHostName() {
071    return this.hostAndPort.getHost();
072  }
073
074  /**
075   * @deprecated Use {@link #getHostName()} instead
076   */
077  @Deprecated
078  public String getHostname() {
079    return this.hostAndPort.getHost();
080  }
081
082  public int getPort() {
083    return this.hostAndPort.getPort();
084  }
085
086  @Override
087  public String toString() {
088    return this.hostAndPort.toString();
089  }
090
091  /**
092   * If hostname is a.b.c and the port is 123, return a:123 instead of a.b.c:123.
093   * @return if host looks like it is resolved -- not an IP -- then strip the domain portion
094   *         otherwise returns same as {@link #toString()}}
095   */
096  public String toStringWithoutDomain() {
097    String hostname = getHostName();
098    String[] parts = hostname.split("\\.");
099    if (parts.length > 1) {
100      for (String part : parts) {
101        if (!StringUtils.isNumeric(part)) {
102          return Address.fromParts(parts[0], getPort()).toString();
103        }
104      }
105    }
106    return toString();
107  }
108
109  @Override
110  // Don't use HostAndPort equals... It is wonky including
111  // ipv6 brackets
112  public boolean equals(Object other) {
113    if (this == other) {
114      return true;
115    }
116    if (other instanceof Address) {
117      Address that = (Address) other;
118      return this.getHostName().equals(that.getHostName()) && this.getPort() == that.getPort();
119    }
120    return false;
121  }
122
123  @Override
124  public int hashCode() {
125    return this.getHostName().hashCode() ^ getPort();
126  }
127
128  @Override
129  public int compareTo(Address that) {
130    int compare = this.getHostName().compareTo(that.getHostName());
131    if (compare != 0) {
132      return compare;
133    }
134
135    return this.getPort() - that.getPort();
136  }
137}