Skip to content

Commit 7008455

Browse files
authored
Merge pull request #148 from basil/lock-strategy
Use NIO Ivy file lock strategy
2 parents c301ab0 + f57c78e commit 7008455

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

src/main/java/org/jenkinsci/plugins/workflow/cps/global/GrapeHack.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,20 @@
3535
import java.net.URLClassLoader;
3636
import java.util.logging.Level;
3737
import java.util.logging.Logger;
38+
import org.apache.ivy.core.cache.DefaultRepositoryCacheManager;
39+
import org.apache.ivy.core.cache.RepositoryCacheManager;
40+
import org.apache.ivy.core.settings.IvySettings;
41+
import org.apache.ivy.plugins.lock.LockStrategy;
42+
import org.apache.ivy.plugins.lock.NoLockStrategy;
3843

3944
public class GrapeHack {
4045

4146
private static final Logger LOGGER = Logger.getLogger(GrapeHack.class.getName());
4247

48+
@SuppressFBWarnings(value = "MS_SHOULD_BE_FINAL", justification = "for script console")
49+
public static boolean DISABLE_NIO_FILE_LOCK =
50+
Boolean.getBoolean(GrapeHack.class.getName() + ".DISABLE_NIO_FILE_LOCK");
51+
4352
@SuppressFBWarnings(value="DP_CREATE_CLASSLOADER_INSIDE_DO_PRIVILEGED", justification="the least of our concerns")
4453
@Initializer(after=InitMilestone.PLUGINS_PREPARED, fatal=false)
4554
public static void hack() throws Exception {
@@ -63,6 +72,32 @@ public static void hack() throws Exception {
6372
l = engine.getClass().getClassLoader();
6473
LOGGER.log(Level.FINE, "was also able to load {0}", l.loadClass(ivyGrabRecordName));
6574
LOGGER.log(Level.FINE, "linked to {0}", l.loadClass("org.apache.ivy.core.module.id.ModuleRevisionId").getProtectionDomain().getCodeSource().getLocation());
75+
if (!DISABLE_NIO_FILE_LOCK) {
76+
try {
77+
/*
78+
* We must use reflection instead of simply casting to GrapeIvy and invoking directly due to the use of
79+
* MaskingClassLoader a few lines above.
80+
*/
81+
IvySettings settings = (IvySettings) c.getMethod("getSettings").invoke(instance.get(null));
82+
RepositoryCacheManager repositoryCacheManager = settings.getDefaultRepositoryCacheManager();
83+
if (repositoryCacheManager instanceof DefaultRepositoryCacheManager) {
84+
DefaultRepositoryCacheManager defaultRepositoryCacheManager =
85+
(DefaultRepositoryCacheManager) repositoryCacheManager;
86+
LockStrategy lockStrategy = defaultRepositoryCacheManager.getLockStrategy();
87+
LOGGER.log(Level.FINE, "default lock strategy {0}", lockStrategy);
88+
if (lockStrategy == null || lockStrategy instanceof NoLockStrategy) {
89+
lockStrategy = settings.getLockStrategy("artifact-lock-nio");
90+
if (lockStrategy != null) {
91+
defaultRepositoryCacheManager.setLockStrategy(lockStrategy.getName());
92+
defaultRepositoryCacheManager.setLockStrategy(lockStrategy);
93+
}
94+
}
95+
LOGGER.log(Level.FINE, "using lock strategy {0}", defaultRepositoryCacheManager.getLockStrategy());
96+
}
97+
} catch (RuntimeException | LinkageError x) {
98+
LOGGER.log(Level.FINE, "failed to enable NIO file lock", x);
99+
}
100+
}
66101
}
67102

68103
private GrapeHack() {}

0 commit comments

Comments
 (0)