View Javadoc

1   /**
2    * Copyright 2007 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
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   * HServerAddress is a "label" for a HBase server made of host and port number.
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     * Construct an instance from an {@link InetSocketAddress}.
45     * @param address InetSocketAddress of server
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     * @param hostAndPort Hostname and port formatted as <code>&lt;hostname> ':' &lt;port></code>
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     * @param bindAddress Hostname
71     * @param port Port number
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     * Copy-constructor.
81     * @param other HServerAddress to copy from
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    /** @return Bind address */
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   /** @return Port number */
111   public int getPort() {
112     return address.getPort();
113   }
114 
115   /** @return Hostname */
116   public String getHostname() {
117     return address.getHostName();
118   }
119 
120   /** @return The InetSocketAddress */
121   public InetSocketAddress getInetSocketAddress() {
122     return address;
123   }
124 
125   /**
126    * @return String formatted as <code>&lt;bind address> ':' &lt;port></code>
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   // Writable
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   // Comparable
184   //
185 
186   public int compareTo(HServerAddress o) {
187     // Addresses as Strings may not compare though address is for the one
188     // server with only difference being that one address has hostname
189     // resolved whereas other only has IP.
190     if (address.equals(o.address)) return 0;
191     return toString().compareTo(o.toString());
192   }
193 }