Using Service Component Runtime


I goal is to remove dependencies on OSGi from my bundles. I use felix (v 4.2.1) as impl and run it embeddable. I install org.apache.felix.scr (v. 1.6.2) bundle to have Service Component Runtime support. But when I run

ServiceReference ref = bundleContext().getServiceReference(ScrService.class.getName()); ScrService s = (ScrService) bundleContext().getService(ref);

I get ClassCastException: org.apache.felix.scr.impl.ComponentRegistry cannot be cast to org.apache.felix.scr.ScrService.

Okay. I will modify System Packages.

config.put(Constants.FRAMEWORK_SYSTEMPACKAGES, "org.apache.felix.scr");

Now I get

Caused by: org.osgi.framework.BundleException: Unresolved constraint in bundle org.apache.felix.scr [1]: Unable to resolve 1.0: missing requirement [1.0] osgi.wiring.package; (&(osgi.wiring.package=org.osgi.framework)(version>=1.4.0)(!(version>=2.0.0))) at org.apache.felix.framework.Felix.resolveBundleRevision(Felix.java:3974) at org.apache.felix.framework.Felix.startBundle(Felix.java:2037) at org.apache.felix.framework.BundleImpl.start(BundleImpl.java:955) at org.apache.felix.framework.BundleImpl.start(BundleImpl.java:942) at com.copyright.rup.communications.felix.Felix.addBundle(Felix.java:86) ... 28 more

How can I solve it?


I suspect your first block where you try to get hold of ScrService is on the embedding side (i.e. outside of the framework, not from within an installed bundle).

If this is the case, then you have two copies of the ScrService - one loaded from your embedding code's ClassLoader and one loaded by ClassLoader of the scr bundle when it's resolved by the framework. This is why you're seeing the ClassCastException.

You can just export what the SCR bundle's exports from the framework bundle.

Section 3.8, page 51 of the <a href="http://www.osgi.org/Download/File?url=/download/r5/osgi.core-5.0.0.pdf" rel="nofollow">OSGi Core spec v5</a> states that in resolution of bundle wiring if a module has both import and export definitions of the same package then the framework will first try to resolve externally and if successful discard the overlapping export definition.

So copy the SCR runtime bundle's Export-Package manifest header as a framework property:

properties.put(Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA, "org.ops4j.pax.url.mvn,org.apache.felix.scr;uses:=\"org.osgi.framework," + "org.osgi.service.component\";version=\"1.7\"," + "org.apache.felix.scr.component;status=provisional;mandatory:=status;" + "uses:=\"org.osgi.service.component\";version=\"1.0\"," + "org.osgi.service.component;uses:=\"org.osgi.framework\";version=\"1.2\""); //Which you pass to the FrameworkFactory ... ServiceLoader<FrameworkFactory> loader = ServiceLoader.load(FrameworkFactory.class); Iterator<FrameworkFactory> iterator = loader.iterator(); Framework framework = iterator.next().newFramework(properties); framework.start();

A couple of things to note:


If you're not already using ConfigurationAdmin, then also install <a href="http://search.maven.org/#artifactdetails%7Corg.osgi%7Corg.osgi.compendium%7C4.3.1%7Cjar" rel="nofollow">org.osgi.compendium</a> to ensure you have at least have the API classes for org.osgi.service.cm.* and org.osgi.service.metatype*, AFAIK these are necessary for the SCR runtime.

</li> <li>

You shouldn't use Constants.FRAMEWORK_SYSTEMPACKAGES unless you're seriously customising the actual framework, instead you'd probably want to use Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA to <em>extend</em> what is exported by the framework bundle. (The framework implementations have pretty good defaults for FRAMEWORK_SYSTEMPACKAGES and it's normally unnecessary to modify this when embedding).

</li> </ul>


  • Drawing Separate Domains for Axes under Multiple Small Barcharts in D3.js
  • Twig in jquery not able to read jquery variable
  • Handle both URL (matrix && query) parameters in Angular 4+
  • WebStorm debugging create-react-native-app
  • How to implement idiomatic operator overloading for values and references in Rust?
  • format of ByteArray returned from C is invalid
  • How to insert and update existing array data with one single query in Codeigniter ?
  • Use tornado future to fetch url, two different ways get different results
  • Issue binding to ListBox WP7
  • How can I pull data (JSON) from a clicked ng-repeat item to load a new, item specific page using $st
  • Getting this “org.xhtmlrenderer.render.BlockBox cannot be cast to org.xhtmlrenderer.newtable.TableBo
  • Run perl file from PHP script but not wait for output on Windows Server
  • PhoneGap FileReader/readAsDataURL Not Triggering Callbacks
  • JSF validateLength question
  • Redmine can't generate secret token
  • Launch Dash from Jupyter Notebook
  • Caffe : train network accuracy = 1 constant ! Accuracy issue
  • Map Annotation Disclosure Indicator - Xamarin.Form
  • How to implement Google Drive and Google Plus sdk in ios project
  • Bison does not appear to recognize C string literals appropriately
  • Connect to a local database from phpmyadmin with R
  • Create an Office365 mailbox from within C# Web API method
  • Year over Year Stats from a Crossfilter Dataset
  • Ruby regex for matching simpliest Ruby's regexes
  • Why is ordered choice in pyparsing failing for my use case?
  • I am consuming a WCF service that requires headers from a .NET 2 website. How can I programmatically
  • matrix multiplication apache pig
  • Google App Engine Datastore: Dealing with eventual consistency
  • Spring Boot fails to start
  • Unity3d lost directional light shadows after generate assetBundle (.unity3d file)
  • Grails - How to implement a foreign key relationship not using an id column?
  • How to use FirstOrDefault inside Include
  • PHP Permalinks.. how to change?
  • media foundation H264 decoder not working properly
  • Running R's aov() mixed effects model from Python using rpy2
  • Access to a Matlab gui from the web
  • Time Complexity of Fibonacci Algorithm [duplicate]