"Fossies" - the Fresh Open Source Software Archive 
Member "gradle-8.1.1/subprojects/core/src/testFixtures/groovy/org/gradle/util/ports/AbstractAvailablePortAllocator.groovy" (20 Apr 2023, 3169 Bytes) of package /linux/misc/gradle-8.1.1.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Java source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
1 /*
2 * Copyright 2015 the original author or authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package org.gradle.util.ports
18 import com.google.common.annotations.VisibleForTesting
19 import com.google.common.collect.ImmutableList
20 import org.gradle.internal.Pair
21
22 import java.util.concurrent.locks.Lock
23 import java.util.concurrent.locks.ReentrantLock
24
25 abstract class AbstractAvailablePortAllocator implements PortAllocator {
26 private final List<ReservedPortRange> reservations = []
27 protected final Lock lock = new ReentrantLock()
28 @VisibleForTesting
29 ReservedPortRangeFactory portRangeFactory = new DefaultReservedPortRangeFactory()
30
31 @VisibleForTesting
32 public List<ReservedPortRange> getReservations() {
33 return ImmutableList.copyOf(reservations)
34 }
35
36 @Override
37 public int assignPort() {
38 try {
39 lock.lock()
40 return reservePort()
41 } finally {
42 lock.unlock()
43 }
44 }
45
46 @Override
47 public void releasePort(int port) {
48 if (port == null || port < MIN_PRIVATE_PORT || port > MAX_PRIVATE_PORT) {
49 return
50 }
51
52 try {
53 lock.lock()
54 for (int i = 0; i < reservations.size(); i++) {
55 ReservedPortRange range = reservations.get(i)
56 if (range.allocated.contains(port)) {
57 range.deallocate(port)
58 if (reservations.size() > 1 && range.allocated.isEmpty()) {
59 releaseRange(range)
60 }
61 }
62 }
63 } finally {
64 lock.unlock()
65 }
66 }
67
68 private int reservePort() {
69 while(true) {
70 for (int i = 0; i < reservations.size(); i++) {
71 ReservedPortRange range = reservations.get(i)
72 int port = range.allocate()
73 if (port > 0) {
74 return port
75 }
76 }
77 // if we couldn't allocate a port from the existing reserved port ranges, get another range
78 reservePortRange()
79 }
80 }
81
82 private ReservedPortRange reservePortRange() {
83 def portRange = getNextPortRange(reservations.size())
84 ReservedPortRange range = portRangeFactory.getReservedPortRange(portRange.left, portRange.right)
85 reservations.add(range)
86 return range
87 }
88
89 protected abstract Pair<Integer, Integer> getNextPortRange(int rangeNumber)
90
91 private void releaseRange(ReservedPortRange range) {
92 try {
93 lock.lock();
94 reservations.remove(range)
95 } finally {
96 lock.unlock();
97 }
98 }
99 }