All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.hpsocket.soa.framework.logging.RuntimeContainer Maven / Gradle / Ivy

Go to download

hp-soa: a fully functional, easy-to-use, and highly scalable microservice framework

There is a newer version: 1.1.4
Show newest version
package io.github.hpsocket.soa.framework.logging;

import static io.github.hpsocket.soa.framework.logging.RuntimeContainerProperties.PROPERTY_LOGSTASH_GELF_FQDN_HOSTNAME;
import static io.github.hpsocket.soa.framework.logging.RuntimeContainerProperties.PROPERTY_LOGSTASH_GELF_HOSTNAME;
import static io.github.hpsocket.soa.framework.logging.RuntimeContainerProperties.PROPERTY_LOGSTASH_GELF_HOSTNAME_RESOLUTION_ORDER;
import static io.github.hpsocket.soa.framework.logging.RuntimeContainerProperties.PROPERTY_LOGSTASH_GELF_SKIP_HOSTNAME_RESOLUTION;
import static io.github.hpsocket.soa.framework.logging.RuntimeContainerProperties.RESOLUTION_ORDER_LOCALHOST_NETWORK_FALLBACK;
import static io.github.hpsocket.soa.framework.logging.RuntimeContainerProperties.RESOLUTION_ORDER_NETWORK_LOCALHOST_FALLBACK;
import static io.github.hpsocket.soa.framework.logging.RuntimeContainerProperties.getProperty;

import java.io.IOException;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

import io.github.hpsocket.soa.framework.logging.gelf.intern.ErrorReporter;

/**
 * Static Details about the runtime container: Hostname (simple/fqdn), Address and timestamp of the first access (time when the
 * application was loaded).
 * 
 * @author Mark Paluch
 */
public class RuntimeContainer {

    /**
     * Current Hostname.
     */
    public static String HOSTNAME;

    /**
     * Current FQDN Hostname.
     */
    public static String FQDN_HOSTNAME;

    /**
     * Current Address.
     */
    public static String ADDRESS;

    /**
     * Load-Time of this class.
     */
    public static final long FIRST_ACCESS;

    private static boolean initialized;

    /**
     * Utility Constructor.
     */
    private RuntimeContainer() {

    }

    static {
        FIRST_ACCESS = System.currentTimeMillis();
    }

    /**
     * Initialize only once.
     * @param errorReporter the error reporter
     */
    public static void initialize(ErrorReporter errorReporter) {

        if (!initialized) {
            lookupHostname(errorReporter);
            initialized = true;
        }
    }

    /**
     * Triggers the hostname lookup.
     * @param errorReporter the error reporter
     */
    public static void lookupHostname(ErrorReporter errorReporter) {
        String myHostName = getProperty(PROPERTY_LOGSTASH_GELF_HOSTNAME, "unknown");
        String myFQDNHostName = getProperty(PROPERTY_LOGSTASH_GELF_FQDN_HOSTNAME, "unknown");
        String myAddress = "";

        if (!Boolean.parseBoolean(getProperty(PROPERTY_LOGSTASH_GELF_SKIP_HOSTNAME_RESOLUTION, "false"))) {

            try {

                String resolutionOrder = getProperty(PROPERTY_LOGSTASH_GELF_HOSTNAME_RESOLUTION_ORDER,
                        RESOLUTION_ORDER_NETWORK_LOCALHOST_FALLBACK);

                InetAddress inetAddress = null;
                if (resolutionOrder.equals(RESOLUTION_ORDER_NETWORK_LOCALHOST_FALLBACK)) {
                    inetAddress = getInetAddressWithHostname();
                }

                if (resolutionOrder.equals(RESOLUTION_ORDER_LOCALHOST_NETWORK_FALLBACK)) {
                    if (isQualified(InetAddress.getLocalHost())) {
                        inetAddress = InetAddress.getLocalHost();
                    } else {
                        inetAddress = getInetAddressWithHostname();
                    }
                }

                if (inetAddress == null) {
                    inetAddress = InetAddress.getLocalHost();
                }

                myHostName = getHostname(inetAddress, false);
                myFQDNHostName = getHostname(inetAddress, true);
                myAddress = inetAddress.getHostAddress();
            } catch (IOException e) {
                errorReporter.reportError("Cannot resolve hostname", e);
            }
        }

        FQDN_HOSTNAME = myFQDNHostName;
        HOSTNAME = myHostName;
        ADDRESS = myAddress;
    }

    private static String getHostname(InetAddress inetAddress, boolean fqdn) throws IOException {

        String hostname = inetAddress.getHostName();
        if (hostname.indexOf('.') != -1 && !fqdn) {
            hostname = hostname.substring(0, hostname.indexOf('.'));
        }

        return hostname;
    }

    private static InetAddress getInetAddressWithHostname() throws SocketException {
        Enumeration netInterfaces = NetworkInterface.getNetworkInterfaces();

        while (netInterfaces.hasMoreElements()) {
            NetworkInterface ni = netInterfaces.nextElement();
            Enumeration ias = ni.getInetAddresses();
            while (ias.hasMoreElements()) {
                InetAddress inetAddress = ias.nextElement();

                if (!isQualified(inetAddress)) {
                    continue;
                }
                return inetAddress;
            }
        }

        return null;
    }

    private static boolean isQualified(InetAddress inetAddress) {
        return !inetAddress.isLoopbackAddress() && !inetAddress.getHostAddress().equals(inetAddress.getCanonicalHostName());
    }

    public static void main(String[] args) {
        System.out.println("Host-Resolution: " + FQDN_HOSTNAME + "/" + HOSTNAME);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy