osgi devcon europe 09 - osgi on scala
DESCRIPTION
Short talk at OSGi Dev Con Europe 09: Ease OSGi development with a Scala DSL.TRANSCRIPT
© 2009 WeigleWilczek. Released under the Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 Germany License. 2009-06-22
OSGi on ScalaEase OSGi development with a Scala DSL
Heiko Seeberger
Sonntag, 21. Juni 2009
© 2009 WeigleWilczek. Released under the Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 Germany License.
OSGi on Scala
Why?
• Scala is a great language
• Runs on JVM & fully interoperable with Java
• Object-functional programming style => Best of OO and FP
• Scalable and flexible language => Domain Specific Languages
• Let’s put OSGi on Scala to ease OSGi development
2Sonntag, 21. Juni 2009
© 2009 WeigleWilczek. Released under the Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 Germany License.
OSGi on Scala
ScalaModules
• Scala DSL for OSGi
• Ease service handling
• Smooth ugly parts of the API, e.g. null references
3Sonntag, 21. Juni 2009
© 2009 WeigleWilczek. Released under the Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 Germany License.
OSGi on Scala
Live Demo
4
Should I really dare?
YES!
Sonntag, 21. Juni 2009
© 2009 WeigleWilczek. Released under the Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 Germany License.
OSGi on Scala
Start Scala REPL with appropriate Classpath
5
tmp$ scala -cp felix.jar:scalamodules-core-...jar:scalamodules-util-...jarWelcome to Scala version 2.7.5.final (Java HotSpot(TM) Client VM, Java 1.5.0_19).Type in expressions to have them evaluated.Type :help for more information.
Sonntag, 21. Juni 2009
© 2009 WeigleWilczek. Released under the Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 Germany License.
OSGi on Scala
Import Felix and ScalaModules
6
scala> import org.apache.felix.framework._import org.apache.felix.framework._
scala> import org.scalamodules.core.RichBundleContext._import org.scalamodules.core.RichBundleContext._
Sonntag, 21. Juni 2009
© 2009 WeigleWilczek. Released under the Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 Germany License.
OSGi on Scala
Start Felix and get BundleContext
7
scala> val felix = new Felix(null)felix: org.apache.felix.framework.Felix = org.apache.felix.framework [0]
scala> felix.start
scala> val ctx = felix.getBundleContextctx: org.osgi.framework.BundleContext = org...BundleContextImpl@d9367a
Sonntag, 21. Juni 2009
© 2009 WeigleWilczek. Released under the Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 Germany License.
OSGi on Scala
Define a Service Interface and Object
8
scala> trait Greeting { def hello: String }defined trait Greeting
scala> val greeting = new Greeting { def hello = "Hello!" }greeting: java.lang.Object with Greeting = $anon$1@8ed249
Sonntag, 21. Juni 2009
© 2009 WeigleWilczek. Released under the Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 Germany License.
OSGi on Scala
Try to consume a Service
9
scala> ctx getOne classOf[Greeting] andApply { _.hello } match { | case Some(s) => println(s) | case None => println("No Greeting service available!") | }No Greeting service available!
Sonntag, 21. Juni 2009
© 2009 WeigleWilczek. Released under the Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 Germany License.
OSGi on Scala
Try to provide a Service with illegal Interface
10
scala> ctx registers classOf[String] theService greeting<console>:13: error: value registers is not a member of org.osgi.framework.BundleContext ctx registers classOf[String] theService greeting ^
Sonntag, 21. Juni 2009
© 2009 WeigleWilczek. Released under the Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 Germany License.
OSGi on Scala
Provide a Service correctly
11
scala> ctx registerAs classOf[Greeting] theService greetingres3: org.osgi.framework.ServiceRegistration = org...ServiceRegistrationImpl@ed63a3
Sonntag, 21. Juni 2009
© 2009 WeigleWilczek. Released under the Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 Germany License.
OSGi on Scala
Try to consume a Service once more
12
scala> ctx getOne classOf[Greeting] andApply { _.hello } match { | case Some(s) => println(s) | case None => println("No Greeting service available!") | }Hello!
Sonntag, 21. Juni 2009
© 2009 WeigleWilczek. Released under the Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 Germany License.
OSGi on Scala
What else can ScalaModules do?
13Sonntag, 21. Juni 2009
© 2009 WeigleWilczek. Released under the Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 Germany License.
OSGi on Scala
Provide a Service with Properties
14
context registerAs classOf[Greeting] withProperties ("name" -> "welcome") theService greeting
Sonntag, 21. Juni 2009
© 2009 WeigleWilczek. Released under the Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 Germany License.
OSGi on Scala
Consume multiple Service applying a Filter
15
context getMany classOf[Greeting] withFilter "(name=*)" andApply { _.welcome} match { case None => noGreetingService() case Some(welcomes) => welcomes.foreach { println }}
Sonntag, 21. Juni 2009
© 2009 WeigleWilczek. Released under the Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 Germany License.
OSGi on Scala
Track Services
16
context track classOf[Greeting] on { case Adding(greeting, _) => println("Adding Greeting: " + greeting.welcome) case Removed(greeting, _) => println("Removed Greeting: " + greeting.goodbye)}
Sonntag, 21. Juni 2009
© 2009 WeigleWilczek. Released under the Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 Germany License.
OSGi on Scala
Service Dependencies
17
context registerAs classOf[Command] dependOn classOf[Greeting] theService { greeting => new Command { ... }}
Sonntag, 21. Juni 2009
© 2009 WeigleWilczek. Released under the Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 Germany License.
OSGi on Scala
And much more ...
18Sonntag, 21. Juni 2009
© 2009 WeigleWilczek. Released under the Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 Germany License.
OSGi on Scala
How to get started?
• www.scalamodules.org
• Wiki / Getting Started
• Reference Guide
• Contact: [email protected]
19Sonntag, 21. Juni 2009