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

The following examples show how to use org.hibernate.service.spi.SessionFactoryServiceRegistry#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: ReactiveIntegrator.java    From hibernate-reactive with GNU Lesser General Public License v2.1 6 votes vote down vote up
private void attachEventContextManagingListenersIfRequired(SessionFactoryServiceRegistry serviceRegistry) {
	if ( ReactiveModeCheck.isReactiveRegistry( serviceRegistry ) ) {

		CoreLogging.messageLogger(ReactiveIntegrator.class).info("HRX000001: Hibernate Reactive Preview");

		EventListenerRegistry eventListenerRegistry = serviceRegistry.getService( EventListenerRegistry.class );
		eventListenerRegistry.addDuplicationStrategy( ReplacementDuplicationStrategy.INSTANCE );

		eventListenerRegistry.getEventListenerGroup( EventType.AUTO_FLUSH ).appendListener( new DefaultReactiveAutoFlushEventListener() );
		eventListenerRegistry.getEventListenerGroup( EventType.FLUSH ).appendListener( new DefaultReactiveFlushEventListener() );
		eventListenerRegistry.getEventListenerGroup( EventType.FLUSH_ENTITY ).appendListener( new DefaultReactiveFlushEntityEventListener() );
		eventListenerRegistry.getEventListenerGroup( EventType.PERSIST ).appendListener( new DefaultReactivePersistEventListener() );
		eventListenerRegistry.getEventListenerGroup( EventType.PERSIST_ONFLUSH ).appendListener( new DefaultReactivePersistOnFlushEventListener() );
		eventListenerRegistry.getEventListenerGroup( EventType.MERGE ).appendListener( new DefaultReactiveMergeEventListener() );
		eventListenerRegistry.getEventListenerGroup( EventType.DELETE ).appendListener( new DefaultReactiveDeleteEventListener() );
		eventListenerRegistry.getEventListenerGroup( EventType.REFRESH ).appendListener( new DefaultReactiveRefreshEventListener() );
		eventListenerRegistry.getEventListenerGroup( EventType.LOCK ).appendListener( new DefaultReactiveLockEventListener() );
		eventListenerRegistry.getEventListenerGroup( EventType.LOAD ).appendListener( new DefaultReactiveLoadEventListener() );
		eventListenerRegistry.getEventListenerGroup( EventType.INIT_COLLECTION ).appendListener( new DefaultReactiveInitializeCollectionEventListener() );
	}
}
 
Example 2
Source File: SessionFactoryImpl.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private void applyCfgXmlValues(LoadedConfig aggregatedConfig, SessionFactoryServiceRegistry serviceRegistry) {
	final JaccService jaccService = serviceRegistry.getService( JaccService.class );
	if ( jaccService.getContextId() != null ) {
		final JaccPermissionDeclarations permissions = aggregatedConfig.getJaccPermissions( jaccService.getContextId() );
		if ( permissions != null ) {
			for ( GrantedPermission grantedPermission : permissions.getPermissionDeclarations() ) {
				jaccService.addPermission( grantedPermission );
			}
		}
	}

	if ( aggregatedConfig.getEventListenerMap() != null ) {
		final ClassLoaderService cls = serviceRegistry.getService( ClassLoaderService.class );
		final EventListenerRegistry eventListenerRegistry = serviceRegistry.getService( EventListenerRegistry.class );
		for ( Map.Entry<EventType, Set<String>> entry : aggregatedConfig.getEventListenerMap().entrySet() ) {
			final EventListenerGroup group = eventListenerRegistry.getEventListenerGroup( entry.getKey() );
			for ( String listenerClassName : entry.getValue() ) {
				try {
					group.appendListener( cls.classForName( listenerClassName ).newInstance() );
				}
				catch (Exception e) {
					throw new ConfigurationException( "Unable to instantiate event listener class : " + listenerClassName, e );
				}
			}
		}
	}
}
 
Example 3
Source File: JaccIntegrator.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private void doIntegration(
		Map properties,
		JaccPermissionDeclarations permissionDeclarations,
		SessionFactoryServiceRegistry serviceRegistry) {
	boolean isSecurityEnabled = properties.containsKey( AvailableSettings.JACC_ENABLED );
	if ( ! isSecurityEnabled ) {
		log.debug( "Skipping JACC integration as it was not enabled" );
		return;
	}

	final String contextId = (String) properties.get( AvailableSettings.JACC_CONTEXT_ID );
	if ( contextId == null ) {
		throw new IntegrationException( "JACC context id must be specified" );
	}

	final JaccService jaccService = serviceRegistry.getService( JaccService.class );
	if ( jaccService == null ) {
		throw new IntegrationException( "JaccService was not set up" );
	}

	if ( permissionDeclarations != null ) {
		for ( GrantedPermission declaration : permissionDeclarations.getPermissionDeclarations() ) {
			jaccService.addPermission( declaration );
		}
	}

	final EventListenerRegistry eventListenerRegistry = serviceRegistry.getService( EventListenerRegistry.class );
	eventListenerRegistry.addDuplicationStrategy( DUPLICATION_STRATEGY );

	eventListenerRegistry.prependListeners( EventType.PRE_DELETE, new JaccPreDeleteEventListener() );
	eventListenerRegistry.prependListeners( EventType.PRE_INSERT, new JaccPreInsertEventListener() );
	eventListenerRegistry.prependListeners( EventType.PRE_UPDATE, new JaccPreUpdateEventListener() );
	eventListenerRegistry.prependListeners( EventType.PRE_LOAD, new JaccPreLoadEventListener() );
}
 
Example 4
Source File: CollectionCacheInvalidator.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private void integrate(SessionFactoryServiceRegistry serviceRegistry, SessionFactoryImplementor sessionFactory) {
	if ( !sessionFactory.getSessionFactoryOptions().isAutoEvictCollectionCache() ) {
		// feature is disabled
		return;
	}
	if ( !sessionFactory.getSessionFactoryOptions().isSecondLevelCacheEnabled() ) {
		// Nothing to do, if caching is disabled
		return;
	}
	EventListenerRegistry eventListenerRegistry = serviceRegistry.getService( EventListenerRegistry.class );
	eventListenerRegistry.appendListeners( EventType.POST_INSERT, this );
	eventListenerRegistry.appendListeners( EventType.POST_DELETE, this );
	eventListenerRegistry.appendListeners( EventType.POST_UPDATE, this );
}
 
Example 5
Source File: OptimisticLockingChildUpdatesRootVersionTest.java    From high-performance-java-persistence with Apache License 2.0 5 votes vote down vote up
@Override
public void integrate(
        Metadata metadata,
        SessionFactoryImplementor sessionFactory,
        SessionFactoryServiceRegistry serviceRegistry) {

    final EventListenerRegistry eventListenerRegistry =
            serviceRegistry.getService( EventListenerRegistry.class );

    eventListenerRegistry.appendListeners(EventType.PERSIST, RootAwareInsertEventListener.INSTANCE);
    eventListenerRegistry.appendListeners(EventType.FLUSH_ENTITY, RootAwareUpdateAndDeleteEventListener.INSTANCE);
}
 
Example 6
Source File: OptimisticLockingBidirectionalChildUpdatesRootVersionTest.java    From high-performance-java-persistence with Apache License 2.0 5 votes vote down vote up
@Override
public void integrate(
        Metadata metadata,
        SessionFactoryImplementor sessionFactory,
        SessionFactoryServiceRegistry serviceRegistry) {

    final EventListenerRegistry eventListenerRegistry =
            serviceRegistry.getService( EventListenerRegistry.class );

    eventListenerRegistry.appendListeners( EventType.PERSIST, RootAwareInsertEventListener.INSTANCE);
    eventListenerRegistry.appendListeners( EventType.FLUSH_ENTITY, RootAwareUpdateAndDeleteEventListener.INSTANCE);
}
 
Example 7
Source File: EntityReplicationTest.java    From high-performance-java-persistence with Apache License 2.0 5 votes vote down vote up
@Override
public void integrate(
        Metadata metadata,
        SessionFactoryImplementor sessionFactory,
        SessionFactoryServiceRegistry serviceRegistry) {

    final EventListenerRegistry eventListenerRegistry =
            serviceRegistry.getService(EventListenerRegistry.class);

    eventListenerRegistry.appendListeners(EventType.POST_INSERT, ReplicationInsertEventListener.INSTANCE);
    eventListenerRegistry.appendListeners(EventType.POST_UPDATE, ReplicationUpdateEventListener.INSTANCE);
    eventListenerRegistry.appendListeners(EventType.PRE_DELETE, ReplicationDeleteEventListener.INSTANCE);
}
 
Example 8
Source File: EventListenerIntegrator.java    From high-performance-java-persistence with Apache License 2.0 5 votes vote down vote up
@Override
public void integrate(
    Metadata metadata,
    SessionFactoryImplementor sessionFactory,
    SessionFactoryServiceRegistry serviceRegistry) {

    final EventListenerRegistry eventListenerRegistry =
        serviceRegistry.getService(EventListenerRegistry.class);

    eventListenerRegistry.appendListeners(
        EventType.POST_LOAD,
        AuditLogPostLoadEventListener.INSTANCE
    );
}
 
Example 9
Source File: EventIntegrator.java    From micronaut-data with Apache License 2.0 4 votes vote down vote up
@Override
public void integrate(
        Metadata metadata,
        SessionFactoryImplementor sessionFactory,
        SessionFactoryServiceRegistry serviceRegistry) {
    EventListenerRegistry eventListenerRegistry =
            serviceRegistry.getService(EventListenerRegistry.class);

    Collection<PersistentClass> entityBindings = metadata.getEntityBindings();
    Map<Class, BeanProperty> lastUpdates = new HashMap<>(entityBindings.size());
    Map<Class, BeanProperty> dateCreated = new HashMap<>(entityBindings.size());

    entityBindings.forEach(e -> {
                Class<?> mappedClass = e.getMappedClass();
                if (mappedClass != null) {
                    BeanIntrospection<?> introspection = BeanIntrospector.SHARED.findIntrospection(mappedClass).orElse(null);
                    if (introspection != null) {
                        introspection.getIndexedProperty(DateCreated.class).ifPresent(bp ->
                                dateCreated.put(mappedClass, bp)
                        );
                        introspection.getIndexedProperty(DateUpdated.class).ifPresent(bp ->
                                lastUpdates.put(mappedClass, bp)
                        );
                    }
                }
            }
    );

    ConversionService<?> conversionService = ConversionService.SHARED;

    if (CollectionUtils.isNotEmpty(dateCreated)) {

        eventListenerRegistry.getEventListenerGroup(EventType.PRE_INSERT)
                .appendListener((PreInsertEventListener) event -> {
                    Object[] state = event.getState();
                    timestampIfNecessary(
                            dateCreated,
                            lastUpdates,
                            conversionService,
                            event,
                            state,
                            true
                    );
                    return false;
                });
    }

    if (CollectionUtils.isNotEmpty(lastUpdates)) {

        eventListenerRegistry.getEventListenerGroup(EventType.PRE_UPDATE)
                .appendListener((PreUpdateEventListener) event -> {
                    timestampIfNecessary(
                            dateCreated, lastUpdates,
                            conversionService,
                            event,
                            event.getState(),
                            false
                    );
                    return false;
                });
    }
}