Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
96 views
in Technique[技术] by (71.8m points)

Scala in OSGI container?

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.

question from:https://stackoverflow.com/questions/5052706/scala-in-osgi-container

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...