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.util;
18
19 import java.io.File;
20 import java.net.InetAddress;
21 import java.net.MalformedURLException;
22 import java.net.NetworkInterface;
23 import java.net.SocketException;
24 import java.net.URI;
25 import java.net.URISyntaxException;
26 import java.net.URL;
27 import java.net.UnknownHostException;
28 import java.util.Enumeration;
29
30 import org.apache.logging.log4j.Logger;
31 import org.apache.logging.log4j.status.StatusLogger;
32
33 /**
34 * Networking-related convenience methods.
35 */
36 public final class NetUtils {
37
38 private static final Logger LOGGER = StatusLogger.getLogger();
39 private static final String UNKNOWN_LOCALHOST = "UNKNOWN_LOCALHOST";
40
41 private NetUtils() {
42 // empty
43 }
44
45 /**
46 * This method gets the network name of the machine we are running on. Returns "UNKNOWN_LOCALHOST" in the unlikely
47 * case where the host name cannot be found.
48 *
49 * @return String the name of the local host
50 */
51 public static String getLocalHostname() {
52 try {
53 final InetAddress addr = InetAddress.getLocalHost();
54 return addr.getHostName();
55 } catch (final UnknownHostException uhe) {
56 try {
57 final Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
58 while (interfaces.hasMoreElements()) {
59 final NetworkInterface nic = interfaces.nextElement();
60 final Enumeration<InetAddress> addresses = nic.getInetAddresses();
61 while (addresses.hasMoreElements()) {
62 final InetAddress address = addresses.nextElement();
63 if (!address.isLoopbackAddress()) {
64 final String hostname = address.getHostName();
65 if (hostname != null) {
66 return hostname;
67 }
68 }
69 }
70 }
71 } catch (final SocketException se) {
72 LOGGER.error("Could not determine local host name", uhe);
73 return UNKNOWN_LOCALHOST;
74 }
75 LOGGER.error("Could not determine local host name", uhe);
76 return UNKNOWN_LOCALHOST;
77 }
78 }
79
80 /**
81 * Converts a URI string or file path to a URI object.
82 *
83 * @param path the URI string or path
84 * @return the URI object
85 */
86 public static URI toURI(final String path) {
87 try {
88 // Resolves absolute URI
89 return new URI(path);
90 } catch (final URISyntaxException e) {
91 // A file path or a Apache Commons VFS URL might contain blanks.
92 // A file path may start with a driver letter
93 try {
94 final URL url = new URL(path);
95 return new URI(url.getProtocol(), url.getHost(), url.getPath(), null);
96 } catch (MalformedURLException | URISyntaxException nestedEx) {
97 return new File(path).toURI();
98 }
99 }
100 }
101
102 }