Minor fix in flow IP Matching
[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 import java.util.concurrent.Callable;
4 import java.util.concurrent.locks.Lock;
5
6 import static com.google.common.base.Preconditions.*;
7
8 import java.lang.reflect.Constructor;
9 import java.lang.reflect.InvocationTargetException;
10 import java.util.Arrays;
11 import java.util.List;
12
13 import com.google.common.base.Joiner;
14 import com.google.common.base.Optional;
15
16 public final class ClassLoaderUtils {
17
18     private ClassLoaderUtils() {
19         throw new UnsupportedOperationException("Utility class");
20     }
21
22     public static <V> V withClassLoader(ClassLoader cls, Callable<V> function) throws Exception {
23         return withClassLoaderAndLock(cls, Optional.<Lock> absent(), function);
24     }
25
26     public static <V> V withClassLoaderAndLock(ClassLoader cls, Lock lock, Callable<V> function) throws Exception {
27         checkNotNull(lock, "Lock should not be null");
28         return withClassLoaderAndLock(cls, Optional.of(lock), function);
29     }
30
31     public static <V> V withClassLoaderAndLock(ClassLoader cls, Optional<Lock> lock, Callable<V> function)
32             throws Exception {
33         checkNotNull(cls, "Classloader should not be null");
34         checkNotNull(function, "Function should not be null");
35         if (lock.isPresent()) {
36             lock.get().lock();
37         }
38         ClassLoader oldCls = Thread.currentThread().getContextClassLoader();
39         try {
40             Thread.currentThread().setContextClassLoader(cls);
41             return function.call();
42         } finally {
43             Thread.currentThread().setContextClassLoader(oldCls);
44             if (lock.isPresent()) {
45                 lock.get().unlock();
46             }
47         }
48     }
49
50     public static Object construct(Constructor<? extends Object> constructor, List<Object> objects)
51             throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
52         Object[] initargs = objects.toArray(new Object[] {});
53         return constructor.newInstance(initargs);
54     }
55
56     public static Class<?> loadClassWithTCCL(String name) throws ClassNotFoundException {
57         if ("byte[]".equals(name)) {
58             return byte[].class;
59         } else if("char[]".equals(name)) {
60             return char[].class;
61         }
62         try {
63             return Thread.currentThread().getContextClassLoader().loadClass(name);
64         } catch (ClassNotFoundException e) {
65             String[] components = name.split("\\.");
66             String potentialOuter;
67             int length = components.length;
68             if (length > 2 && (potentialOuter = components[length - 2]) != null && Character.isUpperCase(potentialOuter.charAt(0))) {
69                 
70                     String outerName = Joiner.on(".").join(Arrays.asList(components).subList(0, length - 1));
71                     String innerName = outerName + "$" + components[length-1];
72                     return Thread.currentThread().getContextClassLoader().loadClass(innerName);
73             } else {
74                 throw e;
75             }
76         }
77     }
78
79     public static Class<?> tryToLoadClassWithTCCL(String fullyQualifiedName) {
80         try {
81             return loadClassWithTCCL(fullyQualifiedName);
82         } catch (ClassNotFoundException e) {
83             return null;
84         }
85     }
86 }