scala modules

58
ScalaModules Scala and OSGi

Upload: skills-matter

Post on 13-Jul-2015

1.023 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Scala modules

ScalaModulesScala and OSGi

Page 2: Scala modules

OSGi Services

• The key to modularity

• Arguably, the key to OOP itself?

Page 3: Scala modules

Dr Alan Kay

Page 4: Scala modules

Dr Alan Kay

“Dude I invented friggin’ OOP. Have you heard of it?”

Page 5: Scala modules

Dr Alan Kay

“OOP to me means only messaging, local retention and protection and hiding of state-process, and extreme late-binding of all things.”

Page 6: Scala modules

Late Binding with Services

• Services answer the question:where do I get an implementation of this interface?

• The registry decentralises the architecture.

Page 7: Scala modules

Consuming Services

Page 8: Scala modules

Harder than Expected!

Page 9: Scala modules

Why...?

Page 10: Scala modules
Page 11: Scala modules

Slippery

Page 12: Scala modules

Dynamics

• A service can come and go

• At any time

• On any thread

• Yes... even while you’re using it

Page 13: Scala modules

Result

•Service usage code can be very complex

Page 14: Scala modules

Service Consumption in Java

Page 15: Scala modules

Consuming a Service

public void consumeService(BundleContext context) { ServiceReference ref = context.getServiceReference(Greeting.class.getName()); Greeting greeting = (Greeting) context.getService(ref); System.out.println(greeting.getMessage()); }

Page 16: Scala modules

Consuming a Service

public void consumeService(BundleContext context) { ServiceReference ref = context.getServiceReference(Greeting.class.getName()); Greeting greeting = (Greeting) context.getService(ref); System.out.println(greeting.getMessage()); }

WRONG

Page 17: Scala modules

Consuming a Service

public void consumeService(BundleContext context) { ServiceReference ref = context.getServiceReference(Greeting.class.getName()); if(ref != null) { Greeting greeting = (Greeting) context.getService(ref); System.out.println(greeting.getMessage()); } }

Page 18: Scala modules

Consuming a Service

public void consumeService(BundleContext context) { ServiceReference ref = context.getServiceReference(Greeting.class.getName()); if(ref != null) { Greeting greeting = (Greeting) context.getService(ref); System.out.println(greeting.getMessage()); } }

WRONG

Page 19: Scala modules

Consuming a Service

public void consumeService(BundleContext context) { ServiceReference ref = context.getServiceReference(Greeting.class.getName()); if(ref != null) { Greeting greeting = (Greeting) context.getService(ref); if(greeting != null) { System.out.println(greeting.getMessage()); } } }

Page 20: Scala modules

Consuming a Service

public void consumeService(BundleContext context) { ServiceReference ref = context.getServiceReference(Greeting.class.getName()); if(ref != null) { Greeting greeting = (Greeting) context.getService(ref); if(greeting != null) { System.out.println(greeting.getMessage()); } } }

WRONG

Page 21: Scala modules

Consuming a Service

public void consumeService(BundleContext context) { ServiceReference ref = context.getServiceReference(Greeting.class.getName()); if(ref != null) { Greeting greeting = (Greeting) context.getService(ref); if(greeting != null) { System.out.println(greeting.getMessage()); } context.ungetService(ref); } }

Page 22: Scala modules

Consuming a Service

public void consumeService(BundleContext context) { ServiceReference ref = context.getServiceReference(Greeting.class.getName()); if(ref != null) { Greeting greeting = (Greeting) context.getService(ref); if(greeting != null) { System.out.println(greeting.getMessage()); } context.ungetService(ref); } }

WRONG

Page 23: Scala modules

Consuming a Service public void consumeService(BundleContext context) { ServiceReference ref = context.getServiceReference(Greeting.class.getName()); if(ref != null) { try { Greeting greeting = (Greeting) context.getService(ref); if(greeting != null) { System.out.println(greeting.getMessage()); } } finally { context.ungetService(ref); } } }

Page 24: Scala modules

Consuming a Service public void consumeService(BundleContext context) { ServiceReference ref = context.getServiceReference(Greeting.class.getName()); if(ref != null) { try { Greeting greeting = (Greeting) context.getService(ref); if(greeting != null) { System.out.println(greeting.getMessage()); } } finally { context.ungetService(ref); } } }

NOT TYPE SAFE

Page 25: Scala modules

Consuming a Service public void consumeService(BundleContext context) { ServiceReference ref = context.getServiceReference(Greeting.class.getName()); if(ref != null) { try { Greeting greeting = (Greeting) context.getService(ref); if(greeting != null) { System.out.println(greeting.getMessage()); } } finally { context.ungetService(ref); } } }

NOT TYPE SAFE

Page 26: Scala modules

An Answer:Stop Writing Code!

