au.edu.anu.metadatastores.harvester.HarvesterHibernateUtil.java Source code

Java tutorial

Introduction

Here is the source code for au.edu.anu.metadatastores.harvester.HarvesterHibernateUtil.java

Source

/*******************************************************************************
 * Australian National University Metadata Stores
 * Copyright (C) 2013  The Australian National University
 * 
 * This file is part of Australian National University Metadata Stores.
 * 
 * Australian National University Metadata Stores 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 3 of the License, or (at your option) any later
 * version.
 * 
 * This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/

package au.edu.anu.metadatastores.harvester;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistryBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import au.edu.anu.metadatastores.util.encrypt.EncryptUtil;

/**
 * <p>HarvesterHibernateUtil<p>
 * 
 * <p>The Australian National University</p>
 * 
 * <p>Class provide the hibernate session factory for the harvester</p>
 * 
 * @author Genevieve Turner
 *
 */
public class HarvesterHibernateUtil {
    static final Logger LOGGER = LoggerFactory.getLogger(HarvesterHibernateUtil.class);
    private static final SessionFactory sessionFactory = buildSessionFactory();

    /**
     * Build the sesion factory
     * 
     * @return The session factory
     */
    private static SessionFactory buildSessionFactory() {
        try {
            Configuration configuration = new Configuration();
            configuration.configure("/harvester.cfg.xml");

            //Provide for having an encrypted password
            String password = configuration.getProperty("hibernate.connection.password");
            String decryptedValue = EncryptUtil.decrypt(password);
            configuration.setProperty("hibernate.connection.password", decryptedValue);

            ServiceRegistryBuilder serviceRegistryBuilder = new ServiceRegistryBuilder()
                    .applySettings(configuration.getProperties());

            return configuration.buildSessionFactory(serviceRegistryBuilder.buildServiceRegistry());
        } catch (Exception e) {
            LOGGER.error("Initial SessionFactory creation failed.", e);
            throw new ExceptionInInitializerError(e);
        }
    }

    /**
     * Get the session factory
     * 
     * @return The session factory
     */
    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}