Table of Contents

Karaf Tutorial

Others:

Start from scratch

Parent Pom

1. What karaf provides?

1.1 Karaf feature

1.2 Adding feature

  feature:repo-add command.

The feature:repo-add command requires the name/url argument. This argument accepts:

The etc/org.apache.karaf.features.repos.cfg defines a list of “pre-installed/available” features repositories:

#
# This file describes the features repository URL
# It could be directly installed using feature:repo-add command
#
enterprise=mvn:org.apache.karaf.features/enterprise/LATEST/xml/features
spring=mvn:org.apache.karaf.features/spring/LATEST/xml/features
cellar=mvn:org.apache.karaf.cellar/apache-karaf-cellar/LATEST/xml/features
cave=mvn:org.apache.karaf.cave/apache-karaf-cave/LATEST/xml/features
camel=mvn:org.apache.camel.karaf/apache-camel/LATEST/xml/features
camel-extras=mvn:org.apache-extras.camel-extra.karaf/camel-extra/LATEST/xml/features
cxf=mvn:org.apache.cxf.karaf/apache-cxf/LATEST/xml/features
cxf-dosgi=mvn:org.apache.cxf.dosgi/cxf-dosgi/LATEST/xml/features
cxf-xkms=mvn:org.apache.cxf.services.xkms/cxf-services-xkms-features/LATEST/xml
activemq=mvn:org.apache.activemq/activemq-karaf/LATEST/xml/features
jclouds=mvn:org.apache.jclouds.karaf/jclouds-karaf/LATEST/xml/features
openejb=mvn:org.apache.openejb/openejb-feature/LATEST/xml/features
wicket=mvn:org.ops4j.pax.wicket/features/LATEST/xml/features
hawtio=mvn:io.hawt/hawtio-karaf/LATEST/xml/features
pax-cdi=mvn:org.ops4j.pax.cdi/pax-cdi-features/LATEST/xml/features
pax-jdbc=mvn:org.ops4j.pax.jdbc/pax-jdbc-features/LATEST/xml/features
pax-jpa=mvn:org.ops4j.pax.jpa/pax-jpa-features/LATEST/xml/features
pax-web=mvn:org.ops4j.pax.web/pax-web-features/LATEST/xml/features
pax-wicket=mvn:org.ops4j.pax.wicket/pax-wicket-features/LATEST/xml/features
ecf=http://download.eclipse.org/rt/ecf/latest/site.p2/karaf-features.xml
decanter=mvn:org.apache.karaf.decanter/apache-karaf-decanter/LATEST/xml/features

You can directly provide a features repository name to the feature:repo-add command. For install, to install Apache Karaf Cellar, you can do:

karaf@root()> feature:repo-add cellar
Adding feature url mvn:org.apache.karaf.cellar/apache-karaf-cellar/LATEST/xml/features

When you don’t provide the optional version argument, Apache Karaf installs the latest version of the features repository available. You can specify a target version with the version argument:

karaf@root()> feature:repo-add cellar 4.0.0.RC1
Adding feature url mvn:org.apache.karaf.cellar/apache-karaf-cellar/4.0.0.RC1/xml/features

Instead of providing a features repository name defined in the etc/org.apache.karaf.features.repos.cfg configuration file, you can directly provide the features repository URL to the feature:repo-add command:

karaf@root()> feature:repo-add mvn:org.apache.karaf.cellar/apache-karaf-cellar/4.0.0.RC1/xml/features
Adding feature url mvn:org.apache.karaf.cellar/apache-karaf-cellar/4.0.0.RC1/xml/features

By default, the feature:repo-add command just registers the features repository, it doesn’t install any feature. If you specify the -i option, the feature:repo-add command registers the features repository and installs all features described in this features repository:

karaf@root()> feature:repo-add -i cellar

1.3 tbd

2. Deploy with feature

You can “hot deploy” a features XML by dropping the file directly in the deploy folder.

Apache Karaf provides a features deployer.

