org.opentestsystem.airose.db.session.DBSetUp.java Source code

Java tutorial

Introduction

Here is the source code for org.opentestsystem.airose.db.session.DBSetUp.java

Source

/*******************************************************************************
 * Copyright (c) 2013 American Institutes for Research
 * 
 * This file is part of AIROSE.
 * 
 * AIROSE 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.
 * 
 * AIROSE 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 AIROSE.  If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package org.opentestsystem.airose.db.session;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Logger;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.opentestsystem.airose.common.config.AbstractConfiguration;
import org.opentestsystem.airose.common.config.ConfigurationFactory;
import org.opentestsystem.airose.common.config.UninitializedException;

/*
 * Do not make this class public. This class is used for only setting up
 */
class DBSetUp {

    private static final Logger LOGGER = Logger.getLogger(DBSetUp.class.getName());

    private SessionFactory mSessionFactory = null;
    private static DBSetUp singleton = null;

    /*
     * opens a new session and returns it. this method is synchronized but that
     * may not be required at all. todo: look into the possibility of using
     * SessionFactory.getCurrentSession().
     */
    public synchronized Session createSession() {
        return mSessionFactory.openSession();
    }

    /*
     * some databases e.g. embedded hsql database require an explicit shutdown.
     */
    public void shutdown() throws UninitializedException, SQLException {
        AbstractConfiguration applicationConfiguration = ConfigurationFactory.getConfiguration();
        String explicitShutdownURL = applicationConfiguration.getExplicitDBShutdownURL();
        if (explicitShutdownURL != null) {
            @SuppressWarnings("unused")
            Connection c = DriverManager.getConnection(explicitShutdownURL, "", "");
        }
    }

    /*
     * initialize the connection and possibly the db - such as in case of embedded
     * databases.
     */
    @SuppressWarnings("deprecation")
    public void startUp() throws UninitializedException {

        /*
         * most of the DB configuration is carried out from hibernate.cfg.xml except
         * possibly for the DB URL which may be overwritten from the main
         * configuration file.
         */
        AbstractConfiguration applicationConfiguration = ConfigurationFactory.getConfiguration();

        Configuration cfg = new Configuration().configure("hibernate.cfg.xml");

        if (applicationConfiguration.getDBURL() != null)
            cfg.setProperty("hibernate.connection.url", applicationConfiguration.getDBURL());

        // A SessionFactory is set up once for an application
        mSessionFactory = cfg.buildSessionFactory(); // configures
        // settings from
        // hibernate.cfg.xml

    }

    /*
     * returns an instance of DBSetUp. Implicitly performs initialization if it
     * has not already been initialized.
     */
    public static DBSetUp getInstance() throws UninitializedException {
        if (singleton != null)
            return singleton;

        init();

        return singleton;
    }

    private static synchronized void init() throws UninitializedException {
        if (singleton != null)
            return;

        singleton = new DBSetUp();
    }

    private DBSetUp() throws UninitializedException {
        startUp();
    }
}