diff --git a/internal/zinc-ivy-integration/src/main/java/sbt/inc/ZincBridgeProvider.java b/internal/zinc-ivy-integration/src/main/java/sbt/inc/ZincBridgeProvider.java index 985861793..f68ff596c 100644 --- a/internal/zinc-ivy-integration/src/main/java/sbt/inc/ZincBridgeProvider.java +++ b/internal/zinc-ivy-integration/src/main/java/sbt/inc/ZincBridgeProvider.java @@ -12,6 +12,7 @@ import xsbti.Logger; import xsbti.compile.CompilerBridgeProvider; import java.io.File; +import java.util.concurrent.Callable; public final class ZincBridgeProvider { /** @@ -46,6 +47,44 @@ public final class ZincBridgeProvider { return ZincComponentCompiler$.MODULE$.getDefaultConfiguration(baseDirectory, ivyHome, resolvers, logger); } + /** + * Cast a CheckedException as an unchecked one. + * + * @param throwable to cast + * @param the type of the Throwable + * @return this method will never return a Throwable instance, it will just throw it. + * @throws T the throwable as an unchecked throwable + */ + @SuppressWarnings("unchecked") + private static RuntimeException rethrow(Throwable throwable) throws T { + throw (T) throwable; // rely on vacuous cast + } + + + /** + * Defines a global lock that does nothing but calling the callable to synchronize + * across threads. The lock file is used to resolve and download dependencies via ivy. + *

+ * This operation is necesary to invoke {@link ZincBridgeProvider#getProvider(File, GlobalLock, ComponentProvider, IvyConfiguration, Logger)}. + * + * @return A default global lock. + */ + public static GlobalLock getDefaultLock() { + return new GlobalLock() { + @Override + public T apply(File lockFile, Callable run) { + T value = null; + try { + value = run.call(); + } catch (Exception e) { + // Rethrow runtime exception because apply does not define throwing Exception + rethrow(e); + } + return value; + } + }; + } + /** * Get a compiler bridge provider that allows the user to fetch Scala and a compiled bridge. *