Category Archives : OSGI

Install and deploy bundle in JBoss FUSE OSGi container

Jboss FUSE is an OSGi container based on Karaf (Felix) framework. In this tutorial, we will learn how to set up Jboss Fuse and deploy a bundle in it.

Download

  1. Go to the Jboss site and download  “JBoss Fuse for Developers”  and  “JBoss Fuse Developer Tooling”. Please note that you may need to have an account with Red Hat before you can download these products. Please follow the instructions in the site. Download  “Stand-alone Installer” for Jboss Developer Studio.
  2. Unzip Jboss Fuse in your favorite directory. We would refer this location as FUSE_HOME. Open <FUSE_HOME>/etc/user.properties and uncomment admin property.
  3. Open <FUSE_HOME>/etc/org.apache.karaf.management.cfg and change the value of rmiRegistryHost to 127.0.0.1.
  4. Go to <FUSE_HOME>/etc/org.ops4j.pax.url.mvn.cfg and set the property value org.ops4j.pax.url.mvn.localRepository=${user.home}/.m2/repository. Please note that your local maven repository location may be different. Please change accordingly. Now, Fuse is ready to use.
  5. Follow the instructions of Standalone installer and start the eclipse based JBoss Developer Studio.
  6. On the Welcome page, click on tab “Software Update”. If you don’t see Jboss Fuse Development in the list, please check if it is already installed.

Jboss Tool Fuse Development

7. Click on “Install/Update” and restart JBoss Developer Studio when asked for.

Create Bundle

8. After update is completed, go to New > Other > Fuse Tooling > Fuse Project

JBossDeveloperStudioFuseProject

9.  Provide a Project name and click Next.

10. Select the archetype “org.apache.camel.archetypes:camel-archetype-blueprint:” Fill in the details of Group Id.

JbossFuseCamelArchtype

11. Click on Finish and you have a deployable fuse bundle project ready.

Jboss Fuse First Bundle

Project Artifacts

 1. Method interface

file: Hello.java

package com.javanbeyond.camel.blueprint;

/**
 * An interface for implementing Hello services.
 */
public interface Hello {

    String hello();
    
}

2. Interface implementation or Bean class

file: HelloBean.java. This class will be referred in blueprint.xml as a bean.

package com.javanbeyond.camel.blueprint;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * A bean which we use in the route
 */
public class HelloBean implements Hello {

    private String say = "Hello World";

    // This method will be called from camel context
    public String hello() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return say + " at " + sdf.format(new Date());
    }

    public String getSay() {
        return say;
    }

    public void setSay(String say) {
        this.say = say;
    }
}

3. Blueprint configuration
file: blueprint.xml. This is the declarative configuration file of the bundle which instructs the blueprint container to parse, instantiate and wire the components inside the blueprint.xml together.

<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="        http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd        http://camel.apache.org/schema/blueprint http://camel.apache.org/schema/blueprint/camel-blueprint.xsd">
    <!-- bean class declaration -->
    <bean id="helloBean" class="com.javanbeyond.camel.blueprint.HelloBean">
        <property name="say" value="Hi from Camel"></property>
    </bean>
    <!-- The routing logic is written in Camel Context -->
    <camelContext xmlns="http://camel.apache.org/schema/blueprint">
        <route id="timerToLog">
            <from uri="timer:foo?period=5000"></from>
            <setBody>
                <method ref="helloBean" method="hello"></method>
            </setBody>
            <log message="The message contains ${body}" loggerRef="helloBean"></log>
            <to uri="mock:result?expectedCount=2&amp;resultMinimumWaitTime=3&amp;retainFirst=3"></to>
        </route>
    </camelContext>
</blueprint>

Deploy

12. Run maven clean and install on the project in Developer Studio.

13. Go to <FUSE_HOME>/bin directory and execute fuse.bat or fuse.

14. Once fuse starts, type the command “osgi:install mvn:com.javanbeyond/camel-blueprint/1.0.0-SNAPSHOT” on the FUSE console and you should get a <BUNDLE_ID>.

15. Type “osgi:start <BUNDLE_ID>”

