Package javango.contrib.hibernate

Source Code of javango.contrib.hibernate.HibernateMiddleware

package javango.contrib.hibernate;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;

import com.google.inject.Inject;

import javango.http.Http500;
import javango.http.HttpException;
import javango.http.HttpRequest;
import javango.http.HttpResponse;
import javango.middleware.Middleware;
import javango.core.servers.RequestProcessor;

public class HibernateMiddleware extends Middleware implements RequestProcessor.Listener, HttpRequest.Listener {

  private final static Log log = LogFactory.getLog(HibernateMiddleware.class);
 
  protected HibernateUtil hibernateUtil;
 
  @Inject
  public HibernateMiddleware(HibernateUtil hibernateUtil) {
    super();
    this.hibernateUtil = hibernateUtil;
  }


  @Override
  public HttpResponse processRequest(HttpRequest req) throws HttpException {
    try {
      hibernateUtil.beginTransaction();   
    } catch (HibernateException e) {
      log.error(e,e);
      throw new Http500(e);     
    }
    return super.processRequest(req);
  }

 
  @Override
  public HttpResponse processMethodAndParams(HttpRequest req, javango.api.MethodObjectParams map) throws HttpException {
    req.addListener(this);
    return super.processMethodAndParams(req, map);
  }


  @Override
  public HttpResponse processResponse(HttpRequest req, HttpResponse response) throws HttpException {
    try {
      log.debug("Attempting commit");
      hibernateUtil.commitTransaction();
    } catch (HibernateException e) {
      log.error(e,e);
      log.error(e.getCause(),e.getCause());
      throw new Http500("Unable to write to database: " + e, e);
    }
    return super.processResponse(req, response);
  }

  @Override
  public HttpResponse processException(HttpRequest request, HttpResponse response, Throwable t) throws HttpException {
    try {
      if (log.isWarnEnabled()) log.warn("Attempting rollback due to " + t);
      hibernateUtil.rollbackTransaction();   
    } catch (HibernateException e) {
      log.error(e,e);
      if (response == null) {
        throw new Http500("Unable to rollback the database: " + e, e);
      } else {
        return response; // basically someone else has already done something,  no need to muck it up.
      }
    }
    return super.processException(request, response, t);
  }

  public void stopRequestProcessor(RequestProcessor requestProcessor) {
    // TODO Can we somehow shutdown hibernate here to avoid the locking problems with Jetty?
  }


  public void closeRequest(HttpRequest request) throws HttpException {
    try {
      log.debug("Closing session");
      hibernateUtil.closeSession();
    } catch (HibernateException e) {
      log.error(e,e);
      throw new Http500("Unable to close database session: " + e, e);
    }   
  }
}
TOP

Related Classes of javango.contrib.hibernate.HibernateMiddleware

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.