Developing Struts Hibernate Plugin

In this section we will develop java code for Struts Hibernate Plugin.

Developing Struts Hibernate Plugin

Developing Struts Hibernate Plugin

     

In this section we will develop java code for Struts Hibernate Plugin. Our Hibernate Plugin will create Hibernate Session factory and cache it in the servlet context. This strategy enhances the performance of the application.

Source Code Of Hibernate Struts Plugin:

package roseindia.net.plugin;

import java.net.URL;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionServlet;
import org.apache.struts.action.PlugIn;
import org.apache.struts.config.ModuleConfig;
import org.hibernate.HibernateException;


public class HibernatePlugIn implements PlugIn {
 private String _configFilePath = "/hibernate.cfg.xml";

  /**
 * the key under which the <code>SessionFactory </code> instance is stored
 * in the <code>ServletContext</code>.
 */
  public static final String SESSION_FACTORY_KEY = SessionFactory.class.getName();

  private SessionFactory _factory = null;

 public void destroy() {
 try{
 _factory.close();
 }catch(HibernateException e){
  System.out.println("Unable to close Hibernate Session Factory: " + e.getMessage());
 }
 
 }

 public void init(ActionServlet servlet, 
ModuleConfig config
throws ServletException {
 System.out.println("*************************************");
 System.out.println("**** Initilizing HibernatePlugIn **********");
  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){
  System.out.println("Error while initializing hibernate: " + e.getMessage());
 }
 System.out.println("**************** *********************");

 }

  /**
 * 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.");
  }
  
  System.out.println("Setting ' configFilePath' to '"  + configFilePath + "'...");
  _configFilePath = configFilePath;
  }


/*(SessionFactory) servletContext.getAttribute
(HibernatePlugIn.SESSION_FACTORY_KEY);
*/
  
}


In our plugin class we have define a variable _configFilePath to hold the name of Hibernate Configuration file.

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

Following code define the key to store the session factory instance in the Servlet context.

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

The init() is called on the startup of the Struts Application. On startup the session factory is initialized and cached in the Servlet context.

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);

Changes to be done in struts-config.xml file

Configuring Hibernate with Struts is very simple work it requires you to have hibernate.cfg.xml in your WEB-INF/classes directory, and to add the following line to the struts-config.xml file.

<plug-in className="roseindia.net.plugin.HibernatePlugIn"></plug-in>

Testing the Plugin

Build your application and deploy on the tomcat server. Start tomcat server and observe the console output. It should display the following line:

 
 log4j:WARN Please initialize the log4j system properly.
*************************************
**** Initilizing HibernatePlugIn **********
*************************************
Aug 7, 2006 10:09:53 AM org.apache.struts.tiles.TilesPlugin initD
  

This means you have successfully configured your Struts Hibernate Plugin with struts application.