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

org.hibernate.testing.util.ServiceRegistryUtil Maven / Gradle / Ivy

There is a newer version: 7.0.0.Beta2
Show newest version
/*
 * Hibernate, Relational Persistence for Idiomatic Java
 *
 * License: GNU Lesser General Public License (LGPL), version 2.1 or later.
 * See the lgpl.txt file in the root directory or .
 */
package org.hibernate.testing.util;

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

import org.hibernate.boot.registry.BootstrapServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.boot.registry.internal.StandardServiceRegistryImpl;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.cfg.Environment;

import org.hibernate.testing.jdbc.SharedDriverManagerConnectionProviderImpl;

public class ServiceRegistryUtil {

	public static StandardServiceRegistryBuilder serviceRegistryBuilder() {
		return applySettings( new StandardServiceRegistryBuilder() );
	}

	public static StandardServiceRegistryBuilder serviceRegistryBuilder(BootstrapServiceRegistry bsr) {
		return applySettings( new StandardServiceRegistryBuilder( bsr ) );
	}

	public static StandardServiceRegistryImpl serviceRegistry() {
		return (StandardServiceRegistryImpl) serviceRegistryBuilder().build();
	}

	public static StandardServiceRegistryBuilder applySettings(StandardServiceRegistryBuilder builder) {
		if ( !Environment.getProperties().containsKey( AvailableSettings.CONNECTION_PROVIDER )
				&& !builder.getSettings().containsKey( AvailableSettings.CONNECTION_PROVIDER ) ) {
			builder.applySetting(
					AvailableSettings.CONNECTION_PROVIDER,
					SharedDriverManagerConnectionProviderImpl.getInstance()
			);
			builder.applySetting(
					AvailableSettings.CONNECTION_PROVIDER_DISABLES_AUTOCOMMIT,
					Boolean.TRUE
			);
		}
		return builder;
	}

	public static void applySettings(Map properties) {
		if ( !properties.containsKey( AvailableSettings.CONNECTION_PROVIDER ) ) {
			//noinspection unchecked
			( (Map) properties ).put(
					AvailableSettings.CONNECTION_PROVIDER,
					SharedDriverManagerConnectionProviderImpl.getInstance()
			);
			( (Map) properties ).put(
					AvailableSettings.CONNECTION_PROVIDER_DISABLES_AUTOCOMMIT,
					Boolean.TRUE
			);
		}
	}

	public static Map createBaseSettings() {
		final Map settings = new HashMap<>();
		ServiceRegistryUtil.applySettings( settings );
		return settings;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy