Set a bundle start level programatically on embedded Apache Felix 5 - osgi

I have embedded Apache Felix 5.0.0 in an application I'm building.
When installing the bundles to the Felix framework object, I have no way of setting the bundles start level from code, and I read everywhere that people do it by hand using Gogo, but because I'm going to automatically install and start the bundles, I'd like to be able to set the start level from my code.
How can I do that? I've read about some StartLevel class and such, but I have found no actual working code that shows how to use it to set the bundle start level.

I guess you have a Framework instance if you use embedded Felix. In this case I think the following could work:
Bundle bundle = framework.getBundleContext().installBundle(location);
BundleStartLevel bundleStartLevel = bundle.adapt(BundleStartLevel.class);
bundleStartLevel.setStartLevel(xxx);

Related

Getting logging/debugging information out of Felix framework?

I am trying to launch the Felix OSGI Framework using AutoProcessor.process(...) to load my OSGI bundles. I have specified the directory containing the bundles using the felix.auto.deploy.dir property.
When that directory is empty I get no messages at all from Felix. When there are bundles in it I get a not very helpful stack dump.
How does one tell the Felix framework to output logging/debugging information? What I really want is for AutoProcessor to tell me which bundle it is working on when the stack dump occurs.
I have tried setting felix.log.level to 4; I have tried setting up an org.osgi.framework.FrameworkListener and an org.osgi.framework.BundleListener; and I have tried specifying a Logger with felix.log.logger; but Felix remains stubbornly taciturn.
So what's the secret trick?
After removing the duplicate org.osgi.core bundle as mentioned above, I tried once more to set up
an org.osgi.framework.FrameworkListener,
an org.osgi.framework.BundleListener, and
an org.osgi.framework.ServiceListener
on the framework that I had created. All 3 listener types fired and I was able to examine and log information from the events passed in as arguments.

How can I run OSGi blueprint in Felix in Scala?

I am able to get OSGi blueprint to work in Karaf, but I don't understand how to do it in Apache Felix, or my question can be also defined as how can I use OSGi blueprint in plain OSGi?
I made an example here https://github.com/PhilAndrew/sbt-osgi-felix-akka-blueprint-camel using https://github.com/doolse/sbt-osgi-felix in which Akka is working but Blueprint and Camel not yet working.
This question may help Is Apache Aries running in Felix?
It's not starting Blueprint because your bundle's code doesn't actually rely on any of the classes in blueprint, so you either need to add "Require-Bundle" header to your manifest:
requireBundle := Seq("org.apache.aries.blueprint")
or add the bundles that needed to be started to the osgiDependencies of run:
osgiDependencies in run := bundleReqs("org.apache.aries.blueprint.core",
"org.apache.aries.proxy")
Apache Karaf also runs on felix. So for the most part you just need to install the correct bundles and maybe do the necessary system package exports if you hit package use constraint violations.
Check what karaf installs and try to do the same.

Does Apache Felix FileInstall works for dynamic configuration of an application?

I am trying to develop an application that can be configured by its users. I need the configuration to be done by installing/updating/stopping/uninstalling bundles. All this should be of course done dynamically during the run-time of the application.
I found a nice framework which is Apache Felix FileInstall that provides a directory in which it seems to add a bundle when you add the bundle file in the directory (update, and remove bundles similarly).
But I can see that this method does not work in my case. I need to have the bundles in the directory but to stop or even uninstall them by my application. And I want to install them when it is appropriate. This is how I am expecting the configuration of my application to be done.
Is what I am trying to achieve supported by Apache FileInstall? Am I making any wrong assumptions about this framework? What are other possible ways that would help me if Apache FileInstall is not enough? Thanks.
You don't need FileInstall for this, just use the OSGi APIs. You specifically mentioned installing, updating, stopping and uninstalling; these are supported with the following API calls respectively:
BundleContext.installBundle
Bundle.update
Bundle.stop
Bundle.uninstall
Incidentally these are exactly the same methods that are called by FileInstall to implements its directory-based bundle management.

How and where configure bundles through "apache felix configAdmin"

I try to use the "Apache Felix" implementation of OSGi for my project, I launch the framework from the distributable jar (/bin /felix.jar). I have created simple example bundles to register or consume services in the registry.
I want to use a configuration management service so I installed the bundle "org.apache.felix.configadmin" felix in the framework, and then I do not know how and where to put the configuration file of another bundle, for example I want to install the bundle "org.apache.felix.http.jetty" and then I want to configure this bundle through the configAdmin, so how I rename my config file, and where to put it?
Another question, what is the difference between setting, for example, the property "org.osgi.service.http.port" of the jetty bundle using the framework properties(conf/config.properties) or configure it through the bundle of configuration admin service.
Best regards,
You probably also need the file-installer from felix for it to pick up the config files.
If you want to see a working example of this take a look at Apache Karaf. It's a OSGi Container with lots of nice preconfigured features. The config admin service works out of the box.
Normally you use the felix config admin service together with the felix fileinstall. So fileinstall takes care of the directory to be monitored for config files and hands over to config admin to make it available as configs in OSGi.
For a fully working example take a look at Apache Karaf. There you can see the configs you need for it to work together. You might also consider to simply install your own bundles into karaf as it makes managing the felix framework a lot easier.

OSGI Embedded Equinox - Bundle to access pojos not instantiated in osgi framework

I have a server side application and want to embed an osgi framework into for dynamic bundle loading.
Suppose I want to expose a QuoteImpl implementing IQuote(instantiated as part of the server container bootstrap/Spring) to be used by different Bundles.
Q1. Is there a clean way of exposing server-application instances to Bundles ? (btw because of legacy it is not possible to make server code into bundle :) and donot want to make entire application osgi'ed.
Tried exposing via a service and bundle to cast into an IQuote. Not sure I am doing it well but fails with unresolved compilation problems as IQuote resides in the core app projects as opposed to the bundle project. any ideas?
Yes the way to do this is with a service. The "host" application would publish the service and the bundles inside OSGi would consume the service in the normal way.
The key to get this working is that the service API (i.e. the package containing IQuote) must be exported by the host application through the system bundle exports. You can control this by setting the org.osgi.framework.system.packages.extra property when you create the embedded OSGi framework. I wrote a blog post on this subject that should help you get started (look for the heading "Exposing Application Packages").
You state that you have compilation problems. To fix those it's necessary to know how you have structured your projects and build system.
This is how I embedded Equinox OSGi runtime in my Java class. I suppose you could do the same. https://github.com/sarxos/equinox-launcher/blob/master/src/main/java/com/github/sarxos/equinox/Launcher.java

Resources