Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
JAX 09 - OSGi on Scala
1. OSGi on Scala
BindForge & ScalaModules: Scala DSLs to ease OSGi development
Roman Roelofsen (ProSyst), Heiko Seeberger (WeigleWilczek)
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. What?
• BindForge - Module Framework
• ScalaModules - DSL for OSGi development in Scala
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
19. What?
• BindForge - Module Framework
• ScalaModules - DSL for OSGi development in Scala
20. ScalaModules
• For Scala developers (and Java geeks)
• General OSGi concepts, e.g. service handling
• Compendium services, e.g. Configuration Admin
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. Register a Service
context registerAs classOf[Greeting] theService new Greeting {
override def welcome = quot;Hello!quot;
override def goodbye = quot;See you!quot;
}
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. 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. 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. Consume a single Service
context getOne classOf[Greeting] andApply {
_.welcome
} match {
case None => noGreetingService()
case Some(welcome) => println(welcome)
}
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. 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. 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. 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. 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. 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)
}
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?