All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.notronix.hibermate.impl.Hibermate Maven / Gradle / Ivy

package com.notronix.hibermate.impl;

import com.notronix.hibermate.api.PersistenceException;
import com.notronix.hibermate.api.PersistenceManager;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.SessionFactory;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.BootstrapServiceRegistry;
import org.hibernate.boot.registry.BootstrapServiceRegistryBuilder;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.type.BasicTypeRegistry;

import java.util.HashMap;
import java.util.Map;

import static com.notronix.albacore.Optionals.ofBlankable;
import static org.apache.commons.lang3.StringUtils.isBlank;
import static org.apache.commons.lang3.StringUtils.trim;
import static org.hibernate.boot.registry.StandardServiceRegistryBuilder.DEFAULT_CFG_RESOURCE_NAME;

public enum Hibermate
{
    MANAGER_FACTORY;

    private static final Map managers = new HashMap<>();
    private static final BasicTypeRegistry typeRegistry = new BasicTypeRegistry();
    private static final Object lock = new Object();

    public PersistenceManager getManager(String namespace) throws PersistenceException {
        namespace = trim(namespace);
        if (isBlank(namespace)) {
            throw new PersistenceException("namespace is blank");
        }

        PersistenceManager manager = managers.get(namespace);
        if (manager != null) {
            return manager;
        }

        synchronized (lock) {
            manager = managers.get(namespace);
            if (manager != null) {
                return manager;
            }

            return managers.get(createManager(namespace));
        }
    }

    private String createManager(String namespace) throws PersistenceException {
        try {
            final DatabaseIntegrator dbIntegrator = new DatabaseIntegrator();
            final BootstrapServiceRegistry bootstrapRegistry = new BootstrapServiceRegistryBuilder()
                    .applyIntegrator(dbIntegrator).build();
            final StandardServiceRegistryBuilder registryBuilder = new StandardServiceRegistryBuilder(bootstrapRegistry)
                    .configure(namespace + "." + DEFAULT_CFG_RESOURCE_NAME);

            applySetting(registryBuilder, namespace, "hibernate.dialect.storage_engine");
            applySetting(registryBuilder, namespace, "hibernate.connection.url");
            applySetting(registryBuilder, namespace, "hibernate.connection.username");
            applySetting(registryBuilder, namespace, "hibernate.connection.password");
            applySetting(registryBuilder, namespace, "hibernate.connection.driver_class");
            applySetting(registryBuilder, namespace, "hibernate.dialect");

            final StandardServiceRegistry registry = registryBuilder.build();
            try {
                SessionFactory sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory();
                managers.put(namespace, new DefaultPersistenceManagerImpl(sessionFactory, dbIntegrator, typeRegistry));
            }
            catch (Exception e) {
                StandardServiceRegistryBuilder.destroy(registry);
            }

            return namespace;
        }
        catch (Exception ex) {
            throw new PersistenceException("An error occurred trying to create manager.", ex);
        }
    }

    private static void applySetting(StandardServiceRegistryBuilder registryBuilder, String namespace, String setting) {
        if (registryBuilder == null || isBlank(namespace) || isBlank(setting)) {
            return;
        }

        String key = StringUtils.join(trim(namespace), ".", trim(setting));
        ofBlankable(System.getProperty(key)).ifPresent(value -> {
            registryBuilder.applySetting(trim(setting), value);
        });
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy