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.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    
020    import java.net.InetAddress;
021    import java.net.UnknownHostException;
022    
023    /**
024     * @author Shuyang Zhou
025     */
026    public class IPDetector {
027    
028            public static boolean isPrefersV4() {
029                    if (_prefersV4 != null) {
030                            return _prefersV4.booleanValue();
031                    }
032    
033                    _prefersV4 = Boolean.valueOf(
034                            System.getProperty("java.net.preferIPv4Stack"));
035    
036                    return _prefersV4.booleanValue();
037            }
038    
039            public static boolean isPrefersV6() {
040                    if (_prefersV6 != null) {
041                            return _prefersV6.booleanValue();
042                    }
043    
044                    _prefersV6 = Boolean.valueOf(
045                            System.getProperty("java.net.preferIPv6Stack"));
046    
047                    return _prefersV6.booleanValue();
048            }
049    
050            public static boolean isSupportsV6() {
051                    if (_suppportsV6 != null) {
052                            return _suppportsV6.booleanValue();
053                    }
054    
055                    try {
056                            InetAddress[] inetAddresses = InetAddress.getAllByName("localhost");
057    
058                            for (InetAddress inetAddress : inetAddresses) {
059                                    if (inetAddress.getHostAddress().contains(":")) {
060                                            _suppportsV6 = Boolean.TRUE;
061    
062                                            break;
063                                    }
064                            }
065                    }
066                    catch (UnknownHostException uhe) {
067                            _log.error(uhe, uhe);
068                    }
069    
070                    if (_suppportsV6 == null) {
071                            _suppportsV6 = Boolean.FALSE;
072                    }
073    
074                    return _suppportsV6.booleanValue();
075            }
076    
077            private static Log _log = LogFactoryUtil.getLog(IPDetector.class);
078    
079            private static Boolean _prefersV4;
080            private static Boolean _prefersV6;
081            private static Boolean _suppportsV6;
082    
083    }