SCALAMODULES
OSGi ganz einfach mit einer Scala DSL
          Heiko Seeberger




             Copyright WeigleWilczek 2009
HOUSTON, WIR HABEN EIN PROBLEM!
      ServiceReference ref =
          ctx.getServiceReference(Greeting.class.getName());
      if (ref != null) {
          try {
               Object service = ctx.getService(ref);
               Greeting greeting = (Greeting) service;         Low-level API
               if (greeting != null) {
Häßliche           System.out.println(greeting.welcome());
               } else {
 Details       }
                   System.out.println("No Greeting service available!");

          } finally {
               ctx.ungetService(ref);
          }
      } else {
          System.out.println("No Greeting service available!");
      }




                                      2
WIE KÖNNTE SCALA HELFEN?




 Ausdrucksstarke high-level DSL
               3
SO ETWAS IN DER ART ...




OSGi giveMe Greeting and call welcome




                  4
WIE BAUE ICH EINE DSL?

    Implicit                            Higher-order
Conversions                             Functions




Fluent API

                         5
SCALAMODULES




Scala DSL to ease OSGi development


                6
SERVICES REGISTRIEREN


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




          ... unter allen Interfaces



                             7
SERVICES REGISTRIEREN II


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




... unter einem bestimmten Interface



                        8
SERVICES REGISTRIEREN III


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




              ... mit Properties



                          9
LIVE DEMO




    10
SERVICES KONSUMIEREN I

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




             ... nur einen einzelnen


                            11
SERVICES KONSUMIEREN II


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




... nur einen einzelnen mit Properties


                       12
SERVICES KONSUMIEREN III


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




                         ... alle


                             13
SERVICES KONSUMIEREN IV

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




                       ... alle mit Filter


                                 14
LIVE DEMO




    15
SERVICES TRACKEN


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


                                 16
SERVICE-ABHÄNGIGKEITEN


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




    ... wie DS Mandatory Dependencies


                             17
FRAGEN / DISKUSSION




         18
DANKE

Kontakt: seeberger@weiglewilczek.com




                 19

W-JAX 09 - ScalaModules

  • 1.
    SCALAMODULES OSGi ganz einfachmit einer Scala DSL Heiko Seeberger Copyright WeigleWilczek 2009
  • 2.
    HOUSTON, WIR HABENEIN PROBLEM! ServiceReference ref = ctx.getServiceReference(Greeting.class.getName()); if (ref != null) { try { Object service = ctx.getService(ref); Greeting greeting = (Greeting) service; Low-level API if (greeting != null) { Häßliche System.out.println(greeting.welcome()); } else { Details } System.out.println("No Greeting service available!"); } finally { ctx.ungetService(ref); } } else { System.out.println("No Greeting service available!"); } 2
  • 3.
    WIE KÖNNTE SCALAHELFEN? Ausdrucksstarke high-level DSL 3
  • 4.
    SO ETWAS INDER ART ... OSGi giveMe Greeting and call welcome 4
  • 5.
    WIE BAUE ICHEINE DSL? Implicit Higher-order Conversions Functions Fluent API 5
  • 6.
    SCALAMODULES Scala DSL toease OSGi development 6
  • 7.
    SERVICES REGISTRIEREN val hello = new Greeting { ... } automatisch ctx register hello ... unter allen Interfaces 7
  • 8.
    SERVICES REGISTRIEREN II val hello = new Greeting { ... } ctx register (hello as classOf[Greeting]) typsicher ... unter einem bestimmten Interface 8
  • 9.
    SERVICES REGISTRIEREN III valhello = new Greeting { ... } ctx register (hello withProps ("Scala" -> "Modules")) ... mit Properties 9
  • 10.
  • 11.
    SERVICES KONSUMIEREN I Funktional ctx getOne classOf[Greeting] andApply { _.welcome } match { case None => // Handle service not available ... case Some(welcome) => println(welcome) } ... nur einen einzelnen 11
  • 12.
    SERVICES KONSUMIEREN II ctx getOne classOf[Greeting] andApply { (greeting, properties) => ... } ... nur einen einzelnen mit Properties 12
  • 13.
    SERVICES KONSUMIEREN III ctxgetMany classOf[Greeting] andApply { _.welcome } match { case Nil => // Handle service not available ... case welcomes => welcomes foreach { println } } ... alle 13
  • 14.
    SERVICES KONSUMIEREN IV ctxgetMany classOf[Greeting] withFilter isTrue("polite") andApply { _.welcome } match { case Nil => // Handle service not available ... case welcomes => welcomes foreach { println } } ... alle mit Filter 14
  • 15.
  • 16.
    SERVICES TRACKEN ctx trackclassOf[Greeting] on { case Adding(grt, _) => println("Adding polite Greeting: " + grt.welcome) case Removed(grt, _) => println("Removed polite Greeting: " + grt.goodbye) } ... mit oder ohne Properties 16
  • 17.
    SERVICE-ABHÄNGIGKEITEN ctx register {(grt: Greeting) => new GreetingReverser(grt) } ... wie DS Mandatory Dependencies 17
  • 18.
  • 19.