Question

I'm trying to build a Desktop Application using E4 with his OSGi(Equinox) Environment. For my User Security im using Shiro. But i can load class from my OSGi but shiro cant!

In my Bundle i try this :

InitActivator.java :

public class InitActivator implements BundleActivator {
private static BundleContext context;

static BundleContext getContext() {
    return context;
}

@Override
public void start(BundleContext context) throws Exception {

    //1. OSGi loadClass function
    System.err.println(context.getBundle().loadClass("com.firm.demo.MyCustomClass")
                    .getName());
    //2. Using Apache Shiro ClassUtils
    System.err.println("Shiro : " + ClassUtils.forName("com.firm.demo.MyCustomClass"));

    }

 }

The 1. system.err return the right class with his qualified name. The 2. system.err return a org.apache.shiro.util.UnknownClassException: Unable to load class named

How can i use Shiro into OSGi to find Class with Name?

Was it helpful?

Solution

If you look at the source of ClassUtils, you will see how it tries to load the classes: http://grepcode.com/file/repo1.maven.org/maven2/org.apache.shiro/shiro-core/1.0.0-incubating/org/apache/shiro/util/ClassUtils.java#ClassUtils.forName%28java.lang.String%29

The first thing it tries is to load the class with the help of the ClassLoader attached to the thread. If it fails, it tries to load with the ClassLoader that loaded ClassUtils. If it fails, it tries to load the class with the system ClassLoader.

You can trick the first one, the thread context classloader. I must mention that this is only a workaround, not a solution that is nice in the OSGi world:

BundleWiring bundleWiring = context.getBundle().adapt(BundleWiring.class);
ClassLoader bundleClassLoader = bundleWiring.getClassLoader();
Thread currentThread = Thread.currentThread();

ClassLoader originalCl = currentThread.getContextClassLoader()
currentThread.setContectClassLoader(bundleClassLoader);
try {
    System.err.println("Shiro : " + ClassUtils.forName("com.firm.demo.MyCustomClass"));
} finally {
    currentThread.setContextClassLoader(originalCl);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top