Karaf development - osgi

Im currently develop bundles for karaf and have some questions...
I wrote a bundle/webservice based on cxf, I try to deploy it in karaf but it could not start that bundle because it could not resolve some packages e.g.
org.osgi.framework.BundleException: Unresolved constraint in bundle org.springframework.aop [56]: Unable to resolve 56.0: missing
requirement [56.0] package; (&(package=org.aopalliance.aop)(version>=1.0.0)(!(version>=2.0.0)))
so here is a question, this package dependency comes from spring-aop (3.1.0.RELEASE), so where is the problem? what dependency is missing? how can I solve such problems?
In that case i did not clearly understand the development process. should i deploy all missing bundles in deploy? because i would like to keep thirdparty libs spereated from my developed bundles. And what bundles i have to deploy? Is it a trial and error process? Is there a common way to let maven do the dependency stuff?
I discovered a folder "system" and read on the docu that it is a repository like maven, is it for the features?
I had for test cases a karaf with some pre deployed bundles and put my webservice bundle into it, but again execeptions...
Caused by: java.lang.ClassNotFoundException: javax.servlet.http.HttpServlet
What dependency is missing?
I already read the tutorial about camel and karaf, but it did not explain the deployment stuff, so could anyone suggest me a good tutorial?
Thanks!
Chris

Short answer
Scroll down to the bit referring to "camel-cxf" and run the two commands features:addurl and features:install. I have a feeling this will resolve all your problems.
spring-aop
On Karaf console type:
exports | grep org.aopalliance.aop
I think you'll see lines like:
XX org.aopalliance.aop; version=3.1.0.RELEASE
So while the spring-aop bundle has the right packages they're the wrong version, the range being requested is >=1.0.0 and <2.0.0, so 3.1.0 doesn't satisfy that.
Deploying/Installing
You can drop bundles into ${karaf.home}/deploy or use the console.
You can install maven bundles from the Karaf console with:
install -s mvn:groupId/artifactId/version/packaging/classifier
Where -s starts the bundle and packaging/classifier are optional.
You can find a lot of OSGi ready maven dependencies here http://ebr.springsource.com/repository/app/ - I had a quick look but your spring aop dependency is very old, what version of CXF are you using?
Read up about Karaf features - they're basically XML files that list suites of bundles that can be installed. Very useful for deploying large numbers of bundles and they can be installed into a maven repository.
There are some standard features available in Karaf, try:
features:install war
This will give you a jetty webcontainer and may resolve your ClassNotFoundException: javax.servlet.http.HttpServlet as long as it's the right version
Camel also has a features file which probably sort all your issues, try this:
features:addurl mvn:org.apache.camel.karaf/apache-camel/2.9.0/xml/features
features:install camel-cxf
Tutorials
There's quite a bit available, some on http://karaf.apache.org and http://fusesource.com but also take a look at the PDF manual that comes in the Karaf distribution.
Always beware that info may be out-of-date

Please post your MANIFEST.MF file. I think you didn't not mention the tag in maven-bundle-plugin dependency.

Related

Why third party dependency is required exclusively from OSGi container even if I have it in my maven dependencies?

I want to know why OSGi do not respect the maven dependenceis.
I want to create one app in OSGi(AEM). I want to communicate(CRUD) to the database with the help of JPA(eclipselink).
I created maven project with aem-archetype.
Added all required dependencies(of JPA) into my maven project's pom file.
No errors in Eclipse, I built the project via mvn clean install and installed it into AEM(CQ5) via mvn sling:install. All good till now. No Errors.
But when I go and see my bundle in the felix console, I see that it is not Active but in Installed state.The error reported is that it could not resolve the javax.persistence package.
I was puzzled, I searched and I read about it here -
You have to make sure that you place the same version in another
bundle and deploy first. https://forums.adobe.com/thread/2325007
I converted JPA jar to OSGi bundle and installed in my OSGi container, and the error was gone. Great!
But why OSGi is not watching out for the dependencies I wrote in pom.xml of my maven project. Why it needs JPA strictly from OSGi bundle?
Maybe this is due to any architectural benefit, but could anyone please explain me here about this behaviour of OSGi? And why/how this feature of OSGi is useful ?
The <dependency> section of your Maven POM only covers your compile time dependencies. That means when you run Maven to build your project those dependencies are used to compile the source code and build your bundle. Maven itself is not aware of AEM or OSGi or any other platform or framework (e.g. Spring).
Maven just compiles your code.
You, as a developer, are responsible that all those required compile time dependencies are also available at runtime.
What we usually do is to create an AEM content package Maven module and put all of our required third party dependencies (e.g. JPA bundles) into it. This content package is then deployed by Maven so that those dependencies are also available at runtime.
Reason is: what you are adding as dependency is getting added in build path of your project and being available for your classes.When you run mvn install,it checks presence of all dependency and creates a bundle/jar for you.By default this bundle has only your project classes not other dependencies.
You need to check in depfinder whether external dependencies are already there in OSGi container,if not you have to load them in OSGi container either by embedding external dependencies in your bundle with the help of maven-bundle-plugin present in pom.xml or by making a bundle of jar file(I wont recommend that)which you have done.
I hope this helps!

