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.camel.util;
018
019import java.net.InetAddress;
020import java.net.UnknownHostException;
021
022/**
023 * Util class for {@link java.net.InetAddress}
024 */
025public final class InetAddressUtil {
026
027    private InetAddressUtil() {
028        // util class
029    }
030
031    /**
032     * When using the {@link java.net.InetAddress#getHostName()} method in an environment where neither a proper DNS
033     * lookup nor an <tt>/etc/hosts</tt> entry exists for a given host, the following exception will be thrown:
034     * <p/>
035     * <code>
036     * java.net.UnknownHostException: &lt;hostname&gt;: &lt;hostname&gt;
037     * at java.net.InetAddress.getLocalHost(InetAddress.java:1425)
038     * ...
039     * </code>
040     * <p/>
041     * Instead of just throwing an UnknownHostException and giving up, this method grabs a suitable hostname from the
042     * exception and prevents the exception from being thrown. If a suitable hostname cannot be acquired from the
043     * exception, only then is the <tt>UnknownHostException</tt> thrown.
044     *
045     * @return                      the hostname
046     * @throws UnknownHostException is thrown if hostname could not be resolved
047     */
048    public static String getLocalHostName() throws UnknownHostException {
049        try {
050            return (InetAddress.getLocalHost()).getHostName();
051        } catch (UnknownHostException uhe) {
052            String host = uhe.getMessage(); // host = "hostname: hostname"
053            if (host != null) {
054                return StringHelper.before(host, ":");
055            }
056            throw uhe;
057        }
058    }
059
060    /**
061     * When using the {@link java.net.InetAddress#getHostName()} method in an environment where neither a proper DNS
062     * lookup nor an <tt>/etc/hosts</tt> entry exists for a given host, the following exception will be thrown:
063     * <p/>
064     * <code>
065     * java.net.UnknownHostException: &lt;hostname&gt;: &lt;hostname&gt;
066     * at java.net.InetAddress.getLocalHost(InetAddress.java:1425)
067     * ...
068     * </code>
069     * <p/>
070     * Instead of just throwing an UnknownHostException and giving up, this method grabs a suitable hostname from the
071     * exception and prevents the exception from being thrown. If a suitable hostname cannot be acquired from the
072     * exception, then <tt>null</tt> is returned
073     *
074     * @return the hostname, or <tt>null</tt> if not possible to resolve
075     */
076    public static String getLocalHostNameSafe() {
077        try {
078            return getLocalHostName();
079        } catch (Exception e) {
080            // ignore
081        }
082        return null;
083    }
084
085}