001    /**
002     * Copyright (c) 2000-2010 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                            _prefersV4 = Boolean.valueOf(
031                                    System.getProperty("java.net.preferIPv4Stack"));
032                    }
033    
034                    return _prefersV4.booleanValue();
035            }
036    
037            public static boolean isPrefersV6() {
038                    if (_prefersV6 == null) {
039                            _prefersV6 = Boolean.valueOf(
040                                    System.getProperty("java.net.preferIPv6Stack"));
041                    }
042    
043                    return _prefersV6.booleanValue();
044            }
045    
046            public static boolean isSupportsV6() {
047                    if (_suppportsV6 == null) {
048                            _suppportsV6 = Boolean.FALSE;
049    
050                            try {
051                                    InetAddress[] inetAddresses = InetAddress.getAllByName(
052                                            "localhost");
053    
054                                    for (InetAddress inetAddress : inetAddresses) {
055                                            if (inetAddress.getHostAddress().contains(":")) {
056                                                    _suppportsV6 = Boolean.TRUE;
057    
058                                                    break;
059                                            }
060                                    }
061                            }
062                            catch (UnknownHostException uhe) {
063                                    _log.error(uhe, uhe);
064                            }
065                    }
066    
067                    return _suppportsV6.booleanValue();
068            }
069    
070            private static Log _log = LogFactoryUtil.getLog(IPDetector.class);
071    
072            private static Boolean _prefersV4;
073            private static Boolean _prefersV6;
074            private static Boolean _suppportsV6;
075    
076    }