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;h=d230fd17f9475c24bd920094a233734291593901;hb=3dcdf33c8cbc2eba8b2e6cb0f110a17edfe57ec4;hp=6478a03bbc025f8a7fa1f6ec937270a80d807797;hpb=d490a11b531a724b9f46ca931b2c98e9527dde7f;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 index 6478a03bbc..d230fd17f9 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,7 +1,5 @@ package org.opendaylight.controller.sal.binding.impl.util; - - import java.util.concurrent.Callable; import java.util.concurrent.locks.Lock; @@ -9,51 +7,79 @@ import static com.google.common.base.Preconditions.*; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; -import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import com.google.common.base.Joiner; import com.google.common.base.Optional; -public class ClassLoaderUtils { - - public static V withClassLoader(ClassLoader cls,Callable function) throws Exception { - return withClassLoaderAndLock(cls, Optional.absent(), function); +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"); + + 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 { + + 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()) { + 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 result; - } finally { + return function.call(); + } finally { Thread.currentThread().setContextClassLoader(oldCls); - if(lock.isPresent()) { + 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); + try { + return Thread.currentThread().getContextClassLoader().loadClass(name); + } catch (ClassNotFoundException e) { + String[] components = name.split("\\."); + String potentialOuter; + int length = components.length; + if (length > 2 && (potentialOuter = components[length - 2]) != null && Character.isUpperCase(potentialOuter.charAt(0))) { + + String outerName = Joiner.on(".").join(Arrays.asList(components).subList(0, length - 1)); + String innerName = outerName + "$" + components[length-1]; + return Thread.currentThread().getContextClassLoader().loadClass(innerName); + } else { + throw e; + } + } + } + + public static Class tryToLoadClassWithTCCL(String fullyQualifiedName) { + try { + return loadClassWithTCCL(fullyQualifiedName); + } catch (ClassNotFoundException e) { + + } + return null; } } \ No newline at end of file