DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • While Performing Dependency Selection, I Avoid the Loss Of Sleep From Node.js Libraries' Dangers
  • Why I Started Using Dependency Injection in Python
  • Understanding ldd: The Linux Dynamic Dependency Explorer
  • Understanding the Two Schools of Unit Testing

Trending

  • Using Python Libraries in Java
  • Next Evolution in Integration: Architecting With Intent Using Model Context Protocol
  • Building Reliable LLM-Powered Microservices With Kubernetes on AWS
  • Software Delivery at Scale: Centralized Jenkins Pipeline for Optimal Efficiency
  1. DZone
  2. Software Design and Architecture
  3. Security
  4. Apache Karaf Features for OSGi Deployment

Apache Karaf Features for OSGi Deployment

When getting started with Apache Karaf, many things appear to happen by magic. This article tries to remove the mystery from one of them.

By 
Alan Hohn user avatar
Alan Hohn
·
Oct. 24, 16 · Tutorial
Likes (12)
Comment
Save
Tweet
Share
25.9K Views

Join the DZone community and get the full member experience.

Join For Free

I've worked on multiple teams that have successfully used Apache Karaf as an OSGi container. In addition to the usual need to understand OSGi bundles and class resolution in the context of multiple class loaders (which deserves at least one article all by itself), Karaf adds the concept of a "feature" on top of OSGi bundles. While there is detailed documentation on features, I've found that they can be confusing for new users, especially when they are resolved from Maven, since there seems to be so much behind the scenes magic involved.

Base OSGi

For everything that Karaf adds to OSGi, the basic unit of installation in the OSGi container is still the bundle. A bundle is a Java Archive (JAR) with some special information in its manifest that identifies it, gives a version, and specifies dependencies. When an OSGi container adds a bundle, it goes through a resolution process to make sure that the bundle's dependencies are met (and that it does not conflict with other installed bundles). However, that resolution process does not include any ability to obtain any dependencies; it just checks to see if they are available and delays or prevents the bundle from starting if a required dependency is missing.

So it is necessary for a user of an OSGi container to identify all of the bundles that need to be available in the container. Unfortunately, this can get unwieldy, as the number of bundles can easily reach the dozens or hundreds.

Karaf Features

To address this, Karaf introduces the concept of a feature. A feature is just a group of bundles that should all be installed together. A feature also gets a name and a version. Features are specified in an XML format. For example, here's part of the XML definition for the "core" feature for Apache Camel:

  <feature name='camel-core' version='2.16.0' resolver='(obr)' start-level='50'>
      <feature version='2.5.0'>xml-specs-api</feature>
      <bundle>mvn:org.apache.camel/camel-core/2.16.0</bundle>
      <bundle>mvn:org.apache.camel/camel-catalog/2.16.0</bundle>
      ...
  </feature>


Notice that a feature can specify a dependency on another feature as well as listing bundles.

Feature Repositories

To simplify things a little bit, Karaf allows multiple features to be specified in a single XML file. That way, a product like Camel with lots of different pieces can have a single location where all the available features are listed, and users can pick and choose the features they need.

This XML file is called a "feature repository". I've seen this cause confusion with new users of Karaf, because we're used to a repository being a collection of files on a remote system. (For example, this is how Artifactory uses the term.)

A feature repository is just a list of features wrapped in an outer XML element:

<features xmlns="http://karaf.apache.org/xmlns/features/v1.3.0">
  <feature name="feature1" version="1.0.0">
    ...
  </feature>
  <feature name="feature2" version="1.1.0">
    ...
  </feature>
</features>


Even though multiple features can be listed in a single feature repository, we install using the name of the individual features inside.

Feature Resolution

Providing a way to specify dependencies is nice, because it helps to modularize the selection of all the bundles we need. (As one small example, we can shift to new a version of a feature dependency and get the transitive dependencies of that feature without having to figure out which versions have changed.)

But in order to be really useful, there needs to be a way to resolve those dependencies. Otherwise we would still have a manual process of putting all the bundles together so the OSGi container could resolve them. Karaf, recognizing that Maven repositories have become very common for storing dependencies, supports using Maven to find features repositories and bundles.

As a result, to fetch down a feature repository XML file for Apache Camel and get access to all its features, we can just do:

karaf@root()> feature:repo-add mvn:org.apache.camel.karaf/apache-camel/2.18.0/xml/features


Now we have all the Camel features available:

karaf@root()> feature:list | grep camel-core
camel-core                              | 2.18.0           |          | Uninstalled | camel-2.18.0                |


And we can install:

karaf@root()> feature:install camel-core
karaf@root()> list | grep camel
52 | Active |  50 | 2.18.0  | camel-catalog
53 | Active |  50 | 2.18.0  | camel-commands-core
54 | Active |  50 | 2.18.0  | camel-core
55 | Active |  50 | 2.18.0  | camel-karaf-commands


What is shown in the list is the set of bundles with 'camel' in the name that were installed as part of the feature. There is no rule that the names of bundles or features have to match.

There is much more to know about using Karaf features, but I'm already running long for a single article. Next I'll walk through how to configure Karaf to load features at startup, and control how it uses Maven.

Dependency

Opinions expressed by DZone contributors are their own.

Related

  • While Performing Dependency Selection, I Avoid the Loss Of Sleep From Node.js Libraries' Dangers
  • Why I Started Using Dependency Injection in Python
  • Understanding ldd: The Linux Dynamic Dependency Explorer
  • Understanding the Two Schools of Unit Testing

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!