X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-binding-broker%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fsal%2Fbinding%2Fimpl%2Futil%2FClassLoaderUtils.java;fp=opendaylight%2Fmd-sal%2Fsal-binding-broker%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fsal%2Fbinding%2Fimpl%2Futil%2FClassLoaderUtils.java;h=ccf99dfe379681b85e613214bfe996d309a318ad;hb=d2f2d5a34ccd0a715ab6ea7c1f5ac61f16f6e6cc;hp=0000000000000000000000000000000000000000;hpb=87246fdb069fad43135b623971bdf6ebe7df828a;p=controller.git 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 new file mode 100644 index 0000000000..ccf99dfe37 --- /dev/null +++ b/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/impl/util/ClassLoaderUtils.java @@ -0,0 +1,36 @@ +package org.opendaylight.controller.sal.binding.impl.util; + + + +import java.util.concurrent.Callable; + +import static com.google.common.base.Preconditions.*; + +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.util.ArrayList; + +import org.opendaylight.yangtools.yang.binding.Identifier; + +public class ClassLoaderUtils { + + public static V withClassLoader(ClassLoader cls,Callable function) throws Exception { + checkNotNull(cls); + checkNotNull(function); + ClassLoader oldCls = Thread.currentThread().getContextClassLoader(); + try { + Thread.currentThread().setContextClassLoader(cls); + V result = function.call(); + Thread.currentThread().setContextClassLoader(oldCls); + return result; + } catch (Exception e) { + Thread.currentThread().setContextClassLoader(oldCls); + throw new Exception(e); + } + } + + public static Object construct(Constructor constructor, ArrayList objects) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { + Object[] initargs = objects.toArray(new Object[]{}); + return constructor.newInstance(initargs); + } +} \ No newline at end of file