Java Code Examples for org.hibernate.event.spi.EventType

The following are top voted examples for showing how to use org.hibernate.event.spi.EventType. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: lams   File: EntityUpdateAction.java   Source Code and License 7 votes vote down vote up
private boolean preUpdate() {
	boolean veto = false;
	final EventListenerGroup<PreUpdateEventListener> listenerGroup = listenerGroup( EventType.PRE_UPDATE );
	if ( listenerGroup.isEmpty() ) {
		return veto;
	}
	final PreUpdateEvent event = new PreUpdateEvent(
			getInstance(),
			getId(),
			state,
			previousState,
			getPersister(),
			eventSource()
	);
	for ( PreUpdateEventListener listener : listenerGroup.listeners() ) {
		veto |= listener.onPreUpdate( event );
	}
	return veto;
}
 
Example 2
Project: lams   File: TwoPhaseLoad.java   Source Code and License 6 votes vote down vote up
/**
 * PostLoad cannot occur during initializeEntity, as that call occurs *before*
 * the Set collections are added to the persistence context by Loader.
 * Without the split, LazyInitializationExceptions can occur in the Entity's
 * postLoad if it acts upon the collection.
 *
 * HHH-6043
 * 
 * @param entity The entity
 * @param session The Session
 * @param postLoadEvent The (re-used) post-load event
 */
public static void postLoad(
		final Object entity,
		final SessionImplementor session,
		final PostLoadEvent postLoadEvent) {
	
	if ( session.isEventSource() ) {
		final PersistenceContext persistenceContext
				= session.getPersistenceContext();
		final EntityEntry entityEntry = persistenceContext.getEntry( entity );

		postLoadEvent.setEntity( entity ).setId( entityEntry.getId() ).setPersister( entityEntry.getPersister() );

		final EventListenerGroup<PostLoadEventListener> listenerGroup = session.getFactory()
						.getServiceRegistry()
						.getService( EventListenerRegistry.class )
						.getEventListenerGroup( EventType.POST_LOAD );
		for ( PostLoadEventListener listener : listenerGroup.listeners() ) {
			listener.onPostLoad( postLoadEvent );
		}
	}
}
 
Example 3
Project: lams   File: SessionImpl.java   Source Code and License 6 votes vote down vote up
@Override
public boolean isDirty() throws HibernateException {
	errorIfClosed();
	checkTransactionSynchStatus();
	LOG.debug( "Checking session dirtiness" );
	if ( actionQueue.areInsertionsOrDeletionsQueued() ) {
		LOG.debug( "Session dirty (scheduled updates and insertions)" );
		return true;
	}
	DirtyCheckEvent event = new DirtyCheckEvent( this );
	for ( DirtyCheckEventListener listener : listeners( EventType.DIRTY_CHECK ) ) {
		listener.onDirtyCheck( event );
	}
	delayedAfterCompletion();
	return event.isDirty();
}
 
Example 4
Project: lams   File: EventListenerGroupImpl.java   Source Code and License 6 votes vote down vote up
public EventListenerGroupImpl(EventType<T> eventType) {
	this.eventType = eventType;
	duplicationStrategies.add(
			// At minimum make sure we do not register the same exact listener class multiple times.
			new DuplicationStrategy() {
				@Override
				public boolean areMatch(Object listener, Object original) {
					return listener.getClass().equals( original.getClass() );
				}

				@Override
				public Action getAction() {
					return Action.ERROR;
				}
			}
	);
}
 
Example 5
Project: lams   File: EventListenerRegistryImpl.java   Source Code and License 6 votes vote down vote up
private static <T> void prepareListeners(EventType<T> type, T defaultListener, Map<EventType,EventListenerGroupImpl> map) {
	final EventListenerGroupImpl<T> listenerGroup;
	if ( type == EventType.POST_COMMIT_DELETE
			|| type == EventType.POST_COMMIT_INSERT
			|| type == EventType.POST_COMMIT_UPDATE ) {
		listenerGroup = new PostCommitEventListenerGroupImpl<T>( type );
	}
	else {
		listenerGroup = new EventListenerGroupImpl<T>( type );
	}

	if ( defaultListener != null ) {
		listenerGroup.appendListener( defaultListener );
	}
	map.put( type, listenerGroup  );
}
 
Example 6
Project: lams   File: PostCommitEventListenerGroupImpl.java   Source Code and License 6 votes vote down vote up
public PostCommitEventListenerGroupImpl(EventType<T> eventType) {
	super( eventType );

	if ( eventType == EventType.POST_COMMIT_DELETE ) {
		this.extendedListenerContract = PostCommitDeleteEventListener.class;
	}
	else if ( eventType == EventType.POST_COMMIT_INSERT ) {
		this.extendedListenerContract = PostCommitInsertEventListener.class;
	}
	else if ( eventType == EventType.POST_COMMIT_UPDATE ) {
		this.extendedListenerContract = PostCommitUpdateEventListener.class;
	}
	else {
		throw new IllegalStateException( "Unexpected usage of PostCommitEventListenerGroupImpl" );
	}
}
 
