Hi all,
I want to call the osgi bundle's service in servlet which is in non-bundle webapp,
i catch the exception:
java.lang.ClassCastException: com.example.time.local.service.LocalTimeService cannot be cast to com.example.time.service.TimeService
My code:
public class HelloWorldServlet extends HttpServlet {
private static final long serialVersionUID = 5423372534281029927L;
@Resource(name="BundleContext")
private BundleContext context;
@SuppressWarnings({ "rawtypes", "unchecked" })
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
ServiceReference helloServiceReference = context.getServiceReference(TimeService.class.getName());
TimeService service = (TimeService)context.getService(helloServiceReference);
String message = service.getTime();
PrintWriter out = resp.getWriter();
out.println(message);
out.close();
}
}
Note: The LocalTimeService is implements TimeService, and the TimeService, LocalTimeService are in osgi bundle.
I think this maybe because of the classloader is different, I have tried add the <module name="org.jboss.osgi.framework" export="true" /> in module.xml and add the <module name="org.jboss.osgi.framework" export="true" /> in jboss-deployment-structure.xml, these two methods dont solve this issue.
Can anybody help me to work out this problem,
Thanks.