16. Type “osgi:list” and you should see your bundle started.

[ 286] [Active  ] [Created        ] [       ] [   80] Camel Blueprint Route (1.0.0.SNAPSHOT)

Bundle in Action

17. Type “log:tail” and every 5 second, the timer component logs the message body to console. The message body is the same message we set in our camel context.

2015-10-03 21:38:03,301 | INFO  | #0 - timer://foo | timerToLog                       | ?                                   ? | 198 - org.apache.camel.camel-core - 2.15.1.redhat-620133 | The message contains Hi from Camel at 2015-10-03 21:38:03
2015-10-03 21:38:08,294 | INFO  | #0 - timer://foo | timerToLog                       | ?                                   ? | 198 - org.apache.camel.camel-core - 2.15.1.redhat-620133 | The message contains Hi from Camel at 2015-10-03 21:38:08
2015-10-03 21:38:13,296 | INFO  | #0 - timer://foo | timerToLog                       | ?                                   ? | 198 - org.apache.camel.camel-core - 2.15.1.redhat-620133 | The message contains Hi from Camel at 2015-10-03 21:38:13
2015-10-03 21:38:18,300 | INFO  | #0 - timer://foo | timerToLog                       | ?                                   ? | 198 - org.apache.camel.camel-core - 2.15.1.redhat-620133 | The message contains Hi from Camel at 2015-10-03 21:38:18

18. Open browser and type “http://localhost:8181”. This is management console. Type in the username and password as in <FUSE_HOME>/etc/user.properties.  Click on “Camel” tab and open “Camel Contexts”. You should see the statistics of your camel context.

Fuse Management Console

 

Resolving OSGi Bundle dependencies

In this tutorial, we will learn how one OSGi bundle A, which is dependent on another bundle B, can call methods of the bundle B.

Example Scenario :

We have two bundles. We have to call a method of one bundle from another bundle in its Activator class.


Project artifacts:

1. Technologies used –

a. Eclipse IDE for Java EE developer 4.5.0.20150621-1200 (Mars)
b. Preinstalled Maven with Eclipse Mars
c. Java 1.8
d. Eclipse Equinox comes preinstalled with Eclipse Mars

2. Eclipse project folder structure:

OSGiDependencyBundles

3. Bundle Project

Create a karaf bundle project in Eclipse by following the below link.

Create an OSGi Bundle

We will create two bundles. One is independent bundle and one dependent and make relevant changes to the source files and deploy the same, first independent followed by dependent bundle.

4. Maven Dependencies

1. Independent Bundle:

file:pom.xml. Paste the below content to pom.xml of the newly created Independent bundle project. It declares dependencies, uses maven bundle plugin to create OSGi bundles and sets values of important bundle headers of MANIFEST.MF file.

<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>
    <groupId>com.javanbeyond</groupId>
    <artifactId>IndependentBundle</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>bundle</packaging>
    <name>IndependentBundle Bundle</name>
    <description>IndependentBundle OSGi bundle project.</description>
    <dependencies>
        <dependency>
            <groupId>org.osgi</groupId>
            <artifactId>org.osgi.core</artifactId>
            <version>5.0.0</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.felix</groupId>
                <artifactId>maven-bundle-plugin</artifactId>
                <version>2.5.0</version>
                <extensions>true</extensions>
                <configuration>
                    <instructions>
                        <Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
                        <Bundle-Version>${project.version}</Bundle-Version>
                        <Bundle-Activator>com.javanbeyond.independent_bundle.Activator</Bundle-Activator>
                        <!-- Check the export package declaration. We are exporting only the interface package. -->
                        <Export-Package>com.javanbeyond.independent_bundle.interf*;version=${project.version}</Export-Package>
                        <Import-Package>*</Import-Package>
                    </instructions>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

 

2. Dependent Bundle:

file:pom.xml. Paste the below content to pom.xml in the newly created Dependent bundle project.It declares dependencies, uses maven bundle plugin to create OSGi bundles and sets values of important bundle headers of MANIFEST.MF file. Please note that it has declared Independent bundle as one of it’s dependencies and its interface package in “Import-Package” bundle header.

<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>
    <groupId>com.javanbeyond</groupId>
    <artifactId>DependentBundle</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>bundle</packaging>
    <name>DependentBundle Bundle</name>
    <description>DependentBundle OSGi bundle project.</description>
    <dependencies>
        <dependency>
            <groupId>org.osgi</groupId>
            <artifactId>org.osgi.core</artifactId>
            <version>5.0.0</version>
            <scope>provided</scope>
        </dependency>
        <!-- Dependency declaration -->
        <dependency>
            <groupId>com.javanbeyond</groupId>
            <artifactId>IndependentBundle</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.felix</groupId>
                <artifactId>maven-bundle-plugin</artifactId>
                <version>2.5.0</version>
                <extensions>true</extensions>
                <configuration>
                    <instructions>
                        <Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
                        <Bundle-Version>${project.version}</Bundle-Version>
                        <Bundle-Activator>com.javanbeyond.dependent_bundle.Activator</Bundle-Activator>
                        <Export-Package>com.javanbeyond.dependent_bundle*;version=${project.version}</Export-Package>
                        <!-- Check the import package decalaration -->
                        <Import-Package>com.javanbeyond.independent_bundle.interf*;version=${project.version},*</Import-Package>
                    </instructions>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

 

5. Activator class

Class containing lifecycle methods of the bundle called during start and stop of bundle by OSGi framework.

1. Independent Bundle. file: com.javanbeyond.independent_bundle.Activator.java

In the start method of Activator class of Independent bundle, its interface and implementation object is registered with BundleContext to make it available for the dependent bundle to access its method. It can be unregistered by calling unregister method on ServiceRegistration in stop method.

package com.javanbeyond.independent_bundle;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;

import com.javanbeyond.independent_bundle.impl.HelloWorldImpl;
import com.javanbeyond.independent_bundle.interf.IHelloWorld;

public class Activator implements BundleActivator {
    ServiceRegistration<IHelloWorld> helloWorldRegistration;

    public void start(BundleContext context) {
        System.out.println("Starting the bundle.Independent.");
        // Registering the implementation object so that it can be made
        // available by Framework when asked for by some other bundle and the
        // other bundle can call its methods. Please note that we are
        // registering
        // the object as Interface
        helloWorldRegistration = context.registerService(IHelloWorld.class, new HelloWorldImpl()null);
    }

    public void stop(BundleContext context) {
        System.out.println("Stopping the bundle.Independent.");
        // Cleaning up when bundle is stopping
        helloWorldRegistration.unregister();
    }

}

2. Dependent Bundle. file: com.javanbeyond.dependent_bundle.Activator.java

In the start method of Dependent bundle, the registered independent bundle object is referenced and fetched by calling BundleContext getService method. Once the reference is available, the hello world method is being called.

package com.javanbeyond.dependent_bundle;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;

import com.javanbeyond.independent_bundle.interf.IHelloWorld;

public class Activator implements BundleActivator {
    ServiceReference<IHelloWorld> helloWorldReference;

    public void start(BundleContext context) {
        System.out.println("Starting the bundle.Dependent.");
        // Getting the object from Framework by service reference
        // and calling it on the interface.
        helloWorldReference = context.getServiceReference(IHelloWorld.class);
        IHelloWorld helloService = context.getService(helloWorldReference);
        System.out.println(helloService.helloWorld());
    }

    public void stop(BundleContext context) {
        System.out.println("Stopping the bundle.Dependent.");
        // Cleaning up when bundle is stopping
        context.ungetService(helloWorldReference);
    }

}

 

6. Interface of Independent Bundle

file: IHelloWorld.java. This interface is used by the dependent bundle to call on the method of Independent bundle.

package com.javanbeyond.independent_bundle.interf;

public interface IHelloWorld {
    public String helloWorld();
}

7. Implementation of Independent Bundle

file: HelloWorldImpl.java. Implementation of the above interface of independent bundle.

package com.javanbeyond.independent_bundle.impl;

import com.javanbeyond.independent_bundle.interf.IHelloWorld;

public class HelloWorldImpl implements IHelloWorld {

    public String helloWorld() {
        return "Hello World";
    }
}

 


Example Execution:

  1. Execute Maven clean and install and build both the projects.
  2. Install and start the Independent bundle in an osgi framework.If you are new to installing bundles in Eclipse, please read Create a bundle and deploy tutorial.
osgi> install file:<PATH TO BUNDLE>/IndependentBundle-0.0.1-SNAPSHOT.jar
Bundle id is 5
Location             file:<PATH TO BUNDLE>/IndependentBundle-0.0.1-SNAPSHOT.jar
State                2
RegisteredServices   null
ServicesInUse        null
Module               osgi.identity; osgi.identity="IndependentBundle"; type="osgi.bundle"; version:Version="0.0.1.SNAPSHOT" [id=5]
Version              0.0.1.SNAPSHOT
Bundle                   5|Installed  |    1|IndependentBundle (0.0.1.SNAPSHOT)
BundleContext        null
BundleId             5
SymbolicName         IndependentBundle
LastModified         1443908223069
Headers               Bnd-LastModified = 1443908185010
 Build-Jdk = 1.8.0_25
 Built-By = <YOUR ID>
 Bundle-Activator = com.javanbeyond.independent_bundle.Activator
 Bundle-Description = IndependentBundle OSGi bundle project.
 Bundle-ManifestVersion = 2
 Bundle-Name = IndependentBundle Bundle
 Bundle-SymbolicName = IndependentBundle
 Bundle-Version = 0.0.1.SNAPSHOT
 Created-By = Apache Maven Bundle Plugin
 Export-Package = com.javanbeyond.independent_bundle.interf;version="0.0.1.SNAPSHOT"
 Import-Package = com.javanbeyond.independent_bundle.interf;version="[0.0,1)",org.osgi.framework;version="[1.7,2)"
 Manifest-Version = 1.0
 Require-Capability = osgi.ee;filter:="(&(osgi.ee=JavaSE)(version=1.5))"
 Tool = Bnd-2.3.0.201405100607



osgi> start 5
Starting the bundle.Independent.

3.  Install and start the Dependent bundle in the OSGi framework. You should see it calling the Independent bundle method as shown in the console.

osgi> install file:<PATH TO BUNDLE>/DependentBundle-0.0.1-SNAPSHOT.jar
Bundle id is 6
Location             file:<PATH TO BUNDLE>/DependentBundle-0.0.1-SNAPSHOT.jar
State                2
RegisteredServices   null
ServicesInUse        null
Module               osgi.identity; osgi.identity="DependentBundle"; type="osgi.bundle"; version:Version="0.0.1.SNAPSHOT" [id=6]
Version              0.0.1.SNAPSHOT
Bundle                   6|Installed  |    1|DependentBundle (0.0.1.SNAPSHOT)
BundleContext        null
BundleId             6
SymbolicName         DependentBundle
LastModified         1443908296492
Headers               Bnd-LastModified = 1443908265166
 Build-Jdk = 1.8.0_25
 Built-By = <YOUR ID>
 Bundle-Activator = com.javanbeyond.dependent_bundle.Activator
 Bundle-Description = DependentBundle OSGi bundle project.
 Bundle-ManifestVersion = 2
 Bundle-Name = DependentBundle Bundle
 Bundle-SymbolicName = DependentBundle
 Bundle-Version = 0.0.1.SNAPSHOT
 Created-By = Apache Maven Bundle Plugin
 Export-Package = com.javanbeyond.dependent_bundle;version="0.0.1.SNAPSHOT";uses:="org.osgi.framework"
 Import-Package = com.javanbeyond.independent_bundle.interf;version="0.0.1.SNAPSHOT",org.osgi.framework;version="[1.7,2)"
 Manifest-Version = 1.0
 Require-Capability = osgi.ee;filter:="(&(osgi.ee=JavaSE)(version=1.5))"
 Tool = Bnd-2.3.0.201405100607



osgi> start 6
Starting the bundle.Dependent.
Hello World
osgi>

 

Create an OSGi Bundle and Deploy in Equinox

In this tutorial, we will learn how to build a simple OSGi bundle and deploy it in an OSGi framework (Eclipse Equinox).

Technologies used –

  • Eclipse IDE for Java EE developer 4.5.0.20150621-1200 (Mars)
  • Preinstalled Maven with Eclipse Mars
  • Java 1.8

Create Bundle:

Using eclipse-

1.  Open eclipse and go to New > Maven Project > Next > Add Archetype …

2.  Fill in the following values:

Archetype Group Id=org.apache.karaf.archetypes
Archetype Artifact Id=karaf-bundle-archetype
Archetype Version=2.4.0

Please note that this step is needed only if you don’t have the archetype already available in your workspace.

KarafBundleArchetypeCreate

 

3.  Close the maven project window. Again, try creating a maven project and you should see karaf bundle archetype in catalogs list. Please select it.

 

karafbundlearchtype

 

4. Click Next and fill the details.

 

Karaf Bundle Project Artifact Id

 

5. Click Finish. You should get your maven bundle project.

 

Karaf Maven Bundle Project

Using command line (In Mac):

1. Download maven from Maven 3.3.3
2. Unzip the folder in your favorite directory.
3. In your Home directory, open the file .bash_profile. If you don’t have the file, please create one.
4. Update the file with the following entry :

export M2_HOME=~/Development/apache-maven-3.3.3
export PATH=$PATH:$M2_HOME/bin
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home

Note: Please note that ~(tilde) used is my home directory since I downloaded maven in my home directory. Also, version of java used may be different for you and so is your java installation directory. Please make changes accordingly.

5.  Open command line and type the below command.

mvn archetype:generate \
    -DarchetypeGroupId=org.apache.karaf.archetypes \
    -DarchetypeArtifactId=karaf-bundle-archetype \
    -DarchetypeVersion=2.4.0 \
    -DgroupId=com.javanbeyond \
    -DartifactId=MyFirstBundle \
    -Dversion=0.0.1-SNAPSHOT \
    -Dpackage=com.javanbeyond.bundle \
    -DinteractiveMode=false

6. Once the command execution completes, you should get the following folder structure inside  MyFirstBundle folder.

.
./pom.xml
./src
./src/main
./src/main/java
./src/main/java/com
./src/main/java/com/javanbeyond
./src/main/java/com/javanbeyond/bundle
./src/main/java/com/javanbeyond/bundle/Activator.java

Project artifacts:

1. Maven dependencies and bundle plugin
file:pom.xml. It uses maven bundle plugin to create bundles and make appropriate entries in the bundle MANIFEST.MF file.

<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">
    <!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. -->
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.javanbeyond</groupId>
    <artifactId>MyFirstBundle</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>bundle</packaging>
    <name>MyFirstBundle Bundle</name>
    <description>MyFirstBundle OSGi bundle project.</description>
    <dependencies>
        <dependency>
            <groupId>org.osgi</groupId>
            <artifactId>org.osgi.core</artifactId>
            <version>5.0.0</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.felix</groupId>
                <artifactId>maven-bundle-plugin</artifactId>
                <version>2.5.0</version>
                <extensions>true</extensions>
                <configuration>
                    <instructions>
                        <Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
                        <Bundle-Version>${project.version}</Bundle-Version>
                        <Bundle-Activator>com.javanbeyond.bundle.Activator</Bundle-Activator>
                        <!-- The package exposed and can be used by other bundles. -->
                        <Export-Package>com.javanbeyond.bundle*;version=${project.version}</Export-Package>
                        <!-- wildcard dynamic import. if there is any dependency on other bundles, it will be loaded automatically, if deployed in the container -->
                        <Import-Package>*</Import-Package>
                    </instructions>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

2. OSGi Life cycle methods

file: Activator.java. Lifecycle class whose methods are called before a bundle is started and after it is stopped.

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.javanbeyond.bundle;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;

// This is a life cycle class of a bundle
public class Activator implements BundleActivator {

