Java Code Examples for org.hibernate.service.spi.ServiceRegistryImplementor#getService()

The following examples show how to use org.hibernate.service.spi.ServiceRegistryImplementor#getService() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DefaultIdentifierGeneratorFactory.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void injectServices(ServiceRegistryImplementor serviceRegistry) {
	this.serviceRegistry = serviceRegistry;
	this.dialect = serviceRegistry.getService( JdbcEnvironment.class ).getDialect();
	final ConfigurationService configService = serviceRegistry.getService( ConfigurationService.class );

	final boolean useNewIdentifierGenerators = configService.getSetting(
			AvailableSettings.USE_NEW_ID_GENERATOR_MAPPINGS,
			StandardConverters.BOOLEAN,
			true
	);

	if(!useNewIdentifierGenerators) {
		register( "sequence", SequenceGenerator.class );
	}
}
 
Example 2
Source File: QueryTranslatorFactoryInitiator.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public QueryTranslatorFactory initiateService(
		Map configurationValues,
		ServiceRegistryImplementor registry) {
	final StrategySelector strategySelector = registry.getService( StrategySelector.class );
	final QueryTranslatorFactory factory = strategySelector.resolveDefaultableStrategy(
			QueryTranslatorFactory.class,
			configurationValues.get( QUERY_TRANSLATOR ),
			ASTQueryTranslatorFactory.INSTANCE
	);

	log.debugf( "QueryTranslatorFactory : %s", factory );
	if ( factory instanceof ASTQueryTranslatorFactory ) {
		log.usingAstQueryTranslatorFactory();
	}

	return factory;
}
 
Example 3
Source File: ManagedBeanRegistryInitiator.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private BeanContainer resoveBeanContainer(Map configurationValues, ServiceRegistryImplementor serviceRegistry) {
	final ClassLoaderService classLoaderService = serviceRegistry.getService( ClassLoaderService.class );
	final ConfigurationService cfgSvc = serviceRegistry.getService( ConfigurationService.class );

	// was a specific container explicitly specified?
	final Object explicitBeanContainer = configurationValues.get( AvailableSettings.BEAN_CONTAINER );
	if ( explicitBeanContainer != null ) {
		return interpretExplicitBeanContainer( explicitBeanContainer, classLoaderService, serviceRegistry );
	}

	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	// simplified CDI support

	final boolean isCdiAvailable = isCdiAvailable( classLoaderService );
	final Object beanManagerRef = cfgSvc.getSettings().get( AvailableSettings.CDI_BEAN_MANAGER );
	if ( beanManagerRef != null ) {
		if ( !isCdiAvailable ) {
			BeansMessageLogger.BEANS_LOGGER.beanManagerButCdiNotAvailable( beanManagerRef );
		}

		return CdiBeanContainerBuilder.fromBeanManagerReference( beanManagerRef, serviceRegistry );
	}
	else {
		if ( isCdiAvailable ) {
			BeansMessageLogger.BEANS_LOGGER.noBeanManagerButCdiAvailable();
		}
	}

	return null;
}
 
Example 4
Source File: ImportSqlCommandExtractorInitiator.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public ImportSqlCommandExtractor initiateService(Map configurationValues, ServiceRegistryImplementor registry) {
	String extractorClassName = (String) configurationValues.get( Environment.HBM2DDL_IMPORT_FILES_SQL_EXTRACTOR );
	if ( StringHelper.isEmpty( extractorClassName ) ) {
		return DEFAULT_EXTRACTOR;
	}
	final ClassLoaderService classLoaderService = registry.getService( ClassLoaderService.class );
	return instantiateExplicitCommandExtractor( extractorClassName, classLoaderService );
}
 
