37

How can I code my bundle in Scala and then deploy it into OSGI container?

Do I compile it into "java" first or can i deploy scala straight into OSGI and use some kind of bundles to recognize it?

Any pointers would be great. Currently I am using Apache Felix as my osgi-container, but just a simple explanation of generic concepts would suffice to get me started.

Andriy Drozdyuk
  • 58,435
  • 50
  • 171
  • 272
  • 1
    scala compiles to java bytecode so everything that you can make with java applies also to scala. Scala libraries at http://scala-tools.org (at least scala-compiler and scala-library) are osgi bundles. If you want you can also use [ScalaModules](https://github.com/weiglewilczek/scalamodules) - scala DSL for OSGi. – tenshi Feb 19 '11 at 19:08
  • Here is example OSGi + Scala + ScalaModules + sbt project: https://github.com/weiglewilczek/scalamodulesexamples – tenshi Feb 19 '11 at 19:12
  • What tool chain would you like your example in? For instance would you like to use Eclipse, Maven, or something else? – rancidfishbreath Sep 21 '11 at 14:46
  • I would love to see an example in a (recent) Eclipse version (3.6 or later). No matter the OSGi implementation - Felix or Equinox. – Andriy Drozdyuk Sep 23 '11 at 16:16
  • The following project has a mixture of Java and Scala code, and it is built with bnd: https://github.com/paremus/examples – Neil Bartlett Sep 26 '11 at 13:13
  • Thanks Neil, that is great. But how does he work in Eclipse with the project? I'm afraid I just don't understand bnd - I am using Equinox container, where I just create a "Run Configuration" and go. Does this mean that the "scala" bundles should be included in the run configuration? Are there any special scala-specific components/services that need to be in the container? – Andriy Drozdyuk Sep 27 '11 at 19:29

4 Answers4

34

Thanks to everyone for the answers, you led me to the solution! I will describe it here in a little simpler terms for a wider audience.

Goal: Code in scala, deploy to OSGi.

Tools used:

  1. Equinox OSGi implementation
  2. Eclipse Helios 3.6,
  3. Scala 2.9

Procedure

  1. Install Scala IDE for Eclipse. Find version that will work with Scala 2.9 and Eclipse 3.6
  2. Create new Scala Project in Eclipse.
  3. Convert the project to OSGi bundle by right clicking on it and selecting: Configure -> Convert to Plug-in Projects...

    Now, the next part was where I got stuck. You see, now we need to deploy this bundle (our project) to OSGi environment. However we are missing the Scala classes (or bundle that contains those classes) that have to be in OSGi container to provide all the Scala packages API we use in our bundle. Unfortunately finding the "Scala bundle" is not that easy. After looking around it turns out, that for some reason, Scala bundle is actually located in the Sonatype Maven Repository.

  4. Download the scala-library-2.9.1.jar from the appropriate location in the Sonatype Maven Repository, and deploy it (by means most comfortable for you) to your OSGi container.

  5. Adjust your manifest file to require the Scala bundle (I am pretty sure that this is one place where bundle dependency (i.e. Require-Bundle) is actually pretty safe - after all, you will never run your Scala code without Scala!):

    Manifest-Version: 1.0
    Bundle-ManifestVersion: 2
    Bundle-Name: Scala Hello
    Bundle-SymbolicName: com.test.scala.hello
    Bundle-Version: 1.0.0.qualifier
    Bundle-Vendor: drozzy
    Import-Package: org.osgi.framework;version="1.5.0"
    Bundle-Activator: com.test.scala.hello.Activator
    Require-Bundle: scala-library;bundle-version="2.9.1"
    
  6. Now, you can write your bundle activator in Scala (wooho!):

    //Activator.scala
    package com.test.scala.hello
    import java.lang.System
    import org.osgi.framework.BundleActivator
    import org.osgi.framework.BundleContext
    
    class Activator extends BundleActivator {
      def start(context: BundleContext) {
          System.out.println("Hello world from scala!");
      }
      def stop(context: BundleContext){}
    }
    
  7. Deploy your project as a bundle to OSGi container and look out for the "Hello world from scala!" message.

