Wednesday, November 23, 2011

Book review: Tomcat 6 Developer's Guide

The book is very wholistic in its approach to educating the reader. It provides a well-written overview of JavaEE and HTTP in Chapter 2. And whenever it mentions a technology that Tomcat uses in some way, it immediately gives a brief overview of that technology. I found this helpful because it gave me the knowledge I needed to continue reading without having to switch to a search engine. For example, in Chapter 5, when discussing the JNDI functionality that Tomcat comes packaged with, the book pauses for a moment to explain the basic concepts of JNDI itself. The book will also often describe the historical origins of the technology, like in Chapter 1, where it discusses the origins of Ant (spoiler alert: it was created as a custom build system for Tomcat).

The instructions for downloading the Tomcat source code and building it are very clear. It provides a specific branch you can checkout from the Tomcat Subversion server, which is the exact version of Tomcat that was used when writing the book. This allows you to follow along without worrying about dealing with differences between what you see in the code and what you see in the book. And it also includes instructions for getting the project properly configured with Eclipse.

The later chapters of the book go into a lot of detail about the various classes that make up the Tomcat source code, so be prepared for some serious code spelunking. Anyone who is interested in doing serious coding in the Tomcat codebase will gain a lot from these chapters.

I'd recommend this book to people who want to work on the Tomcat source code and need something to guide them through it. I'd also recommend the book to JavaEE developers who use Tomcat on a regular basis and who want to expand their knowledge.

Saturday, October 15, 2011

Microsoft Word: Disabling spellcheck for a block of text

Often times, when I'm putting together a piece of documentation for work, it helps to include code samples and console commands in the document. I like to use Word to write documentation because it allows you to stylize the text (such as increasing the font size of headers, making things bold, etc) which I think makes it easier to read (when used in moderation). It's also nice for code samples because when you copy and paste code from Eclipse, it includes the syntax-highlighting of the code. Creating plain text files is simpler, but you loose that readability aspect I think is important.

Anyway, one annoying thing is that Word spell checks and grammar checks these code samples and console commands, so you get all these red and green squiggly lines under the text.

However, it is possible to disable spell and grammar check for selected blocks of text! First, select the text that you want to exclude from being checked. Then, select the "Review" tab from the ribbon at the top of the screen and click on "Set Language".

In the dialog window that appears, click the "Do not check spelling and grammar" checkbox and click "OK". If your document happens to have multiple written languages in it, you can also use this dialog to tell Word which blocks of text are written in which language.

Now, those squiggly lines should disappear!

Saturday, October 8, 2011

Book Review: Play Framework Cookbook

Play Framework Cookbook is divided into seven chapters, each of which contains a dozen or so recipes. The recipes range in skill level from beginner to advanced and are pretty much organized in this order, with the more basic recipes at the start of the book and the more advanced ones at the end. Some examples of the more basic recipes include "Defining your own controllers" and "Basics of caching". Advanced topics include "Understanding bytecode enhancement" and "Implementing your own persistence layer". Nearly all of the recipes are self-contained so you don't have to read the book from start to finish--you can jump around at your leasure and read whatever looks interesting to you.

