Wednesday, October 27, 2004

Congratulations Firefox

Firefox 1.0 will be officially launched on November 9. It's a nice milestone. And they plan to celebrate it. There is also a campaign to raise enough money for a full-page advertisement on New York Times (it appears they already have enough for two pages). For $30 you can have your name printed on the ad :)

Firefox is truly a great browser. I've been using since the days it was called Phoenix.
However, there is more to Firefox than meets the eye. Firefox is based on XUL. A definition of XUL is "a cross-platform language for describing user interfaces of applications". XUL is based on XML and has a rich set of UI components. This allows you to create complex interfaces in XML, with a clean separation between presentation and logic. The combination of XUL and Firefox means that you can make entire applications leveraged on Firefox. Thus, Firefox becomes a framework for complex distributed applications, that aren't suited for plain web pages. This is really a great thing and can boost creativity to create better distributed applications that go beyond HTML's limitations. And it appears some companies are already realizing this too.

Monday, October 25, 2004

Creating PDF documents in Java

Everyday I see more of those pdf-icons, on web-pages, that link to a pdf version of the document. It's a very nice feature. It's very useful if you want to save the document for later reference, or even for printing.

I just experimented with a tool to create PDF documents in Java: iText. It's very simple to use, although the documentation could be better (I have to remember this feeling the next time I don't feel like writing documentation). I just used a few simple options to generate a PDF from a text-only article. However, it also supports graphics, and for what I've seen from their website, I think it supports just about everything you'd need to create a PDF. For me, I just needed the following lines to show a title and a body text (yes, my requirements were simple) :


Font chapterFont = FontFactory.getFont(
FontFactory.HELVETICA, 24, Font.NORMAL, new Color(0, 0, 0));
Paragraph title = new Paragraph(article.getTitle(), chapterFont);
Paragraph body = new Paragraph(article.getBody());
document.add(title);
document.add(body);

Well, in fact you'll need more code to initialize and to generate the actual PDF. Unless you're using Spring. Spring MVC has a very nice integration with iText. By using Spring MVC, a PDF document is just another way of showing the content. It's another kind of view, like a JSP or a XSLT. And it's resolved just like a JSP, by the View Resolver (you'll probably have to use ResourceBundleViewResolver, as, for example, InternalResourceViewResolver seems to be more suited to JSP pages). So I added the following to my "views.properties":
articlePDF.class=view.pdf.ArticlePdfPage
This specifies the class that generates the view to the client. Now, instead of forwarding to a JSP in the Controller, I forward to a PDF page, but for the controller it's completely transparent. I just need to tell it to forward to articlePDF view.

As for the class, it must extend AbstractPdfView. Below is the full class that I used :

public class ArticlePdfPage extends AbstractPdfView {

protected void buildPdfDocument(Map model, Document document,
PdfWriter writer, HttpServletRequest request,
HttpServletResponse response) throws Exception {

ArticleBean article = (ArticleBean) model.get("article");

Font chapterFont = FontFactory.getFont(
FontFactory.HELVETICA, 24, Font.NORMAL,
new Color(0, 0, 0));

Paragraph title = new Paragraph(article.getTitle(),
chapterFont);
Paragraph body = new Paragraph(article.getBody());
document.add(title);
document.add(body);
}
}
iText seems good if you need to dynamically create PDF documents. However it may not be the best solution for every situation. It depends on what kind of content you have and how complex the final document should be. For example, if you already have a XML version of the document, there are simpler ways of generating a PDF from it (Apache FOP, for example).

Monday, October 18, 2004

Simple and small

When I want to learn a new technology, most probably I end up creating a small application as an example. I think it's the best way to learn. So far, whenever I needed a database for that purposes I chose MySQL. It's fast, easy to set up and it's already installed in my Linux box.
However, from now on, I think I'll be using HSQLDB instead. For those of you that don't know, HSQLDB is a relational database, written in java. Its purpose is not to compete with the other major relational databases like oracle, MySQL, etc. Quoting from its website : It is best known for its small size, ability to execute completely in memory and its speed. To those characteristics, I would add another: Simplicity. Installing it, is as simple as copying a Jar to the filesystem. Running it, is as simple as getting a connection through JDBC (there are other ways, but all of them are quite simple to use).
Another example of its simplicity is the way HSQLDB stores information on disc: data is written as text files containing SQL code. The next time the database starts, the script is loaded into memory, creating the tables, inserting elements, etc. Creative :) But, this approach also has its limitations...it can eat up all your memory if your database is too big. Of course, this is not suited for that kind of applications, and as such with small databases it can be quite efficient.

