001/** 002 * 003 * Copyright © 2013-2014 Florian Schmaus 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.jivesoftware.smack.util.dns; 018 019import org.jivesoftware.smack.SmackException.ConnectionException; 020 021public class HostAddress { 022 private final String fqdn; 023 private final int port; 024 private Exception exception; 025 026 /** 027 * Creates a new HostAddress with the given FQDN. The port will be set to the default XMPP client port: 5222 028 * 029 * @param fqdn Fully qualified domain name. 030 * @throws IllegalArgumentException If the fqdn is null. 031 */ 032 public HostAddress(String fqdn) { 033 // Set port to the default port for XMPP client communication 034 this(fqdn, 5222); 035 } 036 037 /** 038 * Creates a new HostAddress with the given FQDN. The port will be set to the default XMPP client port: 5222 039 * 040 * @param fqdn Fully qualified domain name. 041 * @param port The port to connect on. 042 * @throws IllegalArgumentException If the fqdn is null or port is out of valid range (0 - 65535). 043 */ 044 public HostAddress(String fqdn, int port) { 045 if (fqdn == null) 046 throw new IllegalArgumentException("FQDN is null"); 047 if (port < 0 || port > 65535) 048 throw new IllegalArgumentException( 049 "Port must be a 16-bit unsiged integer (i.e. between 0-65535. Port was: " + port); 050 if (fqdn.charAt(fqdn.length() - 1) == '.') { 051 this.fqdn = fqdn.substring(0, fqdn.length() - 1); 052 } 053 else { 054 this.fqdn = fqdn; 055 } 056 this.port = port; 057 } 058 059 public String getFQDN() { 060 return fqdn; 061 } 062 063 public int getPort() { 064 return port; 065 } 066 067 public void setException(Exception e) { 068 this.exception = e; 069 } 070 071 /** 072 * Retrieve the Exception that caused a connection failure to this HostAddress. Every 073 * HostAddress found in {@link ConnectionException} will have an Exception set, 074 * which can be retrieved with this method. 075 * 076 * @return the Exception causing this HostAddress to fail 077 */ 078 public Exception getException() { 079 return this.exception; 080 } 081 082 @Override 083 public String toString() { 084 return fqdn + ":" + port; 085 } 086 087 @Override 088 public boolean equals(Object o) { 089 if (this == o) { 090 return true; 091 } 092 if (!(o instanceof HostAddress)) { 093 return false; 094 } 095 096 final HostAddress address = (HostAddress) o; 097 098 if (!fqdn.equals(address.fqdn)) { 099 return false; 100 } 101 return port == address.port; 102 } 103 104 @Override 105 public int hashCode() { 106 int result = 1; 107 result = 37 * result + fqdn.hashCode(); 108 return result * 37 + port; 109 } 110 111 public String getErrorMessage() { 112 String error; 113 if (exception == null) { 114 error = "No error logged"; 115 } 116 else { 117 error = exception.getMessage(); 118 } 119 return toString() + " Exception: " + error; 120 } 121}