Example 5
Source File: JdbcEnvironmentInitiator.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private JdbcConnectionAccess buildJdbcConnectionAccess(Map configValues, ServiceRegistryImplementor registry) {
	final MultiTenancyStrategy multiTenancyStrategy = MultiTenancyStrategy.determineMultiTenancyStrategy(
			configValues
	);
	if ( !multiTenancyStrategy.requiresMultiTenantConnectionProvider() ) {
		ConnectionProvider connectionProvider = registry.getService( ConnectionProvider.class );
		return new ConnectionProviderJdbcConnectionAccess( connectionProvider );
	}
	else {
		final MultiTenantConnectionProvider multiTenantConnectionProvider = registry.getService( MultiTenantConnectionProvider.class );
		return new MultiTenantConnectionProviderJdbcConnectionAccess( multiTenantConnectionProvider );
	}
}
 
Example 6
Source File: JdbcEnvironmentInitiator.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public static JdbcConnectionAccess buildBootstrapJdbcConnectionAccess(
		MultiTenancyStrategy multiTenancyStrategy,
		ServiceRegistryImplementor registry) {
	if ( !multiTenancyStrategy.requiresMultiTenantConnectionProvider() ) {
		ConnectionProvider connectionProvider = registry.getService( ConnectionProvider.class );
		return new ConnectionProviderJdbcConnectionAccess( connectionProvider );
	}
	else {
		final MultiTenantConnectionProvider multiTenantConnectionProvider = registry.getService( MultiTenantConnectionProvider.class );
		return new MultiTenantConnectionProviderJdbcConnectionAccess( multiTenantConnectionProvider );
	}
}
 
Example 7
Source File: CacheInitiator.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public CacheImplementor initiateService(
		SessionFactoryImplementor sessionFactory,
		SessionFactoryOptions sessionFactoryOptions,
		ServiceRegistryImplementor registry) {
	final RegionFactory regionFactory = registry.getService( RegionFactory.class );
	return ( ! NoCachingRegionFactory.class.isInstance( regionFactory ) )
			? new EnabledCaching( sessionFactory )
			: new DisabledCaching( sessionFactory );
}
 
Example 8
Source File: DataSourceBasedMultiTenantConnectionProviderImpl.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void injectServices(ServiceRegistryImplementor serviceRegistry) {
	final Object dataSourceConfigValue = serviceRegistry.getService( ConfigurationService.class )
			.getSettings()
			.get( AvailableSettings.DATASOURCE );
	if ( dataSourceConfigValue == null || ! String.class.isInstance( dataSourceConfigValue ) ) {
		throw new HibernateException( "Improper set up of DataSourceBasedMultiTenantConnectionProviderImpl" );
	}
	final String jndiName = (String) dataSourceConfigValue;

	jndiService = serviceRegistry.getService( JndiService.class );
	if ( jndiService == null ) {
		throw new HibernateException( "Could not locate JndiService from DataSourceBasedMultiTenantConnectionProviderImpl" );
	}

	final Object namedObject = jndiService.locate( jndiName );
	if ( namedObject == null ) {
		throw new HibernateException( "JNDI name [" + jndiName + "] could not be resolved" );
	}

	if ( DataSource.class.isInstance( namedObject ) ) {
		final int loc = jndiName.lastIndexOf( "/" );
		this.baseJndiNamespace = jndiName.substring( 0, loc );
		this.tenantIdentifierForAny = jndiName.substring( loc + 1 );
		dataSourceMap().put( tenantIdentifierForAny, (DataSource) namedObject );
	}
	else if ( Context.class.isInstance( namedObject ) ) {
		this.baseJndiNamespace = jndiName;
		this.tenantIdentifierForAny = (String) serviceRegistry.getService( ConfigurationService.class )
				.getSettings()
				.get( TENANT_IDENTIFIER_TO_USE_FOR_ANY_KEY );
		if ( tenantIdentifierForAny == null ) {
			throw new HibernateException( "JNDI name named a Context, but tenant identifier to use for ANY was not specified" );
		}
	}
	else {
		throw new HibernateException(
				"Unknown object type [" + namedObject.getClass().getName() +
						"] found in JNDI location [" + jndiName + "]"
		);
	}
}
 
