001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
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 String getLocalHostName() throws Exception {
032                    return LocalHostNameHolder._localHostName;
033            }
034    
035            public static InetAddress getLocalInetAddress() throws Exception {
036                    Enumeration<NetworkInterface> enu1 =
037                            NetworkInterface.getNetworkInterfaces();
038    
039                    while (enu1.hasMoreElements()) {
040                            NetworkInterface networkInterface = enu1.nextElement();
041    
042                            Enumeration<InetAddress> enu2 = networkInterface.getInetAddresses();
043    
044                            while (enu2.hasMoreElements()) {
045                                    InetAddress inetAddress = enu2.nextElement();
046    
047                                    if (!inetAddress.isLoopbackAddress() &&
048                                            (inetAddress instanceof Inet4Address)) {
049    
050                                            return inetAddress;
051                                    }
052                            }
053                    }
054    
055                    throw new SystemException("No local internet address");
056            }
057    
058            private static class LocalHostNameHolder {
059    
060                    private static final String _localHostName;
061    
062                    static {
063                            try {
064                                    InetAddress inetAddress = getLocalInetAddress();
065    
066                                    _localHostName = inetAddress.getHostName();
067                            }
068                            catch (Exception e) {
069                                    throw new ExceptionInInitializerError(e);
070                            }
071                    }
072    
073            }
074    
075    }