package org.jboss.ha.framework.interfaces;
import java.net.InetAddress;
import org.jgroups.stack.IpAddress;
public class ClusterNode
implements Comparable, Cloneable
{
protected String id = null;
protected String jgId = null;
protected IpAddress originalJGAddress = null;
public ClusterNode()
{
}
public ClusterNode(IpAddress jgAddress)
{
if (jgAddress.getAdditionalData() == null)
{
this.id = jgAddress.getIpAddress().getHostAddress() + ":" + jgAddress.getPort();
}
else
{
this.id = new String(jgAddress.getAdditionalData());
}
this.originalJGAddress = jgAddress;
StringBuffer sb = new StringBuffer();
java.net.InetAddress jgIPAddr = jgAddress.getIpAddress();
if (jgIPAddr == null)
sb.append("<null>");
else
{
if (jgIPAddr.isMulticastAddress())
sb.append(jgIPAddr.getHostAddress());
else
sb.append(getShortName(jgIPAddr.getHostName()));
}
sb.append(":" + jgAddress.getPort());
this.jgId = sb.toString();
}
public String getName()
{
return this.id;
}
public String getJGName()
{
return this.jgId;
}
public IpAddress getOriginalJGAddress()
{
return this.originalJGAddress;
}
public InetAddress getIpAddress()
{
return this.originalJGAddress.getIpAddress();
}
public int getPort()
{
return this.originalJGAddress.getPort();
}
public int compareTo(Object o)
{
if ((o == null) || !(o instanceof ClusterNode))
throw new ClassCastException("ClusterNode.compareTo(): comparison between different classes");
ClusterNode other = (ClusterNode) o;
return this.id.compareTo(other.id);
}
public boolean equals(Object obj)
{
if (obj == null) return false;
return compareTo(obj) == 0 ? true : false;
}
public int hashCode()
{
return id.hashCode();
}
public String toString()
{
return this.getName();
}
protected String getShortName(String hostname)
{
int index = hostname.indexOf('.');
if (hostname == null) return "";
if (index > 0 && !Character.isDigit(hostname.charAt(0)))
return hostname.substring(0, index);
else
return hostname;
}
}