diff options
author | jonmv <venstad@gmail.com> | 2022-04-08 22:26:37 +0200 |
---|---|---|
committer | jonmv <venstad@gmail.com> | 2022-04-08 22:26:37 +0200 |
commit | 1c27950698237c7ebe0f8ea7ed5848889d6b4759 (patch) | |
tree | 6748bfa41fac16ad9b22d0afba4d150b44414542 /vespajlib/src/main/java/com/yahoo/net/HostName.java | |
parent | c5c330f63efb47dad355860db70bf3a89a737970 (diff) |
Revert "Merge pull request #22068 from vespa-engine/jonmv/unify-hostname-classes"
This reverts commit 8aa4c83df5ce7843c040afa41706fcc7c3afd030, reversing
changes made to f95ad44fae879da9db19f73eabe62c53baeb0c36.
Diffstat (limited to 'vespajlib/src/main/java/com/yahoo/net/HostName.java')
-rw-r--r-- | vespajlib/src/main/java/com/yahoo/net/HostName.java | 37 |
1 files changed, 29 insertions, 8 deletions
diff --git a/vespajlib/src/main/java/com/yahoo/net/HostName.java b/vespajlib/src/main/java/com/yahoo/net/HostName.java index 7446771f57c..20f1008055e 100644 --- a/vespajlib/src/main/java/com/yahoo/net/HostName.java +++ b/vespajlib/src/main/java/com/yahoo/net/HostName.java @@ -1,18 +1,39 @@ +// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.net; +import ai.vespa.http.DomainName; +import ai.vespa.validation.PatternedStringWrapper; + import java.util.Optional; +import java.util.regex.Pattern; + +import static ai.vespa.validation.Validation.requireLength; /** - * This class has utilities for getting the hostname of the system running the JVM. + * Hostnames match {@link #hostNamePattern}, and are restricted to 64 characters in length. + * + * This class also 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 + * @author jonmv */ -public class HostName { +public class HostName extends PatternedStringWrapper<HostName> { + + static final Pattern labelPattern = Pattern.compile("([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])"); + static final Pattern hostNamePattern = Pattern.compile("(" + labelPattern + "\\.)*" + labelPattern); - private static String preferredHostName = null; + private static HostName preferredHostName = null; + + private HostName(String value) { + super(requireLength(value, "hostname length", 1, 64), hostNamePattern, "hostname"); + } + + public static HostName of(String value) { + return new HostName(value); + } /** * Return a public and fully qualified hostname for localhost that @@ -24,19 +45,19 @@ public class HostName { if (preferredHostName == null) { preferredHostName = getPreferredHostName(); } - return preferredHostName; + return preferredHostName.value(); } - static private String getPreferredHostName() { + static private HostName getPreferredHostName() { Optional<String> vespaHostEnv = Optional.ofNullable(System.getenv("VESPA_HOSTNAME")); if (vespaHostEnv.isPresent() && ! vespaHostEnv.get().trim().isEmpty()) { - return vespaHostEnv.get().trim(); + return of(vespaHostEnv.get().trim()); } - return "localhost"; + return of("localhost"); } public static void setHostNameForTestingOnly(String hostName) { - preferredHostName = hostName; + preferredHostName = HostName.of(hostName); } } |