Contenu connexe
Similaire à OSGi DevCon Europe 09 - OSGi on Scala (20)
Plus de Heiko Seeberger (20)
OSGi DevCon Europe 09 - OSGi on Scala
- 1. OSGi on Scala
Ease OSGi development with a Scala DSL
Heiko Seeberger
© 2009 WeigleWilczek. Released under the Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 Germany License. 2009-06-22
Sonntag, 21. Juni 2009
- 2. 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
© 2009 WeigleWilczek. Released under the Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 Germany License. 2
Sonntag, 21. Juni 2009
- 3. OSGi on Scala
ScalaModules
• Scala DSL for OSGi
• Ease service handling
• Smooth ugly parts of the API, e.g. null references
© 2009 WeigleWilczek. Released under the Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 Germany License. 3
Sonntag, 21. Juni 2009
- 4. OSGi on Scala
Live Demo
Should I really dare?
YES!
© 2009 WeigleWilczek. Released under the Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 Germany License. 4
Sonntag, 21. Juni 2009
- 5. OSGi on Scala
Start Scala REPL with appropriate Classpath
tmp$ scala -cp felix.jar:scalamodules-core-...jar:scalamodules-util-...jar
Welcome 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.
© 2009 WeigleWilczek. Released under the Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 Germany License. 5
Sonntag, 21. Juni 2009
- 6. OSGi on Scala
Import Felix and ScalaModules
scala> import org.apache.felix.framework._
import org.apache.felix.framework._
scala> import org.scalamodules.core.RichBundleContext._
import org.scalamodules.core.RichBundleContext._
© 2009 WeigleWilczek. Released under the Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 Germany License. 6
Sonntag, 21. Juni 2009
- 7. OSGi on Scala
Start Felix and get BundleContext
scala> val felix = new Felix(null)
felix: org.apache.felix.framework.Felix = org.apache.felix.framework [0]
scala> felix.start
scala> val ctx = felix.getBundleContext
ctx: org.osgi.framework.BundleContext = org...BundleContextImpl@d9367a
© 2009 WeigleWilczek. Released under the Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 Germany License. 7
Sonntag, 21. Juni 2009
- 8. OSGi on Scala
Define a Service Interface and Object
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
© 2009 WeigleWilczek. Released under the Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 Germany License. 8
Sonntag, 21. Juni 2009
- 9. OSGi on Scala
Try to consume a Service
scala> ctx getOne classOf[Greeting] andApply { _.hello } match {
| case Some(s) => println(s)
| case None => println("No Greeting service available!")
| }
No Greeting service available!
© 2009 WeigleWilczek. Released under the Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 Germany License. 9
Sonntag, 21. Juni 2009
- 10. OSGi on Scala
Try to provide a Service with illegal Interface
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
^
© 2009 WeigleWilczek. Released under the Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 Germany License. 10
Sonntag, 21. Juni 2009
- 11. OSGi on Scala
Provide a Service correctly
scala> ctx registerAs classOf[Greeting] theService greeting
res3: org.osgi.framework.ServiceRegistration = org...ServiceRegistrationImpl@ed63a3
© 2009 WeigleWilczek. Released under the Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 Germany License. 11
Sonntag, 21. Juni 2009
- 12. OSGi on Scala
Try to consume a Service once more
scala> ctx getOne classOf[Greeting] andApply { _.hello } match {
| case Some(s) => println(s)
| case None => println("No Greeting service available!")
| }
Hello!
© 2009 WeigleWilczek. Released under the Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 Germany License. 12
Sonntag, 21. Juni 2009
- 13. OSGi on Scala
What else can ScalaModules do?
© 2009 WeigleWilczek. Released under the Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 Germany License. 13
Sonntag, 21. Juni 2009
- 14. OSGi on Scala
Provide a Service with Properties
context registerAs classOf[Greeting] withProperties
("name" -> "welcome") theService greeting
© 2009 WeigleWilczek. Released under the Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 Germany License. 14
Sonntag, 21. Juni 2009
- 15. OSGi on Scala
Consume multiple Service applying a Filter
context getMany classOf[Greeting] withFilter "(name=*)" andApply {
_.welcome
} match {
case None => noGreetingService()
case Some(welcomes) => welcomes.foreach { println }
}
© 2009 WeigleWilczek. Released under the Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 Germany License. 15
Sonntag, 21. Juni 2009
- 16. OSGi on Scala
Track Services
context track classOf[Greeting] on {
case Adding(greeting, _) => println("Adding Greeting: " + greeting.welcome)
case Removed(greeting, _) => println("Removed Greeting: " + greeting.goodbye)
}
© 2009 WeigleWilczek. Released under the Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 Germany License. 16
Sonntag, 21. Juni 2009
- 17. OSGi on Scala
Service Dependencies
context registerAs classOf[Command] dependOn classOf[Greeting] theService {
greeting => new Command {
...
}
}
© 2009 WeigleWilczek. Released under the Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 Germany License. 17
Sonntag, 21. Juni 2009
- 18. OSGi on Scala
And much more ...
© 2009 WeigleWilczek. Released under the Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 Germany License. 18
Sonntag, 21. Juni 2009
- 19. OSGi on Scala
How to get started?
• www.scalamodules.org
• Wiki / Getting Started
• Reference Guide
• Contact: seeberger@weiglewilczek.com
© 2009 WeigleWilczek. Released under the Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 Germany License. 19
Sonntag, 21. Juni 2009