November 30, 2006

SiteMesh to decorate any web site?

Okay, according to the project explanations here, it is definitely possible to decorate any web site backed by any server side technology (assuming the server has J2EE support too) using SiteMesh. Anyone reading those explanations for the first time may think of an off-the-shelf support, like you drop SiteMesh jar and your web.xml and it works! But how much is this true in real world especially when you want to apply such solution on a living website which is already in production not just a test "localhost" site? Don't get me wrong... SiteMesh itself is a great framework and I've promoted it everywhere I've started a new project but it's not that easy to apply this framework on other non-java server side technologies. Here is my story:

A few weeks ago, I moved my personal domain to a new hosting which supports J2EE too (from my previous PHP-only one). It had been a mess to maintain the look&feel of my site in a mix of static HTML and PHP environment, so I decided to do such decorations using SiteMesh. The framework I've used in every single J2EE web application I've developed in the recent years, but never got the chance to use it on my own site because of the lack of J2EE support.

Extracting the current look&feel of my site, designing a new navigation system and setting up SiteMesh to decorate my static HTML pages was just a matter of few hours of work. Very easy, straight forward and rock solid.

But as for my PHP pages I was a bit in trouble! The site was going to be backed by Apache2 for non-java stuff (as the master server) and Apache Tomcat for my j2ee applications (as slave), and the connection between these two was via JK_Module. Obviously Apache2 was serving the static contents (like images, CSS files and so) and PHP files. The rest were forwarded to Tomcat.
The first problem was here!! Tomcat (thus SiteMesh) didn't even get the PHP requests because Apache2 was directly catching the requests and serving them without forwarding. The result was that the PHP pages were not getting decorated although SiteMesh was present there and I've set its apply pattern on "/*" :-) Ok! It just took me an email to hosting support and they were kind enough to quickly modify the configuration files so that even *.php requests get forwarded to Tomcat (for my domain only) and Apache2 no more was in charge of processing them.
But wait, the second problem! Does Tomcat have any built-in PHP processing support? NO! So how's it going to do this? Well, poking around a bit (obviously googling), I found out that there is a pre-bundled Servlet with PHP Distributions which makes *.php request processing possible. It's actually a proxy Servlet which you map *.php requests on it, and it processes them using the command line PHP interpreter. So far so good! Again I contacted the support to make PhpServlet jar file available in Tomcat's common/lib folder. It was done within some minutes and in the meanwhile I'd modified my web.xml to include PhpServlet configuration block.
After a couple of hours of debugging because of an output flushing issue (that caused SiteMesh not to get the input from PhpServlet when the volume of input was not hitting the minimum threshold to be flushed), the server was ready to serve and to decorate the PHP files. Yes, finally got it working and started to give it a more thorough test.
Hey wait! Why does my site go completely down after each time I try to access an invalid/unavailable PHP page? Oops! Seems that Tomcat crashes when it gets a "404 Page Not Found" error from PhpServlet. Googling again showed me that this is a common problem and many have reported this issue but there is still no solution. Well, end of the story :-) Fortunately my PHP codes were not a lot or complicated, and I could easily translate them into JSP and you know the rest.

Just three final thoughts which I found them the real hurdles:

  • Suppose that PhpServlet works just fine or I could come up with another solution to tackle this and PHPs are correctly decorated and served from my site... What if I want to decorate an ASP.Net or a Cold Fusion site in future? Are there plugins/wrappers available for any kind of server side technology to be served within my J2EE application server? Obviously not!
  • Even if I find a wrapper for my server-side technology, is it stable enough to be used in production mode? Because such solutions are usually used on sites which are already alive and in production mode but you just want to apply a decoration layer without changing the underlying technology. Is it worth to have a decorated-with-SiteMesh site in price of losing stability because of an unstable Servlet wrapper?
  • Since many of us are using shared hosting environments, sometimes it's impossible to make any changes in your hosting configurations to forward the needed requests to your J2EE server! Or even worse to make the J2EE server as the master one to capture all the requests. Simply not possible with many hostings.

Armond

Posted by armond at November 30, 2006 01:54 PM
Comments

This is very good article! Thanks for this my friend :)

Posted by: Aukcje at December 1, 2006 12:23 PM

A good article Armond.

In the past I have used three approaches to decorating non Java generated content from SiteMesh.

The first is exactly as you described, as many technologies (including PHP and ColdFusion) can be enabled installed as a Servlet.

The second is by using FastCGI as a bridge (which many of these technologies support).

The third (and simplest and most versatile) is to treat the Java server that SiteMesh is running on as a proxy. What's nice about this is there's no modifications needed[*] to the backend servers and a single page can mesh together content from multiple servers/technologies. There is obviously a slight performance impact but this is neglible - it's just a proxy and proxies are used lots.

-Joe

[*] Okay, there's one slight modification... if you rely on the hostname you may need to do some magic to obtain it from the proxy.

Posted by: Joe Walnes at December 4, 2006 10:19 PM

Joe, Thanks for your detailed response.

While I agree that the proxy method is a great way to decorate even multi-technologies sites in just one shot (which is really cool!), in shared hosting environments, where you do not have much privileges to change the configuration, it gets tricky to apply this.

The proxy server should be the last one in the chain (before serving the page to browser) which processes the request. And this is not always applicable if you do not have your own dedicated server. Maybe I'm missing a point here, then please let me know.

Armond

Posted by: Armond Avanes at December 8, 2006 02:08 PM

Good job!

Posted by: Markus at December 13, 2006 12:29 PM