Sunday, May 20, 2007

Plugins in Struts (Writing HibernateUtil Plugin)

In this blog i will take a real life example where the plugins from the struts can be useful.

First have a overview of what we are going to do.

Let us take a scenario. We are using Hibernate with Struts. In hibernate, the very first step that we do is to create a Configuration object. This Object inturns build SessionFactory object, which is responsible for creating Sessions. Anyone having even a iota of interest in Hibernate must be knowing Session is the object that performs all the tasks like persisting, creating Transation etc. SessionFactory is a bulky object. It will consume a lot of resources in creating a SessionFactory object. So it is preferable if we create this object only once and reuse it in the whole application. Just like what Singelton offers. If we are using Struts with Hibernate, this can be accomplished by writing a plugin that builds SessionFactory object.

Now look at what is Plugin in Struts.

In addition to the three classes (ActionServlet, Action and RequestProcessor) Struts provides another way to define your controller. These are Plugins. Struts Plugins are modular extensions to the Struts Controller. They have been introduced in Struts 1.1, and are defined by the org.apache.struts.action.Plugin interface. Struts Plugins are useful when allocating resources or preparing connections to databases or even JNDI resources.

This interface, like the Java Servlet architecture, defines two methods that must be implemented by all used−defined Plugins: init() and destroy(). These are the life−cycle methods of a Struts Plugin. The init() method of a Struts Plugin is called whenever the JSP/Servlet container starts the Struts Web application containing the Plugin. It has a method signature as follows:


public void init(ActionServlet servlet, ModuleConfig config) throws ServletException {


}

This method is convenient when initializing resources that are important to their hosting applications. As you will have noticed, the init() method receives an ModuleConfig parameter when invoked. This object provides access to the configuration information describing a Struts application. The init() method marks the beginning of a Plugin’s life.

Then there is destroy() method. This method marks the end of a Plugin’s life. It is used when you want to purposely close the connection or fluch something out of the buffer. The destroy() method of a Struts Plugin is called whenever the JSP/Servlet container stops the Struts Web application containing the Plugin. It has a method signature as follows:

public void destroy();

Till now i am sure you must have got what is Plugin in Struts. Now look at the programming aspect of it.

Creating a Plugin

Plugin is always created by implementing the interface Plugin. The signature will look like this.


import org.apache.struts.action.PlugIn;
//One has to import plugin to make it available to the implemanting class.

public class HibernateUtilPlugin implements PlugIn{


private static String _configFilePath = "/hibernate.cfg.xml";

// Gives the path of configration file. In my case it is stored in the directory where the class //files are

public static final String SESSION_FACTORY_KEY
= SessionFactory.class.getName();

private SessionFactory _factory = null;

// Initializes the SessionFactory object

private static SessionFactory factoryTest = null;

public void destroy() {
try{
_factory.close();
}catch(HibernateException e){

}

}

public void init(ActionServlet servlet, ModuleConfig config) throws ServletException {

// This method will be called only once. What we are doing is creating a SessionFactory object //here. Init is called when ever the container is initialized with the client request.


Configuration configuration = null;
URL configFileURL = null;
ServletContext context = null;

try{
configFileURL = HibernatePlugin.class.getResource(_configFilePath);
context = servlet.getServletContext();
configuration = (new Configuration()).configure(configFileURL);
_factory = configuration.buildSessionFactory();

//Set the factory into session

context.setAttribute(SESSION_FACTORY_KEY, _factory);

}catch(HibernateException e){
e.printStackTrace();

}

}

/**
* Setter for property configFilePath.
* @param configFilePath New value of property configFilePath.
*/

public void setConfigFilePath(String configFilePath) {
if ((configFilePath == null) || (configFilePath.trim().length() == 0)) {
throw new IllegalArgumentException(
"configFilePath cannot be blank or null.");
}


_configFilePath = configFilePath;
}

public static SessionFactory getFactory() {


Configuration configuration = null;
URL configFileURL = null;
if(factoryTest == null) {
try{

configFileURL = HibernatePlugin.class.getResource(_configFilePath);
configuration = (new Configuration()).configure(configFileURL);


factoryTest = configuration.buildSessionFactory();
//the creation of the sessionFactory.


}catch(HibernateException e){
e.printStackTrace();

}

}
return factoryTest;

}


}
}


This program will create the SessionFactory object which can be used again and again by the application.


But the task is incomplete. We will have to say the Struts that we have a plugin. The best place to do so will be the struts-config.xml file. This can be done by






Hope i am clear for the purpose of why plugin is used and how it can be used. Any question can be directed to kanishkavatsa@hotmail.com

No comments: