KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > jmx > adaptor > control > AddressPort


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.jmx.adaptor.control;
23
24 import java.net.InetAddress JavaDoc;
25 import java.lang.reflect.Method JavaDoc;
26 import java.io.IOException JavaDoc;
27
28 /** A utility class for parsing cluster member addresses
29  *
30  * @author Scott.Stark@jboss.org
31  * @version $Revision: 37459 $
32  */

33 public class AddressPort
34 {
35    InetAddress JavaDoc addr;
36    Integer JavaDoc port;
37
38    /** Use reflection to access the address InetAddress and port if they exist
39     * in the Address implementation
40     */

41    public static AddressPort getMemberAddress(Object JavaDoc addr)
42       throws IOException JavaDoc
43    {
44       AddressPort info = null;
45       try
46       {
47          Class JavaDoc[] parameterTypes = {};
48          Object JavaDoc[] args = {};
49          Method JavaDoc getIpAddress = addr.getClass().getMethod("getIpAddress", parameterTypes);
50          InetAddress JavaDoc inetAddr = (InetAddress JavaDoc) getIpAddress.invoke(addr, args);
51          Method JavaDoc getPort = addr.getClass().getMethod("getPort", parameterTypes);
52          Integer JavaDoc port = (Integer JavaDoc) getPort.invoke(addr, args);
53          info = new AddressPort(inetAddr, port);
54       }
55       catch(Exception JavaDoc e)
56       {
57          if( addr instanceof String JavaDoc )
58          {
59             // Parse as a host:port string
60
String JavaDoc hostAddr = (String JavaDoc) addr;
61             int colon = hostAddr.indexOf(':');
62             String JavaDoc host = hostAddr;
63             Integer JavaDoc port = new Integer JavaDoc(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 JavaDoc("Failed to parse addrType="+addr.getClass()
74                +", msg="+e.getMessage());
75          }
76       }
77       return info;
78    }
79
80    AddressPort(InetAddress JavaDoc addr, Integer JavaDoc port)
81    {
82       this.addr = addr;
83       this.port = port;
84    }
85
86    public Integer JavaDoc getPort()
87    {
88       return port;
89    }
90    public InetAddress JavaDoc getInetAddress()
91    {
92       return addr;
93    }
94    public String JavaDoc getHostAddress()
95    {
96       return addr.getHostAddress();
97    }
98    public String JavaDoc getHostName()
99    {
100       return addr.getHostName();
101    }
102    public String JavaDoc toString()
103    {
104       return "{host("+addr+"), port("+port+")}";
105    }
106 }
Popular Tags