|
32 | 32 | package org.opensearch.gradle.testclusters; |
33 | 33 |
|
34 | 34 | import groovy.lang.Closure; |
| 35 | + |
35 | 36 | import org.opensearch.gradle.FileSystemOperationsAware; |
36 | 37 | import org.opensearch.gradle.test.Fixture; |
37 | 38 | import org.opensearch.gradle.util.GradleUtils; |
|
46 | 47 | import org.gradle.internal.resources.ResourceLock; |
47 | 48 | import org.gradle.internal.resources.SharedResource; |
48 | 49 |
|
| 50 | +import java.lang.invoke.MethodHandles; |
| 51 | +import java.lang.invoke.MethodType; |
49 | 52 | import java.util.ArrayList; |
50 | 53 | import java.util.Collection; |
51 | 54 | import java.util.Collections; |
@@ -124,12 +127,32 @@ public List<ResourceLock> getSharedResources() { |
124 | 127 |
|
125 | 128 | int nodeCount = clusters.stream().mapToInt(cluster -> cluster.getNodes().size()).sum(); |
126 | 129 | if (nodeCount > 0) { |
127 | | - locks.add(resource.getResourceLock(Math.min(nodeCount, resource.getMaxUsages()))); |
| 130 | + locks.add(getResourceLock(resource, Math.min(nodeCount, resource.getMaxUsages()))); |
128 | 131 | } |
129 | 132 |
|
130 | 133 | return Collections.unmodifiableList(locks); |
131 | 134 | } |
132 | 135 |
|
| 136 | + private ResourceLock getResourceLock(SharedResource resource, int nUsages) { |
| 137 | + try { |
| 138 | + try { |
| 139 | + // The getResourceLock(int) is used by Gradle pre-7.5 |
| 140 | + return (ResourceLock) MethodHandles.publicLookup() |
| 141 | + .findVirtual(SharedResource.class, "getResourceLock", MethodType.methodType(ResourceLock.class, int.class)) |
| 142 | + .bindTo(resource) |
| 143 | + .invokeExact(nUsages); |
| 144 | + } catch (NoSuchMethodException | IllegalAccessException ex) { |
| 145 | + // The getResourceLock() is used by Gradle post-7.4 |
| 146 | + return (ResourceLock) MethodHandles.publicLookup() |
| 147 | + .findVirtual(SharedResource.class, "getResourceLock", MethodType.methodType(ResourceLock.class)) |
| 148 | + .bindTo(resource) |
| 149 | + .invokeExact(); |
| 150 | + } |
| 151 | + } catch (Throwable ex) { |
| 152 | + throw new IllegalStateException("Unable to find suitable ResourceLock::getResourceLock", ex); |
| 153 | + } |
| 154 | + } |
| 155 | + |
133 | 156 | @Override |
134 | 157 | public Task dependsOn(Object... dependencies) { |
135 | 158 | super.dependsOn(dependencies); |
|
0 commit comments