1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
43 }
44
45
46
47
48
49
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
82
83
84
85
86 public static byte[] getMacAddress() {
87 byte[] mac = null;
88 try {
89 final InetAddress localHost = InetAddress.getLocalHost();
90 try {
91 final NetworkInterface localInterface = NetworkInterface.getByInetAddress(localHost);
92 if (isUpAndNotLoopback(localInterface)) {
93 mac = localInterface.getHardwareAddress();
94 }
95 if (mac == null) {
96 final Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
97 while (networkInterfaces.hasMoreElements() && mac == null) {
98 final NetworkInterface nic = networkInterfaces.nextElement();
99 if (isUpAndNotLoopback(nic)) {
100 mac = nic.getHardwareAddress();
101 }
102 }
103 }
104 } catch (final SocketException e) {
105 LOGGER.catching(e);
106 }
107 if (mac == null || mac.length == 0) {
108 mac = localHost.getAddress();
109 }
110 } catch (final UnknownHostException ignored) {
111
112 }
113 return mac;
114 }
115
116
117
118
119
120 public static String getMacAddressString() {
121 final byte[] macAddr = getMacAddress();
122 if (macAddr != null && macAddr.length > 0) {
123 StringBuilder sb = new StringBuilder(String.format("%02x", macAddr[0]));
124 for (int i = 1; i < macAddr.length; ++i) {
125 sb.append(":").append(String.format("%02x", macAddr[i]));
126 }
127 return sb.toString();
128
129 }
130 return null;
131 }
132
133 private static boolean isUpAndNotLoopback(final NetworkInterface ni) throws SocketException {
134 return ni != null && !ni.isLoopback() && ni.isUp();
135 }
136
137
138
139
140
141
142
143 public static URI toURI(final String path) {
144 try {
145
146 return new URI(path);
147 } catch (final URISyntaxException e) {
148
149
150 try {
151 final URL url = new URL(path);
152 return new URI(url.getProtocol(), url.getHost(), url.getPath(), null);
153 } catch (MalformedURLException | URISyntaxException nestedEx) {
154 return new File(path).toURI();
155 }
156 }
157 }
158
159 }