Example 9
Source File: MultiTenantConnectionProviderInitiator.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
@SuppressWarnings( {"unchecked"})
public MultiTenantConnectionProvider initiateService(Map configurationValues, ServiceRegistryImplementor registry) {
	final MultiTenancyStrategy strategy = MultiTenancyStrategy.determineMultiTenancyStrategy(  configurationValues );
	if ( !strategy.requiresMultiTenantConnectionProvider() ) {
		// nothing to do, but given the separate hierarchies have to handle this here.
		return null;
	}

	final Object configValue = configurationValues.get( AvailableSettings.MULTI_TENANT_CONNECTION_PROVIDER );
	if ( configValue == null ) {
		// if they also specified the data source *name*, then lets assume they want
		// DataSourceBasedMultiTenantConnectionProviderImpl
		final Object dataSourceConfigValue = configurationValues.get( AvailableSettings.DATASOURCE );
		if ( dataSourceConfigValue != null && String.class.isInstance( dataSourceConfigValue ) ) {
			return new DataSourceBasedMultiTenantConnectionProviderImpl();
		}

		return null;
	}

	if ( MultiTenantConnectionProvider.class.isInstance( configValue ) ) {
		return (MultiTenantConnectionProvider) configValue;
	}
	else {
		final Class<MultiTenantConnectionProvider> implClass;
		if ( Class.class.isInstance( configValue ) ) {
			implClass = (Class) configValue;
		}
		else {
			final String className = configValue.toString();
			final ClassLoaderService classLoaderService = registry.getService( ClassLoaderService.class );
			try {
				implClass = classLoaderService.classForName( className );
			}
			catch (ClassLoadingException cle) {
				log.warn( "Unable to locate specified class [" + className + "]", cle );
				throw new ServiceException( "Unable to locate specified multi-tenant connection provider [" + className + "]" );
			}
		}

		try {
			return implClass.newInstance();
		}
		catch (Exception e) {
			log.warn( "Unable to instantiate specified class [" + implClass.getName() + "]", e );
			throw new ServiceException( "Unable to instantiate specified multi-tenant connection provider [" + implClass.getName() + "]" );
		}
	}
}
 
Example 10
Source File: JdbcEnvironmentImpl.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Constructor form used when the JDBC {@link java.sql.DatabaseMetaData} is not available.
 *
 * @param serviceRegistry The service registry
 * @param dialect The resolved dialect.
 */
public JdbcEnvironmentImpl(ServiceRegistryImplementor serviceRegistry, Dialect dialect) {
	this.dialect = dialect;

	final ConfigurationService cfgService = serviceRegistry.getService( ConfigurationService.class );

	NameQualifierSupport nameQualifierSupport = dialect.getNameQualifierSupport();
	if ( nameQualifierSupport == null ) {
		// assume both catalogs and schemas are supported
		nameQualifierSupport = NameQualifierSupport.BOTH;
	}
	this.nameQualifierSupport = nameQualifierSupport;

	this.sqlExceptionHelper = buildSqlExceptionHelper( dialect, logWarnings( cfgService, dialect ) );

	final IdentifierHelperBuilder identifierHelperBuilder = IdentifierHelperBuilder.from( this );
	identifierHelperBuilder.setGloballyQuoteIdentifiers( globalQuoting( cfgService ) );
	identifierHelperBuilder.setSkipGlobalQuotingForColumnDefinitions( globalQuotingSkippedForColumnDefinitions( cfgService ) );
	identifierHelperBuilder.setAutoQuoteKeywords( autoKeywordQuoting( cfgService ) );
	identifierHelperBuilder.setNameQualifierSupport( nameQualifierSupport );

	IdentifierHelper identifierHelper = null;
	ExtractedDatabaseMetaDataImpl.Builder dbMetaDataBuilder = new ExtractedDatabaseMetaDataImpl.Builder( this );
	try {
		identifierHelper = dialect.buildIdentifierHelper( identifierHelperBuilder, null );
		dbMetaDataBuilder.setSupportsNamedParameters( dialect.supportsNamedParameters( null ) );
	}
	catch (SQLException sqle) {
		// should never ever happen
		log.debug( "There was a problem accessing DatabaseMetaData in building the JdbcEnvironment", sqle );
	}
	if ( identifierHelper == null ) {
		identifierHelper = identifierHelperBuilder.build();
	}
	this.identifierHelper = identifierHelper;

	this.extractedMetaDataSupport = dbMetaDataBuilder.build();

	this.currentCatalog = identifierHelper.toIdentifier(
			cfgService.getSetting( AvailableSettings.DEFAULT_CATALOG, StandardConverters.STRING )
	);
	this.currentSchema = Identifier.toIdentifier(
			cfgService.getSetting( AvailableSettings.DEFAULT_SCHEMA, StandardConverters.STRING )
	);

	this.qualifiedObjectNameFormatter = new QualifiedObjectNameFormatterStandardImpl( nameQualifierSupport );

	this.lobCreatorBuilder = LobCreatorBuilderImpl.makeLobCreatorBuilder();
}
 
