OSGi bundles and wars - osgi

Is it possible to have standard war deployment, which can be deployed on tomcat and also can be build as OSGi bundle and deployed with other bundles in OSGi container tomcat(i think virgo)?

Yes, there's a good deal of interoperability between WARs and WABs. Apache Aries and WebSphere Application Server will convert WARs to WABs on deployment. This is a good way to get up and running, but it's a better practice to use proper WABs in which you build in the OSGi metadata yourself. The extra OSGi headers won't interfere with the deployment in a normal Tomcat container, so the WAB has the greatest flexibility.
For your build, you have a number of options. For example, the maven bundle plugin can be configured to build WABs, or you can use Eclipse PDE's tooling support for OSGi metadata.

Pax-Url-War provides this functionality to containers like Apache Karaf. In brief, this allows you to import an URL like war:file:///path/to/myapp.war and Pax will wrap it as an OSGi bundle, optionally changing the URL root and other parameters on the fly.

Related

Can OSGi components be deployed in any Servlet containers?

Can OSGi components be deployed to any Container like Tomcat/Websphere/JBoss..etc ? (just as we deploy a WAR file) or do they require any special libraries or OSGi containers to run the OSGi components ?
If you want just expose the "component" classes, you can treat the jar files as .. jar files.
If you consider the OSGi "component" as a "bundle" exposing some services and doing something, then answer is "no". You need an OSGi container (Karaf, WebSphere Liberty, Carbon, Glassfish, JBoss WildFly ...).
However - as you can see, some application servers provide OSGi support natively.
OSGi components (I guess you mean bundles) require an OSGi framework to run in. So a plain servlet container will not work. Some versions of JBoss provide support for OSGi though.

Can I deploy a fab bundle on any OSGI-compliant container/application server?

I am currently trying to deploy my camel app as a FAB bundle in the Fuse ESB container. I understand that I can install Fab(Fabric-bundle) as a feature on OSGI containers like Karaf or felix and deploy the bundle. I want to know if this feature is available on all OSGI containers as well as other application servers like IBM Websphere(Websphere supports OSGI) or will this tie me up to the Fuse ecosystem.
Will deploying as a WAR or an OSGI bundle be a better approach as the goal is to support as many ESB Containers/Application Servers as possible.
No, this is a specialty of Fuse, for this it'll only work on the FUSE containers. Even Vanilla Karaf doesn't understand FAB (unless it's a standard Karaf feature, with just another name to it)
For usage of Karaf Featurs outside of Karaf you'd need to add the feature resolver and services of Karaf to the other OSGi containers first. Never tried that but might be possible.

Example using Jersey (REST resource), OSGi (Apache Felix), and Maven (build and deploy)?

I seem to be really struggling here. What I want to do (in this order) is:
1) Build a RESTful resource using a Jersey application and resource w/annotations (this is not the issue).
2) Package, install, and start that bundle into the Felix OSGi container, as an HTTP service, including dependencies.
3) Package, install, and start a WAR in the OSGi container that may incorporate #2 as a dependency.
And I would like to be able to do all of this using Maven.
I cannot seem to find a working example of even the individual steps, especially involving Maven, that work let alone the combination of those steps. I have tried cobbling together various q&a from across the web with varying levels of success but not an end-to-end working example yet.
Any pointers would be appreciated...
I do not have an example for Jersey but I have a tutorial for CXF with Apache Karaf (which uses Felix). It shows how to create a Rest service and build it with maven. Using Apache Karaf you can then deploy the bundle directly from the maven repo. Moving this to Jersey probably just means to exchange the lib and use another blueprint config to initialize the rest service.
Apache Karaf also allows to deploy wars and wabs but I have not yet tested them.
Apache Stanbol does most or all of this (not sure if the war packaging is included out of the box) to implement its RESTful services.
You'll have to dig through its codebase but searching for Jax-RS annotations in there should point you to the right places.
I also am really struggling with exactly what you are attempting to do. So far, I seem to be really close but alas not quite there, here's what I've been doing:
creating a War with Maven that defines my Jersey Resource's
bundling it with maven bundle plugin ( see section Adding OSGi metadata to existing projects without changing the packaging type ). Which allows me to run the restlets in Tomcat and test.
define a target in Eclipse that includes resources from my locally defined p2 site which I create with the p2-maven-plugin plugin. In this way I can gather up any of the dependencies from the WAR project into a p2 site, which I can deploy to an Eclipse defined target
Where I am stuck is trying to register the Jersey Resource's as services, for which I've tried:
using the JAX-RS OSGI connector, for which I eventually gave up on because it uses glassfish jersey which seems to export a version 2 API of Jersey when Jersey hasn't even defined a version 2 API yet. This caused package resolution problems when I wanted to use version 1.17 of Jersey libs.
registering the Resources using a Whiteboard a la Apache Felix HTTP Service, my current approach which doesn't seem to work yet.
And, finally, if the preceding doesn't work I'll try Amdatu
Another route I might try is from the Jersey project OSGI chapter
I tried this combination and made it up and running -
1- Modularity Specification-->OSGi specification
2- OSGi implementation-->Apache Felix 4.4.0
3- OSGi Runtime-->Apache Karaf 3.0.3
4- Software Architecture Specification of REST – JAX-RS
5- JAX-RX implementation-->Apache CXF – 2.7.5
You can refer this nice tutorial - http://java.dzone.com/articles/building-cxf-rest-service-osgi
Now, I am also planning to move to Jersey from CXF as the Jersey is the light wait reference implementation of JAX-RS. Now I am planning to use Jersey with Apache Felix/Apache Karaf. You can install Jersey in Karaf and deploy your Jersey Rest Web Services as Bundle. You can refer this tutorial - https://vzurczak.wordpress.com/2014/09/30/web-applications-with-osgi-working-with-jersey/
Here's a good example on github : https://github.com/ddragosd/jax-rs-on-karaf

