View Javadoc

    1   /***
    2    * JFoxSOAF, Service-Oriented Application Framework
    3    *
    4    * Copyright (C) www.huihoo.org
    5    *
    6    * Distributable under GNU LGPL
    7    *
    8    * For more information, please visit: http://www.huihoo.org/jfox/jfoxsoaf
    9    */
    10
    11  package org.huihoo.jfox.soaf.services.persistence;
    12
    13  import net.sf.hibernate.HibernateException;
    14  import net.sf.hibernate.Session;
    15  import net.sf.hibernate.SessionFactory;
    16  import net.sf.hibernate.cfg.Configuration;
    17
    18  import org.huihoo.jfox.soaf.services.logging.LoggingService;
    19
    20  /***
    21   * <p>
    22   * Hibernate persistence service implementation.
    23   * </p>
    24   *
    25   * @author <a href="mailto:founder_chen@yahoo.com.cn">Peter Cheng </a>
    26   * @version $Revision: 1.3 $ $Date: 2004/10/20 14:22:06 $
    27   * @version Revision: 1.0
    28   */
    29
    30  public class HibernateServiceImpl implements HibernateService {
    31
    32      private LoggingService loggingService;
    33
    34      private Configuration config;
    35
    36      private SessionFactory sessionFactory;
    37
    38      /***
    39       * @param loggingService
    40       */
    41      public HibernateServiceImpl(LoggingService loggingService)
    42              throws HibernateException {
    43          this.loggingService = loggingService;
    44          config = new Configuration();
    45          config.configure(getClass().getClassLoader().getResource(
    46                  "hibernate.cfg.xml"));
    47      }
    48
    49      /***
    50       * @see org.huihoo.jfox.soaf.services.persistence.HibernateService#openSession()
    51       */
    52      public Session openSession() throws HibernateException {
    53          SessionFactory factory = getSessionFactory();
    54          Session session = factory.openSession();
    55          return session;
    56      }
    57
    58      /***
    59       * @see org.huihoo.jfox.soaf.services.persistence.HibernateService#getConfiguration()
    60       */
    61      public Configuration getConfiguration() throws HibernateException {
    62          return config;
    63      }
    64
    65      /***
    66       * @see org.huihoo.jfox.soaf.services.persistence.HibernateService#getSessionFactory()
    67       */
    68      public SessionFactory getSessionFactory() throws HibernateException {
    69          if (sessionFactory == null) {
    70              synchronized (this) {
    71                  try {
    72                      sessionFactory = config.buildSessionFactory();
    73                  } catch (Throwable t) {
    74                      if (loggingService.isInfoEnabled()) {
    75                          loggingService.info("Could not configure hibernate ");
    76                      }
    77                      t.printStackTrace();
    78                  }
    79              }
    80          }
    81          return sessionFactory;
    82      }
    83
    84  }