Example 11
Source File: JdbcEnvironmentImpl.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * The main constructor form.  Builds a JdbcEnvironment using the available DatabaseMetaData
 *
 * @param serviceRegistry The service registry
 * @param dialect The resolved dialect
 * @param databaseMetaData The available DatabaseMetaData
 *
 * @throws SQLException
 */
public JdbcEnvironmentImpl(
		ServiceRegistryImplementor serviceRegistry,
		Dialect dialect,
		DatabaseMetaData databaseMetaData) throws SQLException {
	this.dialect = dialect;

	final ConfigurationService cfgService = serviceRegistry.getService( ConfigurationService.class );

	this.sqlExceptionHelper = buildSqlExceptionHelper( dialect, logWarnings( cfgService, dialect ) );

	this.extractedMetaDataSupport = new ExtractedDatabaseMetaDataImpl.Builder( this )
			.apply( databaseMetaData )
			.setConnectionSchemaName( determineCurrentSchemaName( databaseMetaData, serviceRegistry, dialect ) )
			.setSupportsNamedParameters(dialect.supportsNamedParameters(databaseMetaData))
			.build();

	NameQualifierSupport nameQualifierSupport = dialect.getNameQualifierSupport();
	if ( nameQualifierSupport == null ) {
		nameQualifierSupport = determineNameQualifierSupport( databaseMetaData );
	}
	this.nameQualifierSupport = nameQualifierSupport;

	final IdentifierHelperBuilder identifierHelperBuilder = IdentifierHelperBuilder.from( this );
	identifierHelperBuilder.setGloballyQuoteIdentifiers( globalQuoting( cfgService ) );
	identifierHelperBuilder.setSkipGlobalQuotingForColumnDefinitions( globalQuotingSkippedForColumnDefinitions( cfgService ) );
	identifierHelperBuilder.setAutoQuoteKeywords( autoKeywordQuoting( cfgService ) );
	identifierHelperBuilder.setNameQualifierSupport( nameQualifierSupport );
	IdentifierHelper identifierHelper = null;
	try {
		identifierHelper = dialect.buildIdentifierHelper( identifierHelperBuilder, databaseMetaData );
	}
	catch (SQLException sqle) {
		// should never ever happen
		log.debug( "There was a problem accessing DatabaseMetaData in building the JdbcEnvironment", sqle );
	}
	if ( identifierHelper == null ) {
		identifierHelper = identifierHelperBuilder.build();
	}
	this.identifierHelper = identifierHelper;

	// and that current-catalog and current-schema happen after it
	this.currentCatalog = identifierHelper.toIdentifier( extractedMetaDataSupport.getConnectionCatalogName() );
	this.currentSchema = identifierHelper.toIdentifier( extractedMetaDataSupport.getConnectionSchemaName() );

	this.qualifiedObjectNameFormatter = new QualifiedObjectNameFormatterStandardImpl(
			nameQualifierSupport,
			databaseMetaData
	);

	this.typeInfoSet.addAll( TypeInfo.extractTypeInfo( databaseMetaData ) );

	this.lobCreatorBuilder = LobCreatorBuilderImpl.makeLobCreatorBuilder(
			cfgService.getSettings(),
			databaseMetaData.getConnection()
	);
}
 
