1 22 package org.jboss.jmx.adaptor.control; 23 24 import java.net.InetAddress ; 25 import java.lang.reflect.Method ; 26 import java.io.IOException ; 27 28 33 public class AddressPort 34 { 35 InetAddress addr; 36 Integer port; 37 38 41 public static AddressPort getMemberAddress(Object addr) 42 throws IOException 43 { 44 AddressPort info = null; 45 try 46 { 47 Class [] parameterTypes = {}; 48 Object [] args = {}; 49 Method getIpAddress = addr.getClass().getMethod("getIpAddress", parameterTypes); 50 InetAddress inetAddr = (InetAddress ) getIpAddress.invoke(addr, args); 51 Method getPort = addr.getClass().getMethod("getPort", parameterTypes); 52 Integer port = (Integer ) getPort.invoke(addr, args); 53 info = new AddressPort(inetAddr, port); 54 } 55 catch(Exception e) 56 { 57 if( addr instanceof String ) 58 { 59 String hostAddr = (String ) addr; 61 int colon = hostAddr.indexOf(':'); 62 String host = hostAddr; 63 Integer port = new Integer (0); 64 if( colon > 0 ) 65 { 66 host = hostAddr.substring(0, colon); 67 port = Integer.valueOf(hostAddr.substring(colon+1)); 68 } 69 info = new AddressPort(InetAddress.getByName(host), port); 70 } 71 else 72 { 73 throw new IOException ("Failed to parse addrType="+addr.getClass() 74 +", msg="+e.getMessage()); 75 } 76 } 77 return info; 78 } 79 80 AddressPort(InetAddress addr, Integer port) 81 { 82 this.addr = addr; 83 this.port = port; 84 } 85 86 public Integer getPort() 87 { 88 return port; 89 } 90 public InetAddress getInetAddress() 91 { 92 return addr; 93 } 94 public String getHostAddress() 95 { 96 return addr.getHostAddress(); 97 } 98 public String getHostName() 99 { 100 return addr.getHostName(); 101 } 102 public String toString() 103 { 104 return "{host("+addr+"), port("+port+")}"; 105 } 106 } | Popular Tags |