KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > common > NetUtils


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 2005 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * Initial developer(s): Vivek Lakshmanan
22  * Contributor(s):
23  *
24  * --------------------------------------------------------------------------
25  * $Id: NetUtils.java,v 1.2 2005/05/22 03:48:51 vivekl Exp $
26  * --------------------------------------------------------------------------
27  */

28 package org.objectweb.jonas.common;
29
30
31 import java.net.Inet4Address JavaDoc;
32 import java.net.InetAddress JavaDoc;
33 import java.net.NetworkInterface JavaDoc;
34 import java.net.SocketException JavaDoc;
35 import java.net.UnknownHostException JavaDoc;
36 import java.util.Enumeration JavaDoc;
37
38
39 /**
40  * NetUtils is meant to provide routine network related information
41  * to components of JONAS. One of the main purposes is to allow the
42  * server to extract it's own IP address correctly. This addresses
43  * a bug in java when dealing with Linux boxes on DHCP trying to
44  * extract their own IP addresses and always getting the loop back
45  * address instead of the external one.
46  * @link http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4060691
47  *
48  * @author Vivek Lakshmanan
49  */

50 public class NetUtils {
51
52     public static final String JavaDoc LOOP_BACK_ADDR = "127.0.0.1";
53
54     /**
55      * Get the string form of the local IP address and not naively assume
56      * InetAddress.getLocalHost().getHostAddress() is the currect value.
57      * See the class description above for details.
58      *
59      * @return A string representing the IPv4 localhost IP address.
60      * @throws UnknownHostException
61      */

62     public static String JavaDoc getLocalAddress() throws UnknownHostException JavaDoc {
63
64         // Check if the value of the localhost address is a loop back address.
65
if(InetAddress.getLocalHost().getHostAddress().equals(LOOP_BACK_ADDR)){
66             try{
67                 NetworkInterface JavaDoc inter = null;
68                 InetAddress JavaDoc addr = null;
69                 // Get all network interfaces on this machine.
70
Enumeration JavaDoc networkEnum = NetworkInterface.getNetworkInterfaces();
71                 while(networkEnum.hasMoreElements()){
72                     inter = (NetworkInterface JavaDoc) networkEnum.nextElement();
73                     Enumeration JavaDoc enumAddr = inter.getInetAddresses();
74                     //Check all addresses for this interface.
75
while(enumAddr.hasMoreElements()){
76                         addr = (InetAddress JavaDoc) enumAddr.nextElement();
77                         // Check only IP v4 addresses.
78
if(addr instanceof Inet4Address JavaDoc){
79                             if(!addr.isLoopbackAddress()){
80                                 // Found a non-loop back address so return it.
81
return addr.getHostAddress();
82                             }
83                         }
84
85                     }
86                 }
87
88                 // No non-loop back IP v4 addresses found so return loop back address.
89
return InetAddress.getLocalHost().getHostAddress();
90
91
92             }catch(SocketException JavaDoc e){
93                 return InetAddress.getLocalHost().getHostAddress();
94             }
95         }else{
96             // Got a non-loop back address so return it.
97
return InetAddress.getLocalHost().getHostAddress();
98         }
99     }
100
101 }
Popular Tags