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=6478a03bbc025f8a7fa1f6ec937270a80d807797;hp=ccf99dfe379681b85e613214bfe996d309a318ad;hb=9d90b3f545a3ac32b198a5cabe606b411a6d081b;hpb=d04486e64562956fd518cc35225a924f65cb1c69 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 ccf99dfe37..6478a03bbc 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 @@ -3,6 +3,7 @@ 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.*; @@ -10,22 +11,35 @@ import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; -import org.opendaylight.yangtools.yang.binding.Identifier; +import com.google.common.base.Optional; public class ClassLoaderUtils { public static V withClassLoader(ClassLoader cls,Callable function) throws Exception { - checkNotNull(cls); - checkNotNull(function); + 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 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(); - Thread.currentThread().setContextClassLoader(oldCls); return result; - } catch (Exception e) { + } finally { Thread.currentThread().setContextClassLoader(oldCls); - throw new Exception(e); + if(lock.isPresent()) { + lock.get().unlock(); + } } } @@ -33,4 +47,13 @@ public class ClassLoaderUtils { Object[] initargs = objects.toArray(new Object[]{}); return constructor.newInstance(initargs); } + + + public static Class loadClassWithTCCL(String name) throws ClassNotFoundException { + if("byte[]".equals(name)) { + return byte[].class; + } + + return Thread.currentThread().getContextClassLoader().loadClass(name); + } } \ No newline at end of file