0

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?

Neil Bartlett
  • 23,743
  • 4
  • 44
  • 77
Stan Kurilin
  • 15,614
  • 21
  • 81
  • 132

1 Answers1

5

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.

Neil Bartlett
  • 23,743
  • 4
  • 44
  • 77
  • Thanks. So I should write in my Impl bundle `Export-Package: c.api;version="1.0.0.SNAPSHOT" Import-Package: org.osgi.framework;version="[1.5,2)"` and keep `c.api` otside bundles. Am I right? – Stan Kurilin Mar 07 '13 at 11:59
  • 1
    no. Make sure C is on the normal Java class path (the same that contains your framework). Then on launching the framework you set the property org.osgi.system.packages.extra (please verify that name) with the package of C. The bundle must then import that package. – Peter Kriens Mar 07 '13 at 13:03