When you drop a features XML in the deploy folder, the features deployer does: * register the features XML as a features repository * the features with install attribute set to “auto” will be automatically installed by the features deployer.

For instance, dropping the following XML in the deploy folder will automatically install feature1 and feature2, whereas feature3 won’t be installed:

<?xml version="1.0" encoding="UTF-8"?>
<features name="my-features" xmlns="http://karaf.apache.org/xmlns/features/v1.3.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://karaf.apache.org/xmlns/features/v1.3.0 http://karaf.apache.org/xmlns/features/v1.3.0">
 
    <feature name="feature1" version="1.0" install="auto">
        ...
    </feature>
 
    <feature name="feature2" version="1.0" install="auto">
        ...
    </feature>
 
    <feature name="feature3" version="1.0">
        ...
    </feature>
 
</features>

Troubleshooting bundle

  feature:install -v feature_name
  bundle:list
  la
  feature:list | grep
  feature:info 
  service:list
  service:list NamespaceHandler
  service:list Datasource
  bundle:tree-show [bundle-id]
   exports | grep -i javax.transaction
  karaf@root()> bundle:watch *

will monitor all bundles that have a location matching mvn:* and -SNAPSHOT in their URL.

For not snapshot bundles use bundle:update

  bundle:update sd-rest more-bundle-name [ids, name/version]

Bundle:refresh should update all depending bundles:

bundle:refresh sd-rest more-bundle-name [ids, name/version]

list -t 0 -s | grep aries.transaction.manager

1. javax.transaction

karaf@root()> bundle:list | grep -i directory                                                                                                                           17:24:34
163 │ GracePeriod │  80 │ 1.0.0.SNAPSHOT     │ directory service impl
164 │ Active      │  80 │ 1.0                │ directory-api Bundle
karaf@root()> bundle:tree-show 163                                                                                                                                      17:27:00
Bundle directory [163] is currently ACTIVE