Andriy Drozdyuk
  • 58,435
  • 50
  • 171
  • 272
  • This is a great answer, but also 3 years old now. I wonder what your answer would be today? Clearly you're probably on Scala 2.10 or 2.11 now. Still with Equinox? Still using Eclipse Helios? – Reid Spencer Oct 25 '14 at 18:39
  • @rspencer Today I am using Akka 2.2.4 :-) I would love to try and combine OSGi with Akka - but I've not had the luck to work on a big enough project yet where resilience/fault-tolerance and adaptability is a priority (i.e. aerospace, healthcare...). My initial instinct would be to modularize the system coarsely with OSGi, while having each bundle be implemented with actors. *Oh, and today I use IntelliJ for Scala-based development (it is greatly improved over the state it was in few years ago).* – Andriy Drozdyuk Oct 27 '14 at 23:42
  • Thanks for the update. Seems we are working with the same technologies these days. I too am interested in OSGifying akka actors and have just noted the [akka osgi subproject](https://github.com/akka/akka/tree/master/akka-osgi/src/main/scala/akka/osgi). – Reid Spencer Oct 28 '14 at 13:25
  • Indeed, akka/scala guys do good work! I'm surprised by how high the quality of the code they put out is---I hope they don't stop or go "Sun" on us. Feel free to contact me directly via drozzy@gmail.com – Andriy Drozdyuk Oct 28 '14 at 20:04
14

ScalaModules

A quick intro video by the author here Scala days 2010

Johan Prinsloo
  • 1,188
  • 9
  • 9
  • Great answer, ScalaModules is several times nicer to use than anything available in pure Java. Another good video available here: http://skillsmatter.com/podcast/ajax-ria/scala-modules – Kevin Wright Feb 19 '11 at 21:20
  • So is ScalaModules a subset of Scala language with a different compiler? I am not sure what DSL means here... – Andriy Drozdyuk Feb 20 '11 at 01:48
  • Also I don't need a "module", I need some way of running scala IN OSGI. Scala modules has no documentation on how to deploy it, install it or use it. – Andriy Drozdyuk Feb 22 '11 at 19:09
  • There is a companion examples project: https://github.com/weiglewilczek/scalamodulesexamples - the rest is standard OSGi deployment. – Johan Prinsloo Feb 22 '11 at 21:50
  • Yeah, but what is the project made in? Is it Eclipse, Netbeans, something else? Unless I am blind - i can't find any reference to this information. – Andriy Drozdyuk Feb 24 '11 at 13:24
  • 2
    Like many (most?) Scala projects it uses the [Simple Build Tool](http://code.google.com/p/simple-build-tool/) - from which you can export project files for various IDE's. – Johan Prinsloo Feb 26 '11 at 02:04
  • OK I made a quick example project that uses Scalamodules - it uses SBT and Intellij IDEA (with the scala and SBT plugins): https://github.com/johanprinsloo/scalamoduleplay – Johan Prinsloo Mar 08 '11 at 04:13
  • I may be mistaken, but is Scala modules really *required* to use Scala in OSGi? – Andriy Drozdyuk Sep 28 '11 at 15:32
8

OSGi does not care what language you write your code in: JVM bytecode is just JVM bytecode. So:

  • compile with Scala
  • wrap the resulting classes in a bundle using bnd (just as you would for code compiled from Java sources).
  • deploy the bundle to OSGi, just as you would for a "normal" bundle (because it is a normal bundle).

You'll notice that your bundle has dependencies on the Scala library. Again there is nothing strange about this, it's just like having dependencies in you Java code. In order for those dependencies to resolve, you need to install the Scala library bundle from scala-lang-osgi

Neil Bartlett
  • 23,743
  • 4
  • 44
  • 77
5

There is nothing special to it: write your code in Scala and wrap it up as an OSGi bundle by providing the necessary bundle meta data and service descriptors as you would do with Java.

Apache Maven can help you in the process. Have a look at the Guggla Project (a Scala script engine) for a working example. The maven-bundle-plugin in the pom.xml takes care of generating and including the bundle meta data in the final jar file. It refers to the service descriptor xml file which you need to provide.

michid
  • 10,536
  • 3
  • 32
  • 59