FileDocCategorySizeDatePackage
Host.javaAPI DocphoneME MR2 API (J2ME)5420Wed May 02 18:00:42 BST 2007gov.nist.core

Host

public class Host extends Object
Stores hostname.
version
JAIN-SIP-1.1 This code is in the public domain.

Fields Summary
protected static final int
HOSTNAME
Type of host is a textual host name.
protected static final int
IPV4ADDRESS
Type of the host is a numeric IPV4 address.
protected static final int
IPV6ADDRESS
Type of the host is a numeric IPV6 address.
protected String
hostname
Host name field.
protected int
addressType
Address field.
Constructors Summary
public Host()
Default constructor.


       
      
        addressType = HOSTNAME;
    
public Host(String newHostName)
Constructor given host name or IP address. This method checks the type of and IP address to determine if it is an IpV4 or IPv6 address.

param
newHostName host name to be stored.
throws
IllegalArgumentException in case of invalid host name

        setHostname(newHostName);
    
Methods Summary
public java.lang.Objectclone()
Makes a copy of the current instance.

return
copy of current object

        Host retval = new Host();
        retval.addressType = this.addressType;
        retval.hostname = new String(this.hostname);
        return retval;
    
public java.lang.Stringencode()
Return the host name in encoded form.

return
the host name saved when the instance was created

        if (addressType == IPV6ADDRESS && 
            '[" != hostname.charAt(0))
            return "[" + hostname + "]";
        return hostname;
    
public booleanequals(java.lang.Object obj)
Compares for equality of hosts. Host names are compared by textual equality. No dns lookup is performed.

param
obj Object to set
return
boolean

        if (!this.getClass().equals(obj.getClass())) {
            return false;
        }
        Host otherHost = (Host) obj;
        return otherHost.hostname.equals(hostname);

    
public java.lang.StringgetAddress()
Gets the Address field.

return
the host name saved when the instance was created

        return hostname;
    
public java.lang.StringgetHostname()
Gets the HostName field.

return
the host name saved when the instance was created

        return hostname;
    
public booleanisHostname()
Returns true if the address is a DNS host name (and not an IPV4 address).

return
true if the hostname is a DNS name

        return addressType == HOSTNAME;
    
public booleanisIPAddress()
Returns true if the address is a DNS host name (and not an IPV4 address).

return
true if the hostname is host address.

        return addressType != HOSTNAME;
    
public voidsetAddress(java.lang.String address)
Sets the address member.

param
address address to set
throws
IllegalArgumentException in case of invalid host name

        setHostname(address);
    
public voidsetHostname(java.lang.String h)
Sets the hostname member.

param
h host name to set
throws
IllegalArgumentException in case of invalid host name

        if (h == null) {
            throw new IllegalArgumentException("Null address");
        }
        h = h.trim().toLowerCase();

        // IPv4 has stronger restriction than hostname
        if (Lexer.isValidIpv4Address(h)) {
            addressType = IPV4ADDRESS;
        } else if (Lexer.isValidHostname(h)) {
            addressType = HOSTNAME;
        } else {
            String addr = h;
            // IPv6 reference?
            if (h.charAt(0) == '[" && 
                h.charAt(h.length()-1) == ']") {
                addr = h.substring(1, h.length()-1);
            }
            if (!Lexer.isValidIpv6Address(addr)) {
                throw new IllegalArgumentException(
                                                  "Illegal hostname " + addr);
            }
        }
        hostname = h;