View Javadoc
1   package ch.qos.logback.core.util;
2   
3   import java.net.InetAddress;
4   import java.net.NetworkInterface;
5   import java.net.SocketException;
6   import java.net.UnknownHostException;
7   import java.util.Enumeration;
8   
9   import ch.qos.logback.core.Context;
10  import ch.qos.logback.core.CoreConstants;
11  import ch.qos.logback.core.spi.ContextAwareBase;
12  
13  public class NetworkAddressUtil extends ContextAwareBase {
14  
15      public NetworkAddressUtil(Context context) {
16          setContext(context);
17      }
18  
19      public static String getLocalHostName() throws UnknownHostException, SocketException {
20          try {
21              InetAddress localhost = InetAddress.getLocalHost();
22              return localhost.getHostName();
23          } catch (UnknownHostException e) {
24              return getLocalAddressAsString();
25          }
26      }
27  
28      public static String getCanonicalLocalHostName() throws UnknownHostException, SocketException {
29          try {
30              InetAddress localhost = InetAddress.getLocalHost();
31              return localhost.getCanonicalHostName();
32          } catch (UnknownHostException e) {
33              return getLocalAddressAsString();
34          }
35      }
36  
37      private static String getLocalAddressAsString() throws UnknownHostException, SocketException {
38          Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
39          while (interfaces != null && interfaces.hasMoreElements()) {
40              Enumeration<InetAddress> addresses = interfaces.nextElement().getInetAddresses();
41              while (addresses != null && addresses.hasMoreElements()) {
42                  InetAddress address = addresses.nextElement();
43                  if (acceptableAddress(address)) {
44                      return address.getHostAddress();
45                  }
46              }
47          }
48          throw new UnknownHostException();
49      }
50  
51      private static boolean acceptableAddress(InetAddress address) {
52          return address != null && !address.isLoopbackAddress() && !address.isAnyLocalAddress() && !address.isLinkLocalAddress();
53      }
54  
55      /**
56       * Add the local host's name as a property
57       */
58      public String safelyGetLocalHostName() {
59          try {
60              String localhostName = getLocalHostName();
61              return localhostName;
62          } catch (UnknownHostException | SocketException | SecurityException e) {
63              addError("Failed to get local hostname", e);
64          }
65          return CoreConstants.UNKNOWN_LOCALHOST;
66      }
67  
68      public String safelyGetCanonicalLocalHostName() {
69          try {
70              String localhostName = getCanonicalLocalHostName();
71              return localhostName;
72          } catch (UnknownHostException | SocketException | SecurityException e) {
73              addError("Failed to get canonical local hostname", e);
74          }
75          return CoreConstants.UNKNOWN_LOCALHOST;
76  
77      }
78  
79  }