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()
53                  && !address.isLinkLocalAddress();
54      }
55  
56      /**
57       * Add the local host's name as a property
58       */
59      public String safelyGetLocalHostName() {
60          try {
61              String localhostName = getLocalHostName();
62              return localhostName;
63          } catch (UnknownHostException | SocketException | SecurityException e) {
64              addError("Failed to get local hostname", e);
65          }
66          return CoreConstants.UNKNOWN_LOCALHOST;
67      }
68  
69      public String safelyGetCanonicalLocalHostName() {
70          try {
71              String localhostName = getCanonicalLocalHostName();
72              return localhostName;
73          } catch (UnknownHostException | SocketException | SecurityException e) {
74              addError("Failed to get canonical local hostname", e);
75          }
76          return CoreConstants.UNKNOWN_LOCALHOST;
77  
78      }
79  
80  }