Skip to content
This repository has been archived by the owner on May 21, 2021. It is now read-only.

Getting started

hseeberger edited this page Jan 18, 2011 · 3 revisions

As ScalaModules targets at OSGi development we assume you are familiar with OSGi. If that’s not the case, please refer to the excellent OSGi specification or Neil’s great open source OSGi book.

Why ScalaModules?

Let’s look at the following example: We want to find a Greeting service and call its welcome method. Nothing more. Here comes the Java code we have to write using the plain OSGi API:

ServiceReference reference = context.getServiceReference(Greeting.class.getName());
if (reference != null) {
    try {
        Object service = context.getService(reference);
        Greeting greeting = (Greeting) service;
        if (greeting != null) {
            System.out.println(greeting.welcome());
        } else {
            System.out.println("No Greeting service available!");
        }
    } finally {
        context.ungetService(reference);
    }
} else {
    System.out.println("No Greeting service available!");
}

Ain’t that too much boilerplate? Getting a ServiceReference, checking for null, getting the service using an ugly type cast to Greeting, checking for null again. Then, finally, call Greeting.welcome. After all, don’t forget to call BundleContext.ungetService in a finally clause. Definitely lots of things to forget or get wrong!

And here comes the ScalaModules code, of course written in Scala:

context findService withInterface[Greeting] andApply { _.welcome } match {
  case None => println("No Greeting service available!")
  case Some(welcome) => println(welcome)
}

Ain’t that a relief? Only three lines of code, compared to more than ten. And the compiler will even statically enforce that you get the type of service you are looking for.

Getting ScalaModules

Depending on your “development environment” there are several options. In the following sections we are going to cover SBT, Maven and “manually”.

For SBT users

ScalaModules is published to the Scala-Tools.org Maven repository. It is cross-built against Scala 2.8.0 and 2.8.1 (and possibly future versions). Therefore add the following dependency to your project definition, using the %% operator:

val scalaModulesVersion = "2.0.2" // Or later
val scalaModulesCore = "com.weiglewilczek.scalamodules" %% "scalamodules-core" % "2.0.2"

If you also want to get the sources:

val scalaModulesVersion = "2.0.2" // Or later
val scalaModulesCore = "com.weiglewilczek.scalamodules" %% "scalamodules-core" % "2.0.2" withSources

Don’t forget to call update in order to actually get the artifacts.

For Maven users

As mentioned above, you can get ScalaModules from the Scala-Tools.org Maven repository. Therefore you have to add that one to the repositories section in POM:

<repository>
  <id>scala-tools.releases</id>
  <name>Scala-Tools.org Maven repository for releases</name>
  <url>http://scala-tools.org/repo-releases</url>
  <snapshots>
    <enabled>false</enabled>
  </snapshots>
</repository>

Then, all you need to do is declare the following dependency, assumig you have a property scala.version pointing to the Scala version used to build your project.

<dependency>
  <groupId>com.weiglewilczek.scalamodules</groupId>
  <artifactId>scalamodules-core_${scala.version}</artifactId>
  <version>2.0.2</version> <!-- Or later -->
</dependency>

Manually

If you want to build your project with Ant, an IDE or any other way which makes it necessary to download the library, you can take it from here: http://scala-tools.org/repo-releases/com/weiglewilczek/scalamodules/

Please replace the last element in the above URL with the latest version of ScalaModules.

Clone this wiki locally