Pages

Wednesday, June 18, 2014

Full support for Xtend on Spark

Spark and Xtend

Spark (the micro web framework) accidentally started supporting Xtend lambda syntax to define Routes, since Spark started supporting java8 lambdas.

 For instance:

package spark

import static spark.Spark.before
import static spark.Spark.get
import static spark.Spark.halt

class XtendTest {
    def static void main(String[] args) {
        get("/hello/:name", [request, response |
            System.out.println("request = " + request.pathInfo());
            return "Hello " + request.params("name")
        ])
        
        val boilerplate = "is a necessary evil"
        val builtInTemplatingSystem = "woohoo, gotta love xtend, I ain't gotta escape no nothin'."
        before("/protected/*", "application/json", [request, response |
            halt(401,
            '''
            {"«boilerplate»": "«builtInTemplatingSystem»"}
            ''')
        ])
        
        get("/love/me/some/xml", "application/xml", [rq, rp | '''
        
  «boilerplate»
        
        '''])
    }
}
And the generated code:
package spark;

import org.eclipse.xtend2.lib.StringConcatenation;
import spark.Filter;
import spark.Request;
import spark.Response;
import spark.Route;
import spark.Spark;

@SuppressWarnings("all")
public class XtendTest {
  public static void main(final String[] args) {
    final Route _function = new Route() {
      public Object handle(final Request request, final Response response) {
        String _pathInfo = request.pathInfo();
        String _plus = ("request = " + _pathInfo);
        System.out.println(_plus);
        String _params = request.params("name");
        return ("Hello " + _params);
      }
    };
    Spark.get("/hello/:name", _function);
    final String boilerplate = "is a necessary evil";
    final String builtInTemplatingSystem = "woohoo, gotta love xtend, I ain\'t gotta escape no nothin\'.";
    final Filter _function_1 = new Filter() {
      public void handle(final Request request, final Response response) throws Exception {
        StringConcatenation _builder = new StringConcatenation();
        _builder.append("{\"");
        _builder.append(boilerplate, "");
        _builder.append("\": \"");
        _builder.append(builtInTemplatingSystem, "");
        _builder.append("\"}");
        _builder.newLineIfNotEmpty();
        Spark.halt(401, _builder.toString());
      }
    };
    Spark.before("/protected/*", "application/json", _function_1);
    final Route _function_2 = new Route() {
      public Object handle(final Request rq, final Response rp) {
        StringConcatenation _builder = new StringConcatenation();
        _builder.append("        ");
        _builder.append("");
        _builder.newLine();
        _builder.append(boilerplate, "");
        _builder.newLineIfNotEmpty();
        _builder.append("        ");
        _builder.append("");
        _builder.newLine();
        return _builder;
      }
    };
    Spark.get("/love/me/some/xml", "application/xml", _function_2);
  }
}

So, what do you get for it?
  1. Everything Xtend has to offer, e.g. lambdas, Active Annotations;
  2. Another native templating system, i.e. the ''' blah ''' syntax, that translates to native java, thus negating the necessity for a third party templating system.
So, how do I start playing with it?
  1. Git clone and import the project thru git into eclipse;
  2. (Optional) Setup maven, on your IDE and OS;
  3. Ensure that you have the proper Xtext/Xtend dependencies installed in eclipse, look here for instructions;
  4. Start hacking Xtend code using the Spark framework to build your API or web site, and forget about MVC... and start writing unmanageable, unbuzzworded (e.g. separation of concerns, MVC, etc.) code.

Caveat

It's not necessary to have java8 installed on your system, but some of the examples that rely on java8 features will break. Xtend/Spark with java7 works just fine.

The author (me) tested this stuff on Eclipse Juno, using the latest stable Xtend version (2.3.*) and the latest release of Spark, using apache maven 3.0.5.

Edit (24 June 2014):

A few days after discovering this. I found out about Sparkle. Sparkle is bundled with Xtend and everything else, it was a prescient refactoring of Sparky, somewhere around a year ago, it hasn't been updated for a while.