Sunday, October 10, 2004

Java Puzzles

So, you think you know Java ? Think again...
This past issue from Linux Magazine will challenge your knowledge of Java with some curious and surprising puzzles. Enjoy ;)

Friday, October 08, 2004

Linux distributions and Gentoo

I've been using Linux for a few years, and I have tried several distributions, from Slackware to RedHat and Mandrake. Those are fine distributions, aimed for the general public. However, somewhere on the way I decided to be a little bolder. I wanted to know more about Linux and how it worked. And the best way to learn it was to build it myself. And that's how I started with Linux From Scratch (the name is very well chosen).
It took me a lot of time to have Linux properly installed and configured, but in the end it was worth it. The machine was lightning fast, everything was configured just right...And I had reached my objective: learning about all the small pieces that Linux is made of. No more seeking for configuration files (I had to write them all, so I knew where they were). And it was fast because every little thing was compiled in my machine, with all the right compilation flags.
However, it had a price: upgrading. As the name implies, it is from scratch....Meaning that I'd have to manually compile everything, that I wanted to upgrade, all over again...along with all the dependency tree (with the right versions).
I do like Linux From Scratch. In fact I think everyone should install it once. But I really mean once...unless you really enjoy compiling :)

So, I realized I would have to move on to a different distribution. What distribution would I choose next ? I didn't want to go back to RedHat'ish kind of distributions, now that I tasted the speed of a self-compiled distribution. And so, Gentoo appeared naturally. It's a compile-based distribution (packages are compiled in your machine) but with a great set of tools (portage) that allow you to upgrade all your system automatically. Let me give you an example:

emerge mozilla-firefox
You only need this command to install Firefox in your machine...It will check for all the dependencies, download all the necessary packages, and then compile and install all of them. Upgrading to a newer version is exactly the same command.

So, now you have the power of having everything compiled in your machine and all you need to do is sit back and relax, while Gentoo does all the hard work for you :)
And gets even better....Imagine you want to upgrade all the packages that you have installed :
emerge world
As simple as that...You can even schedule a cron job to do it periodically. So, at least for now, I'm quite happy with the Gentoo distribution :)

Monday, October 04, 2004

Introducing SiteMesh

In my first post I introduced webwork, a nice MVC from OpenSymphony. It turns out that they have a few more projects worth looking at. SiteMesh is a very simple framework, yet very useful. Its purpose is to apply layout and decoration to a web application.
It works in a very simple way. SiteMesh is based on Servlet Filters and so it sits between the user's request and your web application. It is clearly separated from your web application, that isn't even aware it exists.
As your web application finishes a client's request, the resulting HTML is processed by SiteMesh. Custom layout and style is then applied, and finally the modified HTML is returned to the client's browser.
There are several situations that can take full advantage of this architecture:

  • You may need to choose from different layouts based on user's preferences (SiteMesh can choose decorators based on cookies, for example).
  • You can choose to have different style files applied, based on the user's browser or even language.
  • You may simply want to try out a different look for your application, before you actually make the changes visible to everyone else (you can define a decorator to be applied based on a specified request parameter).
With this kind of separation, your JSPs (or whatever you use for presentation) become simpler and hopefully won't need to be modified, if you want to change your application's look and feel.