3. OSGi
• Modulsystem für die JVM
– JSR 291: Dynamic Component
Support for Java SE
– Sichtbarkeitsregeln
– Abhängigkeitsverwaltung
– Versionierung
• Bundles
– JAR + OSGi-Manifest
4. OSGi
• Dynamisches Komponentenmodell
– Zur Laufzeit: install, start, stop,
update, uninstall
• Serviceorientiertes Modulsystem
– Dienste in JVM zur Laufzeit
registrieren und konsumieren
– Service-Registry für Reaktion auf
Hinzufügen oder Entfernen
5. OSGi
• Eine Klasse im Bundle muss
Interface BundleActivator
implementieren
• interface BundleActivator {
void start(BundleContext c);
void stop(BundleContext c);
}
• Über BundleContext Services
registrieren, ...
6. bnd
• „the swiss army knife of OSGi“
• Zum Erzeugen von Bundles
• Generiert Manifest
=> Vereinfacht OSGi-Entwicklung
7. Pax Runner
• Startet OSGi-Plattform mit
angegebenen Bundles
scan-bundle:mvn:http://scala-
tools.org/repo-releases!
com.weiglewilczek.scala-lang-osgi/scala-
library/2.8.0
…
$ pax-run.sh
--bootDelegation="sun.*,com.sun.*"
scan-composite:file:songfinder.composite
• Verschiedene Profile
8. Sbt
• Simple Build Tool
• Konfigurieren und Erweitern in Scala
• Mit interaktiver Shell, z.B.
$ sbt
> ~ compile
• Auch für reine Java-Projekte nützlich
9. ScalaModules
• Scala-DSL für OSGi-Entwicklung
• Spart eine Menge Boilerplate
// Java
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!");
}
// Scala
context findService withInterface[Greeting] andApply { _.welcome } match {
case None => println("No Greeting service available!")
case Some(welcome) => println(welcome)
}
10. bnd4sbt
• Plugin für sbt zur Nutzung von bnd
• Konfiguration in Scala/sbt
• Kein zusätzliches bnd-File nötig
• Erstellen eines OSGi-Bundles mit
$ sbt bnd-bundle
12. Sbt-Projekt
• Projektverzeichnis:
$ mkdir radio
$ cd radio
• sbt:
$ sbt
Project does not exist, create new
project? (y/N/s) y
Name: radio
Organization: org.unsane.radio
Version [1.0]: 0.1.0
...