directory [163]
+- directory-api [164]
|  +- service-model [183]
|     +- org.hibernate.core [175]
|     |  +- org.ops4j.pax.logging.pax-logging-api [

2. jdbc

karaf@root()> feature:install jdbc                                                                                                                                      17:51:10
karaf@root()> jdbc:ds-info                                                                                                                                              17:57:55
Error executing command jdbc:ds-info: argument datasource is required
karaf@root()> jdbc:ds-info jdbc/serviceh2ds                                                                                                                             17:58:03
Property       │ Value
───────────────┼────────────────────────
driver.version │ 1.3.172 (2013-05-25)
db.version     │ 1.3.172 (2013-05-25)
db.product     │ H2
url            │ jdbc:h2:mem:serviceh2ds
driver.name    │ H2 JDBC Driver
username       │ DS
karaf@root()> jdbc:ds-list                                                                                                                                              17:58:18
Name │ Product │ Version │ URL │ Status
─────┼─────────┼─────────┼─────┼───────
karaf@root()> jdbc:tables jdbc/serviceh2ds                                                                                                                              17:58:36
TABLE_CAT   │ TABLE_NAME        │ SELF_REFERENCING_COL_NAME │ TABLE_SCHEM        │ TYPE_SCHEM │ TYPE_CAT │ TABLE_TYPE   │ REMARKS │ REF_GENERATION │ TYPE_NAME │ SQL
────────────┼───────────────────┼───────────────────────────┼────────────────────┼────────────┼──────────┼──────────────┼─────────┼────────────────┼───────────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────
SERVICEH2DS │ CATALOGS          │                 

3. Bundle dependencies

Feature can not be resolved, missing dependencies.

Install bundle:

  bundle:install mvn:groupId/artefactId/version
  bundle_id
  

Show dependencies:

  bundle:tree-show #bundle_id
 

3.1 Declare dependencies

3.2 Sample hibernate troubleshooting

The error:

Error executing command: Unable to resolve root: missing requirement [root] osgi.identity; osgi.identity=sd-model; type=karaf.feature; version="[1.0.0,1.0.0]"; filter:="(&(osgi.identity=sd-model)(type=karaf.feature)(version>=1.0.0)(version<=1.0.0))" 
     [caused by: Unable to resolve sd-model/1.0.0: missing requirement [sd-model/1.0.0] osgi.identity; osgi.identity=service-model; type=osgi.bundle; 
     version="[1.0.0,1.0.0]"; resolution:=mandatory 
         [caused by: Unable to resolve service-model/1.0.0: missing requirement [service-model/1.0.0] osgi.service; 
         objectClass=javax.persistence.spi.PersistenceProvider; javax.persistence.provider=org.hibernate.jpa.HibernatePersistenceProvider; effective:=active]]

3.2.1 Disable Required capability

Only feature 1.3.0

vim karaf-distribution/target/assembly/etc/org.apache.karaf.features.cfg

#
# Service requirements enforcement
#
# By default, the feature resolver checks the service requirements/capabilities of
# bundles for new features (xml schema >= 1.3.0) in order to automatically installs
# the required bundles.
# The following flag can have those values:
#   - disable: service requirements are completely ignored
#   - default: service requirements are ignored for old features
#   - enforce: service requirements are always verified
#
serviceRequirements=disable

Using command:

  karaf@root()> config:edit org.apache.karaf.log
  config:property-list
  config:property-set serviceRequirements disable
  config:update and config:cancel

3.2.2 tBD

It looks like persistenceProvider not available.

n persistence.xml the persistence provider is set to "org.hibernate.jpa.HibernatePersistenceProvider".
So perhaps there is no suitable persistence provider installed.

You can use "service:list PersistenceProvider" to see which providers are installed. 
This is what it shows for me:
[javax.persistence.spi.PersistenceProvider]
-------------------------------------------
 javax.persistence.provider = org.hibernate.jpa.HibernatePersistenceProvider
 service.id = 601
 service.bundleid = 275
 service.scope = bundle
Provided by : 
 hibernate-osgi (275)

The service property "javax.persistence.provider" will have to match the name given in persistence.xml.
So 
It should work if you do feature:install hibernate/4.3.6.Final .

3.2.3 Dump install process

  feature:install --store /tmp/karaf.store sd-model 

3.2.4 Header command

If I hack a camel feature (I modify the system/org/apache/camel/karaf/apache-camel/2.16.2/apache-camel-2.16.2-features.xml file) and comment the other features it depends on like in this picture:

 http://i.imgur.com/IFiIKTq.png

Then I (first refresh the camel feature repo and then) I try to install this feature, the resolver output this kind of error:

 http://i.imgur.com/cRhWyuR.png

which only partially covers the issues in the feature…

So usually I install manually all the bundles and inspect the one that won't start with the headers command, and the output is this:

 http://i.imgur.com/0LxNEBB.png
  headers #bundle_id
  

When it was possible to use the -c options, I didn't need to install manually each bundle…

3.2.5 bundle dependency=true

4. Sources

1. Root maven project

mvn archetype:generate \
-DarchetypeGroupId=org.codehaus.mojo.archetypes \
-DarchetypeArtifactId=pom-root \
-DarchetypeVersion=RELEASE

Define value for property 'groupId': : de.gtarc.tutorial.karaf.echo
Define value for property 'artifactId': : echo-project
Define value for property 'version':  1.0-SNAPSHOT: : 1.0
Define value for property 'package':  de.gtarc.tutorial.karaf.echo: : 
Confirm properties configuration:
groupId: de.gtarc.tutorial.karaf.echo
artifactId: echo-project
version: 1.0
package: de.gtarc.tutorial.karaf.echo

karaf_tutorial]$ ls
echo-project

The other archetype can be generated as following

mvn archetype:generate -DarchetypeGroupId=org.apache.karaf.archetypes -DarchetypeArtifactId=ARCHETYPE_NAME

Adding parameters:

