• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

coditory / quark-uri / #3

pending completion
#3

push

github-actions

ogesaku
init

1212 of 1212 new or added lines in 21 files covered. (100.0%)

926 of 1212 relevant lines covered (76.4%)

0.76 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

52.0
/src/main/java/com/coditory/quark/uri/Hosts.java
1
package com.coditory.quark.uri;
2

3
import org.jetbrains.annotations.NotNull;
4

5
import java.io.BufferedReader;
6
import java.io.IOException;
7
import java.io.InputStream;
8
import java.io.InputStreamReader;
9
import java.net.InetAddress;
10
import java.net.UnknownHostException;
11
import java.util.function.Predicate;
12
import java.util.function.Supplier;
13
import java.util.regex.Pattern;
14

15
import static com.coditory.quark.uri.Preconditions.expectNonBlank;
16
import static com.coditory.quark.uri.Preconditions.expectNonNull;
17

18
public final class Hosts {
19
    private Hosts() {
×
20
        throw new UnsupportedOperationException("Do not instantiate utility class");
×
21
    }
22

23
    /**
24
     * @return local host name. Example: john-macbook-pro
25
     */
26
    @NotNull
27
    public static String getLocalHostName() {
28
        return LocalHostNameResolver.LOCAL_HOST_NAME;
1✔
29
    }
30

31
    /**
32
     * @return local host ip address. Example: 127.0.1.1
33
     */
34
    @NotNull
35
    public static String getLocalHostAddress() {
36
        return LocalHostAddressResolver.LOCAL_HOST_ADDRESS;
1✔
37
    }
38

39
    /**
40
     * Java Language Specification specifies that a class initialization occurs
41
     * the first time its methods or fields are used.
42
     * Nested static class can be used to implement lazy initialization.
43
     *
44
     * @link https://docs.oracle.com/javase/specs/jls/se7/html/jls-12.html#jls-12.4.2
45
     */
46
    private static class LocalHostNameResolver {
47
        private static final Predicate<String> IP_ADDRESS = Pattern.compile("(\\d{1,3}\\.){3}\\d{1,3}").asPredicate();
1✔
48
        static final String LOCAL_HOST_NAME = ResolverWithOsCommandFallback
1✔
49
                .resolveWithOsFallback(
1✔
50
                        "local host name",
51
                        LocalHostNameResolver::resolveLocalHostNameUsingJVM,
52
                        "hostname -f"
53
                );
54

55
        private LocalHostNameResolver() {
×
56
            throw new IllegalStateException("This is a utility class and cannot be instantiated");
×
57
        }
58

59
        private static String resolveLocalHostNameUsingJVM() {
60
            InetAddress localhost = getLocalHost();
1✔
61
            String canonicalHostName = localhost.getCanonicalHostName();
1✔
62
            String hostName = localhost.getHostName();
1✔
63
            return IP_ADDRESS.test(canonicalHostName)
1✔
64
                    ? hostName
×
65
                    : canonicalHostName;
1✔
66
        }
67

68
        private static InetAddress getLocalHost() {
69
            try {
70
                return InetAddress.getLocalHost();
1✔
71
            } catch (UnknownHostException e) {
×
72
                throw new RuntimeException("Could not get localhost address", e);
×
73
            }
74
        }
75
    }
76

77
    private static class LocalHostAddressResolver {
78
        static final String LOCAL_HOST_ADDRESS = ResolverWithOsCommandFallback
1✔
79
                .resolveWithOsFallback(
1✔
80
                        "local host address",
81
                        LocalHostAddressResolver::resolveLocalHostAddress,
82
                        "hostname -i"
83
                );
84

85
        private LocalHostAddressResolver() {
×
86
            throw new IllegalStateException("This is a utility class and cannot be instantiated");
×
87
        }
88

89
        private static String resolveLocalHostAddress() {
90
            InetAddress localhost = getLocalHost();
1✔
91
            return localhost.getHostAddress();
1✔
92
        }
93

94
        private static InetAddress getLocalHost() {
95
            try {
96
                return InetAddress.getLocalHost();
1✔
97
            } catch (UnknownHostException e) {
×
98
                throw new RuntimeException("Could not get localhost address", e);
×
99
            }
100
        }
101
    }
102

103
    private static class ResolverWithOsCommandFallback {
104
        static String resolveWithOsFallback(String name, Supplier<String> jvmSupplier, String systemCommand) {
105
            return new ResolverWithOsCommandFallback(name, jvmSupplier, systemCommand)
1✔
106
                    .resolve();
1✔
107
        }
108

109
        private final String name;
110
        private final Supplier<String> jvmSupplier;
111
        private final String[] systemCommand;
112

113
        ResolverWithOsCommandFallback(String name, Supplier<String> jvmSupplier, String systemCommand) {
1✔
114
            expectNonBlank(systemCommand, "systemCommand");
1✔
115
            this.name = expectNonBlank(name, "name");
1✔
116
            this.jvmSupplier = expectNonNull(jvmSupplier, "jvmSupplier");
1✔
117
            this.systemCommand = systemCommand.split(" ");
1✔
118
        }
1✔
119

120
        String resolve() {
121
            try {
122
                return resolveUsingJVM();
1✔
123
            } catch (Exception exception) {
×
124
                return resolveUsingOs();
×
125
            }
126
        }
127

128
        private String resolveUsingJVM() {
129
            return jvmSupplier.get();
1✔
130
        }
131

132
        private String resolveUsingOs() {
133
            try (
134
                    InputStream is = Runtime.getRuntime()
×
135
                            .exec(systemCommand)
×
136
                            .getInputStream();
×
137
                    BufferedReader reader = new BufferedReader(new InputStreamReader(is))
×
138
            ) {
139
                StringBuilder builder = new StringBuilder();
×
140
                String line;
141
                while ((line = reader.readLine()) != null) {
×
142
                    builder.append(line).append("\n");
×
143
                }
144
                return builder.toString().trim();
×
145
            } catch (IOException exception) {
×
146
                String cmd = String.join(" ", systemCommand);
×
147
                throw new RuntimeException(
×
148
                        "Could not resolve " + name + ". OS does not support command: " + cmd,
149
                        exception
150
                );
151
            }
152
        }
153
    }
154
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc