Package ch.hortis.sonar.jpa

Source Code of ch.hortis.sonar.jpa.JPAUtil

/*
* This program is copyright (c) 2007 Hortis-GRC SA.
*
* This file is part of Sonar.
* Sonar is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Sonar is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Sonar; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
package ch.hortis.sonar.jpa;

import ch.hortis.sonar.model.JdbcData;
import org.hibernate.dialect.*;
import org.hibernate.ejb.Ejb3Configuration;

import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
* Hibernate utils class to retreive an entitymanager factory for EJB3
* persistance
*/
public class JPAUtil {

  private static Map<Integer, EntityManagerFactory> factories = new HashMap<Integer, EntityManagerFactory>();

  /**
   * Reset the created EntityManagerFactory objects in "cache"
   */
  protected static void reset() {
    for (EntityManagerFactory factory : factories.values()) {
      factory.close();
    }
    factories.clear();
  }

  /**
   * Retreive an entitymanager factory for EJB3 persistance : always the same
   * for same JdbcData.
   *
   * @param jdbcData
   *            jdbcData connection informations
   * @param configuration
   *            additional properties to provide to create the entitymanager
   *            factory, can be null for default values
   * @param isDebug
   *            set the EntityManagerFactory in isDebug mode
   * @return a singleton instance of EntityManagerFactory for the given params
   */
  protected static synchronized EntityManagerFactory getEntityManagerFactory(JdbcData jdbcData, Map<String, String> configuration) {

    Integer key = jdbcData.hashCode();
       
    EntityManagerFactory factory = factories.get(key);
    if (factory != null)
      return factory;
   
    Ejb3Configuration cfg = new Ejb3Configuration();
    cfg.addAnnotatedClass(ch.hortis.sonar.model.ProjectSnapshot.class);
    cfg.addAnnotatedClass(ch.hortis.sonar.model.MavenProject.class);
    cfg.addAnnotatedClass(ch.hortis.sonar.model.Metric.class);
    cfg.addAnnotatedClass(ch.hortis.sonar.model.ProjectMetricMeasure.class);
    cfg.addAnnotatedClass(ch.hortis.sonar.model.SchemaInfo.class);
    cfg.addAnnotatedClass(ch.hortis.sonar.model.SnapshotGroup.class);
   
    Properties props = new Properties();
    if ( jdbcData.getDatasource() == null ) {
      props.put("hibernate.connection.url", jdbcData.getUrl());
      props.put("hibernate.connection.driver_class", jdbcData.getDriverClassName());
      props.put("hibernate.connection.username", jdbcData.getUsername());
      props.put("hibernate.connection.password", jdbcData.getPassword());
      props.put("hibernate.dialect", getHibernateDialect(jdbcData.getUrl()));
    } else {
      props.put("hibernate.connection.datasource", jdbcData.getDatasource());
      props.put("hibernate.dialect", getHibernateDialect(jdbcData.getDsDialect()));
    }

    props.put("hibernate.hbm2ddl.auto", "validate");
    props.put("hibernate.current_session_context_class", "thread");
    if (configuration != null) {
      props.putAll(configuration);
    }
    cfg.addProperties(props);
    factory = cfg.createEntityManagerFactory ();
     
    /*Map<String, String> settings = new HashMap<String, String>();
    settings.put("hibernate.connection.url", jdbcData.getUrl());
    settings.put("hibernate.connection.driver_class", jdbcData.getDriverClassName());
    settings.put("hibernate.connection.username", jdbcData.getUsername());
    settings.put("hibernate.connection.password", jdbcData.getPassword());
    settings.put("hibernate.dialect", getHibernateDialect(jdbcData.getUrl()));
    settings.put("hibernate.hbm2ddl.auto", "validate");
    settings.put("hibernate.current_session_context_class", "thread");
    if (isDebug) {
      settings.put("hibernate.show_sql", "true");
      settings.put("hibernate.format_sql", "true");
      settings.put("hibernate.use_sql_comments", "true");
    } else {
     
       LogManager.getLogManager(); // JDK6 API Logger.global has been
       deprecated //Logger logGolbal = Logger.getLogger(
       Logger.GLOBAL_LOGGER_NAME ); Logger logGolbal = Logger.global;
       logGolbal.getParent().setLevel(Level.SEVERE);
     
    }
    if (configuration != null) {
      settings.putAll(configuration);
    }
    factory = Persistence.createEntityManagerFactory("sonar", settings);*/
    factories.put(key, factory);
    return factory;
  }

  protected static String getHibernateDialect( JdbcData.JDBCDialect dialect ) throws PersistenceException {
    if (dialect == JdbcData.JDBCDialect.DB2) {
      return DB2Dialect.class.getName();
    } else if (dialect == JdbcData.JDBCDialect.DERBY) {
      return DerbyDialect.class.getName();
    } else if (dialect == JdbcData.JDBCDialect.HSQLDB) {
      return HSQLDialect.class.getName();
    } else if (dialect == JdbcData.JDBCDialect.MSSQL) {
      return SQLServerDialect.class.getName();
    } else if (dialect == JdbcData.JDBCDialect.MYSQL) {
      return MySQLDialect.class.getName();
    } else if (dialect == JdbcData.JDBCDialect.ORACLE) {
      return OracleDialect.class.getName();
    } else if (dialect == JdbcData.JDBCDialect.POSTGRE) {
      return PostgreSQLDialect.class.getName();
    } else {
      throw new PersistenceException("Unsupported database for Dialect " + dialect );
    }
  }
 
  /**
   * Hibernate dialect -> supported db mapping
   *
   * @param url
   *            the JDBC URL
   * @return a string containing the hibernate dialect class name to use
   * @throws PersistenceException
   *             if the given JDBC URL is not supported
   */
  protected static String getHibernateDialect(String url) throws PersistenceException {
    if (url.toLowerCase().startsWith("jdbc:db2:")) {
      return DB2Dialect.class.getName();
    } else if (url.toLowerCase().startsWith("jdbc:derby:")) {
      return DerbyDialect.class.getName();
    } else if (url.toLowerCase().startsWith("jdbc:hsqldb:")) {
      return HSQLDialect.class.getName();
    } else if (url.toLowerCase().startsWith("jdbc:microsoft:sqlserver:")) {
      return SQLServerDialect.class.getName();
    } else if (url.toLowerCase().startsWith("jdbc:mysql:")) {
      return MySQLDialect.class.getName();
    } else if (url.toLowerCase().startsWith("jdbc:oracle:")) {
      return OracleDialect.class.getName();
    } else if (url.toLowerCase().startsWith("jdbc:postgresql:")) {
      return PostgreSQLDialect.class.getName();
    } else {
      throw new PersistenceException("Unsupported database for URL " + url);
    }
  }
}
TOP

Related Classes of ch.hortis.sonar.jpa.JPAUtil

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.