X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-binding-broker%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fsal%2Fbinding%2Fimpl%2Futil%2FClassLoaderUtils.java;h=cb3206a3b30f8709fda36137ebd218edb8d4e06e;hp=87f31ac5c435820c5ddb5ec505e3dabbab94eba8;hb=287dad5e6d7196591f25af5bddd8b28c7b067304;hpb=da5e7b7312777d4a59e099ed207f61f730092416 diff --git a/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/impl/util/ClassLoaderUtils.java b/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/impl/util/ClassLoaderUtils.java index 87f31ac5c4..cb3206a3b3 100644 --- a/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/impl/util/ClassLoaderUtils.java +++ b/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/impl/util/ClassLoaderUtils.java @@ -1,43 +1,62 @@ package org.opendaylight.controller.sal.binding.impl.util; - - import java.util.concurrent.Callable; +import java.util.concurrent.locks.Lock; import static com.google.common.base.Preconditions.*; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; +import java.util.List; + +import com.google.common.base.Optional; + +public final class ClassLoaderUtils { + + private ClassLoaderUtils() { + throw new UnsupportedOperationException("Utility class"); + } + + public static V withClassLoader(ClassLoader cls, Callable function) throws Exception { + return withClassLoaderAndLock(cls, Optional. absent(), function); + } + + public static V withClassLoaderAndLock(ClassLoader cls, Lock lock, Callable function) throws Exception { + checkNotNull(lock, "Lock should not be null"); + return withClassLoaderAndLock(cls, Optional.of(lock), function); + } -public class ClassLoaderUtils { - - public static V withClassLoader(ClassLoader cls,Callable function) throws Exception { - checkNotNull(cls); - checkNotNull(function); + public static V withClassLoaderAndLock(ClassLoader cls, Optional lock, Callable function) + throws Exception { + checkNotNull(cls, "Classloader should not be null"); + checkNotNull(function, "Function should not be null"); + if (lock.isPresent()) { + lock.get().lock(); + } ClassLoader oldCls = Thread.currentThread().getContextClassLoader(); try { Thread.currentThread().setContextClassLoader(cls); - V result = function.call(); + return function.call(); + } finally { Thread.currentThread().setContextClassLoader(oldCls); - return result; - } catch (Exception e) { - Thread.currentThread().setContextClassLoader(oldCls); - throw new Exception(e); + if (lock.isPresent()) { + lock.get().unlock(); + } } } - public static Object construct(Constructor constructor, ArrayList objects) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { - Object[] initargs = objects.toArray(new Object[]{}); - return constructor.newInstance(initargs); + public static Object construct(Constructor constructor, List objects) + throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { + Object[] initargs = objects.toArray(new Object[] {}); + return constructor.newInstance(initargs); } - - + public static Class loadClassWithTCCL(String name) throws ClassNotFoundException { - if("byte[]".equals(name)) { + if ("byte[]".equals(name)) { return byte[].class; } - + return Thread.currentThread().getContextClassLoader().loadClass(name); } } \ No newline at end of file