1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
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  }