Unified Two Phase Commit implementation, fixed BA to BI connection
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / main / java / org / opendaylight / controller / sal / binding / impl / util / ClassLoaderUtils.java
1 package org.opendaylight.controller.sal.binding.impl.util;
2
3
4
5 import java.util.concurrent.Callable;
6
7 import static com.google.common.base.Preconditions.*;
8
9 import java.lang.reflect.Constructor;
10 import java.lang.reflect.InvocationTargetException;
11 import java.util.ArrayList;
12
13 import org.opendaylight.yangtools.yang.binding.Identifier;
14
15 public class ClassLoaderUtils {
16     
17     public static <V> V withClassLoader(ClassLoader cls,Callable<V> function) throws Exception {
18         checkNotNull(cls);
19         checkNotNull(function);
20         ClassLoader oldCls = Thread.currentThread().getContextClassLoader();
21         try {
22             Thread.currentThread().setContextClassLoader(cls);
23             V result = function.call();
24             Thread.currentThread().setContextClassLoader(oldCls);
25             return result;
26         } catch (Exception e) {
27             Thread.currentThread().setContextClassLoader(oldCls);
28             throw new Exception(e);
29         }
30     }
31
32     public static Object construct(Constructor<? extends Object> constructor, ArrayList<Object> objects) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
33     Object[] initargs = objects.toArray(new Object[]{});
34     return constructor.newInstance(initargs);
35     }
36 }