no osgi ready dependencies

Currently I'm working with osgi and karaf.
My problem is the no "osgi ready" dependencies , which means a jar that is not ready to be deployed as a bundle into karaf for example.
I tried two solutions in order to deal with this kind of problems :
I tried to to use "Embed-Dependency" which will include the jar
dependency with the project... I don't think this could be a solution
because when I try to embed the jar , it will ask me to include other
jars that the first jar depend on , and so on ..
I tried to convert the no "osgi ready" jars into bundles using bnd tool or from "Plug-in from Existing JAR Archive" from eclipse project.
And this led to the same result , each jar will call another jar that it depend on it..
I am not sure if I'm doing it the wrong way or what is the problem exactly.
Any tips how to deal with no osgi ready dependencies ?
The simplest way to start is to use the wrap: protocol to auto create a jar. Behind the scenes it uses bnd to create a bundle on the fly. Simply prepend wrap: to the mvn url of the jar.
When you try to install the jar using bundle:install -s wrap:mvn:... karaf will tell you which imported packages are missing. Install jars that provide these packages in the same way. The pom of the jar can give you a hint what is missing.
This can mean to install lots of jars if your initial jars has lots of dependencies.
Once you have a list of jars that are installable together you can either create a feature using wrap protocol or you can make bundles from the individual at build time.
In any case you should take a look are the servicemix bundles. It provides OSGi ready bundles for many libraries.

What is the point of a features.xml if I have to install the bundles in it manually?

I have a features.xml with a few bundles listed. But they all need to be installed apparently. So all that the features.xml does is gather the dependencies in one spot....
Unless I am horribly mistaken. In which case how does one resolve a situation where I have a lot of dependencies in features.xml and they are all not in any order. Right now I am going through the exercise of installing each bundle and starting them up one by one...
Tell me what I am doing wrong- of which there may be several things
Edit:
Features were generated by maven plugin.
When I install feature, I get unresolved error
Error executing command: Unable to resolve root: missing requirement [root] osgi.identity; osgi.identity=myBundleApp; type=karaf.feature; version="[1.0.0.RC1,1.0.0.RC1]"; filter:="(&(osgi.identity=myBundleApp)(type=karaf.feature)(version>=1.0.0.RC1)(version<=1.0.0.RC1))" [caused by: Unable to resolve myBundleApp/1.0.0.RC1: missing requirement [myBundleApp/1.0.0.RC1] osgi.identity; osgi.identity=org.eclipse.jetty.websocket.server; type=osgi.bundle; version="[9.3.6.v20151106,9.3.6.v20151106]"; resolution:=mandatory [caused by: Unable to resolve org.eclipse.jetty.websocket.server/9.3.6.v20151106: missing requirement [org.eclipse.jetty.websocket.server/9.3.6.v20151106] osgi.extender; filter:="(osgi.extender=osgi.serviceloader.registrar)"]]
But the feature.xml already has <bundle>mvn:org.eclipse.jetty.websocket/websocket-server/9.3.6.v20151106</bundle>
The point of features.xml is to define a repository of features that can be used to provision an OSGi application in Apache Karaf. In another words, it's purpose is exactly to NOT have to install bundles manually! As documentation states:
When you install a feature, Apache Karaf installs all resources described in the feature. It means that it will automatically resolves and installs all bundles, configurations, and dependency features described in the feature.
That said, please keep in mind:
this is Karaf specific functionality. It will not work with other OSGi containers
before you can install a feature you need to make Karaf aware of the repository containing it. You can use feature:repo-list command to check what repos Karaf is aware of and feature:repo-add command to add repositories. Please see this documentation for more details.
URLs pointing to bundles, features, configurations, ... inside features.xml have to point to a reasoures Karaf understands and can access
Features (much like bundles) need to be resolved before they be activated.