Example 7
Project: lams   File: EntityDeleteAction.java   Source Code and License 6 votes vote down vote up
private void postDelete() {
	final EventListenerGroup<PostDeleteEventListener> listenerGroup = listenerGroup( EventType.POST_DELETE );
	if ( listenerGroup.isEmpty() ) {
		return;
	}
	final PostDeleteEvent event = new PostDeleteEvent(
			getInstance(),
			getId(),
			state,
			getPersister(),
			eventSource()
	);
	for ( PostDeleteEventListener listener : listenerGroup.listeners() ) {
		listener.onPostDelete( event );
	}
}
 
Example 8
Project: lams   File: EntityInsertAction.java   Source Code and License 6 votes vote down vote up
private void postInsert() {
	final EventListenerGroup<PostInsertEventListener> listenerGroup = listenerGroup( EventType.POST_INSERT );
	if ( listenerGroup.isEmpty() ) {
		return;
	}
	final PostInsertEvent event = new PostInsertEvent(
			getInstance(),
			getId(),
			getState(),
			getPersister(),
			eventSource()
	);
	for ( PostInsertEventListener listener : listenerGroup.listeners() ) {
		listener.onPostInsert( event );
	}
}
 
Example 9
Project: lams   File: EntityIdentityInsertAction.java   Source Code and License 6 votes vote down vote up
private void postInsert() {
	if ( isDelayed ) {
		getSession().getPersistenceContext().replaceDelayedEntityIdentityInsertKeys( delayedEntityKey, generatedId );
	}

	final EventListenerGroup<PostInsertEventListener> listenerGroup = listenerGroup( EventType.POST_INSERT );
	if ( listenerGroup.isEmpty() ) {
		return;
	}
	final PostInsertEvent event = new PostInsertEvent(
			getInstance(),
			generatedId,
			getState(),
			getPersister(),
			eventSource()
	);
	for ( PostInsertEventListener listener : listenerGroup.listeners() ) {
		listener.onPostInsert( event );
	}
}
 
Example 10
Project: lams   File: EntityUpdateAction.java   Source Code and License 6 votes vote down vote up
private void postUpdate() {
	final EventListenerGroup<PostUpdateEventListener> listenerGroup = listenerGroup( EventType.POST_UPDATE );
	if ( listenerGroup.isEmpty() ) {
		return;
	}
	final PostUpdateEvent event = new PostUpdateEvent(
			getInstance(),
			getId(),
			state,
			previousState,
			dirtyFields,
			getPersister(),
			eventSource()
	);
	for ( PostUpdateEventListener listener : listenerGroup.listeners() ) {
		listener.onPostUpdate( event );
	}
}
 
Example 11
Project: spring-data-rest-webhook   File: EventListenerRegistryAdapter.java   Source Code and License 6 votes vote down vote up
@PostConstruct
public void registerListeners() {
    // TODO: This method of getting a reference to the SessionFactory (and thereforce the ServiceRegistry) is Deprecated. Find out the right Hibernate 5.2 way to do this.
    EventListenerRegistry registry = entityManagerFactory.getSessionFactory().getServiceRegistry().getService(EventListenerRegistry.class);
    if (preInsertEventListeners != null) {
        registry.appendListeners(EventType.PRE_INSERT, preInsertEventListeners.toArray(new PreInsertEventListener[preInsertEventListeners.size()]));
    }
    if (postInsertEventListeners != null) {
        registry.appendListeners(EventType.POST_INSERT, postInsertEventListeners.toArray(new PostInsertEventListener[postInsertEventListeners.size()]));
    }
    if (preUpdateEventListeners != null) {
        registry.appendListeners(EventType.PRE_UPDATE, preUpdateEventListeners.toArray(new PreUpdateEventListener[preUpdateEventListeners.size()]));
    }
    if (postUpdateEventListeners != null) {
        registry.appendListeners(EventType.POST_UPDATE, postUpdateEventListeners.toArray(new PostUpdateEventListener[postUpdateEventListeners.size()]));
    }
    if (preDeleteEventListeners != null) {
        registry.appendListeners(EventType.PRE_DELETE, preDeleteEventListeners.toArray(new PreDeleteEventListener[preDeleteEventListeners.size()]));
    }
    if (postDeleteEventListeners != null) {
        registry.appendListeners(EventType.POST_DELETE, postDeleteEventListeners.toArray(new PostDeleteEventListener[postDeleteEventListeners.size()]));
    }
}
 
Example 12
Project: ipf-flow-manager   File: CustomEventRegistration.java   Source Code and License 6 votes vote down vote up
@Override
public void integrate(Metadata metadata, SessionFactoryImplementor sessionFactory, SessionFactoryServiceRegistry serviceRegistry) {
    EventListenerRegistry service = serviceRegistry.getService(org.hibernate.event.service.spi.EventListenerRegistry.class);

    StandardPBEStringEncryptor encrypt = new StandardPBEStringEncryptor();
    encrypt.setPassword("test_password");
    RenderedMessageEncryptEventListener encryptListener = new RenderedMessageEncryptEventListener();
    encryptListener.setStringEncryptor(encrypt);

    RenderedMessageDecryptEventListener decryptListener = new RenderedMessageDecryptEventListener();
    decryptListener.setStringEncryptor(encrypt);

    FullTextIndexEventListener fullTextListener = new FullTextIndexEventListener();

    service.appendListeners(EventType.PRE_UPDATE, encryptListener);
    service.prependListeners(EventType.POST_UPDATE, decryptListener);
    service.appendListeners(EventType.PRE_INSERT, encryptListener);
    service.prependListeners(EventType.POST_INSERT, decryptListener);
    service.appendListeners(EventType.POST_LOAD, decryptListener);
}
 
