Search code examples
osgiapache-felixjetty-9

OSGi Bundle in Felix - ClassNotFoundException for Jetty class loaded by name


pom.xml:

<Import-Package>
         org.eclipse.jetty.websocket.server,*
</Import-Package>

MANIFEST.MF:

Import-Package: org.eclipse.jetty.websocket.server;version="[9.2,10)"

Exception in Logs:

javax.servlet.ServletException: java.lang.ClassNotFoundException: org.eclipse.jetty.websocket.server.WebSocketServerFactory

Felix Web Console:

Imported Packages org.eclipse.jetty.websocket.server,version=9.2.6 from org.apache.felix.http.jetty (39)

Relevant code in org.eclipse.jetty.websocket.servlet.WebSocketServletFactory:

Class<WebSocketServletFactory> wssf = (Class<WebSocketServletFactory>)loader
                    .loadClass("org.eclipse.jetty.websocket.server.WebSocketServerFactory");

Please help me figure out what I am doing wrong here?


Solution

  • I've ran into the same problem, and asked on the Felix mailing list. As pointed out by Balazs, it has to do with the "loader" part. The thread at http://www.mail-archive.com/users%40felix.apache.org/msg16222.html contains a URL to some examples in which the ContextClassLoader is used.

    With thanks to Paul, basically you need something like this:

    // Cache the current classloader
    ClassLoader ccl = Thread.currentThread().getContextClassLoader();
    // Find the classloader used by the bundle providing jetty
    ClassLoader classLoader = jettyBundle.getClassLoader();
    // Set the classloader
    Thread.currentThread().setContextClassLoader(classLoader);
    
    // Register the servlet
    httpService.registerServlet("/servletName", new MyWebSocketServlet(), null, null);         
    
    // Restore the classloader
    Thread.currentThread().setContextClassLoader(ccl);
    

    See the examples for more complete code.