View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  package org.apache.logging.log4j.core.helpers;
18  
19  import java.net.InetAddress;
20  import java.net.NetworkInterface;
21  import java.net.SocketException;
22  import java.net.UnknownHostException;
23  import java.util.Enumeration;
24  
25  import org.apache.logging.log4j.Logger;
26  import org.apache.logging.log4j.status.StatusLogger;
27  
28  /**
29   *
30   */
31  public final class NetUtils {
32  
33      private static final Logger LOGGER = StatusLogger.getLogger();
34  
35      private NetUtils() {
36      }
37  
38      /**
39       * This method gets the network name of the machine we are running on.
40       * Returns "UNKNOWN_LOCALHOST" in the unlikely case where the host name
41       * cannot be found.
42       *
43       * @return String the name of the local host
44       */
45      public static String getLocalHostname() {
46          try {
47              final InetAddress addr = InetAddress.getLocalHost();
48              return addr.getHostName();
49          } catch (final UnknownHostException uhe) {
50              try {
51                  final Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
52                  while (interfaces.hasMoreElements()) {
53                      final NetworkInterface nic = interfaces.nextElement();
54                      final Enumeration<InetAddress> addresses = nic.getInetAddresses();
55                      while (addresses.hasMoreElements()) {
56                          final InetAddress address = addresses.nextElement();
57                          if (!address.isLoopbackAddress()) {
58                              final String hostname = address.getHostName();
59                              if (hostname != null) {
60                                  return hostname;
61                              }
62                          }
63                      }
64                  }
65              } catch (final SocketException se) {
66                  LOGGER.error("Could not determine local host name", uhe);
67                  return "UNKNOWN_LOCALHOST";
68              }
69              LOGGER.error("Could not determine local host name", uhe);
70              return "UNKNOWN_LOCALHOST";
71          }
72      }
73  }