Example 13
Project: Audit4j-Hibernate   File: Audit4jIntegrator.java   Source Code and License 6 votes vote down vote up
public void integrate(Configuration configuration, SessionFactoryImplementor sessionFactory,
        SessionFactoryServiceRegistry serviceRegistry) {
    System.out.println("Integrating......");

    //final AuditService auditService = serviceRegistry.getService(AuditService.class);
    final AuditService auditService = new AuditServiceImpl();
    auditService.init();
    if (!auditService.isInitialized()) {
        throw new InitializationException(
                "Audit4j hibernate integration can not be initialized..");
    }

    // Register listeners..
    final EventListenerRegistry listenerRegistry = serviceRegistry
            .getService(EventListenerRegistry.class);

    listenerRegistry.appendListeners(EventType.POST_INSERT,
            new AuditPostInsertEventListenerImpl(auditService));
    listenerRegistry.appendListeners(EventType.POST_UPDATE,
            new AuditPostUpdateEventListenerImpl(auditService));
    listenerRegistry.appendListeners(EventType.POST_DELETE,
            new AuditPostDeleteEventListenerImpl(auditService));
}
 
Example 14
Project: gorm-hibernate5   File: EventListenerIntegrator.java   Source Code and License 6 votes vote down vote up
protected <T> void appendListeners(EventListenerRegistry listenerRegistry,
                                   EventType<T> eventType, Collection<T> listeners) {

    EventListenerGroup<T> group = listenerRegistry.getEventListenerGroup(eventType);
    for (T listener : listeners) {
        if (listener != null) {
            if(shouldOverrideListeners(eventType, listener)) {
                // since ClosureEventTriggeringInterceptor extends DefaultSaveOrUpdateEventListener we want to override instead of append the listener here
                // to avoid there being 2 implementations which would impact performance too
                group.clear();
                group.appendListener(listener);
            }
            else {
                group.appendListener(listener);
            }
        }
    }
}
 
Example 15
Project: gorm-hibernate5   File: EventListenerIntegrator.java   Source Code and License 6 votes vote down vote up
@SuppressWarnings("unchecked")
protected <T> void appendListeners(final EventListenerRegistry listenerRegistry,
                                   final EventType<T> eventType, final Map<String, Object> listeners) {

    Object listener = listeners.get(eventType.eventName());
    if (listener != null) {
        if(shouldOverrideListeners(eventType, listener)) {
            // since ClosureEventTriggeringInterceptor extends DefaultSaveOrUpdateEventListener we want to override instead of append the listener here
            // to avoid there being 2 implementations which would impact performance too
            listenerRegistry.setListeners(eventType, (T) listener);
        }
        else {
            listenerRegistry.appendListeners(eventType, (T)listener);
        }
    }
}
 
Example 16
Project: windup-rulesets   File: MyIntegrator.java   Source Code and License 6 votes vote down vote up
@Override
public void integrate(org.hibernate.cfg.Configuration configuration,
            org.hibernate.engine.spi.SessionFactoryImplementor sessionFactory,
            org.hibernate.service.spi.SessionFactoryServiceRegistry serviceRegistry) 
{
        
    
    final EventListenerRegistry eventRegistry =
        serviceRegistry.getService(EventListenerRegistry.class);

    eventRegistry.prependListeners(EventType.POST_COMMIT_INSERT, listener);
    
    final AuditConfiguration enversConfiguration = AuditConfiguration.getFor( configuration );
    AuditStrategy auditStrategy = enversConfiguration.getAuditStrategy();

    if (enversConfiguration.getEntCfg().hasAuditedEntities()) {
        listenerRegistry.appendListeners( EventType.POST_DELETE, new EnversPostDeleteEventListenerImpl( enversConfiguration ) );
        listenerRegistry.appendListeners( EventType.POST_INSERT, new EnversPostInsertEventListenerImpl( enversConfiguration ) );
        listenerRegistry.appendListeners( EventType.POST_UPDATE, new EnversPostUpdateEventListenerImpl( enversConfiguration ) );
        listenerRegistry.appendListeners( EventType.POST_COLLECTION_RECREATE, new EnversPostCollectionRecreateEventListenerImpl( enversConfiguration ) );
        listenerRegistry.appendListeners( EventType.PRE_COLLECTION_REMOVE, new EnversPreCollectionRemoveEventListenerImpl( enversConfiguration ) );
        listenerRegistry.appendListeners( EventType.PRE_COLLECTION_UPDATE, new EnversPreCollectionUpdateEventListenerImpl( enversConfiguration ) );
    }
    
}
 
