001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache license, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the license for the specific language governing permissions and
015 * limitations under the license.
016 */
017package org.apache.logging.log4j.core.util;
018
019import java.io.File;
020import java.net.InetAddress;
021import java.net.MalformedURLException;
022import java.net.NetworkInterface;
023import java.net.SocketException;
024import java.net.URI;
025import java.net.URISyntaxException;
026import java.net.URL;
027import java.net.UnknownHostException;
028import java.util.Enumeration;
029
030import org.apache.logging.log4j.Logger;
031import org.apache.logging.log4j.status.StatusLogger;
032
033/**
034 *
035 */
036public final class NetUtils {
037
038    private static final Logger LOGGER = StatusLogger.getLogger();
039    private static final String UNKNOWN_LOCALHOST = "UNKNOWN_LOCALHOST";
040
041    private NetUtils() {
042        // empty
043    }
044
045    /**
046     * This method gets the network name of the machine we are running on.
047     * Returns "UNKNOWN_LOCALHOST" in the unlikely case where the host name
048     * cannot be found.
049     *
050     * @return String the name of the local host
051     */
052    public static String getLocalHostname() {
053        try {
054            final InetAddress addr = InetAddress.getLocalHost();
055            return addr.getHostName();
056        } catch (final UnknownHostException uhe) {
057            try {
058                final Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
059                while (interfaces.hasMoreElements()) {
060                    final NetworkInterface nic = interfaces.nextElement();
061                    final Enumeration<InetAddress> addresses = nic.getInetAddresses();
062                    while (addresses.hasMoreElements()) {
063                        final InetAddress address = addresses.nextElement();
064                        if (!address.isLoopbackAddress()) {
065                            final String hostname = address.getHostName();
066                            if (hostname != null) {
067                                return hostname;
068                            }
069                        }
070                    }
071                }
072            } catch (final SocketException se) {
073                LOGGER.error("Could not determine local host name", uhe);
074                return UNKNOWN_LOCALHOST;
075            }
076            LOGGER.error("Could not determine local host name", uhe);
077            return UNKNOWN_LOCALHOST;
078        }
079    }
080
081    /**
082     * Converts a URI string or file path to a URI object
083     * @param path the URI string or path
084     * @return the URI object
085     */
086    public static URI toURI(final String path) {
087        try {
088            // Resolves absolute URI
089            return new URI(path);
090        } catch (final URISyntaxException e) {
091            // A file path or a Apache Commons VFS URL might contain blanks.
092            // A file path may start with a driver letter
093            try {
094                final URL url = new URL(path);
095                return new URI(url.getProtocol(), url.getHost(), url.getPath(), null);
096            } catch (MalformedURLException | URISyntaxException nestedEx) {
097                return new File(path).toURI();
098            }
099        }
100    }
101
102}