1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.kernel.util;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  
20  import java.net.InetAddress;
21  import java.net.UnknownHostException;
22  
23  /**
24   * <a href="IPDetector.java.html"><b><i>View Source</i></b></a>
25   *
26   * @author Shuyang Zhou
27   */
28  public class IPDetector {
29  
30      public static boolean isPrefersV4() {
31          if (_prefersV4 == null) {
32              _prefersV4 = Boolean.valueOf(
33                  System.getProperty("java.net.preferIPv4Stack"));
34          }
35  
36          return _prefersV4.booleanValue();
37      }
38  
39      public static boolean isPrefersV6() {
40          if (_prefersV6 == null) {
41              _prefersV6 = Boolean.valueOf(
42                  System.getProperty("java.net.preferIPv6Stack"));
43          }
44  
45          return _prefersV6.booleanValue();
46      }
47  
48      public static boolean isSupportsV6() {
49          if (_suppportsV6 == null) {
50              _suppportsV6 = Boolean.FALSE;
51  
52              try {
53                  InetAddress[] inetAddresses = InetAddress.getAllByName(
54                      "localhost");
55  
56                  for (InetAddress inetAddress : inetAddresses) {
57                      if (inetAddress.getHostAddress().contains(":")) {
58                          _suppportsV6 = Boolean.TRUE;
59  
60                          break;
61                      }
62                  }
63              }
64              catch (UnknownHostException uhe) {
65                  _log.error(uhe, uhe);
66              }
67          }
68  
69          return _suppportsV6.booleanValue();
70      }
71  
72      private static Log _log = LogFactoryUtil.getLog(IPDetector.class);
73  
74      private static Boolean _prefersV4;
75      private static Boolean _prefersV6;
76      private static Boolean _suppportsV6;
77  
78  }