-public class ClassLoaderUtils {
-
- public static <V> V withClassLoader(ClassLoader cls,Callable<V> function) throws Exception {
- checkNotNull(cls);
- checkNotNull(function);
+ public static <V> V withClassLoader(ClassLoader cls, Callable<V> function) throws Exception {
+ return withClassLoaderAndLock(cls, Optional.<Lock> absent(), function);
+ }
+
+ public static <V> V withClassLoaderAndLock(ClassLoader cls, Lock lock, Callable<V> function) throws Exception {
+ checkNotNull(lock, "Lock should not be null");
+ return withClassLoaderAndLock(cls, Optional.of(lock), function);
+ }
+
+ public static <V> V withClassLoaderAndLock(ClassLoader cls, Optional<Lock> lock, Callable<V> function)
+ throws Exception {
+ checkNotNull(cls, "Classloader should not be null");
+ checkNotNull(function, "Function should not be null");
+ if (lock.isPresent()) {
+ lock.get().lock();
+ }