ocoon is a powerful tool for publishing content to multiple formats from XML. It can be used for static content, but its most powerful application is publishing dynamically generated XML streams. The XML streams can come from database information wrapped in XML tags, from XHTML Web site content from a remote site (as would be common in portal integration), from Web services, or from a multitude of other sources.
This article describes how to integrate Cocoon with PostgreSQL, a leading open source database. Once you learn how to access PostgreSQL data (or any other relational database) from Cocoon, you can develop robust XML-driven applications with a relational backend. Think about it: XML is highly structured data; relational databases store highly structured data—it's a natural fit.
Because it assumes that you already have some familiarity with Cocoon, it does not describe how to set up the tool. If you have not set up Cocoon previously, visit the Cocoon Web site and read the INSTALL.txt file included in the Cocoon distribution.
Why PostgreSQL for Your Relational Database?
When you develop with Cocoon and use XML for your source data, chances are you will need to integrate with a relational database. Most Web sites or applications that store or access large amounts of data interface with a relational database, because relational databases are reliable, have good performance, and are familiar to many developers.
In the open source world, PostgreSQL and MySQL are two of the best-known databases. MySQL is in wider use, but PostgreSQL bears closer resemblance to the commercial database servers—in fact, it was the basis for Informix, now part of IBM. I prefer PostgreSQL for the following reasons:
- It more closely follows the ANSI SQL standard.
- It has had transaction support for longer (and thus is presumably more robust)
- It feels better designed, possibly because, as the successor to the INGRES database project at UC Berkeley, it has really been in development since the 1970's.
Find additional information on MySQL and PostgreSQL at sql-info.de's MySQL gotchas and PostgreSQL gotchas pages.
Set Up Tomcat and Cocoon
Begin by downloading Tomcat. You will want the Tomcat 5.5.4 release (or later). It should be called "5.5.4 tar.gz". Unpack the archive once you have downloaded it.
Next, download Cocoon. You should get the latest release (2.1.6 as of the writing of this article). You should also download the patch I created (cocoon-2.1.6_patch_for_java_1.5.diff). To obtain the cocoon-2.1.6_patch_for_java_1.5.diff file, look in the downloadable code accompanying this article. You need the patch because Cocoon 2.1.6 does not compile under Java 1.5. Luckily, the changes are fairly minor: just changing "enum" to "enumeration" (because enum is a reserved keyword in Java 1.5) and fixing some of the build files in Cocoon to recognize Java 1.5.
I run Cocoon in a Tomcat servlet engine on Linux. Your interaction with Cocoon may differ if your setup is different. Since the rest of this article uses Unix commands, you will also have to adjust them if you run Windows rather than Linux.
The versions of software I use are Java 1.5.0_01, Cocoon 2.1.6, Tomcat 5.5.4, and PostgreSQL 8.0.0. Again, if you use different versions, you may have different results. If you use a version of Cocoon released after this article was published (in other words, greater than 2.1.6), you may not need to perform the patch described in the next paragraph because a newer version of Cocoon may already include the necessary changes.
If you use Java 1.5 and Cocoon 2.1.6, type the following commands:
- cd /path/to/cocoon-2.1.6 (Replace /path/to/cocoon-2.1.6 with the real path to the Cocoon directory.)
- patch -p1 < /path/to/cocoon-2.1.6_patch_for_java_1.5.diff
- ./build.sh webapp
- cp -a build/webapp /path/to/tomcat-5.5.4/webapp/cocoon (Replace /path/to/tomcat-5.5.4 with real path.)
Going forward, I use $TOMCAT_HOME to represent /path/to/tomcat-5.5.4 and $COCOON_HOME to represent /path/to/tomcat-5.5.4/webapp/cocoon. When you see these references, replace them with the full path to Tomcat and to Cocoon, respectively, in your filesystem.
Test that your Tomcat and Cocoon installation works. First, start up Tomcat with the following two commands:
- cd $TOMCAT_HOME/bin
|Figure 1. Welcome Screen for Successful Cocoon Installation|
View $TOMCAT_HOME/logs/catalina.out and look for something like "INFO: Server startup in 18312 ms" at the very end of the log file (the "18312 ms" will appear different because your computer will probably take a different amount of time to start Tomcat). Once that appears, you know the server has started up.
Open a Web browser and go to http://$SERVER_HOSTNAME:8080/cocoon, where $SERVER_HOSTNAME is the name of the host on which you run Tomcat and Cocoon. I have a Linux machine called galleon on which I run Cocoon. To test whether Cocoon works after following the Cocoon installation instructions, I visit http://galleon:8080/cocoon. Replace "galleon" with the name of the machine on which you run Cocoon (localhost is valid if you run Cocoon on your local computer). You should see something like Figure 1 in your browser. If you see it, you have successfully installed Cocoon and are ready to set up and test the PostgreSQL integration.
First, you should shut down Tomcat because you need to install more software and make further changes to your server configuration files. Shut down Tomcat with the following two commands:
- cd $TOMCAT_HOME/bin