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                int colon = host.indexOf(':');
055                if (colon > 0) {
056                    return host.substring(0, colon);
057                }
058            }
059            throw uhe;
060        }
061    }
062
063    /**
064     * When using the {@link java.net.InetAddress#getHostName()} method in an environment where neither a proper DNS
065     * lookup nor an <tt>/etc/hosts</tt> entry exists for a given host, the following exception will be thrown:
066     * <p/>
067     * <code>
068     * java.net.UnknownHostException: &lt;hostname&gt;: &lt;hostname&gt;
069     * at java.net.InetAddress.getLocalHost(InetAddress.java:1425)
070     * ...
071     * </code>
072     * <p/>
073     * Instead of just throwing an UnknownHostException and giving up, this method grabs a suitable hostname from the
074     * exception and prevents the exception from being thrown. If a suitable hostname cannot be acquired from the
075     * exception, then <tt>null</tt> is returned
076     *
077     * @return the hostname, or <tt>null</tt> if not possible to resolve
078     */
079    public static String getLocalHostNameSafe() {
080        try {
081            return getLocalHostName();
082        } catch (Exception e) {
083            // ignore
084        }
085        return null;
086    }
087
088}