Exemplo n.º 1
0
  /**
   * Initialize JDBC DataSource instance with the given database configuration instances and the
   * given class loader.
   *
   * @param database Database configuration.
   * @param loader ClassLoader to use.
   * @return The initalized DataSource.
   * @throws MappingException Problem related to analysing the JDO configuration.
   */
  public static DataSource loadDataSource(final Database database, final ClassLoader loader)
      throws MappingException {
    DataSource dataSource;
    Param[] parameters;

    DatabaseChoice dbChoice = database.getDatabaseChoice();
    String className = dbChoice.getDataSource().getClassName();
    ClassLoader classLoader = loader;
    if (classLoader == null) {
      classLoader = Thread.currentThread().getContextClassLoader();
    }

    try {
      Class dsClass = Class.forName(className, true, classLoader);
      dataSource = (DataSource) dsClass.newInstance();
    } catch (Exception e) {
      String msg = Messages.format("jdo.engine.classNotInstantiable", className);
      LOG.error(msg, e);
      throw new MappingException(msg, e);
    }

    parameters = database.getDatabaseChoice().getDataSource().getParam();
    setParameters(dataSource, parameters);

    return dataSource;
  }
 @Override
 protected JDOManager getJDOManager() throws MappingException {
   org.castor.jdo.conf.Database dbConfig = getDbConfig(DBNAME);
   dbConfig.addClassMapping(JDOConfFactory.createClassMapping(MAPPING));
   return getJDOManager(dbConfig);
 }