W-JAX 09 - ScalaModules

Post on 30-Jun-2015

1.367 views 3 download

description

ScalaModules - OSGi ganz einfach mit einer Scala DSL

Transcript of W-JAX 09 - ScalaModules

Copyright WeigleWilczek 2009

SCALAMODULESOSGi ganz einfach mit einer Scala DSL

Heiko Seeberger

HOUSTON, WIR HABEN EIN PROBLEM!

2

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

Low-level APIHäßliche

Details

WIE KÖNNTE SCALA HELFEN?

3

Ausdrucksstarke high-level DSL

SO ETWAS IN DER ART ...

4

OSGi giveMe Greeting and call welcome

WIE BAUE ICH EINE DSL?

5

ImplicitConversions

Fluent API

Higher-orderFunctions

SCALAMODULES

6

Scala DSL to ease OSGi development

SERVICES REGISTRIEREN

7

val hello = new Greeting { ... }ctx register hello

... unter allen Interfaces

automatisch

SERVICES REGISTRIEREN II

8

val hello = new Greeting { ... }ctx register (hello as classOf[Greeting])

... unter einem bestimmten Interface

typsicher

SERVICES REGISTRIEREN III

9

val hello = new Greeting { ... }ctx register (hello withProps ("Scala" -> "Modules"))

... mit Properties

LIVE DEMO

10

SERVICES KONSUMIEREN I

11

ctx getOne classOf[Greeting] andApply { _.welcome } match { case None => // Handle service not available ... case Some(welcome) => println(welcome)}

... nur einen einzelnen

Funktional

SERVICES KONSUMIEREN II

12

ctx getOne classOf[Greeting] andApply { (greeting, properties) => ...}

... nur einen einzelnen mit Properties

SERVICES KONSUMIEREN III

13

ctx getMany classOf[Greeting] andApply { _.welcome } match { case Nil => // Handle service not available ... case welcomes => welcomes foreach { println }}

... alle

SERVICES KONSUMIEREN IV

14

ctx getMany classOf[Greeting] withFilter isTrue("polite") andApply { _.welcome} match { case Nil => // Handle service not available ... case welcomes => welcomes foreach { println }}

... alle mit Filter

LIVE DEMO

15

SERVICES TRACKEN

16

ctx track classOf[Greeting] on { case Adding(grt, _) => println("Adding polite Greeting: " + grt.welcome) case Removed(grt, _) => println("Removed polite Greeting: " + grt.goodbye)}

... mit oder ohne Properties

SERVICE-ABHÄNGIGKEITEN

17

ctx register { (grt: Greeting) => new GreetingReverser(grt) }

... wie DS Mandatory Dependencies

FRAGEN / DISKUSSION

18