Consuming Services from Embedded OSGi Framework

梦想与她 提交于 2020-01-21 10:38:18

问题


I have embeddable Felix. I have some API bundle and Impl. API exports interface C.Impl imports that interface and register impl in activator. Now I want get C impl otside OSGi

  FrameworkFactory ff = new FrameworkFactory();
  ...
  BundleContext bc = fwk.getBundleContext();
  ...
  final ServiceReference[] serviceReferences = bc.getServiceReferences(C.class.getName(), "(objectclass=" + C.class.getName() + ")");
  for(ServiceReference serviceReference : serviceReferences){
     final Object service = bc.getService(serviceReference);
     ...
  }

Now I want to interact with it. I can do it with reflection

     System.out.println(service.getClass().getMethod("some").invoke(service)); //using 

But I can't cast it

     System.out.println(service instanceof C); //prints false

I guess that comes from different ClassLoaders. But how I can solve it? How we can interract with OSGi context from outside? Or we can obly put it all into OSGi container?


回答1:


If you are embedding OSGi, the API for the service (i.e. interface "C") has be to visible to the outer application and exported into OSGi via the system bundle exports. The outer application cannot import packages from the bundles contained inside the OSGi framework.



来源:https://stackoverflow.com/questions/15270044/consuming-services-from-embedded-osgi-framework

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!