Example 17
Project: debop4j   File: HibernateTool.java   Source Code and License 6 votes vote down vote up
/**
 * Register event listener.
 *
 * @param sessionFactory the session factory
 * @param listener       the listener
 * @param eventTypes     the event types
 */
@SuppressWarnings("unchecked")
public static void registerEventListener(SessionFactory sessionFactory, Object listener, EventType... eventTypes) {
    shouldNotBeNull(sessionFactory, "sessionFactory");
    shouldNotBeNull(listener, "listener");


        log.trace("sessionFactory에 event listener를 등록합니다... listener=[{}], eventTypes=[{}]",
                  listener, StringTool.listToString(eventTypes));

    EventListenerRegistry registry =
            ((SessionFactoryImpl) sessionFactory)
                    .getServiceRegistry()
                    .getService(EventListenerRegistry.class);

    for (EventType eventType : eventTypes) {
        try {
            registry.getEventListenerGroup(eventType).appendListener(listener);
        } catch (Exception ignored) {}
    }

}
 
Example 18
Project: lams   File: TypeSafeActivator.java   Source Code and License 5 votes vote down vote up
@SuppressWarnings( {"UnusedDeclaration"})
public static void applyCallbackListeners(ValidatorFactory validatorFactory, ActivationContext activationContext) {
	final Set<ValidationMode> modes = activationContext.getValidationModes();
	if ( ! ( modes.contains( ValidationMode.CALLBACK ) || modes.contains( ValidationMode.AUTO ) ) ) {
		return;
	}

	// de-activate not-null tracking at the core level when Bean Validation is present unless the user explicitly
	// asks for it
	if ( activationContext.getConfiguration().getProperty( Environment.CHECK_NULLABILITY ) == null ) {
		activationContext.getSessionFactory().getSettings().setCheckNullability( false );
	}

	final BeanValidationEventListener listener = new BeanValidationEventListener(
			validatorFactory,
			activationContext.getConfiguration().getProperties()
	);

	final EventListenerRegistry listenerRegistry = activationContext.getServiceRegistry()
			.getService( EventListenerRegistry.class );

	listenerRegistry.addDuplicationStrategy( DuplicationStrategyImpl.INSTANCE );

	listenerRegistry.appendListeners( EventType.PRE_INSERT, listener );
	listenerRegistry.appendListeners( EventType.PRE_UPDATE, listener );
	listenerRegistry.appendListeners( EventType.PRE_DELETE, listener );

	listener.initialize( activationContext.getConfiguration() );
}
 
Example 19
Project: lams   File: AbstractEntityTuplizer.java   Source Code and License 5 votes vote down vote up
private static Iterable<PersistEventListener> persistEventListeners(SessionImplementor session) {
	return session
			.getFactory()
			.getServiceRegistry()
			.getService( EventListenerRegistry.class )
			.getEventListenerGroup( EventType.PERSIST )
			.listeners();
}
 
Example 20
Project: lams   File: SessionImpl.java   Source Code and License 5 votes vote down vote up
private void internalClear() {
	persistenceContext.clear();
	actionQueue.clear();

	final ClearEvent event = new ClearEvent( this );
	for ( ClearEventListener listener : listeners( EventType.CLEAR ) ) {
		listener.onClear( event );
	}
}
 
Example 21
Project: lams   File: SessionImpl.java   Source Code and License 5 votes vote down vote up
private void fireSaveOrUpdate(SaveOrUpdateEvent event) {
	errorIfClosed();
	checkTransactionSynchStatus();
	checkNoUnresolvedActionsBeforeOperation();
	for ( SaveOrUpdateEventListener listener : listeners( EventType.SAVE_UPDATE ) ) {
		listener.onSaveOrUpdate( event );
	}
	checkNoUnresolvedActionsAfterOperation();
}
 
Example 22
Project: lams   File: SessionImpl.java   Source Code and License 5 votes vote down vote up
private Serializable fireSave(SaveOrUpdateEvent event) {
	errorIfClosed();
	checkTransactionSynchStatus();
	checkNoUnresolvedActionsBeforeOperation();
	for ( SaveOrUpdateEventListener listener : listeners( EventType.SAVE ) ) {
		listener.onSaveOrUpdate( event );
	}
	checkNoUnresolvedActionsAfterOperation();
	return event.getResultId();
}
 
Example 23
Project: lams   File: SessionImpl.java   Source Code and License 5 votes vote down vote up
private void fireUpdate(SaveOrUpdateEvent event) {
	errorIfClosed();
	checkTransactionSynchStatus();
	checkNoUnresolvedActionsBeforeOperation();
	for ( SaveOrUpdateEventListener listener : listeners( EventType.UPDATE ) ) {
		listener.onSaveOrUpdate( event );
	}
	checkNoUnresolvedActionsAfterOperation();
}
 
Example 24
Project: lams   File: SessionImpl.java   Source Code and License 5 votes vote down vote up
private void fireLock(LockEvent event) {
	errorIfClosed();
	checkTransactionSynchStatus();
	for ( LockEventListener listener : listeners( EventType.LOCK ) ) {
		listener.onLock( event );
	}
	delayedAfterCompletion();
}
 
