Package org.olat.core.commons.persistence

Source Code of org.olat.core.commons.persistence.DBModule

/**
* OLAT - Online Learning and Training<br>
* http://www.olat.org
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); <br>
* you may not use this file except in compliance with the License.<br>
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing,<br>
* software distributed under the License is distributed on an "AS IS" BASIS, <br>
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. <br>
* See the License for the specific language governing permissions and <br>
* limitations under the License.
* <p>
* Copyright (c) 1999-2006 at Multimedia- & E-Learning Services (MELS),<br>
* University of Zurich, Switzerland.
* <p>
*/

package org.olat.core.commons.persistence;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.olat.core.configuration.OLATModule;
import org.olat.core.helpers.Settings;
import org.olat.core.logging.AssertException;
import org.olat.core.logging.StartupException;
import org.olat.core.logging.Tracing;
import org.olat.core.util.ObjectCloner;

import com.anthonyeden.lib.config.Configuration;

/**
* The <b>DBModule</b> is used to initialize database.
*
* @author Andreas Ch. Kapp
*
*/
public class DBModule implements OLATModule {

  public static final String HIBERNATE_CONFIG_PRODUCTIONDB = "production";
  public static final String HIBERNATE_CONFIG_TESTDB = "test";

  private static Map dbPropertiesMap = new HashMap(3);
  private static boolean activated = false;

  /**
   * @see org.olat.core.configuration.OLATModule#destroy()
   */
  public void destroy() {
    try {
      // closing database session for this thread
      DBImpl.getInstance(false).closeSession()
    } catch (Exception e) {
      throw new StartupException("Error closing database. ", e.getCause());
    }
  }

  /**
   * Initialize database mappings on startup.
   * @see org.olat.core.configuration.OLATModule#init(com.anthonyeden.lib.config.Configuration)
   */
  public void init(Configuration moduleConfig) {
    try {
      DBModule.setActivated(true);
      // process database configuration
   
      Configuration databaseConfig = moduleConfig.getChild("database");
      // get database identificator
      String dbmsName = databaseConfig.getAttribute("dbms");
      if (dbmsName == null) throw new AssertException("Missing attribute 'dbms' in element 'database' in olat_config.xml");
 
      // load hibernate configuration for this database identificator
      Configuration databaseElement = databaseConfig.getChild(dbmsName);
      List parameterlist = databaseElement.getChildren();
      Iterator iter = parameterlist.iterator();
      Properties dbProperties = new Properties();
      boolean logDebug = Tracing.isDebugEnabled(DBModule.class);
      while (iter.hasNext()) {
        Configuration conf = (Configuration) iter.next();
        if(logDebug) Tracing.logDebug("Hibernate config: " + conf.getName() + " = " + conf.getValue(), DBModule.class);
        dbProperties.setProperty(conf.getName(), conf.getValue());
      }
      dbPropertiesMap.put(HIBERNATE_CONFIG_PRODUCTIONDB, dbProperties);
     
      // add configuration for junit tests
      if (Settings.isJUnitTest()) {
        Tracing.logInfo("Configuring database for jUnit mode", DBModule.class);
        // copy configuration from production database
        Properties testProperties = (Properties) ObjectCloner.deepCopy(dbProperties);
        // and add override properties for testdb
        Configuration testdboverride = databaseConfig.getChild("hibernate.overridefortestdb");
        List overridelist = testdboverride.getChildren();
        Iterator iterator = overridelist.iterator();
        while (iterator.hasNext()) {
          Configuration conf = (Configuration) iterator.next();
          if(logDebug) Tracing.logDebug("Junit mode, overriding hibernate config: " + conf.getName() + " = " + conf.getValue(), DBModule.class);
          testProperties.setProperty(conf.getName(), conf.getValue());
        }
        dbPropertiesMap.put(HIBERNATE_CONFIG_TESTDB, testProperties);
        // TODO: cg: Workaround to clear database for each test
        Tracing.logInfo("In Junit test mode : Clear database", this.getClass());
        DBJunitImpl.getJunitInstance().clearDatabase();
      }
     
      // test database settings, if something is wrong we will get exception here
      DBFactory.getInstance()
    } catch (Exception e) {
      throw new StartupException("Database mapping problem. Check your build.properties for valid entries. Empty values can cause problems!", e);
    }
    DBImpl.getInstance(false).intermediateCommit();
  }

  /**
   * @param name Name of DB configuration set
   * @return Properties
   */
  public static Properties getDbProperties(String name) {
    if (!dbPropertiesMap.containsKey(name))
      throw new AssertException("Trying to get undefined database configuration for key: "+name);
    return (Properties)dbPropertiesMap.get(name);
  }

  private static void setActivated(boolean value) {
    activated = value;
  }

  /**
   *
   * @return true if this module has been initialized
   */
  public static boolean isActivated() {
    return activated;
  }


}
TOP

Related Classes of org.olat.core.commons.persistence.DBModule

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.