Each recipe is usually divided into four sections. "Getting Ready" describes how to prepare your development environment for the recipe. "How to do it..." covers the steps you have to take to complete the recipe, like what code you have to write. A detailed explaination of those steps can be found in the next section, "How it works...". The last section, "There's more..." (what's with all the ellipses?), contains supplemental information on how to improve upon the recipe and broaden its scope. I like the way the author divided each recipe this way because it makes it easier to find what you're looking for. For example, if I'm in the midst of coding something and need to refresh my memory about a particular method call or class name, I can jump right to the "How to do it..." section and not have to wade through a lot of text to find what I'm looking for.

I would say that the only prerequisite of the book is that you know how to program in Java, though having basic knowledge of Play beforehand will help. The Forward contains an informative overview that describes where Play fits into wider the Java web application world and how it differentiates itself from other web frameworks. And the recipes in the first chapter contain a lot of the basics of the framework, like how to define routes and create controllers.

But all the information in the first chapter can be found in the official Play documentation online. Where the book really shines is in the subsequent chapters. They describe how to perform specific tasks such as creating an RSS feed, creating PDFs, and generating JSON data. They also describe how to integrate Play with a variety of systems like Apache, MongoDB, Spring, Google Charts, and Jenkins. Getting an application to "play nice" with the other parts of a system, no matter what technology you are using, can be a hairy ordeal, so I think these recipes are especially helpful.

All of the code samples can be downloaded from the book's website. Each sample corresponds to a single recipe and is a complete Play application, which is great because it means the samples are self-contained and can be run right off the bat. However, I had a little trouble getting some of them to run. I had to manually enable the in-memory database (the "db=mem" config property) because I was getting JPA errors on application startup. But otherwise, the samples seemed to work fine.

All in all, I would recommend this book to anyone interested in learning more about Play. It covers a wide variety of topics, so you're bound to find something useful no matter what kind of Play application you're developing (or want to develop) or what your skill level is.

Monday, September 19, 2011

Upcoming book review: "Play Framework Cookbook"

I got an interesting email last Friday. A publisher, Packt Publishing, noticed my recent blog posts about the Play Framework and asked if I would review the book, Play Framework Cookbook by Alexander Reelsen. I've never been asked to write a book review before, so I'm really excited about it! I'll be posting a review of the book within the next 2-3 weeks.

In the mean time, feel free to check out a sample chapter from the book entitled "Using Controllers" where the author covers topics such as how to generate PDFs and implement HTTP digest authentication.

Saturday, September 17, 2011

The Play Framework - Part 6

The Play Framework is a MVC web application framework for Java that focuses on being light-weight and easy to use--two qualities that you don't see very often in the Java web application world.


Here are a couple miscellaneous features of the Play Framework:

In the "conf/routes" file, a regular expression can be used to match a URL to a controller action. The regular expression is enclosed within < > characters and comes before the variable name. For example, the following route definition only matches if the last part of the URL is a number:

GET  /posts/{<[0-9]+>id}  Application.show

In a template, when looping through a list of elements, it is often helpful to know if the current element is the first or last element in the list. To do this, append _isFirst or _isLast to the end of the variable name. For example, the code below will output the contents of a list with each element separated by a comma. A comma should always be printed after each element--except for the last element, which shouldn't print a comma:

#{list items:tags, as:tag}
  ${tag}${tag_isLast ? '' : ', '}
#{/list}

Thursday, September 15, 2011

The Play Framework - Part 5 - CAPTCHAs

The Play Framework is a MVC web application framework for Java that focuses on being light-weight and easy to use--two qualities that you don't see very often in the Java web application world.


Play comes packaged with a CAPTCHA generator, which is good for ensuring that form submissions are sent by a human being and not by an automated bot.

A CAPTCHA is an image that contains random letters and numbers that the user has to type in order to submit a form. The letters and numbers are rendered in a non-uniform way in order to make it as difficult as possible for an automated image recognition algorithm to read (but still clear enough for a human to read). For example, the characters may each be a different size and font. It stands for (take a deep breath) "Completely Automated Public Turing test to tell Computers and Humans Apart".

To generate a CAPTCHA in Play, use the Images.Captcha class:

public class Application extends Controller{
  public static void captcha(){
    Images.Captcha captcha = Images.captcha();
    String code = captcha.getText("#990000");
    captcha.addNoise("#CCCCCC");
    captcha.setBackground("#996633", "#FF9900");
    renderBinary(captcha);
  }
}

In this code, I'm generating a CAPTCHA image and returning it in the response. Calling the getText() method sets the color of the text and returns the random string that the CAPTCHA is rendering. As you can see, the notation used to define the color is conveniently the same notation that is used in HTML and CSS (however, you cannot use the three-character abbreviated form used by CSS).

I'm also adding noise to the image to make it even harder for an image recognition algorithm to read (it will add a gray squiggly line through the image). And then, I'm setting a background gradient to have it fade from a dark to a light orange.

Because the Captcha class implements InputStream, it can be passed into the renderBinary() method to send the image to the response. It generates a PNG image.

Tuesday, September 13, 2011

The Play Framework - Part 4

The Play Framework is a MVC web application framework for Java that focuses on being light-weight and easy to use--two qualities that you don't see very often in the Java web application world.


A special template syntax is used to link to other pages in the application (the actual URL of the page is not used). This makes things more flexible, because you can change the page's URL in the "conf/routes" file without having to go through each template file and edit all the links that point to that page. Take this example:

Template file:

<a href="@{Application.index()}">Home</a>

"conf/routes" file:

GET  /home   Application.index

Generated HTML:

<a href="/home">Home</a>

Play sees that the Application.index method is assigned to the URL "/home" and generates the appropriate HTML when a user requests the page.

To access a static resource (like images, Javascript files, and CSS files), put the path to that resource in single quotes. With Play, all static resources go in the "public" directory.

<script src="@{'/public/javascripts/jquery-1.4.2.min.js'}"></script>

As with seemingly every type of file in a Play application, any changes made to the "conf/routes" file while the application is running are applied immediately. No restart is needed.

The "form" template tag can be used to create HTML forms:

#{form @Application.postComment(post.id)}
  Name: <input type="text" name="author" value="${params.author}" />
  <input type="submit" value="Post" />
#{/form}

This ends up adding two more attributes to the generated <form> tag ("accept-charset" and "enctype") and also adds a hidden parameter called "authenticityToken" (probably used for security purposes). You can also add attributes of your own, by supplying them as tag parameters after the action URL:

#{form @Application.postComment(post.id), onsubmit:'return validateForm()'}