Over a million developers have joined DZone.
Platinum Partner

Scala OSGi Bundle With Spring DM

· Java Zone

The Java Zone is brought to you in partnership with ZeroTurnaround. Check out this 8-step guide to see how you can increase your productivity by skipping slow application redeploys and by implementing application profiling, as you code!

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

The Java Zone is brought to you in partnership with ZeroTurnaround. Check out this 8-step guide to see how you can increase your productivity by skipping slow application redeploys and by implementing application profiling, as you code!

Topics:

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}