June 2013

Embedding Jasper

Jasper is the JSP compiler inside Tomcat. For reasons, mainly of curiosity, I wanted to build a Pragmatach plugin which exposes Jasper.  Pragmatach supports some template engines such as FreeMarker, ThymeLeaf and Velocity, but I thought Jasper would be a good addition. I chose to use Tomcat 6, mainly because Tomcat 7 uses Servlet 3.0.  Pragmatach is currently on Servlet 2.5 .   Luckily, there is a really helpful example of compiling JSPs right inside Tomcat; the JspC shell.  JspC is a simple command-line executable which can consume jsp files and produce both .java files and .class files. The code I ended up with, is here.

Pragmatach

Firstly, here’s the site, and here’s the code.  There’s a slideshow, here. Pragmatach is a Java-based web development platform that I wrote, based on ideas from Struts2,  Play, JSF2 and Grails.   My goals in writing it were: Configuration via annotations.  I wanted to be able to fully configure a controller using only annotations and without having to write routes files, or XML Simplicity.  I wanted to be able to quickly create a skeleton project with only a couple keystrokes Modularity.  I wanted to be able to extend the framework quickly and easily JSON and XML support. Client-side programmers prefer to communicate with servers via JSON and XML, so I wanted simple, easy JSON and XML support Multiple Template Engines. I wanted to have support for multiple template engines. Maven support.  I didn’t want to use any other build system than maven. Multiple container support.  I wanted to always produce a generic war file, and to have that war file run in any standard J2EE container. Pragmatach supports these goals.  To create a Pragmatach program, simply use the Maven Archetype, here, at the command line.  This will create a fully functional Pragmatach application with: A working pom.xml A fully maven-compliant file system Support for multiple containers, detailed here The pragmatach Admin console A single “hello world” Controller and Template. The generated controller looks like this: import java.util.Date; import com.khubla.pragmatach.framework.annotation.Controller; import com.khubla.pragmatach.framework.annotation.Route; import com.khubla.pragmatach.framework.annotation.View; import com.khubla.pragmatach.framework.api.PragmatachException; import com.khubla.pragmatach.framework.api.Response; import com.khubla.pragmatach.plugin.freemarker.FreemarkerController; @Controller(name=”IndexController”) @View(view=”index.ftl”) public class IndexController extends FreemarkerController { /** * the message */ private final String message = “Welcome to Pragmatach”; public String getMessage() { return message; } @Route(uri = “/”) public Response render() throws PragmatachException { return super.render(); } public String getTime() { return new Date().toString(); } } A key aspect of the generated controller is the annotations. The @Controller annotation tells Pragmatach that the class is a Controller.  By default controllers are session-scoped, but other scopes are available. The @View annotation tells Pragmatach where to find the view template to render.  In this case “index.ftl”, a FreeMarker template The @Route annotation on the method “render” tells Pragmatach to bind the method “IndexController:render” to the HTTP context “/”.  It’s possible to bind routes to multiple methods on a class, which can be quite useful.   Additionally, via the @RouteParameter annotation, it’s possible to bind parameters in the HTTP context path to Java method parameters. In the case of the generated application, the view template is quite simple also.  It looks like this: <!DOCTYPE html> <head> <title>Pragmatach</title> </head> <body> <h1>Pragmatach</h1> ${controller.message} <h2>Current Time</h2> ${controller.time} <a href=”${session.getServletContext().getContextPath()}/pragmatach/admin”>Admin</a> </body> Since we have chosen FreeMarker as the template engine, we bind controller class methods using the ${} syntax.  The code “${controller.message} ends up binding the method “IndexController:getMessage”. There is a lot more to Pragmatach, including the Admin console, ORM support, and a small ecosystem of plugins.  The documentation is here.  If you’d like to see some working examples, they are here. The pragmatach web site is here.  If you’re interested in contributing, have a comment or need to ask a question, you can do that on Google Groups.  The documentation is here and the latest Javadoc is here.