diff options
author | jonmv <venstad@gmail.com> | 2022-04-08 19:09:50 +0200 |
---|---|---|
committer | jonmv <venstad@gmail.com> | 2022-04-08 19:09:50 +0200 |
commit | 8167db32acfebd835e1c5ecbb54b2dc1f16a3ccc (patch) | |
tree | 819530a97ce1499dabe94922c27c1da4eed85342 /vespajlib | |
parent | dfbdd8f1593d16031e8d1adbda79d4d58c49164b (diff) |
Add files that INtelliJ removed
Diffstat (limited to 'vespajlib')
-rw-r--r-- | vespajlib/src/main/java/com/yahoo/net/HostName.java | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/vespajlib/src/main/java/com/yahoo/net/HostName.java b/vespajlib/src/main/java/com/yahoo/net/HostName.java new file mode 100644 index 00000000000..7446771f57c --- /dev/null +++ b/vespajlib/src/main/java/com/yahoo/net/HostName.java @@ -0,0 +1,42 @@ +package com.yahoo.net; + +import java.util.Optional; + +/** + * This class has utilities for getting the hostname of the system running the JVM. + * Detection of the hostname is now done before starting any Vespa + * programs and provided in the environment variable VESPA_HOSTNAME; + * if that variable isn't set a default of "localhost" is always returned. + * + * @author arnej + */ +public class HostName { + + private static String preferredHostName = null; + + /** + * Return a public and fully qualified hostname for localhost that + * resolves to an IP address on a network interface. + * + * @return the preferred name of localhost + */ + public static synchronized String getLocalhost() { + if (preferredHostName == null) { + preferredHostName = getPreferredHostName(); + } + return preferredHostName; + } + + static private String getPreferredHostName() { + Optional<String> vespaHostEnv = Optional.ofNullable(System.getenv("VESPA_HOSTNAME")); + if (vespaHostEnv.isPresent() && ! vespaHostEnv.get().trim().isEmpty()) { + return vespaHostEnv.get().trim(); + } + return "localhost"; + } + + public static void setHostNameForTestingOnly(String hostName) { + preferredHostName = hostName; + } + +} |