JavaOneDemo in Scala

For some light entertainment I converted the JavaOneDemo over to a Scala-based version, see here. It is by no means complete, there is still some Java code (which shows how one can mix the two in the same project) and i know it can be improved by using Scala's native XML support.

I am getting increasingly grumpy with the verbosity of Java and the reduction that Scala offers is good (i wish i could write Jersey completely in Scala, i would be more productive and i think the code would be better, but the downside is it would limit adoption for those wishing to mess around with the code base).

Here is a bit of the Java code:

@ProduceMime("application/xhtml+xml")
public class SystemProperty {
    Properties properties;
    String name;    
    String value;
    SystemProperty(Properties properties, String name) {
        this.properties = properties;
        this.name = name;
        this.value = properties.getProperty(name);
        if (value == null)
            throw new WebApplicationException(Response.Status.NOT_FOUND);
    } 
    @GET
    public Property getProperty() {
        return new Property(name, value);
    }    
    @PUT
    @ConsumeMime("text/plain")
    public void updateProperty(String newValue) {
        properties.setProperty(name, newValue);
    }    
    @DELETE
    public void deleteProperty() {
        properties.remove(name);
    }
}

Now compare that with the equivalent Scala code:

@ProduceMime(Array("application/xhtml+xml"))
class SystemProperty(properties : Properties, name : String) {
    val value = properties.getProperty(name)
    if (value == null)
        throw new WebApplicationException(Response.Status.NOT_FOUND)
    @GET
    def getProperty = new Property(name, value)
    @PUT
    @ConsumeMime(Array("text/plain"))
    def updateProperty(newValue : String) : unit = 
        properties.setProperty(name, newValue)    
    @DELETE
    def deleteProperty() : unit = properties.remove(name)
}

This class uses Scala concise constructors and the Scala compiler is smart enough to know whether the constructor parameters need to be fields on the class. Unfortunately Scala does not perform auto-boxing of arrays, which is why you see Array("application/xhtml+xml") in the @ProduceMime annotation (this gets more annoying when using the JAX-RS builder-related methods). Notice how you can write the equivalent constructor logic directly in the class.

For first @GET method Scala does not require that you declare the return type, it works it out. So this can be written concisely in one line.

I would like to write some "Scala-sugar" for classes like UriInfo and Response to improve EOU in Scala in addition to further investigating use of Scala features like closures as experimented with previously.

Comments:

Hi Paul,

Long time no speak! I've recently been looking at Scala since I've read Bruce Eckel's blog entry about Java's possible evolutionary dead end (http://www.artima.com/weblogs/viewpost.jsp?thread=221903), where he suggested Scala as an alternative.

I also started getting annoyed with how verbose some of the new language features were making the already verbose Java language. But no one can tear me away strongly types languages with their scripting languages.

Can you use Jersey with Scala (apologies if this is an obvious question, I'm only starting to look at Scala)

Regards,
John.

Posted by John Farrelly on June 11, 2008 at 05:25 AM CEST #

John!

What's the craic?

Yes you can use Scala with Jersey as the zipped example shows (which, if using NetBeans 6.x should compile and run for you).

Note that i have done nothing special to get Jersey to work with Scala. There is really good interoperability at the byte code level.

The NetBeans Scala plugin is progressing nicely:

http://wiki.netbeans.org/Scala
http://blogtrader.org/page/dcaoyuan/

Paul.

Posted by Paul Sandoz on June 11, 2008 at 05:41 AM CEST #

"For first @GET method Scala does not require that you declare the return type, it works it out. So this can be written concisely in one line."

Doesn't make sense, because

Property getProperty = new Property(name, value)

is also only one line, without Scala figuring out what the return value is.

I also write

public Property getProperty() {
return new Property(name, value);
}

in one line recently, since I've found the config option in IDEA to keep small blocks in one line instead of formatting them into 3 lines. One of the best IDEA options!

Otherwise Scala with Jersey looks interesting,

Peace
-stephan

Posted by guest on June 28, 2008 at 04:00 AM CEST #

Hi Stephan,

Point taken about that example, i never realized that such a config option exists in some IDEs. Although... when one compares:

@GET
public Property getProperty() { return new Property(name, value); }

with

@GET
def getProperty = new Property(name, value)

i know which one i prefer :-)

BTW if interested there is a Scala example distributed with Jersey.

Posted by Paul Sandoz on June 30, 2008 at 02:21 AM CEST #

Thanks,

@GET
def property = new Property(name, value)

would be even nicer ;-)

For now I'm staying with Jersey and Java, perhaps Jersey might be even shorter with Groovy, I'll have to try.

Peace
-stephan

Posted by Stephan Schmidt on July 01, 2008 at 12:47 AM CEST #

Yes, i was planning to write a Groovy version of the JavaOne demo as well. I suspect there will be similar levels of conciseness (from previous investigations with Groovy). The main advantage i see with Groovy over Scala is no compile step is required (although one can use Scala in "scripting" mode, without a compile step, i have not tried it).

But... what we really need are sweet Scala and Groovy wrappers for Jersey to take advantage of closers etc.

Posted by Paul Sandoz on July 01, 2008 at 01:25 AM CEST #

Scala with the free JavaRebel for Lift works class-reload too.

Peace
-stephan

Posted by Stephan Schmidt on July 01, 2008 at 02:05 AM CEST #

Right! I have also experimented with JavaRebel for Jersey, see here:

http://blogs.sun.com/sandoz/entry/javarebel_and_jersey_take_2

It works OK, but it is a brute force approach and needs some refinement. I am wondering if there something that could be done automatically with a NetBeans/Jersey development mode using JavaRebel.

Paul.

Posted by Paul Sandoz on July 01, 2008 at 02:11 AM CEST #

Post a Comment:
  • HTML Syntax: NOT allowed
About

sandoz

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today