-DarchetypeVersion=4.0.0 \
    -DgroupId=com.mycompany \
    -DartifactId=com.mycompany.blueprint \
    -Dversion=1.0-SNAPSHOT \
    -Dpackage=com.mycompany.blueprint

2. Karaf assembly / distribution

The karaf-assembly-archetype create a Maven project which create a custom Karaf distribution. It allows you to create your own Karaf distribution. The project downloads a Karaf standard distribution (in tar.gz and zip formats), unpack it, and create a new distribution. The easiest way to use it is to use the interactive mode:

mvn archetype:generate -DarchetypeGroupId=org.apache.karaf.archetypes -DarchetypeArtifactId=karaf-assembly-archetype -DarchetypeVersion=2.2.5-SNAPSHOT

3. Bundle Archetype

If basically a bundle is a jar file with some special statement in the MANIFEST, the easiest way to create a bundle is to use the Felix maven-bundle-plugin. Karaf provides an archetype which prepare a Maven project and provide a bundle Activator (a special callback class when the bundle start/stop). To generate this project, simply type:

mvn archetype:generate -DarchetypeGroupId=org.apache.karaf.archetypes -DarchetypeArtifactId=karaf-bundle-archetype -DarchetypeVersion=2.2.5-SNAPSHOT

NB: I think that this archetype should be in Felix (as the same level as maven-bundle-plugin), I will propose a donation to the Felix community.

4. Blueprint Archetype

Blueprint is IoC approach applied to OSGi (it comes from Spring DM in fact). It allows you to avoid to write bundle Activator, ServiceTracker, etc. Karaf provides a karaf-blueprint-archetype which prepare a Maven project including the maven-bundle-plugin, and a blueprint descriptor with a sample of OSGi service definition:

mvn archetype:generate -DarchetypeGroupId=org.apache.karaf.archetypes -DarchetypeArtifactId=karaf-blueprint-archetype -DarchetypeVersion=2.2.5-SNAPSHOT

NB: I think that this archetype should be in Aries (as it’s the blueprint implementation used in Karaf), I will propose a donation to the Aries community.

In Action

5. Feature Archetype

In Karaf, a feature is an application descriptor, defining all bundles, configurations/configuration files, others features. This features descriptor could be generated by hand (it’s my favorite way, and I guess the recommended one), we also provide a archetype which prepare a project to generate features file regarding the POM dependencies:

mvn archetype:generate -DarchetypeGroupId=org.apache.karaf.archetypes -DarchetypeArtifactId=karaf-feature-archetype -DarchetypeVersion=2.2.5-SNAPSHOT

6. Kar Archetype

We saw in my previous post a new goal of the features-maven-plugin to create a kar file starting from a features XML. A KAR file is a zip file containing the features XML and all its dependencies. We also provide an archetype to prepare a Maven project containing a features XML and generate a KAR file:

mvn archetype:generate -DarchetypeGroupId=org.apache.karaf.archetypes -DarchetypeArtifactId=karaf-kar-archetype -DarchetypeVersion=2.2.5-SNAPSHOT

Pax-Web Archetypes