Page 27: Scala modules

Declarative Services (DS)

@Reference public void setGreeting(Greeting greeting) { this.greeting = greeting; } public void consumeService() { System.out.println(greeting.getMessage()); }

Page 28: Scala modules

Blueprint public void setGreeting(Greeting greeting) { this.greeting = greeting; } public void consumeService() { System.out.println(greeting.getMessage()); }

<blueprint>

<reference id="greeting" interface="org.example.Greeting"/>

<bean id="foo" class="com.foo.Foo"/> <property name="greeting" ref="greeting"/> </bean>

</blueprint>

Page 29: Scala modules

Problem

• These frameworks provide abstractions

• Abstraction hide detail

• Sometimes we need the detail

Page 30: Scala modules

Example: Cardinality?

Component

Service

Service

Service

Service

Page 31: Scala modules

Single

Component

Service

Service

Service

Service

Page 32: Scala modules

Multiple

Component

Service

Service

Service

Service

Page 33: Scala modules

???

Component

Service

Service

Service

Service

Component

Component

Component

Not supported by DS

Page 34: Scala modules

♥IDeclarative

Services(and Blueprint)

Page 35: Scala modules

♥IDeclarative

Services(and Blueprint)

Page 36: Scala modules

The “80%” Solution

“Normal” Users

Page 37: Scala modules

The “80%” Solution

“Normal” UsersDS support

Page 38: Scala modules

The “80%” Solution

“Normal” UsersDS support

Page 39: Scala modules

The “80%” Solution

“Normal” UsersDS support“Power” Users

Page 40: Scala modules

The “80%” Solution

“Normal” UsersDS support“Power” Users

Page 41: Scala modules

The “80%” Solution

“Normal” UsersDS support“Power” UsersEasy in Java

Page 42: Scala modules

The “80%” Solution

“Normal” UsersDS support“Power” UsersEasy in Java

Page 43: Scala modules

The “80%” Solution

“Normal” UsersDS support“Power” UsersEasy in JavaEasy in Scala

Page 44: Scala modules

The “80%” Solution

“Normal” UsersDS support“Power” UsersEasy in JavaEasy in Scala

Page 45: Scala modules

Java ServiceTracker tracker = new ServiceTracker(context, Greeting.class.getName(), new ServiceTrackerCustomizer() {

public Object addingService(ServiceReference reference) { Greeting greeting = (Greeting) context.getService(reference); ServiceRegistration reg = context.registerService(IFoo.class.getName(), new Foo(greeting), null); return reg; }

public void modifiedService(ServiceReference reference, Object service) { }

public void removedService(ServiceReference reference, Object service) { ServiceRegistration reg = (ServiceRegistration) service; reg.unregister(); } });

Page 46: Scala modules

Java ServiceTracker tracker = new ServiceTracker(context, Greeting.class.getName(), new ServiceTrackerCustomizer() {

public Object addingService(ServiceReference reference) { Greeting greeting = (Greeting) context.getService(reference); ServiceRegistration reg = context.registerService(IFoo.class.getName(), new Foo(greeting), null); return reg; }

public void modifiedService(ServiceReference reference, Object service) { }

public void removedService(ServiceReference reference, Object service) { ServiceRegistration reg = (ServiceRegistration) service; reg.unregister(); } });

Type Safety???

Page 47: Scala modules

What is ScalaModules?

Page 48: Scala modules

NOT Another Module System (phew!)

Page 49: Scala modules
Page 50: Scala modules
Page 51: Scala modules

“A Scala internal DSL to ease OSGi development”

Page 52: Scala modules

• Now:

• Services

• Future:

• Extender Pattern?

• Resources?

Page 53: Scala modules

An Eclipse Project!

Page 54: Scala modules

Examples

Page 55: Scala modules

Java ServiceTracker tracker = new ServiceTracker(context, Greeting.class.getName(), new ServiceTrackerCustomizer() {

public Object addingService(ServiceReference reference) { Greeting greeting = (Greeting) context.getService(reference); ServiceRegistration reg = context.registerService(IFoo.class.getName(), new Foo(greeting), null); return reg; }

public void modifiedService(ServiceReference reference, Object service) { }

public void removedService(ServiceReference reference, Object service) { ServiceRegistration reg = (ServiceRegistration) service; reg.unregister(); } });

Page 56: Scala modules

ScalaModules

val greetingTrack = context track classOf[Greeting] on { case Adding(greeting) => context createService (classOf[IFoo], new Foo(greeting)) case Removed(_, reg) => reg unregister }

Page 57: Scala modules

Plan

• Started moving the code & docs to Eclipse

• 19 March: M1

• 7 May: M2

• 28 May: RC1

• 11 June: RC2

• 23 June: ScalaModules 2.0 Release & Graduation