Example 25
Project: lams   File: SessionImpl.java   Source Code and License 5 votes vote down vote up
private void firePersist(Map copiedAlready, PersistEvent event) {
	errorIfClosed();
	checkTransactionSynchStatus();
	for ( PersistEventListener listener : listeners( EventType.PERSIST ) ) {
		listener.onPersist( event, copiedAlready );
	}
	delayedAfterCompletion();
}
 
Example 26
Project: lams   File: SessionImpl.java   Source Code and License 5 votes vote down vote up
private void firePersist(PersistEvent event) {
	errorIfClosed();
	checkTransactionSynchStatus();
	checkNoUnresolvedActionsBeforeOperation();
	for ( PersistEventListener listener : listeners( EventType.PERSIST ) ) {
		listener.onPersist( event );
	}
	checkNoUnresolvedActionsAfterOperation();
}
 
Example 27
Project: lams   File: SessionImpl.java   Source Code and License 5 votes vote down vote up
private void firePersistOnFlush(Map copiedAlready, PersistEvent event) {
	errorIfClosed();
	checkTransactionSynchStatus();
	for ( PersistEventListener listener : listeners( EventType.PERSIST_ONFLUSH ) ) {
		listener.onPersist( event, copiedAlready );
	}
	delayedAfterCompletion();
}
 
Example 28
Project: lams   File: SessionImpl.java   Source Code and License 5 votes vote down vote up
private void firePersistOnFlush(PersistEvent event) {
	errorIfClosed();
	checkTransactionSynchStatus();
	checkNoUnresolvedActionsBeforeOperation();
	for ( PersistEventListener listener : listeners( EventType.PERSIST_ONFLUSH ) ) {
		listener.onPersist( event );
	}
	checkNoUnresolvedActionsAfterOperation();
}
 
Example 29
Project: lams   File: SessionImpl.java   Source Code and License 5 votes vote down vote up
private Object fireMerge(MergeEvent event) {
	errorIfClosed();
	checkTransactionSynchStatus();
	checkNoUnresolvedActionsBeforeOperation();
	for ( MergeEventListener listener : listeners( EventType.MERGE ) ) {
		listener.onMerge( event );
	}
	checkNoUnresolvedActionsAfterOperation();
	return event.getResult();
}
 
Example 30
Project: lams   File: SessionImpl.java   Source Code and License 5 votes vote down vote up
private void fireMerge(Map copiedAlready, MergeEvent event) {
	errorIfClosed();
	checkTransactionSynchStatus();
	for ( MergeEventListener listener : listeners( EventType.MERGE ) ) {
		listener.onMerge( event, copiedAlready );
	}
	delayedAfterCompletion();
}
 
Example 31
Project: lams   File: SessionImpl.java   Source Code and License 5 votes vote down vote up
private void fireDelete(DeleteEvent event) {
	errorIfClosed();
	checkTransactionSynchStatus();
	for ( DeleteEventListener listener : listeners( EventType.DELETE ) ) {
		listener.onDelete( event );
	}
	delayedAfterCompletion();
}
 
Example 32
Project: lams   File: SessionImpl.java   Source Code and License 5 votes vote down vote up
private void fireDelete(DeleteEvent event, Set transientEntities) {
	errorIfClosed();
	checkTransactionSynchStatus();
	for ( DeleteEventListener listener : listeners( EventType.DELETE ) ) {
		listener.onDelete( event, transientEntities );
	}
	delayedAfterCompletion();
}
 
Example 33
Project: lams   File: SessionImpl.java   Source Code and License 5 votes vote down vote up
private void fireLoad(LoadEvent event, LoadType loadType) {
	errorIfClosed();
	checkTransactionSynchStatus();
	for ( LoadEventListener listener : listeners( EventType.LOAD ) ) {
		listener.onLoad( event, loadType );
	}
	delayedAfterCompletion();
}
 
Example 34
Project: lams   File: SessionImpl.java   Source Code and License 5 votes vote down vote up
private void fireResolveNaturalId(ResolveNaturalIdEvent event) {
	errorIfClosed();
	checkTransactionSynchStatus();
	for ( ResolveNaturalIdEventListener listener : listeners( EventType.RESOLVE_NATURAL_ID ) ) {
		listener.onResolveNaturalId( event );
	}
	delayedAfterCompletion();
}
 
Example 35
Project: lams   File: SessionImpl.java   Source Code and License 5 votes vote down vote up
private void fireRefresh(RefreshEvent event) {
	errorIfClosed();
	checkTransactionSynchStatus();
	for ( RefreshEventListener listener : listeners( EventType.REFRESH ) ) {
		listener.onRefresh( event );
	}
	delayedAfterCompletion();
}
 
Example 36
Project: lams   File: SessionImpl.java   Source Code and License 5 votes vote down vote up
private void fireRefresh(Map refreshedAlready, RefreshEvent event) {
	errorIfClosed();
	checkTransactionSynchStatus();
	for ( RefreshEventListener listener : listeners( EventType.REFRESH ) ) {
		listener.onRefresh( event, refreshedAlready );
	}
	delayedAfterCompletion();
}
 
