HostAddress.java

  1. /**
  2.  *
  3.  * Copyright © 2013-2014 Florian Schmaus
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smack.util.dns;

  18. import org.jivesoftware.smack.SmackException.ConnectionException;
  19. import org.jivesoftware.smack.util.Objects;

  20. public class HostAddress {
  21.     private final String fqdn;
  22.     private final int port;
  23.     private Exception exception;

  24.     /**
  25.      * Creates a new HostAddress with the given FQDN. The port will be set to the default XMPP client port: 5222
  26.      *
  27.      * @param fqdn Fully qualified domain name.
  28.      * @throws IllegalArgumentException If the fqdn is null.
  29.      */
  30.     public HostAddress(String fqdn) {
  31.         // Set port to the default port for XMPP client communication
  32.         this(fqdn, 5222);
  33.     }

  34.     /**
  35.      * Creates a new HostAddress with the given FQDN. The port will be set to the default XMPP client port: 5222
  36.      *
  37.      * @param fqdn Fully qualified domain name.
  38.      * @param port The port to connect on.
  39.      * @throws IllegalArgumentException If the fqdn is null or port is out of valid range (0 - 65535).
  40.      */
  41.     public HostAddress(String fqdn, int port) {
  42.         Objects.requireNonNull(fqdn, "FQDN is null");
  43.         if (port < 0 || port > 65535)
  44.             throw new IllegalArgumentException(
  45.                     "Port must be a 16-bit unsiged integer (i.e. between 0-65535. Port was: " + port);
  46.         if (fqdn.charAt(fqdn.length() - 1) == '.') {
  47.             this.fqdn = fqdn.substring(0, fqdn.length() - 1);
  48.         }
  49.         else {
  50.             this.fqdn = fqdn;
  51.         }
  52.         this.port = port;
  53.     }

  54.     public String getFQDN() {
  55.         return fqdn;
  56.     }

  57.     public int getPort() {
  58.         return port;
  59.     }

  60.     public void setException(Exception e) {
  61.         this.exception = e;
  62.     }

  63.     /**
  64.      * Retrieve the Exception that caused a connection failure to this HostAddress. Every
  65.      * HostAddress found in {@link ConnectionException} will have an Exception set,
  66.      * which can be retrieved with this method.
  67.      *
  68.      * @return the Exception causing this HostAddress to fail
  69.      */
  70.     public Exception getException() {
  71.         return this.exception;
  72.     }

  73.     @Override
  74.     public String toString() {
  75.         return fqdn + ":" + port;
  76.     }

  77.     @Override
  78.     public boolean equals(Object o) {
  79.         if (this == o) {
  80.             return true;
  81.         }
  82.         if (!(o instanceof HostAddress)) {
  83.             return false;
  84.         }

  85.         final HostAddress address = (HostAddress) o;

  86.         if (!fqdn.equals(address.fqdn)) {
  87.             return false;
  88.         }
  89.         return port == address.port;
  90.     }

  91.     @Override
  92.     public int hashCode() {
  93.         int result = 1;
  94.         result = 37 * result + fqdn.hashCode();
  95.         return result * 37 + port;
  96.     }

  97.     public String getErrorMessage() {
  98.         if (exception == null) {
  99.             return "No error logged";
  100.         }
  101.         return "'" + toString() + "' failed because " + exception.toString();
  102.     }
  103. }