1 22 package org.jboss.ha.framework.interfaces; 23 24 import java.net.InetAddress ; 25 import java.io.Serializable ; 26 27 import org.jgroups.stack.IpAddress; 28 29 47 48 public class ClusterNode 49 implements Comparable , Cloneable , Serializable 50 { 51 52 54 56 protected String id = null; 57 protected String jgId = null; 58 protected IpAddress originalJGAddress = null; 59 60 62 64 public ClusterNode() 65 { 66 } 67 68 public ClusterNode(IpAddress jgAddress) 69 { 70 if (jgAddress.getAdditionalData() == null) 71 { 72 this.id = jgAddress.getIpAddress().getHostAddress() + ":" + jgAddress.getPort(); 73 } 74 else 75 { 76 this.id = new String (jgAddress.getAdditionalData()); 77 } 78 79 this.originalJGAddress = jgAddress; 80 StringBuffer sb = new StringBuffer (); 81 java.net.InetAddress jgIPAddr = jgAddress.getIpAddress(); 82 if (jgIPAddr == null) 83 sb.append("<null>"); 84 else 85 { 86 if (jgIPAddr.isMulticastAddress()) 87 sb.append(jgIPAddr.getHostAddress()); 88 else 89 sb.append(getShortName(jgIPAddr.getHostName())); 90 } 91 sb.append(":" + jgAddress.getPort()); 92 this.jgId = sb.toString(); 93 } 94 95 97 public String getName() 98 { 99 return this.id; 100 } 101 102 public String getJGName() 103 { 104 return this.jgId; 105 } 106 107 public IpAddress getOriginalJGAddress() 108 { 109 return this.originalJGAddress; 110 } 111 public InetAddress getIpAddress() 112 { 113 return this.originalJGAddress.getIpAddress(); 114 } 115 public int getPort() 116 { 117 return this.originalJGAddress.getPort(); 118 } 119 120 122 124 public int compareTo(Object o) 125 { 126 if ((o == null) || !(o instanceof ClusterNode)) 127 throw new ClassCastException ("ClusterNode.compareTo(): comparison between different classes"); 128 129 ClusterNode other = (ClusterNode) o; 130 131 return this.id.compareTo(other.id); 132 } 133 135 public boolean equals(Object obj) 136 { 137 if (obj == null || !(obj instanceof ClusterNode)) return false; 138 139 ClusterNode other = (ClusterNode) obj; 140 return this.id.equals(other.id); 141 } 142 143 public int hashCode() 144 { 145 return id.hashCode(); 146 } 147 148 public String toString() 149 { 150 return this.getName(); 151 } 152 153 155 157 protected String getShortName(String hostname) 158 { 159 int index = hostname.indexOf('.'); 160 161 if (hostname == null) return ""; 162 if (index > 0 && !Character.isDigit(hostname.charAt(0))) 163 return hostname.substring(0, index); 164 else 165 return hostname; 166 } 167 168 170 172 } 173 | Popular Tags |