We also add several archetypes in Pax Web to deal with the web bundle. Charles talk about wab-gwt archetype in his last blog entry (http://cmoulliard.blogspot.com/2011/12/run-google-web-toolkit-2-project-on.html), but I added also two others.

Web Bundle Archetype

The Web Bundle Archetype create a “special” bundle containing web resources and statements. It creates a Maven project with webapp resources and WebApp Context in the POM:

mvn archetype:generate -DarchetypeGroupId=org.ops4j.pax.web.archetypes -DarchetypeArtifactId=wab-archetype -DarchetypeVersion=1.1.2-SNAPSHOT

War Archetype

We also provide an archetype to create a Maven project which generate a standard war (that you can deploy “outside” of OSGi) but including OSGi statements in the MANIFEST (which allows you to use OSGi values):

mvn archetype:generate -DarchetypeGroupId=org.ops4j.pax.web.archetypes -DarchetypeArtifactId=war-archetype -DarchetypeVersion=1.1.2-SNAPSHOT

Camel Archetypes --> better

Camel also provides a set of very useful archetypes, especially:

  camel-archetype-blueprint to generate a Maven project with a blueprint XML in which you can define your routes
  camel-archetype-component providing a template to create your own Camel component
  camel-archetype-dataformat providing a template to create your own Camel data format
  camel-archetype-java providing a template with a class in which you can define your routes
  camel-archetype-spring to generate a Maven project with a Spring XML in which you can define your routes

USE THIS For instance, to create a Camel blueprint Maven project, simply type:

mvn archetype:generate -DarchetypeGroupId=org.apache.camel.archetypes -DarchetypeArtifactId=camel-archetype-blueprint -DarchetypeVersion=2.9-SNAPSHOT

* http://ggl-consulting.blogspot.de/2014/02/osgi-bundle-using-blueprint-dymisfied.html

Load & Run: Simple Bundle

install mvn:groupid/artefactid/version

Load and run bundle:

   karaf@root> install mvn:com.michaelrice/osgi-demo-next/1.0.0-SNAPSHOT
   karaf@root> install file:/var/project/osgi-blueprint-tutorial/hascode-blueprint-bundle/target/hascode-blueprint-bundle-0.0.1.jar

Introductory

Why should we? This custom distribution could contain:

1.1 Maven assembly

The recommended way to create a Karaf server assembly is to use the karaf-assembly packaging with the karaf-maven-plugin. This assembles a server from the maven dependencies in the project pom. After explanation of the configuration options we present an example.

The Karaf project effectively uses this packaging to assemble the official Karaf distributions, but due to maven limitations we have to simulate rather than use the karaf-assembly packaging.

This packaging creates tar.gz and zip archives containing the assembled server. They are identical except that zip archives don’t unpack with appropriate unix file permissions for the scripts.

1.2 Maven dependencies

Maven dependencies in a karaf-assembly project can be feature repositories (classifier “features”) or kar archives. Feature repositories are installed in the internal “system” Maven structured repository. Kar archives have their content unpacked on top of the server as well as contained feature repositories installed.

The Maven scope of a dependency determines whether its feature repository is listed in the features service configuration file etc/org.apache.karaf.features.cfg featuresRepositories property:

compile (default): All the features in the repository (or for a kar repositories) will be installed into the startup.properties. The feature repo is not listed in the features service configuration file.

runtime: feature installation is controlled by <startupFeature>, <bootFeature>, and <installedFeature> elements in the karaf-maven-plugin configuration. The feature repo uri is listed in the features service configuration file.

Note: Using runtime, the feature can not be refreshed!!

1.3 Plugin configuration

Control how features are installed using these elements referring to features from installed feature repositories:

<startupFeature>foo</startupFeature> - This will result in the feature bundles being listed in startup.properties at the appropriate start level and the bundles being copied into the “system” internal repository. You can use featurename or featurename/feature_version formats.

<bootFeature>bar</bootFeature> - This will result in the feature name added to boot-features in the features service configuration file and all the bundles in the feature copied into the “system” internal repository. You can use featurename or featurename/feature_version formats.

<installedFeature>baz</installedFeature> - This will result in all the bundles in the feature being installed in the “system” internal repository. Therefore at runtime the feature may be installed without access to external repositories. You can use featurename or featurename/feature_version formats.

You can also define the libraries shipped in your custom distribution. For instance, it could be interesting if you want to extend your distribution with some JDBC drivers.

The plugin accepts the <libraries/> element where you can add <library/> containing the URL of the library. For instance:

<libraries>
    <library>mvn:org.postgresql/postgresql/9.3-1102-jdbc41;type:=endorsed</library>
</libraries>

2. Development Tips

Bundle Activation

HttpService

Pax Web

Aries

Top of osgi container, providing enterprise services.

http://aries.apache.org/modules/samples/blog-sample.html

Rest CXF

1. Angular

Datasource