ru.taximaxim.dbreplicator2.utils.Core.java Source code

Java tutorial

Introduction

Here is the source code for ru.taximaxim.dbreplicator2.utils.Core.java

Source

/* The MIT License (MIT)
 *
 * Copyright (c) 2013 Technologiya
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 * the Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

package ru.taximaxim.dbreplicator2.utils;

import java.io.File;

import org.apache.log4j.Logger;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

import ru.taximaxim.dbreplicator2.cf.BoneCPConnectionsFactory;
import ru.taximaxim.dbreplicator2.cf.ConnectionFactory;
import ru.taximaxim.dbreplicator2.model.ApplicatonSettingsService;
import ru.taximaxim.dbreplicator2.model.BoneCPSettingsService;
import ru.taximaxim.dbreplicator2.model.TaskSettingsService;
import ru.taximaxim.dbreplicator2.el.ErrorsLog;
import ru.taximaxim.dbreplicator2.qr.RunnersQueue;
import ru.taximaxim.dbreplicator2.qr.ThreadPoolQueue;
import ru.taximaxim.dbreplicator2.stats.StatsService;
import ru.taximaxim.dbreplicator2.tasks.TasksPool;
import ru.taximaxim.dbreplicator2.tp.ThreadPool;

/**
 * @author volodin_aa
 *
 */
public final class Core {

    private static final Logger LOG = Logger.getLogger(Core.class);

    /**
     *  ?? ? ?.
     */
    private Core() {
    }

    private static SessionFactory sessionFactory;

    private static ConnectionFactory connectionFactory;

    private static TaskSettingsService taskSettingsService;

    private static TasksPool tasksPool;

    private static Configuration configuration;

    private static ThreadPool threadPool;

    private static StatsService statsService;

    private static RunnersQueue runnersQueue;

    /**
     *  ?  
     * 
     * @param hibernateXmlFile -    ?
     * @return
     */
    public static synchronized Configuration getConfiguration(String hibernateXmlFile) {
        if (configuration == null) {
            configuration = new Configuration();
            if (hibernateXmlFile != null) {
                File conf = new File(hibernateXmlFile);
                configuration.configure(conf);
            } else {
                configuration.configure();
            }
        }
        return configuration;
    }

    /**
     *  ?  
     * 
     * @return
     */
    public static Configuration getConfiguration() {
        return getConfiguration((String) null);
    }

    /**
     *  
     */
    public static void configurationClose() {
        configuration = null;
    }

    /**
     *   ??? .
     * 
     * @param configuration - ? ?
     * @return  ??? .
     */
    public static synchronized SessionFactory getSessionFactory(Configuration configuration) {
        LOG.debug(" ?   ??? hibernate");

        if (sessionFactory == null) {
            ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
                    .applySettings(configuration.getProperties()).buildServiceRegistry();
            sessionFactory = configuration.buildSessionFactory(serviceRegistry);

            LOG.info(" ?  ??? hibernate");
        }
        return sessionFactory;
    }

    /**
     *   ??? .
     * 
     * @return  ??? .
     */
    public static SessionFactory getSessionFactory() {
        return getSessionFactory(getConfiguration());
    }

    /**
     *   ??? .
     * 
     * @param hibernateXmlFile -    ?
     * @return  ??? .
     */
    public static SessionFactory getSessionFactory(String hibernateXmlFile) {
        if (sessionFactory == null) {
            configuration = getConfiguration(hibernateXmlFile);

            return getSessionFactory(configuration);
        }

        LOG.debug(" ?   ??? hibernate");

        return sessionFactory;
    }

    /**
     *  sessionFactory
     */
    public static synchronized void sessionFactoryClose() {
        if (sessionFactory != null) {
            sessionFactory.close();
            sessionFactory = null;
        }
    }

    /**
     *   ?
     * 
     * @return  ?
     */
    public static synchronized ConnectionFactory getConnectionFactory() {
        //  ?  ?  ?  
        // ?.

        if (connectionFactory == null) {
            connectionFactory = new BoneCPConnectionsFactory(new BoneCPSettingsService(getSessionFactory()));
        }

        return connectionFactory;
    }

    /**
     *  connectionFactory
     */
    public static synchronized void connectionFactoryClose() {
        if (connectionFactory != null) {
            connectionFactory.close();
            connectionFactory = null;
        }
    }

    /**
     *  ?? ? ?
     * 
     * @return ?? ? ?
     */
    public static synchronized TaskSettingsService getTaskSettingsService() {
        //  ?  ?  ?  
        // ?.
        if (taskSettingsService == null) {
            taskSettingsService = new TaskSettingsService(getSessionFactory());
        }

        return taskSettingsService;
    }

    /**
     *  ?? ? ?
     */
    public static void taskSettingsServiceClose() {
        taskSettingsService = null;
    }

    /**
     *   
     * 
     * @return  
     */
    public static synchronized TasksPool getTasksPool() {
        //  ?  ?  ?  
        // ?.
        if (tasksPool == null) {
            tasksPool = new TasksPool(getTaskSettingsService());
        }

        return tasksPool;
    }

    /**
     *   
     * 
     * @return  
     * @throws InterruptedException 
     */
    public static synchronized RunnersQueue getRunnersQueue() throws InterruptedException {
        if (runnersQueue == null) {
            runnersQueue = new RunnersQueue();

            ApplicatonSettingsService aService = new ApplicatonSettingsService(sessionFactory);
            int count = Integer.parseInt(aService.getValue("tp.threads"));
            new ThreadPoolQueue(count, runnersQueue);

            LOG.info(" ?   RunnersQueue");
        }

        return runnersQueue;
    }

    /**
     *  ?? ? ?
     */
    public static void tasksPoolClose() {
        tasksPool = null;
    }

    /**
     *   
     * 
     * @return  
     * @throws InterruptedException 
     */
    public static synchronized ThreadPool getThreadPool() throws InterruptedException {
        if (threadPool == null) {
            ApplicatonSettingsService aService = new ApplicatonSettingsService(sessionFactory);
            int count = Integer.parseInt(aService.getValue("tp.threads"));
            threadPool = new ThreadPool(count);
        }

        return threadPool;
    }

    /**
     *   
     * @throws InterruptedException 
     */
    public static synchronized void threadPoolClose() throws InterruptedException {
        if (threadPool != null) {
            threadPool.shutdownThreadPool();
            threadPool = null;
        }
    }

    /**
     *  ?? ??
     * 
     * @return ?? ??
     */
    public static synchronized StatsService getStatsService() {
        if (statsService == null) {
            ApplicatonSettingsService aService = new ApplicatonSettingsService(getSessionFactory());
            String baseConnName = aService.getValue("stats.dest");
            statsService = new StatsService(baseConnName);
        }

        return statsService;
    }

    /**
     *  ?? ??
     */
    public static synchronized void statsServiceClose() {
        statsService = null;
    }

    /**
     *  ?? ErrorsLog
     * 
     * @return ?? ErrorsLog
     */
    public static synchronized ErrorsLog getErrorsLog() {
        ApplicatonSettingsService aService = new ApplicatonSettingsService(getSessionFactory());
        String baseConnName = aService.getValue("error.dest");
        ErrorsLog errorLog = new ErrorsLog(baseConnName, getConnectionFactory());

        return errorLog;
    }
}