Ouertani Slim was born in Tunisia in 1981. Now he is a software engineer since 2004 and he is Java 6 and Spring certified with 100% and 94% respectively. He is very interested in Java technology, Scala and open source projects. He believes that programming is one of the hardest jobs and most beautiful in the world. Slim has posted 32 posts at DZone. You can read more from them at their website. View Full User Profile

Scala OSGi Bundle With Spring DM

08.05.2008
| 10450 views |
  • submit to reddit

Today, the OSGi bundles concept with SSAP as the application server is one of the most interesting things that developer should know. In addition, Scala is one of the most powerful languages . Reading some blogs on how to integrate Scala with OSGi : an introduction given by Neil and also the DSL concept presented by Roman , I am asking, is it possible to do Scala with Spring DM as a Scala generated .class file?

I will try here to present how the Scala language can be easy integrated with Spring DM and deployed by the Maven Felix plugin.

First of all, I downloaded one of the daily Netbeans build versions (6.5 M1 ) and installed the Scala plugin on it. Next I created a Maven project. As Java, I create a service trait and the implementation class :

package com.jtunisie.osgi.scala

trait TService {
def getMessage(): String
}

 

package com.jtunisie.osgi.scala.impl


class Service (message: String)extends TService {
def getMessage()={
message
}
def init()={
println( message);
}
}

 The service declaration will be done using Spring DM in the declarative way :

<?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-2.5.xsd
http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi.xsd">



<bean id="service_" class="com.jtunisie.osgi.scala.impl.Service">
<constructor-arg index="0" value="sTunisie"/>
</bean>

<osgi:service id="serviceOsgi" interface="com.jtunisie.osgi.scala.TService" ref="service_" />
</beans>

 The init method will be called by Spring after initialisation and to check if our bundle works. Next, we modify the pom.xml to code compile: add scala plugins

 <plugin>
<groupId>org.scala-tools</groupId>
<artifactId>maven-scala-plugin</artifactId>
</plugin>

 

and Felix bnd plugins to generate the bundle.

<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<instructions>
<Export-Package>com.jtunisie.osgi.scala.*</Export-Package>
<Include-Resource>src/main/resources</Include-Resource>
<Private-Package>com.jtunisie.osgi.scala.impl.*</Private-Package>
</instructions>
</configuration>
</plugin>

The idea here is that the Scala plugins are used to compile the project and as the Bnd tool inspects the .class files and not sources, it will be used generate the bundle.

$>mvn scala:compile install

 I have used the Bnd tool also on the command line to general the scala-library bundle :

$>java -jar bnd.jar wrap scala-library.jar 

Now after deploying the two bundles on Osgi container, starting our bundle we will see sTunisie on console output.

Full source project code is available : svn checkout http://osgiscala.googlecode.com/svn/trunk/ osgiscala-read-only

Published at DZone with permission of its author, Slim Ouertani.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Slim Ouertani replied on Wed, 2008/08/06 - 4:17am

Note : if we scala version newer than 2.5 we don't need bnd for scala library as it's released with OSGi support.

Correction : Add default-init-method="init" to spring dm xml file to enable init method call when bean is created.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.