001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.jmx.adaptor.control;
023:
024: import java.net.InetAddress;
025: import java.lang.reflect.Method;
026: import java.io.IOException;
027:
028: /** A utility class for parsing cluster member addresses
029: *
030: * @author Scott.Stark@jboss.org
031: * @version $Revision: 57210 $
032: */
033: public class AddressPort {
034: InetAddress addr;
035: Integer port;
036:
037: /** Use reflection to access the address InetAddress and port if they exist
038: * in the Address implementation
039: */
040: public static AddressPort getMemberAddress(Object addr)
041: throws IOException {
042: AddressPort info = null;
043: try {
044: Class[] parameterTypes = {};
045: Object[] args = {};
046: Method getIpAddress = addr.getClass().getMethod(
047: "getIpAddress", parameterTypes);
048: InetAddress inetAddr = (InetAddress) getIpAddress.invoke(
049: addr, args);
050: Method getPort = addr.getClass().getMethod("getPort",
051: parameterTypes);
052: Integer port = (Integer) getPort.invoke(addr, args);
053: info = new AddressPort(inetAddr, port);
054: } catch (Exception e) {
055: if (addr instanceof String) {
056: // Parse as a host:port string
057: String hostAddr = (String) addr;
058: int colon = hostAddr.indexOf(':');
059: String host = hostAddr;
060: Integer port = new Integer(0);
061: if (colon > 0) {
062: host = hostAddr.substring(0, colon);
063: port = Integer.valueOf(hostAddr
064: .substring(colon + 1));
065: }
066: info = new AddressPort(InetAddress.getByName(host),
067: port);
068: } else {
069: throw new IOException("Failed to parse addrType="
070: + addr.getClass() + ", msg=" + e.getMessage());
071: }
072: }
073: return info;
074: }
075:
076: AddressPort(InetAddress addr, Integer port) {
077: this .addr = addr;
078: this .port = port;
079: }
080:
081: public Integer getPort() {
082: return port;
083: }
084:
085: public InetAddress getInetAddress() {
086: return addr;
087: }
088:
089: public String getHostAddress() {
090: return addr.getHostAddress();
091: }
092:
093: public String getHostName() {
094: return addr.getHostName();
095: }
096:
097: public String toString() {
098: return "{host(" + addr + "), port(" + port + ")}";
099: }
100: }
|