Example 37
Project: lams   File: SessionImpl.java   Source Code and License 5 votes vote down vote up
private void fireReplicate(ReplicateEvent event) {
	errorIfClosed();
	checkTransactionSynchStatus();
	for ( ReplicateEventListener listener : listeners( EventType.REPLICATE ) ) {
		listener.onReplicate( event );
	}
	delayedAfterCompletion();
}
 
Example 38
Project: lams   File: SessionImpl.java   Source Code and License 5 votes vote down vote up
private void fireEvict(EvictEvent event) {
	errorIfClosed();
	checkTransactionSynchStatus();
	for ( EvictEventListener listener : listeners( EventType.EVICT ) ) {
		listener.onEvict( event );
	}
	delayedAfterCompletion();
}
 
Example 39
Project: lams   File: SessionImpl.java   Source Code and License 5 votes vote down vote up
/**
 * detect in-memory changes, determine if the changes are to tables
 * named in the query and, if so, complete execution the flush
 */
protected boolean autoFlushIfRequired(Set querySpaces) throws HibernateException {
	errorIfClosed();
	if ( ! isTransactionInProgress() ) {
		// do not auto-flush while outside a transaction
		return false;
	}
	AutoFlushEvent event = new AutoFlushEvent( querySpaces, this );
	for ( AutoFlushEventListener listener : listeners( EventType.AUTO_FLUSH ) ) {
		listener.onAutoFlush( event );
	}
	return event.isFlushRequired();
}
 
Example 40
Project: lams   File: SessionImpl.java   Source Code and License 5 votes vote down vote up
@Override
public void flush() throws HibernateException {
	errorIfClosed();
	checkTransactionSynchStatus();
	if ( persistenceContext.getCascadeLevel() > 0 ) {
		throw new HibernateException("Flush during cascade is dangerous");
	}
	FlushEvent flushEvent = new FlushEvent( this );
	for ( FlushEventListener listener : listeners( EventType.FLUSH ) ) {
		listener.onFlush( flushEvent );
	}
	delayedAfterCompletion();
}
 
Example 41
Project: lams   File: SessionImpl.java   Source Code and License 5 votes vote down vote up
@Override
public void initializeCollection(PersistentCollection collection, boolean writing)
throws HibernateException {
	errorIfClosed();
	checkTransactionSynchStatus();
	InitializeCollectionEvent event = new InitializeCollectionEvent( collection, this );
	for ( InitializeCollectionEventListener listener : listeners( EventType.INIT_COLLECTION ) ) {
		listener.onInitializeCollection( event );
	}
	delayedAfterCompletion();
}
 
Example 42
Project: lams   File: JaccIntegrator.java   Source Code and License 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 43
Project: lams   File: StandardCacheEntryImpl.java   Source Code and License 5 votes vote down vote up
/**
 * Assemble the previously disassembled state represented by this entry into the given entity instance.
 *
 * Additionally manages the PreLoadEvent callbacks.
 *
 * @param instance The entity instance
 * @param id The entity identifier
 * @param persister The entity persister
 * @param interceptor (currently unused)
 * @param session The session
 *
 * @return The assembled state
 *
 * @throws HibernateException Indicates a problem performing assembly or calling the PreLoadEventListeners.
 *
 * @see org.hibernate.type.Type#assemble
 * @see org.hibernate.type.Type#disassemble
 */
public Object[] assemble(
		final Object instance,
		final Serializable id,
		final EntityPersister persister,
		final Interceptor interceptor,
		final EventSource session) throws HibernateException {
	if ( !persister.getEntityName().equals( subclass ) ) {
		throw new AssertionFailure( "Tried to assemble a different subclass instance" );
	}

	//assembled state gets put in a new array (we read from cache by value!)
	final Object[] assembledProps = TypeHelper.assemble(
			disassembledState,
			persister.getPropertyTypes(),
			session, instance
	);

	//persister.setIdentifier(instance, id); //before calling interceptor, for consistency with normal load

	//TODO: reuse the PreLoadEvent
	final PreLoadEvent preLoadEvent = new PreLoadEvent( session )
			.setEntity( instance )
			.setState( assembledProps )
			.setId( id )
			.setPersister( persister );

	final EventListenerGroup<PreLoadEventListener> listenerGroup = session
			.getFactory()
			.getServiceRegistry()
			.getService( EventListenerRegistry.class )
			.getEventListenerGroup( EventType.PRE_LOAD );
	for ( PreLoadEventListener listener : listenerGroup.listeners() ) {
		listener.onPreLoad( preLoadEvent );
	}

	persister.setPropertyValues( instance, assembledProps );

	return assembledProps;
}
 
