简体   繁体   中英

Can't get a working reference to an OSGi service


I'm trying to embed apache felix into a simple hello world java project with maven, but I can't find a way to get a reference to a service of a bundle. I've installed org.apache.felix.bundlerepository bundle into OSGi from a jar and also added it as a maven dependency to my project. After that I'm starting the bundle, getting BundleContext from it and then calling getServiceReference(RepositoryAdmin.class.getName()) on that bundle context. The first thing I'm unhappy with is that I have to use BundleContext from the installed bundle, if I'd use BundleContext of the Framework the ServiceReference will be always null. This is not convinient.

The second, more important issue, is that when I finally receiving a reference to RepositoryAdmin service from bundlerepository bundle I can't cast it to org.apache.felix.bundlerepository.RepositoryAdmin, executing the following code:

(RepositoryAdmin)admin.getBundleContext().getService(ref)

will throw this exception:

java.lang.ClassCastException: org.apache.felix.bundlerepository.impl.RepositoryAdminImpl cannot be cast to org.apache.felix.bundlerepository.RepositoryAdmin 

I know this is a kind of classpath issue and may be caused by incompatibility of interfaces, but I'm using a bundle jar of the same version (2.0.2) as a maven dependency of my project. I'm also aware of Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA osgi configuration option which should force a bundle to use a package from the host application, but it didnt help me.

Here is the main class of my project https://github.com/ArtemZ/osgi-study/blob/master/src/main/java/com/artemz/demo/Main.java which is messy because I was trying different things on it in order to get a reference to a service, but none actually worked for me.

Hope someone will help me, because I'm really desperate with this issue.

Thanks for giving some more details about what you are doing. I already followed the mails on the felix list. Now I think I understand what happens. The reason why you can not get the service from outside the RepositoryAdmin bundle is that the package you define in

FRAMEWORK_SYSTEMPACKAGES_EXTRA is "org.apache.felix.bundlerepository; version=2.0.2"

is not the same version as the package from the RepositoryAdmin bundle. I downloaded the bundle and looked into the Manifest:

Export-Package: org.osgi.service.repository;version="1.0";uses:="org.osg
 i.resource",org.apache.felix.bundlerepository;version="2.1";uses:="org.
 osgi.framework"

So as you see the version you should export from the system bundle is 2.1 not 2.0.2. In OSGi the versions are defined per package not on the bundle level. So while most times they are the same this is not always true. Espcecially for OSGi spec packages.

So when the package version are different you have two effects: 1. You will not be able to find a service with a different package 2. If you get a service object in some other way like you did then you will have a class cast exception as they are loaded by different classloaders.

So can you try the 2.1 version and report if it works?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM