Knopflerfish OSGi 5.2.0

org.knopflerfish.util
Class ClassLoaderUtil

java.lang.Object
  extended by org.knopflerfish.util.ClassLoaderUtil

public class ClassLoaderUtil
extends java.lang.Object

Utility class for handling common class loading cases, like wrapping external libraries in the correct context class loader.


Constructor Summary
ClassLoaderUtil()
           
 
Method Summary
static java.lang.Object doContextClassLoader(java.lang.ClassLoader classloader, java.security.PrivilegedAction<java.lang.Object> action)
          Run the specified action in a specified ContextClassLoader.
static java.lang.Object doContextClassLoader(java.lang.ClassLoader classloader, java.security.PrivilegedExceptionAction<java.lang.Object> action)
          Run the specified action in a specified ContextClassLoader.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ClassLoaderUtil

public ClassLoaderUtil()
Method Detail

doContextClassLoader

public static java.lang.Object doContextClassLoader(java.lang.ClassLoader classloader,
                                                    java.security.PrivilegedAction<java.lang.Object> action)
Run the specified action in a specified ContextClassLoader.

The doContextClassLoader sets the current thread's context class loader to the specified classloader, calls the action's run method, resets the thread's context class loader and finally returns the resulting value.

Example:

    Object result = ClassLoaderUtil
      .doContextClassLoader(Activator.getClass().getClassLoader(),
                            new PrivilegedAction() {
                              public Object run() {
                                // Use external library
                                // ...
                                return someresult;
                              }
                            };
 
where Activator is the bundle activator, or any other class loaded by the bundle class loader.

Parameters:
classloader - Class loader to be used as the thread's context class loader.
action - Action code to run in the specified class loader.
The usage of the PrivilegedAction interface is to avoid creating a new interface with exactly the same methods. It does not imply that the code is run using AccessController.doPrivileged
Returns:
Value returned from the action.run

doContextClassLoader

public static java.lang.Object doContextClassLoader(java.lang.ClassLoader classloader,
                                                    java.security.PrivilegedExceptionAction<java.lang.Object> action)
                                             throws java.lang.Exception
Run the specified action in a specified ContextClassLoader.

As above, but accepts and rethrows exceptions from the action.

Parameters:
classloader - Class loader to be used as the thread's context class loader.
action - Action code to run in the specified class loader.
The usage of the PrivilegedAction interface is to avoid creating a new interface with exactly the same methods. It does not imply that the code is run using AccessController.doPrivileged
Returns:
Value returned from the action.run
Throws:
java.lang.Exception - if action.run throws an exception, pass this upwards

Knopflerfish OSGi 5.2.0