Example 44
Project: lams   File: CollectionCacheInvalidator.java   Source Code and License 5 votes vote down vote up
private void integrate(SessionFactoryServiceRegistry serviceRegistry, SessionFactoryImplementor sessionFactory) {
	if ( !sessionFactory.getSettings().isAutoEvictCollectionCache() ) {
		// feature is disabled
		return;
	}
	if ( !sessionFactory.getSettings().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 45
Project: lams   File: EventListenerRegistryImpl.java   Source Code and License 5 votes vote down vote up
@SuppressWarnings({ "unchecked" })
public <T> EventListenerGroupImpl<T> getEventListenerGroup(EventType<T> eventType) {
	EventListenerGroupImpl<T> listeners = registeredEventListenersMap.get( eventType );
	if ( listeners == null ) {
		throw new HibernateException( "Unable to find listeners for type [" + eventType.eventName() + "]" );
	}
	return listeners;
}
 
Example 46
Project: lams   File: EventListenerRegistryImpl.java   Source Code and License 5 votes vote down vote up
@SuppressWarnings( {"unchecked"})
private <T> T[] resolveListenerInstances(EventType<T> type, Class<? extends T>... listenerClasses) {
	T[] listeners = (T[]) Array.newInstance( type.baseListenerInterface(), listenerClasses.length );
	for ( int i = 0; i < listenerClasses.length; i++ ) {
		listeners[i] = resolveListenerInstance( listenerClasses[i] );
	}
	return listeners;
}
 
Example 47
Project: lams   File: EventListenerRegistryImpl.java   Source Code and License 5 votes vote down vote up
@Override
public <T> void setListeners(EventType<T> type, T... listeners) {
	EventListenerGroupImpl<T> registeredListeners = getEventListenerGroup( type );
	registeredListeners.clear();
	if ( listeners != null ) {
		for ( int i = 0, max = listeners.length; i < max; i++ ) {
			registeredListeners.appendListener( listeners[i] );
		}
	}
}
 
Example 48
Project: lams   File: DefaultLoadEventListener.java   Source Code and License 5 votes vote down vote up
private Iterable<PostLoadEventListener> postLoadEventListeners(EventSource session) {
	return session
			.getFactory()
			.getServiceRegistry()
			.getService( EventListenerRegistry.class )
			.getEventListenerGroup( EventType.POST_LOAD )
			.listeners();
}
 
Example 49
Project: lams   File: AbstractFlushingEventListener.java   Source Code and License 5 votes vote down vote up
/**
 * 1. detect any dirty entities
 * 2. schedule any entity updates
 * 3. search out any reachable collections
 */
private int flushEntities(final FlushEvent event, final PersistenceContext persistenceContext) throws HibernateException {

	LOG.trace( "Flushing entities and processing referenced collections" );

	final EventSource source = event.getSession();
	final Iterable<FlushEntityEventListener> flushListeners = source.getFactory().getServiceRegistry()
			.getService( EventListenerRegistry.class )
			.getEventListenerGroup( EventType.FLUSH_ENTITY )
			.listeners();

	// Among other things, updateReachables() will recursively load all
	// collections that are moving roles. This might cause entities to
	// be loaded.

	// So this needs to be safe from concurrent modification problems.

	final Map.Entry<Object,EntityEntry>[] entityEntries = persistenceContext.reentrantSafeEntityEntries();
	final int count = entityEntries.length;

	for ( Map.Entry<Object,EntityEntry> me : entityEntries ) {

		// Update the status of the object and if necessary, schedule an update

		EntityEntry entry = me.getValue();
		Status status = entry.getStatus();

		if ( status != Status.LOADING && status != Status.GONE ) {
			final FlushEntityEvent entityEvent = new FlushEntityEvent( source, me.getKey(), entry );
			for ( FlushEntityEventListener listener : flushListeners ) {
				listener.onFlushEntity( entityEvent );
			}
		}
	}

	source.getActionQueue().sortActions();

	return count;
}
 
Example 50
Project: lams   File: EntityAction.java   Source Code and License 5 votes vote down vote up
protected <T> EventListenerGroup<T> listenerGroup(EventType<T> eventType) {
	return getSession()
			.getFactory()
			.getServiceRegistry()
			.getService( EventListenerRegistry.class )
			.getEventListenerGroup( eventType );
}
 
Example 51
Project: lams   File: EntityDeleteAction.java   Source Code and License 5 votes vote down vote up
private boolean preDelete() {
	boolean veto = false;
	final EventListenerGroup<PreDeleteEventListener> listenerGroup = listenerGroup( EventType.PRE_DELETE );
	if ( listenerGroup.isEmpty() ) {
		return veto;
	}
	final PreDeleteEvent event = new PreDeleteEvent( getInstance(), getId(), state, getPersister(), eventSource() );
	for ( PreDeleteEventListener listener : listenerGroup.listeners() ) {
		veto |= listener.onPreDelete( event );
	}
	return veto;
}
 
Example 52
Project: lams   File: EntityDeleteAction.java   Source Code and License 5 votes vote down vote up
private void postCommitDelete(boolean success) {
	final EventListenerGroup<PostDeleteEventListener> listenerGroup = listenerGroup( EventType.POST_COMMIT_DELETE );
	if ( listenerGroup.isEmpty() ) {
		return;
	}
	final PostDeleteEvent event = new PostDeleteEvent(
			getInstance(),
			getId(),
			state,
			getPersister(),
			eventSource()
	);
	for ( PostDeleteEventListener listener : listenerGroup.listeners() ) {
		if ( PostCommitDeleteEventListener.class.isInstance( listener ) ) {
			if ( success ) {
				listener.onPostDelete( event );
			}
			else {
				((PostCommitDeleteEventListener) listener).onPostDeleteCommitFailed( event );
			}
		}
		else {
			//default to the legacy implementation that always fires the event
			listener.onPostDelete( event );
		}
	}
}
 
Example 53
Project: lams   File: EntityDeleteAction.java   Source Code and License 5 votes vote down vote up
@Override
protected boolean hasPostCommitEventListeners() {
	final EventListenerGroup<PostDeleteEventListener> group = listenerGroup( EventType.POST_COMMIT_DELETE );
	for ( PostDeleteEventListener listener : group.listeners() ) {
		if ( listener.requiresPostCommitHanding( getPersister() ) ) {
			return true;
		}
	}

	return false;
}
 
Example 54
Project: lams   File: EntityInsertAction.java   Source Code and License 5 votes vote down vote up
private void postCommitInsert(boolean success) {
	final EventListenerGroup<PostInsertEventListener> listenerGroup = listenerGroup( EventType.POST_COMMIT_INSERT );
	if ( listenerGroup.isEmpty() ) {
		return;
	}
	final PostInsertEvent event = new PostInsertEvent(
			getInstance(),
			getId(),
			getState(),
			getPersister(),
			eventSource()
	);
	for ( PostInsertEventListener listener : listenerGroup.listeners() ) {
		if ( PostCommitInsertEventListener.class.isInstance( listener ) ) {
			if ( success ) {
				listener.onPostInsert( event );
			}
			else {
				((PostCommitInsertEventListener) listener).onPostInsertCommitFailed( event );
			}
		}
		else {
			//default to the legacy implementation that always fires the event
			listener.onPostInsert( event );
		}
	}
}
 
Example 55
Project: lams   File: EntityInsertAction.java   Source Code and License 5 votes vote down vote up
private boolean preInsert() {
	boolean veto = false;

	final EventListenerGroup<PreInsertEventListener> listenerGroup = listenerGroup( EventType.PRE_INSERT );
	if ( listenerGroup.isEmpty() ) {
		return veto;
	}
	final PreInsertEvent event = new PreInsertEvent( getInstance(), getId(), getState(), getPersister(), eventSource() );
	for ( PreInsertEventListener listener : listenerGroup.listeners() ) {
		veto |= listener.onPreInsert( event );
	}
	return veto;
}
 
Example 56
Project: lams   File: EntityInsertAction.java   Source Code and License 5 votes vote down vote up
@Override
protected boolean hasPostCommitEventListeners() {
	final EventListenerGroup<PostInsertEventListener> group = listenerGroup( EventType.POST_COMMIT_INSERT );
	for ( PostInsertEventListener listener : group.listeners() ) {
		if ( listener.requiresPostCommitHanding( getPersister() ) ) {
			return true;
		}
	}

	return false;
}
 
Example 57
Project: lams   File: CollectionRecreateAction.java   Source Code and License 5 votes vote down vote up
private void preRecreate() {
	final EventListenerGroup<PreCollectionRecreateEventListener> listenerGroup = listenerGroup( EventType.PRE_COLLECTION_RECREATE );
	if ( listenerGroup.isEmpty() ) {
		return;
	}
	final PreCollectionRecreateEvent event = new PreCollectionRecreateEvent( getPersister(), getCollection(), eventSource() );
	for ( PreCollectionRecreateEventListener listener : listenerGroup.listeners() ) {
		listener.onPreRecreateCollection( event );
	}
}
 
Example 58
Project: lams   File: CollectionRecreateAction.java   Source Code and License 5 votes vote down vote up
private void postRecreate() {
	final EventListenerGroup<PostCollectionRecreateEventListener> listenerGroup = listenerGroup( EventType.POST_COLLECTION_RECREATE );
	if ( listenerGroup.isEmpty() ) {
		return;
	}
	final PostCollectionRecreateEvent event = new PostCollectionRecreateEvent( getPersister(), getCollection(), eventSource() );
	for ( PostCollectionRecreateEventListener listener : listenerGroup.listeners() ) {
		listener.onPostRecreateCollection( event );
	}
}
 
Example 59
Project: lams   File: CollectionAction.java   Source Code and License 5 votes vote down vote up
protected <T> EventListenerGroup<T> listenerGroup(EventType<T> eventType) {
	return getSession()
			.getFactory()
			.getServiceRegistry()
			.getService( EventListenerRegistry.class )
			.getEventListenerGroup( eventType );
}
 
Example 60
Project: lams   File: CollectionUpdateAction.java   Source Code and License 5 votes vote down vote up
private void preUpdate() {
	final EventListenerGroup<PreCollectionUpdateEventListener> listenerGroup = listenerGroup( EventType.PRE_COLLECTION_UPDATE );
	if ( listenerGroup.isEmpty() ) {
		return;
	}
	final PreCollectionUpdateEvent event = new PreCollectionUpdateEvent(
			getPersister(),
			getCollection(),
			eventSource()
	);
	for ( PreCollectionUpdateEventListener listener : listenerGroup.listeners() ) {
		listener.onPreUpdateCollection( event );
	}
}