001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.kernel.util;
016    
017    import com.liferay.portal.kernel.exception.SystemException;
018    
019    import java.net.Inet4Address;
020    import java.net.InetAddress;
021    import java.net.NetworkInterface;
022    
023    import java.util.Enumeration;
024    
025    /**
026     * @author Michael C. Han
027     * @author Shuyang Zhou
028     */
029    public class InetAddressUtil {
030    
031            public static InetAddress getLocalInetAddress() throws Exception {
032                    Enumeration<NetworkInterface> enu1 =
033                            NetworkInterface.getNetworkInterfaces();
034    
035                    while (enu1.hasMoreElements()) {
036                            NetworkInterface networkInterface = enu1.nextElement();
037    
038                            Enumeration<InetAddress> enu2 =
039                                    networkInterface.getInetAddresses();
040    
041                            while (enu2.hasMoreElements()) {
042                                    InetAddress inetAddress = enu2.nextElement();
043    
044                                    if (!inetAddress.isLoopbackAddress() &&
045                                            (inetAddress instanceof Inet4Address)) {
046    
047                                            return inetAddress;
048                                    }
049                            }
050                    }
051    
052                    throw new SystemException("No local internet address");
053            }
054    
055    }