org.dynamise.sample.hibernate.DataAccessBundle.java Source code

Java tutorial

Introduction

Here is the source code for org.dynamise.sample.hibernate.DataAccessBundle.java

Source

/*******************************************************************************
 *  Copyright 2015 Efe Kahraman
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 ******************************************************************************/

package org.dynamise.sample.hibernate;

import org.dynamise.shared.annotation.Create;
import org.dynamise.shared.annotation.Lifecycle;
import org.dynamise.shared.annotation.Service;
import org.dynamise.shared.bundle.BundleException;
import org.dynamise.shared.bundle.BundleProperties;
import org.dynamise.shared.bundle.LifecycleEvent;
import org.dynamise.shared.bundle.annotation.Signal;
import org.dynamise.shared.context.ServiceContext;
import org.dynamise.shared.service.ServiceLogger;
import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;

/**
 * @author Efe Kahraman
 *
 */

@Service("bookStore")
@Lifecycle(value = "dataAccessBundle", version = 0)
public class DataAccessBundle {

    private ServiceLogger logger;

    private BundleProperties properties;

    private SessionFactory sessionFactory;

    @Create
    public void create() {
        if (null == this.properties) {
            throw new BundleException("Missing property file");
        }
    }

    @Signal(LifecycleEvent.START)
    public void start(ServiceContext serviceCtx) {

        this.logger.info("Building session factory");

        // Pre-loading driver class
        String driverClass = this.properties.getString("db.driver");
        try {
            Class.forName(driverClass);
        } catch (ClassNotFoundException e) {
            throw new BundleException("Driver not found", e);
        }

        // Prepare config
        Configuration configuration = new Configuration();
        configuration.addAnnotatedClass(Book.class);
        configuration.setProperty("connection.driver_class", driverClass);
        configuration.setProperty("hibernate.connection.url", this.properties.getString("db.url"));
        configuration.setProperty("hibernate.connection.username", this.properties.getString("db.username"));
        configuration.setProperty("hibernate.connection.password", this.properties.getString("db.password"));
        configuration.setProperty("dialect", this.properties.getString("db.hDialect"));
        configuration.setProperty("hibernate.hbm2ddl.auto", this.properties.getString("db.hbm2ddl.auto"));

        try {

            StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder()
                    .applySettings(configuration.getProperties());
            this.sessionFactory = configuration.buildSessionFactory(builder.build());

            serviceCtx.put(Constants.KEY_SESSION_FACTORY, this.sessionFactory);

        } catch (HibernateException e) {
            throw new BundleException(e);
        }

    }

    @Signal(LifecycleEvent.STOP)
    public void stop() {
        if (this.sessionFactory != null) {
            try {
                this.sessionFactory.close();
            } catch (HibernateException e) {
                this.logger.error("Couldn't close session factory", e);
            }
        }
    }

}