Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Note: The sample project can be downloaded, see the resources section.

Step 1 : Initial Project Setup

First, we create two eclipse projects using the maven archetype 'spring-osgi-bundle-archetype'. This archetype is helpful because it generates a pom.xml file that we will use with maven goal(s) to create the :

...

Next, you import these projects into your favorite workspace of Eclipse.

Step 2 : Develop the interface

...

Developing an OSGI project could be 'potentially' time consuming regarding to :
- The learning curve of the new technology to acquire,
- Packaging of the components, registering of the components,
- How to call the OSGI server ? How Can I have access to the bundle context ?

You could figure out that developing OSGI is similar to EJB 1.0. Remember that time when the only way to design an enterprise solution was to use EJB with proxy, stub-skeleton classes, protocol RMI/IIOP for the communication between the client and the server, ...

Luckily, this is not the case because the specification has tried to avoid such pitfalls of EJB spefications and because two Two interesting projects exist today to simplify the life of a Java Developer or an architect when they would like to develop a bundle our life :

  • iPOJO (Apache Felix)
  • Spring Dynamic Modules (Spring)

The goals of these frameworks are to :

  • Design the business logic as POJO,
  • Inject dependency(ies) though IoC,
  • Handle lifecycle of the bundle and its relation with BundleContext
    With such frameworks, The developer focus its development time on the business logic and not on How to call the OSGI server ? How Can I have access to the bundle context ? How can I register my bundle in the Osgi Bundle Registry registry ?

Although this tutorial is based on Spring Dynamic Modules, iPOJO can be used as an alternative.

So now, it is time to create the interface that we will use through this project. Open Eclipse environment if not already done and create a new folder "service" under src/main/java/demo tree. Add the interface 'TransformService.java' and copy paste the code hereunder :

Code Block
java
java

package demo.service;

public interface TransformService {
	
	public Object transform(Object obj);

}

Step 3 : Create the class implementing the interface

In this first partNext, we will not re-invent the wheel but instead we will use the MyTransform class of the Camel osgi example. So, copy the following code from Camel SVN project and create a class inside create the class 'TransformServiceImpl' implmenting the interface 'TransformService'.

Create the class 'TransformServiceImpl.java' under the folder src/main/java/main/demo named MyTransform.java.demo/service

Code Block
java
java
package demo.service;

import java.util.Date;

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

/**
 * @version $Revision: 640450 $
 */
public class MyTransformTransformServiceImpl implements TransformService {
    private static final transient Log LOG = LogFactory.getLog(MyTransformTransformServiceImpl.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 spring configuration files

The next step concerns the creation of the configuration files who will allow the dependency injection and later the deployment of the bundle into the OSGI server and its registration as a 'service'.

a) Dependency Injection

Create the file 'demo-service-bundle-contxt.xml' under the folder 'src/main/resources/META-INF/spring'

Code Block
xml
xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

  <!-- regular spring configuration file defining the beans for this
       bundle. The configuration of OSGi definitions is kept in a separate 
       configuration file so that this file can easily be used
       for integration testing outside of an OSGi environment -->
    <bean id="transformService"
	      class="demo.service.TransformServiceImpl">
	</bean>
</beans>

b) OSGI 'Injection'

Create the file 'demo-service-bundle-contxt-osgi.xml' under the folder 'src/main/resources/META-INF/spring'

Code Block
xml
xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:osgi="http://www.springframework.org/schema/osgi"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/osgi
    http://www.springframework.org/schema/osgi/spring-osgi.xsd">

	<!--
		definitions using elements of the osgi namespace can be included in
		this file. There is no requirement to keep these definitions in a
		separate file if you do not want to. The rationale for keeping these
		definitions separate is to facilitate integration testing of the
		bundle outside of an OSGi container
	-->

	<osgi:service ref="transformService">
		<osgi:interfaces>
			<value>demo.service.TransformService</value>
		</osgi:interfaces>
	</osgi:service>

</beans>

Remark : for more information about Spring Dynamic Modules and configuration, I recommend to read their documentation

Step 5 : Generate the MANIFEST.MF file and jar of the bundle

#Resources

  • Attachments
    patterns.*part-one.zip