1
votes

After Googling a lot on Apache Karaf ,I understood that it is used for testing and building of OSGI bundles.I am still yet to figure out what to do with Apache Karaf and how to use it I didnt quite understand the need for OSGI bundles also.

Moreover I would also like to know the answers to my following doubts:

  1. What exacltly is Apache karaf in simple words?
  2. What purpose does it solve?Can it be used for solving day-to-day algorithmic problems?
  3. And where can I find nice tutorial problems on Karaf to start with
  4. What is the difference between Apache Karaf,Apache Felix and Apache Maven and how are they related to OSGI?
2

2 Answers

4
votes
  1. Apache Karaf is a OSGi Container bringing the infrastructure to run OSGi-Bundles (std. Jar with OSGi Manifest), as Tomcat is a Web Container giving the infrastructure to run web-Applications.
  2. Don't know about your day-to-day algorithmic problems but it solves a lot of other day-to-day issues due to it's OSGi nature. For this you'd better take a look at OSGi and what it is about. In short:
    OSGi helps you on two main issues, high cohesion and low coupling. Due to the classloader seperation (it's a graph not a tree) it's possible to run multiple versions of the same artifact.
    Due to the service registry it's possible to have a plugin like architecture where one application can listen for multiple implementations of one Interface (service)
  3. There are plenty around, even Books to get you started you just need to google for it. Or take a look at the Apache Karaf documentation and the corresponding Articles
    • Apache maven: it's a build system it's there to manage your dependencies for Build time.
    • Apache Felix: it's a OSGi Framework implementation (like Eclipse Equinox).
    • Apache Karaf: It's as said in 1. a OSGi Container, solving a lot of infrastructural questions, like: logging. easy Deployment, ease on provisioning, and lot's more. For the complete stack again, take a look at the documentation
8
votes

To understand Apache Karaf, you should first know about OSGi.

Java applications are deployed in the form of Jar files as their artifact. If the application depends on other jar files, then those jars must be present on the application's classpath. When application runs, required classes are loaded and instantiated as required.

If your application is deployed inside any container (e.g: a web server like tomcat), then the container follows some standard logic for loading or initializing your application. This involves loading the classes. It is required that classes referred inside a program must be present on the classpath of the application(e.g: the web-inf directory of your war file). If the container does not find the required classes inside the war, it consults the application 's parent classloader to locate the class files. So, this way multiple hierarchical classloaders comes in picture while working with normal java runtime. This has a problem that the same class is loaded multiple times depending on the applications deployed.

Welcome to the OSGi. In OSGi a jar will be called as a Bundle. A bundle looks similar like a jar but with extra headers in its manifest.mf file. The headers provides the information about what packages are exported/imported by the bundle. When your application wants to use any library jar file, you mentions the required packages in the manidest 's Import-package header.When any other bundle wants to use packages inside your bundl,you must expose the package using Export-package header.

An OSGi runtime looks for such headers and handles class loading efficiently. So, a class will be loaded only once using its jar's classloader only and you will be rescuid from annoying ClassNotFoundExceptions.

OSGi is the specification. It has a list of headers that can be used inside manifest.mf. Period.

Apache Felix and Eclipse equinox are few of the implementation of this specification.

Apache Karaf is a console. A console helps in interacting with any application. Here, the application is OSGi runtime which is Apache Felix. Karaf provides lot of commands to interact with OSGi. You can deploy new bundle to the OSGi at runtime ( no need to restart the Karaf) using its hot deploy feature. Just put the bundle file in deploy folder and you are done.

With Karaf, you can see services, packages exposed by any bundle. Karaf provides lot of out of the box frameworks like spring, jetty server , logging utilities.

With Karaf you can start, stop any bundle during runtime.

So, to conclude , Karaf is just a console for OSGi runtime.

You can write any normal java programs and play with OSGi. You just have to hook up your application initialization logic with bundle activator.

Apache maven is a build tool. With this, you can build( prepare jar artifact) any java project.

While working with OSGi you need to adhere with manifest.mf symantics. Packages should be exported and imported as required. To help with building the appropriate manifest.mf , there is a 'bnd' tool. This tool analyses your application's classpath and prepares final OSGi bundle with appropriate manifest .

Apache maven makes use of this 'bnd' tool with its maven-bundle-plugin. Just attach this plugin to the project's life cycle goal and you are done. Maven will prepare a bundle for you.