| InetAddressUtil.java |
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.SystemException;
18
19 import java.net.Inet4Address;
20 import java.net.InetAddress;
21 import java.net.NetworkInterface;
22
23 import java.util.Enumeration;
24
25 /**
26 * <a href="InetAddressUtil.java.html"><b><i>View Source</i></b></a>
27 *
28 * @author Michael C. Han
29 * @author Shuyang Zhou
30 */
31 public class InetAddressUtil {
32
33 public static InetAddress getLocalInetAddress() throws Exception {
34 Enumeration<NetworkInterface> enu1 =
35 NetworkInterface.getNetworkInterfaces();
36
37 while (enu1.hasMoreElements()) {
38 NetworkInterface networkInterface = enu1.nextElement();
39
40 Enumeration<InetAddress> enu2 =
41 networkInterface.getInetAddresses();
42
43 while (enu2.hasMoreElements()) {
44 InetAddress inetAddress = enu2.nextElement();
45
46 if (!inetAddress.isLoopbackAddress() &&
47 (inetAddress instanceof Inet4Address)) {
48
49 return inetAddress;
50 }
51 }
52 }
53
54 throw new SystemException("No local internet address");
55 }
56
57 }