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

org.hibernate.event.spi.EventType Maven / Gradle / Ivy

There is a newer version: 7.0.0.Alpha1
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.event.spi;

import java.lang.reflect.Field;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

import org.hibernate.HibernateException;

/**
 * Enumeration of the recognized types of events, including meta-information about each.
 *
 * @author Steve Ebersole
 */
public final class EventType {

	private static AtomicInteger typeCounter = new AtomicInteger( 0 );

	public static final EventType LOAD = create( "load", LoadEventListener.class );
	public static final EventType RESOLVE_NATURAL_ID = create( "resolve-natural-id", ResolveNaturalIdEventListener.class );

	public static final EventType INIT_COLLECTION = create( "load-collection", InitializeCollectionEventListener.class );

	public static final EventType SAVE_UPDATE = create( "save-update", SaveOrUpdateEventListener.class );
	public static final EventType UPDATE = create( "update", SaveOrUpdateEventListener.class );
	public static final EventType SAVE = create( "save", SaveOrUpdateEventListener.class );
	public static final EventType PERSIST = create( "create", PersistEventListener.class );
	public static final EventType PERSIST_ONFLUSH = create( "create-onflush", PersistEventListener.class );

	public static final EventType MERGE = create( "merge", MergeEventListener.class );

	public static final EventType DELETE = create( "delete", DeleteEventListener.class );

	public static final EventType REPLICATE = create( "replicate", ReplicateEventListener.class );

	public static final EventType FLUSH = create( "flush", FlushEventListener.class );
	public static final EventType AUTO_FLUSH = create( "auto-flush", AutoFlushEventListener.class );
	public static final EventType DIRTY_CHECK = create( "dirty-check", DirtyCheckEventListener.class );
	public static final EventType FLUSH_ENTITY = create( "flush-entity", FlushEntityEventListener.class );

	public static final EventType CLEAR = create( "clear", ClearEventListener.class );
	public static final EventType EVICT = create( "evict", EvictEventListener.class );

	public static final EventType LOCK = create( "lock", LockEventListener.class );

	public static final EventType REFRESH = create( "refresh", RefreshEventListener.class );

	public static final EventType PRE_LOAD = create( "pre-load", PreLoadEventListener.class );
	public static final EventType PRE_DELETE = create( "pre-delete", PreDeleteEventListener.class );
	public static final EventType PRE_UPDATE = create( "pre-update", PreUpdateEventListener.class );
	public static final EventType PRE_INSERT = create( "pre-insert", PreInsertEventListener.class );

	public static final EventType POST_LOAD = create( "post-load", PostLoadEventListener.class );
	public static final EventType POST_DELETE = create( "post-delete", PostDeleteEventListener.class );
	public static final EventType POST_UPDATE = create( "post-update", PostUpdateEventListener.class );
	public static final EventType POST_INSERT = create( "post-insert", PostInsertEventListener.class );

	public static final EventType POST_COMMIT_DELETE = create( "post-commit-delete", PostDeleteEventListener.class );
	public static final EventType POST_COMMIT_UPDATE = create( "post-commit-update", PostUpdateEventListener.class );
	public static final EventType POST_COMMIT_INSERT = create( "post-commit-insert", PostInsertEventListener.class );

	public static final EventType PRE_COLLECTION_RECREATE = create( "pre-collection-recreate", PreCollectionRecreateEventListener.class );
	public static final EventType PRE_COLLECTION_REMOVE = create( "pre-collection-remove", PreCollectionRemoveEventListener.class );
	public static final EventType PRE_COLLECTION_UPDATE = create( "pre-collection-update", PreCollectionUpdateEventListener.class );

	public static final EventType POST_COLLECTION_RECREATE = create( "post-collection-recreate", PostCollectionRecreateEventListener.class );
	public static final EventType POST_COLLECTION_REMOVE = create( "post-collection-remove", PostCollectionRemoveEventListener.class );
	public static final EventType POST_COLLECTION_UPDATE = create( "post-collection-update", PostCollectionUpdateEventListener.class );


	private static  EventType create(String name, Class listenerClass) {
		return new EventType( name, listenerClass );
	}

	/**
	 * Maintain a map of {@link EventType} instances keyed by name for lookup by name as well as {@link #values()}
	 * resolution.
	 */
	private static final Map EVENT_TYPE_BY_NAME_MAP = AccessController.doPrivileged(
			new PrivilegedAction>() {
				@Override
				public Map run() {
					final Map typeByNameMap = new HashMap();
					for ( Field field : EventType.class.getDeclaredFields() ) {
						if ( EventType.class.isAssignableFrom( field.getType() ) ) {
							try {
								final EventType typeField = (EventType) field.get( null );
								typeByNameMap.put( typeField.eventName(), typeField );
							}
							catch (Exception t) {
								throw new HibernateException( "Unable to initialize EventType map", t );
							}
						}
					}
					return typeByNameMap;
				}
			}
	);

	/**
	 * Find an {@link EventType} by its name
	 *
	 * @param eventName The name
	 *
	 * @return The {@link EventType} instance.
	 *
	 * @throws HibernateException If eventName is null, or if eventName does not correlate to any known event type.
	 */
	public static EventType resolveEventTypeByName(final String eventName) {
		if ( eventName == null ) {
			throw new HibernateException( "event name to resolve cannot be null" );
		}
		final EventType eventType = EVENT_TYPE_BY_NAME_MAP.get( eventName );
		if ( eventType == null ) {
			throw new HibernateException( "Unable to locate proper event type for event name [" + eventName + "]" );
		}
		return eventType;
	}

	/**
	 * Get a collection of all {@link EventType} instances.
	 *
	 * @return All {@link EventType} instances
	 */
	public static Collection values() {
		return EVENT_TYPE_BY_NAME_MAP.values();
	}

	private final String eventName;
	private final Class baseListenerInterface;
	private final int ordinal;

	private EventType(String eventName, Class baseListenerInterface) {
		this.eventName = eventName;
		this.baseListenerInterface = baseListenerInterface;
		this.ordinal = typeCounter.getAndIncrement();
	}

	public String eventName() {
		return eventName;
	}

	public Class baseListenerInterface() {
		return baseListenerInterface;
	}

	@Override
	public String toString() {
		return eventName();
	}

	/**
	 * EventType is effectively an enumeration. Since there is a known, limited number of possible types, we expose an
	 * ordinal for each in order to be able to efficiently do associations elsewhere in the codebase (array vs. Map)
	 *
	 * For the total number of types, see {@link #values()}
	 *
	 * @return An unique ordinal for this {@link EventType}, starting at 0 and up to the number of distinct events
	 */
	public int ordinal() {
		return ordinal;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy