Showing posts with label idea. Show all posts
Showing posts with label idea. Show all posts

Thursday, September 27, 2012

Creating OSGi Bundles with Maven Bundle Plugin

OSGi is a framework specification to create java applications based on a complete and dynamic component model. That means your application can be entirely consisted of cohesive and loosely-coupled modules. These modules are called "bundles" in OSGi jargon.

There are several implementations including Apache Felix and Eclipse Equinox. These implementations use existing capabilities of Java Archive (jar) files to implement OSGi bundles. If you look inside a general jar file (which is basically a zip file with .jar extension), there will be a file called MANIFEST.MF inside a directory named META-INF. If the jar was built by maven it will contain some meta data like: 
Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven
Built-By: amilas
Build-Jdk: 1.6.0_35


There are manifest headers specific to OSGi so that runtime can recognize the jar file as a bundle ( and more). Bnd is a tool used to insert those headers into jar files.

While there's built-in support for OSGI in famous IDEs such as Eclipse and IntelliJ IDEA, if your project is maven-based, it's convenient to use maven for creating bundles too.
Maven Bundle Plugin can be used for this purpose. We can specify OSGi parameters in the module POM and the bundle plugin will use Bnd to automatically insert them and create an OSGi bundle.

This is a introductory post to show how to use Maven with IntelliJ IDEA to create OSGi bundles.

Goto File-> New Project... and select "Create  project from scratch"
At the next screen, enter give a project name and a location as shown. Make sure to select the type as Maven Module.



Finally enter a group id as shown, and click finish.

This will create a new project and a module from maven archtype.

Now let's create two sub modules for our project. these will be the resulting OSGi bundles.

Goto File-> Add Module... again "Create  project from scratch" and name the module as "module-a". In the final screen "Add module to" and "parent" fields should be automatically filled as shown.


Similarly add another module named module-b

 Create two packages "api" and "impl" inside org.amila.sample.osgi.a in module-a, and similarly in module-b so the project pane will show the project structure as follows.

You can use Bundle activator to perform some tasks when the OSGi framework starts or stops a certain bundle. We can implement org.osgi.framework.BundleActivator interface to utilize this function.
Create a new class Adapter in your impl package in module A. Use the following code:

package org.amila.sample.osgi.a.impl;

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

public class Activator implements BundleActivator {

    /**
     * Implements BundleActivator.start().
     * @param bundleContext - the framework context for the bundle.
     **/
    @Override
    public void start(BundleContext bundleContext) throws Exception {
        System.out.println("Module A is starting");
    }

    /**
     * Implements BundleActivator.stop().
     * @param bundleContext - the framework context for the bundle.
     **/
    @Override
    public void stop(BundleContext bundleContext) throws Exception {
        System.out.println("Module A is shutting down");
    }
}

Add the felix dependency to the module POM.


Set the packaging to "bundle" and add the maven bundle plugin.

After these changes, pom.xml of module-a should look like this:

<?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">
    <parent>
        <artifactId>OsgiDemo</artifactId>
        <groupId>org.amila.sample.osgi</groupId>
        <version>1.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>module-a</artifactId>
    <packaging>bundle</packaging>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.felix</groupId>
                <artifactId>maven-bundle-plugin</artifactId>
                <version>2.3.7</version>
                <extensions>true</extensions>
                <configuration>
                    <instructions>
                        <Bundle-SymbolicName>${project.groupId}.${project.artifactId}</Bundle-SymbolicName>
                        <Bundle-Name>${project.artifactId}</Bundle-Name>
                        <Bundle-Version>1.0.0</Bundle-Version>
                        <Bundle-Activator>org.amila.sample.osgi.a.impl.Activator</Bundle-Activator>
                        <Private-Package>org.amila.sample.osgi.a.impl</Private-Package>
                   </instructions>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>org.apache.felix</groupId>
            <artifactId>org.osgi.core</artifactId>
            <version>1.4.0</version>
        </dependency>
    </dependencies>

</project>

Similarly add Activator and bundle configuration for module B.

 Now, you can build the project using maven. Resulting jar files will be OSGi bundles. If you open MANIFEST.MF file of a bundle now it will contain bundle headers as we configured in the module POM.

You don't have to specify packages to import. If your module uses a package from another module, its import header will be automatically added to the manifest.mf file.

Thursday, September 22, 2011

Running / Debugging Apache Axis2 inside IntelliJ IDEA

  1. Introduction
  2. Required tools
  3. Setting up
  4. Getting the source and Building
  5. Open Axis2 source with IDEA
  6. Run/debug configuration
  7. Add required libraries
  8. Run/debug Axis2 within IDEA

1.Introduction

Apache Axis2 is a famous web services engine and toolkit which is heavily used in industry.



2.Required Tools

Linux
Sun JDK 1.6
IntelliJ IDEA 10.5
Maven 2
Subversion 1.6

3.Setting up

Make sure you've correctly installed Sun JDK, Maven2 and SVN. (refer to my previous posts for details)

4.Getting the source and Building


Create the folder where you want to get sources.
for example, if you want it in "axis2Source"

then get the source from apache subversion repository by,



goto the modules/distribution directory and execute:
 

That will compile the sources and build binary and source distributions.
Goto taget directory. You will see the binary distribution pack created as axis2-1.7.0-SNAPSHOT-bin.zi.
Extract it in the same directory.

5.Open Axis2 source with IDEA

Goto IDEA.
Goto File->Open Project
Browse to the axis2Source directory

Select the parent pom and press OK. IDEA will automatically import all the modules and create the project structure.


6.Run/debug configuration

Goto Run->Edit Configurations
Add "Application" configuration using the "+" button.
Select org.apache.axis2.transport.SimpleAxis2Server as the Mail class


Set the desired log4j properties file as a VM parameter:
For example:



You have to pass axis2 repository location and axis2.xml path as program parameters.
Set the working directory as extracted bin distro root



Select the module you want to debug in "Use classpath and JDK of module" and press OK

 
 7.Add required libraries

Then you have to make sure all the libs required for axis2 to start are available in the classpath.
To configure that, select the desired module (axis2kernal) in project window and press F4 to open configure dialog box.
Goto dependencies tab and press Add, select Library from drop down menu.

Click "New Library..."
Select "Attatch jar directories..."

Select the lib directory of the binary distribution

Give a name such as "axis2Lib" and press OK.
You can also select other modules as dependencies if you want to debug them as well.



Finally, press OK to exit the configuration dialog.

8.Run/debug Axis2 within IDEA

Now you can Start the Axis2 server inside IDEA by the Run command (Shift+F10).
Try pointing your browser to http://localhost:8080/ and see if services get listed.



And you can debug axis2 as you debug any other code. Try setting a breakpoint in SimpleAxis2Server and start debugging (Shift+F9)


Advantage here is that, you don't have to maven build every time you change something, as opposed to remote debugging.