Example 12
Source File: DialectFactoryImpl.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void injectServices(ServiceRegistryImplementor serviceRegistry) {
	this.strategySelector = serviceRegistry.getService( StrategySelector.class );
	this.dialectResolver = serviceRegistry.getService( DialectResolver.class );
}
 
Example 13
Source File: RegionFactoryInitiator.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@SuppressWarnings({"unchecked", "WeakerAccess"})
protected RegionFactory resolveRegionFactory(Map configurationValues, ServiceRegistryImplementor registry) {
	final Properties p = new Properties();
	p.putAll( configurationValues );

	final Boolean useSecondLevelCache = ConfigurationHelper.getBooleanWrapper(
			AvailableSettings.USE_SECOND_LEVEL_CACHE,
			configurationValues,
			null
	);
	final Boolean useQueryCache = ConfigurationHelper.getBooleanWrapper(
			AvailableSettings.USE_QUERY_CACHE,
			configurationValues,
			null
	);

	// We should immediately return NoCachingRegionFactory if either:
	//		1) both are explicitly FALSE
	//		2) USE_SECOND_LEVEL_CACHE is FALSE and USE_QUERY_CACHE is null
	if ( useSecondLevelCache != null && useSecondLevelCache == FALSE ) {
		if ( useQueryCache == null || useQueryCache == FALSE ) {
			return NoCachingRegionFactory.INSTANCE;
		}
	}

	final Object setting = configurationValues.get( AvailableSettings.CACHE_REGION_FACTORY );

	final StrategySelector selector = registry.getService( StrategySelector.class );
	final Collection<Class<? extends RegionFactory>> implementors = selector.getRegisteredStrategyImplementors( RegionFactory.class );

	if ( setting == null && implementors.size() != 1 ) {
		// if either are explicitly defined as TRUE we need a RegionFactory
		if ( ( useSecondLevelCache != null && useSecondLevelCache == TRUE )
				|| ( useQueryCache != null && useQueryCache == TRUE ) ) {
			throw new CacheException( "Caching was explicitly requested, but no RegionFactory was defined and there is not a single registered RegionFactory" );
		}
	}

	final RegionFactory regionFactory = registry.getService( StrategySelector.class ).resolveStrategy(
			RegionFactory.class,
			setting,
			(RegionFactory) null,
			new StrategyCreatorRegionFactoryImpl( p )
	);

	if ( regionFactory != null ) {
		return regionFactory;
	}


	final RegionFactory fallback = getFallback( configurationValues, registry );
	if ( fallback != null ) {
		return fallback;
	}

	if ( implementors.size() == 1 ) {
		final RegionFactory registeredFactory = selector.resolveStrategy( RegionFactory.class, implementors.iterator().next() );
		configurationValues.put( AvailableSettings.CACHE_REGION_FACTORY, registeredFactory );
		configurationValues.put( AvailableSettings.USE_SECOND_LEVEL_CACHE, "true" );

		return registeredFactory;
	}
	else {
		LOG.debugf(
				"Cannot default RegionFactory based on registered strategies as `%s` RegionFactory strategies were registered",
				implementors
		);
	}

	return NoCachingRegionFactory.INSTANCE;
}
 
Example 14
Source File: IgniteDatastoreProvider.java    From hibernate-ogm-ignite with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public void injectServices(ServiceRegistryImplementor serviceRegistryImplementor) {
	this.classLoaderService = serviceRegistryImplementor.getService( ClassLoaderService.class );
	this.jtaPlatform = serviceRegistryImplementor.getService( JtaPlatform.class );
	this.jdbcServices = serviceRegistryImplementor.getService( JdbcServices.class );
}