Apache Karaf + Pax CDI OSGI Service
OSGI
OSGi (Open Service Gateway Initiative) is a Java framework for developing and deploying modular software programs and libraries. OSGi has two parts. The first part is a specification for modular components called bundles. Which are commonly referred to as plug-ins
Contexts and Dependency Injection for OSGi Applications
PAX CDI
https://ops4j1.jira.com/wiki/display/PAXCDI/Pax+CDI
PAX CDI is a Dependency injection implementation in OSGI
Let us a sample for how to implement the OSGI Service
How to use
@Inject
- @OsgiService
- @OsgiServiceProvider
etc.,
Pax CDI API
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>osgi.example</groupId>
<artifactId>osgi-example-osgi</artifactId>
<version>1.0</version>
</parent>
<artifactId>osgi-example-osgi.api</artifactId>
<packaging>bundle</packaging>
</project>
OSGI API Class
Interface which exposes the OSGI Bundle Service
package com.osgi.api;
public interface HelloService {
public String sayHello(String name);
}
META-INF/beans.xml
An empty beans xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
</beans>
Pax CDI IMPL
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>osgi.example</groupId>
<artifactId>osgi-example-osgi</artifactId>
<version>1.0</version>
</parent>
<artifactId>osgi-example-osgi.provider</artifactId>
<packaging>bundle</packaging>
<dependencies>
<dependency>
<groupId>osgi.example</groupId>
<artifactId>osgi-example-osgi.api</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
</project>
OSGI Implementation
@OsgiServiceProvider register the service to the class
import org.ops4j.pax.cdi.api.OsgiServiceProvider;
import org.ops4j.pax.cdi.api.Properties;
import org.ops4j.pax.cdi.api.Property;
import com.osgi.api.HelloService;
@OsgiServiceProvider(classes = HelloService.class)
@Properties(@Property(name = "service.exported.interfaces", value = "*"))
public class HelloServiceImpl implements HelloService {
@Override
public String sayHello(String name) {
return "Hello, "+name;
}
}
BND file for OSGI IMPL Provider
BND is a eclipse plugin which can create the manifest dynamically
Manifest hold the OSGI configuration
Some of the important property like
Require-Capability
Import-Package
Export-Package
For Enabling the PAX CDI
Require-Capability will have the following entry
Require-Capability : osgi.extender; filter:="(osgi.extender=pax.cdi)"
OSGI Service Extension
Pax CDI also provides an integration with OSGi services, enabling you to reference an OSGi service or to publish an OSGi service using CDI annotations. This capability is provided by the Pax CDI OSGi Services Extension, which is not enabled by default.
For this project it has the following
bndfile.bnd
Bundle-Name: OSGI Provider
Require-Capability: org.ops4j.pax.cdi.extension; filter:="(&(extension=pax-cdi-extension)(version>=${version;==;${pax.cdi.version}})(!(version>=${version;=+;${pax.cdi.version}})))", osgi.extender; filter:="(osgi.extender=pax.cdi)"
Import-Package: javax.enterprise.*; version="1.0", *;
Export-Package: com.osgi.api
Project Structure Looks as Below:
Apache Karaf Installation of Bundle
Installing PAX CDI
Karaf Console
__ __ ____
/ //_/____ __________ _/ __/
/ ,< / __ `/ ___/ __ `/ /_
/ /| |/ /_/ / / / /_/ / __/
/_/ |_|\__,_/_/ \__,_/_/
Apache Karaf (4.0.8)
Hit '<tab>' for a list of available commands
and '[cmd] --help' for help on a specific command.
Hit '<ctrl-d>' or type 'system:shutdown' or 'logout' to shutdown Karaf.
karaf@root()> feature:install pax-cdi
You can use the list command to list the bundles that show pax cdi bundles
karaf@root()> list
START LEVEL 100 , List Threshold: 50
ID | State | Lvl | Version | Name
-------------------------------------------------------------------------------------------------------
54 | Active | 80 | 1.0 | Apache Geronimo JSR-330 Spec API
55 | Active | 80 | 1.0.3 | Apache Geronimo Expression Language Spec 2.2
56 | Active | 80 | 1.0 | Interceptor 1.1
57 | Active | 80 | 1.0 | Apache Geronimo Java Contexts and Dependency Injection (JSR-299) Spec API
60 | Active | 80 | 4.1.0 | Apache XBean OSGI Bundle Utilities
61 | Active | 80 | 1.5.0 | OPS4J Base - All
62 | Active | 80 | 0.12.0 | OPS4J Pax CDI Bean Bundle API
63 | Active | 80 | 0.12.0 | OPS4J Pax CDI Extender for Bean Bundles
64 | Active | 80 | 0.12.0 | OPS4J Pax CDI Portable Extension for OSGi
65 | Active | 80 | 0.12.0 | OPS4J Pax CDI Service Provider Interface
66 | Active | 80 | 1.8.0 | OPS4J Pax Swissbox :: OSGi Core
67 | Active | 80 | 1.8.0 | OPS4J Pax Swissbox :: Lifecycle
68 | Active | 80 | 1.8.0 | OPS4J Pax Swissbox :: Tracker
Now let us install OSGI
install -s mvn command used for installing the bundles from the mvn repository
karaf@root()> install -s mvn:osgi.example/osgi-example-osgi.api/1.0
Bundle ID: 70
karaf@root()>
karaf@root()> install -s mvn:osgi.example/osgi-example-osgi.provider/1.0
Bundle ID: 71
karaf@root()> list
START LEVEL 100 , List Threshold: 50
ID | State | Lvl | Version | Name
-------------------------------------------------------------------------------------------------------
54 | Active | 80 | 1.0 | Apache Geronimo JSR-330 Spec API
55 | Active | 80 | 1.0.3 | Apache Geronimo Expression Language Spec 2.2
56 | Active | 80 | 1.0 | Interceptor 1.1
57 | Active | 80 | 1.0 | Apache Geronimo Java Contexts and Dependency Injection (JSR-299) Spec API
60 | Active | 80 | 4.1.0 | Apache XBean OSGI Bundle Utilities
61 | Active | 80 | 1.5.0 | OPS4J Base - All
62 | Active | 80 | 0.12.0 | OPS4J Pax CDI Bean Bundle API
63 | Active | 80 | 0.12.0 | OPS4J Pax CDI Extender for Bean Bundles
64 | Active | 80 | 0.12.0 | OPS4J Pax CDI Portable Extension for OSGi
65 | Active | 80 | 0.12.0 | OPS4J Pax CDI Service Provider Interface
66 | Active | 80 | 1.8.0 | OPS4J Pax Swissbox :: OSGi Core
67 | Active | 80 | 1.8.0 | OPS4J Pax Swissbox :: Lifecycle
68 | Active | 80 | 1.8.0 | OPS4J Pax Swissbox :: Tracker
70 | Active | 80 | 1.0.0 | osgi-example-osgi.api
71 | Active | 80 | 1.0.0 | OSGI Provider
Next Post we can see how we can consume this service in using Karaf CMD prompt.
Karaf Command we can configure custom command to pass value to invoke the bundle.
Comments
Post a Comment