Brian's Waste of Time

Fri, 23 Apr 2004

It's All So Simple

Guillaume LaForge of Groovy fame (he keeps adding all the absurdly convenient methods to existing java classes when used from Groovy) pointed me towards Simple, a Java based http server.

The API for writing services for Simple makes the Servlet API look painful to use. I am not kidding.

Oh yeah, as a side benefit, Niall (Simple's creator) has benchmarks demonstrating that it slightly outperforms the Apache Web Server 1.3.X.

Giving it the ten minute test -- it passed. I had an embedded http server up and running fast. The class below implements a full embedded web server. All requests go to the org.skife.simple.HelloService.

package org.skife.simple;

import simple.http.ProtocolHandler;
import simple.http.connect.Connection;
import simple.http.connect.ConnectionFactory;
import simple.http.load.LoaderEngine;
import simple.http.serve.HandlerFactory;

import java.net.ServerSocket;

public class Main
{
    public static void main(String[] args) throws Exception
    {
        LoaderEngine engine = new LoaderEngine();
        engine.load("default", "org.skife.simple.HelloService");
        engine.link("*", "default");
        ProtocolHandler handler = HandlerFactory.getInstance(engine);
        Connection connection = ConnectionFactory.getConnection(handler);
        connection.connect(new ServerSocket(8282));
    }
}

The hello service in turn simple creates a list of things in my src/java blosxom entries directory:

package org.skife.simple;

import simple.http.Request;
import simple.http.Response;
import simple.http.load.BasicService;
import simple.http.serve.Context;

import java.io.File;
import java.io.PrintStream;

public class HelloService extends BasicService
{
    public static final File entry_dir =
            new File("/secret/path/to/blosxom/entries");

    public HelloService(Context context)
    {
        super(context);
    }

    public void process(Request req, Response resp) throws Exception
    {
        PrintStream out = resp.getPrintStream();
        out.println("<html><body><ul>");
        File[] entries = entry_dir.listFiles();
        for (int i = 0; i < entries.length; i++)
        {
            out.println("<li>" + entries[i].getName() + "</li>");
        }
        out.println("</ul></body></html>");
        out.close();
    }
}

And voila. I likes what I have played with so far quite a bit. Cookies, templates (Velocity), etc are all bundled into the main distro. Mamba also exists as a standalone server built on Simple.

0 writebacks [/src/java] permanent link