    // this life cycle method is called when the bundle starts
    // it get Bundle Context parameter which can used to register a class, get
    // other bundles etc.
    public void start(BundleContext context) {
        System.out.println("Starting the bundle");
    }

    // this method is called when the bundle stops. Can be used for resource
    // cleaning etc.
    public void stop(BundleContext context) {
        System.out.println("Stopping the bundle");
    }

}

 

Start OSGi framework (Eclipse Equinox):

Eclipse Mars comes preinstalled with Equinox OSGi framework.

  1. Right click on the project and go to  Run As  > Run Configurations…> OSGi Framework.
  2. Double click on OSGi Framework and name the configuration of your choice.
  3. Uncheck all the boxes of Target Platform in Bundles column.
  4. Select only the below bundles:
  • org.apache.felix.gogo.command
  • org.apache.felix.gogo.runtime
  • org.apache.felix.gogo.shell
  • org.eclipse.osgi
  • org.eclipse.equinox.console

Eclipse OSGi Equinox

5. Go to Settings tab above and check “Clear the configuration area before launching” and click on Apply and then  Run.

Start OSGi Framework Console

Deploy the created OSGi Bundle:

1. Run maven clean and install on the newly created project. A jar named “MyFirstBundle-0.0.1-SNAPSHOT.jar” should get created in the target folder.

2. On the OSGi console, type “install file:<PATH TO JAR>/MyFirstBundle-0.0.1-SNAPSHOT.jar” where <PATH TO JAR> is location of jar.

Please note that in case of Windows, you might need to provide double slash for path to jar.

3. The jar should get deployed with a snapshot of it’s MANIFEST.MF file.

Install OSGi Bundle initiative

MANIFEST.MF

The MANIFEST.MF file, which resides in META-INF folder of jar is to Bundle as web.xml is to War (Web archive) file. The MANIFEST.MF file acts as a deployment descriptor of the bundle. Here’s a brief description of the headers of MANIFEST.MF file.

Bnd-LastModified
This header is set by Bnd API to give information on the last modified time of the bundle.

Build-Jdk
The Jdk version used to build this bundle.

Bundle-Activator
It names our optional listener Activator class to be notified of bundle start and stop events. Please note that the class should be public and contain a public constructor without any arguments.

Bundle-Description
A brief description of the bundle. This is set to some default value if no value is mentioned in pom.xml. In practice, it can be a description of the role the bundle plays in the application.

Bundle-ManifestVersion
It indicates the OSGi specification to use for reading this bundle. 1 indicates OSGi release 3 while 2 OSGi release 4 and later.

Bundle-Name
It defines a short, human-readable name for the bundle.

Bundle-SymbolicName
It specifies a unique identifier for the bundle. This name will be used while referring a given bundle from other bundles. In our case, the project name is given to the value of this header.

Created-By
The API name that created it.

Bundle-Version
The version of the bundle.In our case, project version is given to the value of this header.

Export-Package
It indicates what packages (available in the bundle) are exported so they can be imported by other bundles. Only the packages specified by this header will be exported, the rest will be private and will not be seen outside the containing bundle.

Import-Package
It indicates the packages that are imported by a bundle. Only the packages specified by this header will be imported. By default, imported packages are mandatory – the importing bundle will fail to start, if the imported package is not available. In our case, we have given wildcard expression to the value of this header. It means that the Maven Bnd api would traverse though all the import statements of bundle to get the name of the dependent packages for this bundle and accordingly, set its value. Please note that if you are using declarative configuration where you define dependencies in xml files, it may not be reported here. In such case, please set the values specifically.

4. Type “start <BUNDLE ID>”. In this case, <BUNDLE ID> is 5. So the command is “start 5”.

5. The bundle should start with the output of Activator’s start method. Similarly, if it is stopped, it will output the print of stop method.

6. Stop the bundle with command “stop <BUNDLE ID>”.

7. If you want to uninstall the bundle, type “uninstall <BUNDLE ID>”.

osgi> start 5
Starting the bundle
osgi> stop 5
Stopping the bundle
osgi> uninstall 5
osgi>

 

Back to Top