Gert Vanthienen's blog

Tuesday, January 26, 2010

Scala on Karaf

Over the holiday season, I had a bit of spare time to work the Camel Scala DSL again. While doing this, I realized that it was a bit too hard to actually use the Scala DSL on Karaf (need to configure Maven to use both the Scala plugin and the Felix bundle plugin to build the deployable jar).

Wouldn't it be nice to just drop a Scala source file in Karaf's deploy folder and get everything done automatically? With this idea in mind, I started a new project called Slang on the FUSE Forge to add support for additional languages to Karaf.

You can now try out a first shot at the Scala language support - you still have to build it yourself at the moment, but once you installed the feature on Karaf, you can just drop this source file in the deploy folder:


import org.fusesource.slang.scala.common.ScalaActivator

package org.fusesource.slang.scala.examples

class Activator extends ScalaActivator {

  publish(new HelloWorldServiceImpl())
   .as[HelloWorldService]
}

class HelloWorldServiceImpl extends HelloWorldService {
  def hello = println("Saying hello")
}

trait HelloWorldService {
  def hello
}


The result will be a bundle on Karaf that exports the HelloWorldService interface and has published the HelloWorldServiceImpl in the OSGi Service Registry. Next on my todo list is to actually get it to run the Camel route built in the Scala DSL this way.

Labels: ,

Friday, April 25, 2008

Unwrapping the wrapper

When we started development on the Scala DSL for Camel, we were using implicit conversion to convert a Java DSL type into a Scala DSL type like this:

implicit def enrichChoice(choice: ChoiceType) = new RichChoiceType(choice)

However, when we try to create a nice-looking DSL this way, we run into problems with the explicits-first rule for implicit conversion in Scala sometimes: methods on the original Java DSL type would take precedence over the 'new' Scala DSL methods.

We actually wanted the behavior to be the other way around: prefer the Scala DSL methods and only fail back to the Java DSL when necessary. Just to whet your appetite for Scala, all it takes to implement this is a simple trait...

trait Wrapper[T] {
val unwrap : T
}
... implemented on our Scala DSL types ...

class RichChoiceType(val unwrap: ChoiceType) with Wrapper[ChoiceType]

... and one equally simple implicit conversion method.

implicit def unwrap[T](wrapper: Wrapper[T]) = wrapper.unwrap

Labels: , ,

Saturday, April 12, 2008

Scala DSL for Camel

I attended ApacheCon Europe this week in Amsterdam. Finally got a chance to meet Guillaume Nodet after chatting with him for hours on IRC and Bruce Snyder was there as well, so I guess the the ServiceMix team was duly represented.

Personally, I was particularly interested in the talks on JCR, Jackrabbit and Sling. I was planning to build a message archiving and monitoring solution for ServiceMix and the talks have convinced me to go for it.

Also took this opportunity to give a 15 minute Fast Feather talk on the new Scala DSL in Camel. The slides are available here.

Labels: , , ,