    View Javadoc
    1   /*
    2    * Copyright (c) 2003
    3    * Information Desire GmbH
    4    * All rights reserved.
    5    */
    6   package com.infodesire.infobit.hibernate;
    7
    8   import net.sf.hibernate.HibernateException;
    9   import net.sf.hibernate.MappingException;
    10  import net.sf.hibernate.tool.hbm2ddl.SchemaExport;
    11  import net.sf.hibernate.cfg.Configuration;
    12  import net.sf.hibernate.Session;
    13  import net.sf.hibernate.SessionFactory;
    14
    15  import org.apache.commons.logging.Log;
    16  import org.apache.commons.logging.LogFactory;
    17
    18  import java.util.Properties;
    19  import java.util.Iterator;
    20
    21  import com.infodesire.infobit.dao.ConfigurationBuilder;
    22
    23  import com.infodesire.infobit.InfobitException;
    24  /***
    25   * abstract base session provider. handles session factory configuration.
    26   * concrete classes shall call init() before own initialization
    27   *
    28   * @author    konstantin
    29   * @created   August 6, 2003
    30   * @version   $Revision: 1.2 $
    31   */
    32  public abstract class BaseSessionProvider implements SessionProvider {
    33
    34      private static Log _log = LogFactory.getLog(BaseSessionProvider.class);
    35      private final static String HIBERNATE_PREFIX = "hibernate.";
    36      private SessionFactory _factory;
    37
    38
    39      /***
    40       * initialize from propeties. construct session factory.
    41       *
    42       * @param properties            Description of Parameter
    43       * @exception InfobitException  Description of Exception
    44       */
    45      public void init(Properties properties) throws InfobitException {
    46
    47          try {
    48              Configuration configuration = ConfigurationBuilder.getConfiguration();
    49              if (properties != null) {
    50                  String name;
    51                  for (Iterator iter = properties.keySet().iterator(); iter.hasNext(); ) {
    52                      name = (String) iter.next();
    53                      if (name.startsWith(HIBERNATE_PREFIX)) {
    54                          configuration.setProperty(name, properties.getProperty(name));
    55                      }
    56                  }
    57              }
    58
    59              _factory = configuration.buildSessionFactory();
    60
    61              if ("true".equals(properties.getProperty("create.tables", "false"))) {
    62                  if (_log.isDebugEnabled()) {
    63                      _log.debug("creating tables");
    64                  }
    65                  SchemaExport ex = new SchemaExport(configuration);
    66                  ex.drop(true, true);
    67                  ex.create(true, true);
    68              }
    69
    70          } catch (Exception ex) {
    71              _log.error("exception occured while configuring session factory", ex);
    72              throw new InfobitException(ex);
    73          }
    74      }
    75
    76
    77      /***
    78       * Gets the Factory attribute of the BaseSessionProvider object
    79       *
    80       * @return   The Factory value
    81       */
    82      SessionFactory getFactory() {
    83          return _factory;
    84      }
    85
    86
    87      /***
    88       * Gets the SessionFactory attribute of the BaseSessionProvider object
    89       *
    90       * @return   The SessionFactory value
    91       */
    92      SessionFactory getSessionFactory() {
    93          return _factory;
    94      }
    95  }

This page was automatically generated by Maven