Hello, my name is Luke Bunselmeyer. I’m a fun loving Full-Stack engineer who delights in polyglot programming from back-end to front-end, Java to JavaScript, Web services to Web development.

[ Java ]

Many languages support concise and self documenting method calls. In JavaScript we typically use object literals to shorten method signatures. Here’s a contrived createCookie example…

/**
 * Contrived method to create a HTTP cookie
 * @param {String} name     Name of the cookie
 * @param {String} value    Value of the cookie
 * @param {Object} options  Additional cookie parameters
 */
function createCookie(name, value, options) {
    var cookie = new Cookie(name, value);
    return _.extend(cookie, options || {});
}

And we can call the method like so…

var c = createCookie('foo', 'bar', {
    path: '/aa/bb',
    expires: new Date(2015, 2, 18)
});

In comparison, the corresponding method signature in Java 1.7 blights our eyes…

Cookie createCookie(String name, String value, String path, Long maxAge, Boolean secure, Boolean httpOnly, String comment);

And calling it further assaults our asthetics. What’s with all those null’s!!!???

Cookie c = createCookie("foo", "bar", "/aa/bb", Long.MAX_VALUE, null, null, null);

Fortunately, with lambda support now baked into Java 1.8, we can use the Consumer functional interface to sweeten up createCookie

public Cookie createCookie(String name, String value, Consumer<Cookie> cookieOptions) {
    Cookie cookie = new DefaultCookie(name, value);
    cookieOptions.accept(cookie);
    return cookie;
}

Now we can call createCookie drama free…

Cookie c = createCookie("foo", "bar", (cookie) -> {
   cookie.setPath("/aa/bb");
   cookie.setMaxAge(Long.MAX_VALUE);
   cookie.setSecure(true);
});

While this is just a simple and contrived example, I feel it illustrates how Java 1.8 opens the door for many popular coding patterns practiced throughout many other language communities. I’m excited to see how the Java community embraces these new options.

Shameless plug

If you’re interested in my other hacks with Java 1.8, please check out my most recent naive pursuit: Hitch: Middleware for Java.

References

I recently had the opportunity to integrate the Play Framework with a Guice managed Java web app. This presented two main challeges: 1) where to hook-in the Guice Injector and 2) how to support Guice RequestScoped resources. Fortunately, both Play 2.1-RC1 and Guice have several API’s to simplify things. Moreover, I was able to wrap the entire solution into a reusable Play Module, play2-guice-module, which I’ll detail here.


Inspired by Guice’s excellent ServletModule integration, play2-guice-module takes advantage of some of the new Play 2.1 RC1 features to provide several utility classes to integrate Google Guice into Play.

Getting Started

Play 2.1-RC1 provides hooks to create a “dynamic” controller class with each request. In conf/routes simply prefix each controller action with an @.

# Tell Play to create a new controller with each request by prefixing the action with an `@`:
GET     /                           @controllers.Application.index()

Create a Global object which extends GuiceGlobalSettings as illustrated below…

public class Global extends GuiceGlobalSettings {
    @Override
    protected Injector createInjector() {
        return Guice.createInjector(Stage.PRODUCTION, new PlayModule() {

            @Override
            protected void configurePlay() {
                // bind some stuff!
                bind(FooPresenter.class).in(RequestScoped.class);
            }
        });
    }
}

Finally, just annotate your controller method with @With(RequestScopedAction.class) to enable @RequestScoped resources.

public class Application extends Controller {

    // ahhh immutable...
    private final FooPresenter _fooPresenter;

    // yeah, constructor injector!
    @Inject
    Application(FooPresenter fooPresenter) {
        _fooPresenter = fooPresenter;
    }

    // Enable RequestScoped resources for this action
    @With(RequestScopedAction.class)
    public Result index() {
        String bar = _fooPresenter.getBar();
        return ok(index.render("Request: " + bar));
    }
}

Available Injections

Installing the PlayModule automatically gives you access to several classes from the Play framework. These classes are injectable in any Guice injected object by default, when you install the PlayModule

@RequestScoped
public class FooPresenter {

    @Inject
    FooPresenter(Http.Request request, Http.Response response, Http.Context context) {
        ...
    }

    ...
}

See the sample-app for more details.