Error CSS Stylesheet macro - Import URL 'http://felix.apache.org/ipojo/site/superfish.css' is not on the allowlist. If you want to include this content, contact your Confluence administrator to request adding this URL to the Allowlist.
Error CSS Stylesheet macro - Import URL 'http://felix.apache.org/ipojo/site/style.css' is not on the allowlist. If you want to include this content, contact your Confluence administrator to request adding this URL to the Allowlist.

Developing Camel mediators with iPOJO

The Camel OSGi tutorial provides a very simple and neat overview of Camel. This article mentions the usage of iPOJO to implement the mediator. This document provides this implementation.

Step 1 : Initial project setup

Instead of using the 'spring-osgi-bundle-archetype' maven archetype, we will use the iPOJO one to create the iPOJO bundle.
To create the simple iPOJO service project, execute the following command in your Unix/Dos console.

mvn archetype:create -DarchetypeGroupId=org.apache.felix -DarchetypeArtifactId=maven-ipojo-plugin -DarchetypeVersion=1.0.0 -DgroupId=demo -DartifactId=demo.service-ipojo-bundle -Dversion=0.1

Step 2 : Develop the interface

So now, it is time to create the interface that we will use in the project. Create a new folder "service" in src/main/java/demo tree. Add the interface TransformService.java and copy paste the code below:

TransformService.java
package demo.service;
public interface TransformService {
    public Object transform(Object obj);
}

Step 3 : Create the class implementing the interface

Next, we will create the class TransformServiceImpl implementing the interface TransformService.
Create the class TransformServiceImpl.java in the folder src/main/java/demo/service/

TransformServiceImpl.java
package demo.service;

import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class TransformServiceImpl implements TransformService {
    private static final transient Log LOG = LogFactory.getLog(TransformServiceImpl.class);
    private boolean verbose = true;
    private String prefix = "MyTransform";

    public Object transform(Object body) {
        String answer = prefix + " set body:  " + new Date();
        if (verbose) {
            System.out.println(">> call >> " + answer);
        }
        LOG.info(">> call >>" + answer);
        return answer;
    }

    public boolean isVerbose() {
        return verbose;
    }

    public void setVerbose(boolean verbose) {
        this.verbose = verbose;
    }

    public String getPrefix() {
        return prefix;
    }

    public void setPrefix(String prefix) {
        this.prefix = prefix;
    }
}

Step 4 : Create the iPOJO configuration files

The next step concerns the creation of the iPOJO configuration file who will allow the registration of the TranformService as an OSGi service.
Create or edit the file metadata.xml in the folder src/main/resources/

metadata.xml
<?xml version="1.0" encoding="UTF-8"?>
<ipojo>
	<component classname="demo.service.TransformServiceImpl" name="my-ipojo-transformer">
   		<provides/>
	</component>
	<instance component="my-ipojo-transformer"/>
</ipojo>

Step 5 : Generate the bundle

Before generating the bundle, we need to edit the pom.xml file to describe our bundle.

pom.xml
<project>
  <modelVersion>4.0.0</modelVersion>
  <packaging>bundle</packaging>
  <groupId>demo</groupId>
  <artifactId>demo.service-ipojo-bundle</artifactId>
  <version>0.1</version>
  <name>demo.service-ipojo-bundle</name>
  
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.felix</groupId>
        <artifactId>maven-bundle-plugin</artifactId>
        <version>1.4.2</version>
        <extensions>true</extensions>
        <configuration>
          <instructions>
            <Bundle-SymbolicName>${pom.artifactId}</Bundle-SymbolicName>
            <Import-Package>*</Import-Package>
            <Export-Package>*</Export-Package>
          </instructions>
        </configuration>
      </plugin>
      <plugin>
	      <groupId>org.apache.felix</groupId>
	      <artifactId>maven-ipojo-plugin</artifactId>
		  <executions>
          	<execution>
            	<goals>
	              <goal>ipojo-bundle</goal>
               </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  
  <dependencies>
	  <dependency>
          <groupId>org.slf4j</groupId>
          <artifactId>com.springsource.slf4j.org.apache.commons.logging</artifactId>
          <version>1.5.0</version>
          <scope>provided</scope>
        </dependency>
        <dependency>
          <groupId>org.slf4j</groupId>
          <artifactId>com.springsource.slf4j.api</artifactId>
          <version>1.5.0</version>
          <scope>provided</scope>
        </dependency>
        <dependency>
          <groupId>org.slf4j</groupId>
          <artifactId>com.springsource.slf4j.log4j</artifactId>
          <version>1.5.0</version>
          <scope>provided</scope>
          <exclusions>
            <exclusion>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.apache.log4j</groupId>
                <artifactId>com.springsource.org.apache.log4j</artifactId>
            </exclusion>
          </exclusions>
        </dependency>
    </dependencies>
</project>

Step 6 & 7: Create the Camel context file and the bundle

These steps are exactly the same as into the OSGi Camel Tutorial. So refer to the step 6 and 7 of the article.

Step 8: Deploy the bundles

We will see how to execute the Canel route on ServiceMix Kernel.
As mentioned in the documentation, Apache ServiceMix Kernel is a small OSGi based runtime which provides a lightweight container onto which various components and applications can be deployed. Moreover, the server provides administration, security, logging and provisioning features that will help you during the deployment and administration steps.
If this is not yet done, download ServiceMix Kernel 1.0.0 server and install it. Launch the server by executing the command in the bin folder:

/Users/clement/apache-servicemix-kernel-1.0.0/bin>servicemix

If this is the first time Servicemix is started, then you will see that a new data folder is created. Notes that this application requires Java 6.
In order to allow our bundles to work with Camel, execute the following commands to download and install the 'Camel and iPOJO bundles':

servicemix> osgi install -s mvn:org.springframework/spring-tx/2.5.5
servicemix> osgi install -s mvn:org.apache.camel/camel-core/1.5.0
servicemix> osgi install -s mvn:org.apache.camel/camel-spring/1.5.0
servicemix> osgi install -s mvn:org.apache.camel/camel-osgi/1.5.0
servicemix> osgi install -s mvn:org.apache.felix/org.apache.felix.ipojo/1.0.0

Next, copy the two jar files into the deploy folder, first the iPOJO service and next the Camel bundle. After a few seconds, you should see on the Servicemix log console the following text:

servicemix> >> call >> MyTransform set body:  Sat Dec 27 19:29:16 CET 2008

Remarks :

  • In case of trouble, use the command: osgi list to see if all the deployed bundles are installed correctly and have their status equals to active
  • To see the log of ServiceMix, use the command: log d

  • No labels