Helping ordinary people create extraordinary websites!
HOME TUTORIALS SCRIPTS WEB HOSTING BLOG FORUM
Get Our Newsletter
Email:

Practically Groovy: Go Server-Side Up, with Groovy

By Andrew Glover
2005-05-05


A Diagnostic Groovlet

Not only is writing a Groovlet as simple as creating a Groovy script but you can also define functions with the def keyword and call them directly within the Groovlet. To demonstrate, I'll create a nontrivial Groovlet that performs some diagnostic checks for a Web application.

Imagine you've written a Web application that has been purchased by various customers around the world. You have a large customer base and have been releasing this application for some time now. Learning from past support issues, you've noticed that you receive many frantic customer calls related to a problem stemming from incorrect JVM versions and incorrect object-relational mapping (ORM).

You're busy, so you ask me to come up with a solution. Using Groovlets, I quickly can create a simple diagnostic script that verifies the VM version and attempts to create a Hibernate session (see Resources). I start by creating two functions and calling them when the script is hit via a browser. The diagnostic Groovlet is defined in Listing 6:

Listing 6. A diagnostic Groovlet

import com.vanward.resource.hibernate.factory.DefaultHibernateSessionFactory

/**
* Tests VM version from environment- note, even 1.5 will
* cause an assertion error.
*/
def testVMVersion(){
println "<h3>JVM Version Check: </h3>"
vers = System.getProperty("java.version")
assert vers.startsWith("1.4"): "JVM must be at least 1.4"
println "<p>JVM version: ${vers} </p>"
}
/**
* Attempts to create an instance of a hibernate session. If this
* works we have a connection to a database; additionally, we
* have a properly configured hibernate instance.
*/
def testHibernate(){
println "<h3>Hibernate Configuration Check: </h3>"
try{
sessFactory = DefaultHibernateSessionFactory.getInstance()
session = sessFactory.getHibernateSession()
assert session != null: "Unable to create hibernate session.
Session was null"
println "<p>Hibernate configuration check was successful</p>"
}catch(Throwable tr){
println """
<p>Unable to create hibernate session. Exception type is: <br/>
<i>${tr.toString()} </i><br/>
</p>
"""
}
}
println """
<html><head>
<title>Diagnostics Check</title></head>
<body>
"""
testVMVersion()
testHibernate()
println """
</body></html>
"""
The Groovlet's verification logic is fairly simple, but it will do the trick. You'll simply bundle the diagnostic script with your Web application, and when your customer support desk receives a call, they'll point the customer to the Diagnostics.groovy script in their browser and ask them to report their findings. The results could look something like what you see in Figure 2.

Figure 2. Output from the diagnostic Groovlet


Tutorial Pages:
» On-the-fly Server-Side Programming with Groovlets and GSPs
» Defining Functions in Scripts
» Groovlets and GSPs
» The Groovlet, Please
» A Diagnostic Groovlet
» What About Those GSPs?
» Refactor me this ...
» Conclusion
» Resources


First published by IBM DeveloperWorks


 | Bookmark
Related Tutorials:
» All about JAXP, Part 1
» Make Database Queries Without the Database
» Load List Values for Improved Efficiency
» 2 Ways To Implement Session Tracking
» A Simple Way to Read an XML File in Java
» Develop Aspect-Oriented Java Applications with Eclipse and AJDT