Basic Apache Camel FileRoute deployed as OSGi bundle

I'm trying to do some "hello world" example with Apache Camel. Just a simple route, deployed as OSGi bundle to Apache Karaf. And it's crazy, too many things to learn at once, Camel itself, integration with spring, integration with OSGi and Karaf...ugh.
I'm trying to follow the idea from here: http://www.andrejkoelewijn.com/wp/2008/10/19/simple-camel-dsl-osgi-bundle-example/.
I've created the bundle, the package with FileRoute.java file, etc, but I can't produce valid manifest.mf file, it's generated by "bundle plugin for maven" - so the POM file is used to generate the manifest file.
What's the problem is this in the manifest file, line with import packages:
Import-Package: com.company.foo.bar.demo;version="0.1",org.apache.ca
mel.builder;version="2.10",org.apache.camel.model;version="2.10",org.
apache.commons.logging
With error: "No available bundle exports package 'org.apache.camel.builder'" (and .model package).
Well, I understand what is the problem, but…How to expose these packages for the bundle?
Next I have there another bundle with Apache Camel context, so I thought that good idea is to expose the packages from there, through "export package", but it doesn't work, the camel bundle can't be build then.
I'm quite lost :( I'd really appreciate help or hint.
Thanks!
If you use Apache Camel 2.10 or better then the Maven archetypes is a good place to start a new Camel project. We have archetypes that can create OSGi based project for either spring-dm or OSGi blueprint. You chose which one you want to use. The project is then ready for OSGi as it setup a maven plugin that generated the needed osgi stuff for you. Only in more advanced cases you would need to tweak this plugin.
See details at: http://camel.apache.org/camel-maven-archetypes.html
If you use Maven 3.0.x then its much easier as you can just type
mvn archetype:generate
Then it runs in interactive mode, then type camel to filter only camel archetypes.
And then select the number for the archetype. eg for example the spring-dm to use OSGi with Spring.

OSGi: What are the differences between Apache Felix and Apache Karaf?

Apache Karaf is a sub project of Apache Felix. It is defined as "a lightweight OSGi container".
I don't understand when should I use the heavyweight and when to use the lightweight. Their site doesn't explain this too much.
The 'lightweight OSGi container' label is contrasting Karaf with more feature rich OSGi containers, not with Felix.
To quote Guillaume Nodet (Karaf's author) from here:
Felix is just the OSGi core runtime. Karaf provides a "distribution" based on Felix by adding other features such as a console, an SSH remoting mechanism, a file deployer and more.
In this diagram of the Karaf architecture, Felix (or other OSGi implementation - currently Equinox is also supported) is the OSGi box, the other boxes are the features added by Karaf:
Therefore, unless you have specific needs which are not met by Karaf (requiring access to the underlying implementation) it usually makes sense to use this since it provides more 'out of the box'.
#Marcel
Karaf isn't monolithic or huge like a Java EE server. It is a very slim and minimalist implementation. It is a core product without much functionality other than a basic console, file loading so that you can actually read configuration files, etc. The latest zip file of it is 19.1 MB. Yeah. Small. It can easily run on small devices and work in embedded.
What you may be thinking of is something more like ServiceMix which uses Karaf as a core/kernel. But it includes CXF, ActiveMQ, Camel and other libraries in its full configuration. Even then they have minimal, medium and full sizes.
Really, for 99% of the projects out there it wouldn't make sense to start directly with Felix and not use Karaf instead.
Currently, there are three popular open source OSGi containers:
- Equinox. It is the modular Java runtime at the heart of the Eclipse IDE,
- Knopflerfish, Is an open source implementation of the OSGi R3 and OSGi R4 specifications.
- Apache Felix. Is the open source OSGi container from the Apache Software Foundation.
http://www.javaworld.com/article/2077837/java-se/java-se-hello-osgi-part-1-bundles-for-beginners.html
The Apache project maintains a general-purpose OSGi container called Felix.
Here is used as ServiceMix and
The main difference between the ServiceMix and Karaf is that
ServiceMix bundles a bunch of other integration components — ActiveMQ
message broker, the Camel routing engine, and some other things
According: http://kevinboone.net/osgitest.html

Resources