1 6 7 package SOFA.SOFAnet.Repository; 8 9 import java.net.*; 10 import SOFA.Util.VMProperties; 11 12 17 public class NodeInfo 18 { 19 private String name; 20 private InetAddress address; 21 private int port; 22 23 public static class InvalidNodeNameException extends SOFA.SOFAnode.SOFAException 24 { 25 public InvalidNodeNameException(String message) 26 { 27 super(message); 28 } 29 30 public InvalidNodeNameException(String message, Throwable cause) 31 { 32 super(message, cause); 33 } 34 35 public InvalidNodeNameException(Throwable cause) 36 { 37 super(cause); 38 } 39 } 40 41 44 public static NodeInfo getLocalhost() 45 { 46 String localhost; 47 try 48 { 49 localhost = InetAddress.getLocalHost().getHostName(); 50 } 51 catch (UnknownHostException e) 52 { 53 localhost = "localhost"; 54 } 55 56 NodeInfo nodeInfo = new NodeInfo(); 57 58 try 59 { 60 nodeInfo.setNodeName(localhost); 61 } 62 catch (InvalidNodeNameException e) 63 { 64 } 65 66 return nodeInfo; 67 } 68 69 70 public static String getLocalNodeName() 71 { 72 String nodeName = System.getProperty(VMProperties.NODE_NAME).trim(); 73 if (!nodeName.toLowerCase().startsWith("sofa://")) nodeName = "sofa://" + nodeName; 74 return nodeName; 75 } 76 77 78 public NodeInfo() 79 { 80 port = -1; 81 } 82 83 92 public void setNodeName(String nodeName) throws InvalidNodeNameException 93 { 94 name = nodeName; 95 96 nodeName = nodeName.trim(); 97 if (nodeName.toLowerCase().startsWith("sofa://")) 98 nodeName = nodeName.substring(7).trim(); 99 100 if (nodeName.length() <= 0) throw new InvalidNodeNameException("Empty node name"); 101 102 port = -1; 103 104 int i = nodeName.indexOf(':'); 105 106 if (i != -1) 107 { 108 try 109 { 110 port = Integer.parseInt(nodeName.substring(i + 1)); 111 } 112 catch (NumberFormatException e) 113 { 114 throw new NodeInfo.InvalidNodeNameException("Invalid node name format (cannot read port)", e); 115 } 116 nodeName = nodeName.substring(0, i); 117 } 118 119 try 120 { 121 address = InetAddress.getByName(nodeName); 122 } 123 catch (UnknownHostException e) 124 { 125 throw new NodeInfo.InvalidNodeNameException("Unknown host '" + nodeName + "'", e); 126 } 127 } 128 129 173 174 public boolean matchPattern(String nodeNamePattern) throws InvalidNodeNameException 175 { 176 if (address == null) return false; 177 178 180 nodeNamePattern = nodeNamePattern.trim(); 181 if (nodeNamePattern.toLowerCase().startsWith("sofa://")) 182 nodeNamePattern = nodeNamePattern.substring(7).trim(); 183 184 if (nodeNamePattern.compareTo("*") == 0) return true; 185 186 188 int i = nodeNamePattern.indexOf(':'); 189 190 if (i != -1) 191 { 192 int patternPort = -1; 193 try 194 { 195 patternPort = Integer.parseInt(nodeNamePattern.substring(i + 1)); 196 } 197 catch (NumberFormatException e) 198 { 199 throw new NodeInfo.InvalidNodeNameException("Invalid node name pattern (cannot read port)", e); 200 } 201 202 if (patternPort != port) return false; 204 205 nodeNamePattern = nodeNamePattern.substring(0, i); 207 } 208 209 210 213 i = nodeNamePattern.indexOf('/'); 214 215 if (i != -1) { 217 byte [] ipAddr = address.getAddress(); 218 if (ipAddr.length != 4) return false; 220 int maskBits = 32; 221 222 try 223 { 224 maskBits = Integer.parseInt(nodeNamePattern.substring(i + 1)); 225 } 226 catch (NumberFormatException e) 227 { 228 throw new NodeInfo.InvalidNodeNameException("Invalid node name pattern (cannot read number of mask bits)", e); 229 } 230 nodeNamePattern = nodeNamePattern.substring(0, i); 231 232 String [] ipParts = nodeNamePattern.split("\\."); 233 if (ipParts.length > 4) 234 throw new NodeInfo.InvalidNodeNameException("Invalid node name pattern (invalid \"IP address\" part)"); 235 236 for (i = 0; i < ipParts.length; i++) 237 { 238 int number; 239 int bits = 0; 240 try 241 { 242 number = Integer.parseInt(ipParts[i]); 243 } 244 catch (NumberFormatException e) 245 { 246 throw new NodeInfo.InvalidNodeNameException("Invalid node name pattern (cannot read \"IP address\" part)", e); 247 } 248 249 if (maskBits >= 8) bits = 8; 250 else bits = maskBits; 251 maskBits -= bits; 252 if (bits == 0) return true; 253 254 int mask = 256 - (1 << (8 - bits)); 255 if ((number & mask) != (ipAddr[i] & mask)) return false; 256 } 257 258 if (maskBits > 0) 259 throw new NodeInfo.InvalidNodeNameException("Invalid node name pattern (incomplete \"IP address\" part or to many mask bits)"); 260 261 return true; 262 } 263 264 String nodeHost = address.getCanonicalHostName(); 266 if (nodeHost.endsWith("." + nodeNamePattern)) return true; 267 268 try 270 { 271 InetAddress addr = InetAddress.getByName(nodeNamePattern); 272 if (addr.equals(address)) return true; 273 } 274 catch (UnknownHostException e) 275 { 276 } 277 278 return false; 279 } 280 281 286 public boolean isSameHost(String host) throws InvalidNodeNameException 287 { 288 if (address == null) throw new NodeInfo.InvalidNodeNameException("Empty address"); 289 290 try 291 { 292 InetAddress addr = InetAddress.getByName(host); 293 if (addr.equals(address)) return true; 294 else return false; 295 } 296 catch (UnknownHostException e) 297 { 298 throw new NodeInfo.InvalidNodeNameException(e.getMessage(), e); 299 } 300 } 301 302 public String toString() 303 { 304 return name; 312 } 313 314 public String getName() 315 { 316 return name; 317 } 318 319 public InetAddress getAddress() 320 { 321 return address; 322 } 323 324 public int getPort() 325 { 326 return port; 327 } 328 329 public String getAddressAndPort() 330 { 331 if (address == null) return ""; 332 if (port == -1) return address.getHostAddress(); 333 else return address.getHostAddress() + ":" + port; 334 } 335 } 336 | Popular Tags |