OSGi on Scala
BindForge & ScalaModules: Scala DSLs to ease OSGi development
    Roman Roelofsen (ProSyst), Heiko Seeberger...
Why?

• OSGi is a great module system
• Scala is a great language
  •   Compiles to Java bytecode

  •   Object-functional...
What?



• BindForge - Module Framework
• ScalaModules - DSL for OSGi development in Scala
BindForge
• Module Framework
  •   Dependency Injection, OSGi service abstraction, etc.

  •   Useful for Java and Scala d...
Example Scenario

CustomerRepository        CustomerService


CustomerRep.Impl        CustomerServiceImpl

              C...
Example Scenario
// repository
interface CustomerRepository
class CustomerRepositoryImpl implements CustomerRepository

//...
Bundle Configuration

• MANIFEST.MF
   BindForge-Config: com.acme.app.MyConfig




• /OSGI-INF/bindforge/config.scala
   pac...
Binding Interfaces to
          Implementations

CustomerRepository        CustomerService


CustomerRep.Impl        Custo...
Binding Interfaces to
      Implementations

package com.acme.app

class MyConfig extends org.bindforge.Config {

    bind...
Inject Bindings

CustomerRepository        CustomerService


CustomerRep.Impl        CustomerServiceImpl

              Cu...
Inject Bindings
            (by type)


bind [CustomerRepository, impl.CustomerRepositoryImpl]

bind [CustomerService, imp...
Inject Bindings
              (by name)


“customerRepository” :: bind [impl.CustomerRepositoryImpl]

bind [CustomerServic...
Export Customer Service

CustomerRepository        CustomerService


CustomerRep.Impl        CustomerServiceImpl

        ...
Export Customer Service


  bind [CustomerRepository, impl.CustomerRepositoryImpl]

  bind [CustomerService, impl.Customer...
Export Customer Service
   (with Properties)

 bind [CustomerRepository, impl.CustomerRepositoryImpl]

 bind [CustomerServ...
Import LogService

CustomerRepository        CustomerService


CustomerRep.Impl        CustomerServiceImpl

              ...
Import LogService

bind [LogService] importService

bind [CustomerRepository, impl.CustomerRepositoryImpl]

bind [Customer...
Full Configuration
package com.acme.app

class MyConfig extends org.bindforge.Config {

    bind [LogService] importService...
What?



• BindForge - Module Framework
• ScalaModules - DSL for OSGi development in Scala
ScalaModules


• For Scala developers (and Java geeks)
• General OSGi concepts, e.g. service handling
• Compendium service...
Register a Service

Greeting hello = new Greeting() {
                                                            afe
    ...
Register a Service


context registerAs classOf[Greeting] theService new Greeting {
  override def welcome = quot;Hello!qu...
Register a Service
             with Properties
Greeting welcome = new Greeting() {
                                      ...
Register a Service
   with Properties


context registerAs classOf[Greeting] withProperties
  Map(quot;namequot; -> quot;w...
Consume a single Service
ServiceReference reference =
    context.getServiceReference(Greeting.class.getName());
if (refer...
Consume a single Service


    context getOne classOf[Greeting] andApply {
      _.welcome
    } match {
      case None  ...
Consume many Services
    with their Properties
try {
    ServiceReference[] refs = context.getServiceReferences(
        ...
Consume many Services
 with their Properties
 context getMany classOf[Greeting] andApply {
   (greeting, properties) => {
...
Consume Services
           using a Filter
try {
    ServiceReference[] refs = context.getServiceReferences(
            G...
Consume Services
           using a Filter

context getMany classOf[Greeting] withFilter quot;(name=*)quot; andApply {
  _...
Track Services
ServiceTracker tracker = new ServiceTracker(context, Greeting.class.getName(), null) {

   @Override
   pub...
Track Services


greetingTrack = context track classOf[Greeting] on {
  case Adding(greeting, _)   => println(quot;Adding ...
Service Dependencies


context registerAs classOf[Command] dependOn classOf[Greeting] theService {
  greeting => new Comma...
And now?

• Forge ahead at www.bindforge.org
• And get fluent at www.scalamodules.org

• Please give feedback:
  •   What d...
Upcoming SlideShare
Loading in …5
×

OSGi DevCon 09 - OSGi on Scala

1,703 views
1,622 views

Published on

BindForge & ScalaModules: Scala DSLs to ease OSGi development.

Published in: Technology, Business
0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,703
On SlideShare
0
From Embeds
0
Number of Embeds
274
Actions
Shares
0
Downloads
79
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide

OSGi DevCon 09 - OSGi on Scala

  1. 1. OSGi on Scala BindForge & ScalaModules: Scala DSLs to ease OSGi development Roman Roelofsen (ProSyst), Heiko Seeberger (WeigleWilczek)
  2. 2. Why? • OSGi is a great module system • Scala is a great language • Compiles to Java bytecode • Object-functional programming style • Why wait for Java 7 (or 8)? Use Scala now! • Let’s combine them to ease OSGi development
  3. 3. What? • BindForge - Module Framework • ScalaModules - DSL for OSGi development in Scala
  4. 4. BindForge • Module Framework • Dependency Injection, OSGi service abstraction, etc. • Useful for Java and Scala development • Configuration is written in Scala • Based on • Scala • Guice • Peaberry
  5. 5. Example Scenario CustomerRepository CustomerService CustomerRep.Impl CustomerServiceImpl Customer Bundle LogService OSGi Log Service Bundle
  6. 6. Example Scenario // repository interface CustomerRepository class CustomerRepositoryImpl implements CustomerRepository // service interface CustomerService class CustomerServiceImpl implements CustomerService { public void setCustomerRepository(CustomerRepository cr) { ... } public void setLogService(LogService ls) { ... } }
  7. 7. Bundle Configuration • MANIFEST.MF BindForge-Config: com.acme.app.MyConfig • /OSGI-INF/bindforge/config.scala package com.acme.app class MyConfig extends org.bindforge.Config { ... }
  8. 8. Binding Interfaces to Implementations CustomerRepository CustomerService CustomerRep.Impl CustomerServiceImpl Customer Bundle
  9. 9. Binding Interfaces to Implementations package com.acme.app class MyConfig extends org.bindforge.Config { bind [CustomerRepository, impl.CustomerRepositoryImpl] bind [CustomerService, impl.CustomerServiceImpl] }
  10. 10. Inject Bindings CustomerRepository CustomerService CustomerRep.Impl CustomerServiceImpl Customer Bundle
  11. 11. Inject Bindings (by type) bind [CustomerRepository, impl.CustomerRepositoryImpl] bind [CustomerService, impl.CustomerServiceImpl] spec { property(“customerRepository”) }
  12. 12. Inject Bindings (by name) “customerRepository” :: bind [impl.CustomerRepositoryImpl] bind [CustomerService, impl.CustomerServiceImpl] spec { property(“customerRepository”) = ref(“customerRepository”) }
  13. 13. Export Customer Service CustomerRepository CustomerService CustomerRep.Impl CustomerServiceImpl Customer Bundle
  14. 14. Export Customer Service bind [CustomerRepository, impl.CustomerRepositoryImpl] bind [CustomerService, impl.CustomerServiceImpl] spec { exportService property(“customerRepository”) }
  15. 15. Export Customer Service (with Properties) bind [CustomerRepository, impl.CustomerRepositoryImpl] bind [CustomerService, impl.CustomerServiceImpl] spec { exportService(“vendor” -> “acme”, “security” -> “none”) property(“customerRepository”) }
  16. 16. Import LogService CustomerRepository CustomerService CustomerRep.Impl CustomerServiceImpl Customer Bundle LogService OSGi Log Service Bundle
  17. 17. Import LogService bind [LogService] importService bind [CustomerRepository, impl.CustomerRepositoryImpl] bind [CustomerService, impl.CustomerServiceImpl] spec { exportService property(“customerRepository”) property(“logService”) }
  18. 18. Full Configuration package com.acme.app class MyConfig extends org.bindforge.Config { bind [LogService] importService bind [CustomerRepository, impl.CustomerRepositoryImpl] bind [CustomerService, impl.CustomerServiceImpl] spec { exportService property(“customerRepository”) property(“logService”) } }
  19. 19. What? • BindForge - Module Framework • ScalaModules - DSL for OSGi development in Scala
  20. 20. ScalaModules • For Scala developers (and Java geeks) • General OSGi concepts, e.g. service handling • Compendium services, e.g. Configuration Admin
  21. 21. Register a Service Greeting hello = new Greeting() { afe es public String welcome() { yp return quot;Hello!quot;; tt } no public String goodbye() { return quot;See you!quot;; } }; context.registerService(Greeting.class.getName(), hello, null);
  22. 22. Register a Service context registerAs classOf[Greeting] theService new Greeting { override def welcome = quot;Hello!quot; override def goodbye = quot;See you!quot;; }
  23. 23. Register a Service with Properties Greeting welcome = new Greeting() { se o public String welcome() { rb ve return quot;Welcome!quot;; } public String goodbye() { return quot;Good bye!quot;; } }; Dictionary<String, Object> properties = new Hashtable<String, Object>(); properties.put(quot;namequot;, quot;welcomequot;); context.registerService(Greeting.class.getName(), welcome, properties);
  24. 24. Register a Service with Properties context registerAs classOf[Greeting] withProperties Map(quot;namequot; -> quot;welcomequot;) theService new Greeting { override def welcome = quot;Welcome!quot; override def goodbye = quot;Goodbye!quot; }
  25. 25. Consume a single Service 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 { noGreetingService(); } } finally { context.ungetService(reference); inv } olv } else { ed noGreetingService(); }
  26. 26. Consume a single Service context getOne classOf[Greeting] andApply { _.welcome } match { case None => noGreetingService() case Some(welcome) => println(welcome) }
  27. 27. Consume many Services with their Properties try { ServiceReference[] refs = context.getServiceReferences( Greeting.class.getName(), null); if (refs != null) { for (ServiceReference ref : refs) { Object service = context.getService(ref); Greeting greeting = (Greeting) service; if (greeting != null) { Object name = (ref.getProperty(quot;namequot;) != null) ? ref.getProperty(quot;namequot;) : quot;UNKNOWNquot;; String message = name + quot; says: quot; + greeting.welcome(); System.out.println(message); } } ver } else { y inv noGreetingService(); olv } ed } catch (InvalidSyntaxException e) { // Do something meaningful ... }
  28. 28. Consume many Services with their Properties context getMany classOf[Greeting] andApply { (greeting, properties) => { val name = properties.get(quot;namequot;) match { case None => quot;UNKNOWNquot; case Some(s) => s } name + quot; sais: quot; + greeting.welcome } } match { case None => noGreetingService() case Some(welcomes) => welcomes.foreach { println } }
  29. 29. Consume Services using a Filter try { ServiceReference[] refs = context.getServiceReferences( Greeting.class.getName(), quot;(name=*)quot;); if (refs != null) { for (ServiceReference ref : refs) { Object service = context.getService(ref); Greeting greeting = (Greeting) service; if (greeting != null) { System.out.println(greeting.welcome()); ver } y } inv } else { olv ed noGreetingService(); aga } in } catch (InvalidSyntaxException e) { // Do something meaningful ... }
  30. 30. Consume Services using a Filter context getMany classOf[Greeting] withFilter quot;(name=*)quot; andApply { _.welcome } match { case None => noGreetingService() case Some(welcomes) => welcomes.foreach { println } }
  31. 31. Track Services ServiceTracker tracker = new ServiceTracker(context, Greeting.class.getName(), null) { @Override public Object addingService(ServiceReference reference) { Object service = super.addingService(reference); Greeting greeting = (Greeting)service; System.out.println(quot;Adding Greeting: quot; + greeting.welcome()); return service; } @Override public void removedService(ServiceReference reference, Object service) { Greeting greeting = (Greeting)service; System.out.println(quot;Removed Greeting: quot; + greeting.welcome()); and super.removedService(reference, service); aga } in }; tracker.open();
  32. 32. Track Services greetingTrack = context track classOf[Greeting] on { case Adding(greeting, _) => println(quot;Adding Greeting: quot; + greeting.welcome) case Modified(greeting, _) => case Removed(greeting, _) => println(quot;Removed Greeting: quot; + greeting.goodbye) }
  33. 33. Service Dependencies context registerAs classOf[Command] dependOn classOf[Greeting] theService { greeting => new Command { ... } }
  34. 34. And now? • Forge ahead at www.bindforge.org • And get fluent at www.scalamodules.org • Please give feedback: • What do you like? • What do you not like? • What feature would you like to see implemented?

×