Scala - OSGi Bundles from Outer (Java) Space

Embed Size (px)

DESCRIPTION

Talk auf dem Scala-Day der W-JAX 2010 http://jax.de/wjax2010/sessions/?tid=1732#session-15038

Citation preview

  • 1. Prof. Dr. Oliver Braun | FH Schmalkalden Christoph Schmidt | FH Schmalkalden Scala OSGi-Bundles from Outer (Java) Space

2. Inhalt Framework / Tools OSGi Bnd, Pax Runner Sbt, ScalaModules, bnd4sbt Beispiel: Songfinder Code bzw. Vorgehen beim Coden Live-Demo Code unter: http://github.com/c-schmidt/songfinder_example_wjax 3. OSGi Modulsystem fr die JVM JSR 291: Dynamic Component Support for Java SE Sichtbarkeitsregeln Abhngigkeitsverwaltung 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 fr Reaktion auf Hinzufgen 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 fr reine Java-Projekte ntzlich 9. ScalaModules Scala-DSL fr 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 fr sbt zur Nutzung von bnd Konfiguration in Scala/sbt Kein zustzliches bnd-File ntig Erstellen eines OSGi-Bundles mit $ sbt bnd-bundle 11. Der Songfinder 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 ... 13. Sbt-Projekt Konfigurieren des Projekts: radio/ `-- project |-- build | `-- Project.scala |-- build.properties `-- plugins `-- Plugins.scala 14. Plugins.scala class Plugins(info: ProjectInfo) extends PluginDefinition(info) { lazy val aquteRepo = "aQute Maven Repository" at "http://www.aqute.biz/repo" lazy val aquteModuleConfig = ModuleConfiguration("biz.aQute", aquteRepo) lazy val bnd4sbt = "com.weiglewilczek.bnd4sbt" % "bnd4sbt" % "1.0.0" } 15. Project.scala class RadioParentProject(info: ProjectInfo) extends ParentProject(info) { object Dependencies { val osgiVersion = "4.2.0" val scalaModulesCore = "com.weiglewilczek.scalamodules" %% "scalamodules-core" % "2.0.0" val osgiCore = "org.osgi" % "org.osgi.core" % osgiVersion % "provided" } ... } 16. Das API-Bundle als Sub-Projekt: songfinder-api/ `-- src `-- main `-- scala `-- Songfinder.scala 17. Erstellen des API-Bundles Der Songfinder-Trait: trait Songfinder { def find def remove(songTitle: String) def stopMsg: String } Bundle in songfinder.composite aufnehmen 18. Konfiguration: Project.scala ... val songfinderAPIProject = project("songfinder-api", "radio-songfinder-api", new SongfinderAPIProject(_)) class SongfinderAPIProject(info: ProjectInfo) extends DefaultProject(info) with BNDPlugin { override def bndExportPackage = "org.unsane.radio.songfinder;version= %s".format(projectVersion.value) :: Nil }... 19. Erstellen eines Services Ein neues Sub-Projekt: songfinder-create/ `-- src `-- main `-- scala `-- Activator.scala Sub-Projekt in Project.scala konfigurieren Service-Bundle in songfinder.composite aufnehmen 20. Erstellen eines Services //songfinder-create/../Activator.scala class Activator extends BundleActivator {... override def start(context: BundleContext) { val songfinder = new Songfinder { override def find = { ... }... override def stopMsg = "shutdown now!" } context createService songfinder } override def stop(context: BundleContext) {} } 21. Der Watcher Ein weiteres Sub-Projekt: songfinder-watch Project.scala konfigurieren Client-Bundle in die Datei songfinder.composite aufnehmen 22. Der Watcher //songfinder-watch/../Activator.scala class Activator extends BundleActivator { case class Stop() class WatchActor(context: BundleContext) extends Actor {} private[this] var watcher: WatchActor = _ override def start(context: BundleContext) { watcher = new WatchActor(context) watcher.start() } override def stop(context: BundleContext) { watcher ! Stop } } 23. Der Watcherclass WatchActor(context: BundleContext) extends Actor { def act() { println("[radio/songfinder-watch] watching") context watchServices withInterface[Songfinder] andHandle { case AddingService(songfinder, _) => songfinder.find case ServiceRemoved(songfinder, _) => println(songfinder stopMsg) } receiveWithin(3000) { case Stop => case _ => act() } } 24. Demo Im Projektverzeichnis radio OSGi-Plattform mit Pax Runner starten: $ pax-run.sh --bootDelegation="sun.*,com.sun.*" scan-composite:file:songfinder.composite 25. Quellen http://github.com/c- schmidt/songfinder_example_wjax http://github.com/weiglewilczek/scalamodules http://github.com/weiglewilczek/bnd4sbt http://code.google.com/p/simple-build-tool/ http://www.osgi-buch.com/ http://paxrunner.ops4j.org/space/Pax+Runner 26. Und zum Schluss ein kleines bisschen Werbung: 27. [email protected] http://github.com/c-schmidt [email protected] http://pads.fh-schmalkalden.de/ http://twitter.com/obcode http://github.com/obcode http://slideshare.net/obcode Vielen Dank fr Ihre Aufmerksamkeit