How to deploy easily to Karaf Osgi container with maven project

I'm developing an OSGI bundle for parsing a PDF file using PDFBox library. I use maven to build the project and Karaf as the OSGI container. The PDFBox library is OSGI compatible so I thought this would be easy. But I just can't get the deployment model right.
In a traditional web app I would build a single WAR-file containing all the dependencies and put it in a Servlet container and it would get deployed. On the other hand the only way I've figured how to install an osgi bundle is by doing it by hand. I have to create an installation instruction file that lists all the dependencies that have to be manually downloaded and copied to the Karaf deploy folder, and be sure to do it in the right order. I feel like I'm back in the stone ages.
There has got to be an easier way, right? I still use maven to declare dependencies but I just have to use the provided scope. It would be great if those dependencies could be automatically installed.
I'm using the maven-bundle-plugin to generate a bundle from my application. It does generate an OBR repository(repository.xml) and I tried installing my bundle using obr karaf plugin but it still doesn't help with dependencies.
There are different possibilities for provisioning bundles. I prefer to install a bundle using Maven via the Karaf console such as:
install mvn:org.apache.pdfbox/pdfbox/1.8.4
If you don't want to install every bundle one by one, you could use so called features as described here. A feature lists all needed bundles:
<feature name='my-project' version='1.0.0'>
<feature version='2.4.0'>camel-spring</feature>
<bundle start-level='80' start='false'>mvn:com.mycompany.myproject/myproject-dao</bundle>
<bundle start-level='85' start='false'>mvn:com.mycompany.myproject/myproject-service</bundle>
<bundle start-level='85' start='false'>mvn:com.mycompany.myproject/myproject-camel-routing</bundle>
</feature>
You add a feature via Karaf console:
features:addUrl mvn:org.apache.servicemix.nmr/apache-servicemix-nmr/1.0.0-m2/xml/features
features:install nmr
Instead of the mvn handler, you could also use the file handler:
features:addUrl file:base/features/features.xml

Beginner starting large OSGi migration - osgi.wiring.package=android.dalvik?

I have a large Java application that's mostly networking and file processing with lots of DB access. There's no UI. We expose a few web services (embedded Netty) and call some external rest web services. The project is built with Ant. It's about 10 different jars plus maybe 30-40 libraries.
My current task is to move the project to the OSGi framework. I am startling slowly.
Following the examples in "OSGi In Action" chapter 6, I have used the BND ant task to put the entire project into one huge jar file. This worked. I am able to run the program using java -jar. Here's my current .bnd file:
-output: bundle/MerchantServicesBundle.jar
-include: manifest/merchantservices.manifest
Bundle-Name: MerchantServices
Bundle-SymbolicName: com.shopping.services.merchant
Bundle-Version: 4.1
Main-Class: com.shopping.merchant.services.netty.MerchantServices
Class-Path: /home/ppantera/repositories/MerchantJava/modules/MerchantServices/conf/
Private-Package: *
I am using Apache Felix 4.0.3. From the Gogo shell I can install the bundle but when I start it I get this:
org.osgi.framework.BundleException: Unresolved constraint in bundle com.shopping.services.merchant [8]: Unable to resolve 8.0: missing requirement [8.0] osgi.wiring.package; (osgi.wiring.package=android.dalvik)
Why does Felix think this is an Android project?
There doesn't seem to be much about this on the 'net. Would you recommend using an older version of Felix so I'm sheltered from the newer OSGi features that could cause me confusion?
I tried adding this to my .bnd file:
Require-Capability: osgi.ee; filter:="(&(osgi.ee=JavaSE)(version>=1.7))"
That didn't help. What am I doing wrong? Any other pointers?
It looks like somehow bnd detected a requirement to an Android package, and added that to the MANIFEST.MF, it could be in your code, but could also be in one of your 3rd party libraries.
Check your manifest to be sure, I guess you'll find something like
Import-Package:android.dalvik.
If that's the case you can test the bundle by manually removing that header and see if that helps. When you've got that clear, you can resolve it for example by making that import optional in bnd.
One of easy solution is:
Go to FuseESB console:
Type the command:
osgi:install mvn:commons-io/commons-io/2.1
Replace 'common's-io' with your your dependency's group id and artifact id (Maven)
e.g my dependency was:
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
Cheers

Resources