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

evolvedbinary / elemental / 982

29 Apr 2025 08:34PM UTC coverage: 56.409% (+0.007%) from 56.402%
982

push

circleci

adamretter
[feature] Improve README.md badges

28451 of 55847 branches covered (50.94%)

Branch coverage included in aggregate %.

77468 of 131924 relevant lines covered (58.72%)

0.59 hits per line

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

62.5
/exist-core/src/main/java/org/exist/util/IPUtil.java
1
/*
2
 * Elemental
3
 * Copyright (C) 2024, Evolved Binary Ltd
4
 *
5
 * admin@evolvedbinary.com
6
 * https://www.evolvedbinary.com | https://www.elemental.xyz
7
 *
8
 * Use of this software is governed by the Business Source License 1.1
9
 * included in the LICENSE file and at www.mariadb.com/bsl11.
10
 *
11
 * Change Date: 2028-04-27
12
 *
13
 * On the date above, in accordance with the Business Source License, use
14
 * of this software will be governed by the Apache License, Version 2.0.
15
 *
16
 * Additional Use Grant: Production use of the Licensed Work for a permitted
17
 * purpose. A Permitted Purpose is any purpose other than a Competing Use.
18
 * A Competing Use means making the Software available to others in a commercial
19
 * product or service that: substitutes for the Software; substitutes for any
20
 * other product or service we offer using the Software that exists as of the
21
 * date we make the Software available; or offers the same or substantially
22
 * similar functionality as the Software.
23
 */
24
package org.exist.util;
25

26
import net.jcip.annotations.GuardedBy;
27

28
import java.io.IOException;
29
import java.net.ServerSocket;
30
import java.util.Random;
31

32
/**
33
 * IP Utilities
34
 *
35
 * @author <a href="mailto:adam@evolvedbinary.com">Adam Retter</a>
36
 */
37
public class IPUtil {
×
38

39
    @GuardedBy("class")
40
    private static final Random random = new Random();
1✔
41

42
    /**
43
     * Attempts to get the next random free IP port in the range {@code from} and {@code to}.
44
     *
45
     * @param from start of the port range
46
     * @param to end of the port range
47
     * @param maxAttempts the number of attempts to make to find a free port
48
     *
49
     * @return a potentially free IP port. This is done on a best effort basis! It is possible that the port returned
50
     *     is free, but by time you come to use it, it is then in-use; if so, just try calling this again.
51
     *
52
     * @throws IllegalStateException if maxAttempts is exceeded
53
     */
54
    public static int nextFreePort(final int from, final int to, final int maxAttempts) {
55
        for (int attempts = 0; attempts < maxAttempts; attempts++) {
1!
56
            final int port = random(from, to);
1✔
57
            if (isLocalPortFree(port)) {
1!
58
                return port;
1✔
59
            }
60
        }
61

62
        throw new IllegalStateException("Exceeded MAX_RANDOM_PORT_ATTEMPTS");
×
63
    }
64

65
    private synchronized static int random(final int min, final int max) {
66
        return random.nextInt((max - min) + 1) + min;
1✔
67
    }
68

69
    private static boolean isLocalPortFree(final int port) {
70
        try {
71
            new ServerSocket(port).close();
1✔
72
            return true;
1✔
73
        } catch (final IOException e) {
×
74
            return false;
×
75
        }
76
    }
77
}
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