001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 2005 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * Initial developer(s): Vivek Lakshmanan
022: * Contributor(s):
023: *
024: * --------------------------------------------------------------------------
025: * $Id: NetUtils.java 6800 2005-05-22 03:48:51Z vivekl $
026: * --------------------------------------------------------------------------
027: */package org.objectweb.jonas.common;
028:
029: import java.net.Inet4Address;
030: import java.net.InetAddress;
031: import java.net.NetworkInterface;
032: import java.net.SocketException;
033: import java.net.UnknownHostException;
034: import java.util.Enumeration;
035:
036: /**
037: * NetUtils is meant to provide routine network related information
038: * to components of JONAS. One of the main purposes is to allow the
039: * server to extract it's own IP address correctly. This addresses
040: * a bug in java when dealing with Linux boxes on DHCP trying to
041: * extract their own IP addresses and always getting the loop back
042: * address instead of the external one.
043: * @link http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4060691
044: *
045: * @author Vivek Lakshmanan
046: */
047: public class NetUtils {
048:
049: public static final String LOOP_BACK_ADDR = "127.0.0.1";
050:
051: /**
052: * Get the string form of the local IP address and not naively assume
053: * InetAddress.getLocalHost().getHostAddress() is the currect value.
054: * See the class description above for details.
055: *
056: * @return A string representing the IPv4 localhost IP address.
057: * @throws UnknownHostException
058: */
059: public static String getLocalAddress() throws UnknownHostException {
060:
061: // Check if the value of the localhost address is a loop back address.
062: if (InetAddress.getLocalHost().getHostAddress().equals(
063: LOOP_BACK_ADDR)) {
064: try {
065: NetworkInterface inter = null;
066: InetAddress addr = null;
067: // Get all network interfaces on this machine.
068: Enumeration networkEnum = NetworkInterface
069: .getNetworkInterfaces();
070: while (networkEnum.hasMoreElements()) {
071: inter = (NetworkInterface) networkEnum
072: .nextElement();
073: Enumeration enumAddr = inter.getInetAddresses();
074: //Check all addresses for this interface.
075: while (enumAddr.hasMoreElements()) {
076: addr = (InetAddress) enumAddr.nextElement();
077: // Check only IP v4 addresses.
078: if (addr instanceof Inet4Address) {
079: if (!addr.isLoopbackAddress()) {
080: // Found a non-loop back address so return it.
081: return addr.getHostAddress();
082: }
083: }
084:
085: }
086: }
087:
088: // No non-loop back IP v4 addresses found so return loop back address.
089: return InetAddress.getLocalHost().getHostAddress();
090:
091: } catch (SocketException e) {
092: return InetAddress.getLocalHost().getHostAddress();
093: }
094: } else {
095: // Got a non-loop back address so return it.
096: return InetAddress.getLocalHost().getHostAddress();
097: }
098: }
099:
100: }
|