use of org.hibernate.event.service.spi.EventListenerRegistry in project hibernate-orm by hibernate.
the class MergeListPreAndPostPersistTest method addEntityListeners.
private void addEntityListeners(final Order order) {
EventListenerRegistry registry = sessionFactory().getServiceRegistry().getService(EventListenerRegistry.class);
registry.setListeners(EventType.PRE_INSERT, new PreInsertEventListener() {
@Override
public boolean onPreInsert(PreInsertEvent event) {
if (Order.class.isInstance(event.getEntity())) {
assertEquals(order, event.getEntity());
assertEquals(order.items, ((Order) event.getEntity()).items);
}
return false;
}
});
registry.setListeners(EventType.POST_INSERT, new PostInsertEventListener() {
public void onPostInsert(PostInsertEvent event) {
if (Order.class.isInstance(event.getEntity())) {
assertEquals(order, event.getEntity());
assertEquals(order.items, ((Order) event.getEntity()).items);
}
}
public boolean requiresPostCommitHanding(EntityPersister persister) {
return false;
}
});
}
use of org.hibernate.event.service.spi.EventListenerRegistry in project hibernate-orm by hibernate.
the class MergeListPreAndPostPersistWithIdentityTest method addEntityListeners.
private void addEntityListeners(final Order order) {
EventListenerRegistry registry = sessionFactory().getServiceRegistry().getService(EventListenerRegistry.class);
registry.setListeners(EventType.PRE_INSERT, new PreInsertEventListener() {
@Override
public boolean onPreInsert(PreInsertEvent event) {
if (Order.class.isInstance(event.getEntity())) {
assertEquals(order, event.getEntity());
assertEquals(order.items, ((Order) event.getEntity()).items);
}
return false;
}
});
registry.setListeners(EventType.POST_INSERT, new PostInsertEventListener() {
public void onPostInsert(PostInsertEvent event) {
if (Order.class.isInstance(event.getEntity())) {
assertEquals(order, event.getEntity());
assertEquals(order.items, ((Order) event.getEntity()).items);
}
}
public boolean requiresPostCommitHanding(EntityPersister persister) {
return false;
}
});
}
use of org.hibernate.event.service.spi.EventListenerRegistry in project hibernate-orm by hibernate.
the class JpaIntegrator method integrate.
/**
* Perform integration.
*
* @param metadata The "compiled" representation of the mapping information
* @param sessionFactory The session factory being created
* @param serviceRegistry The session factory's service registry
*/
public void integrate(Metadata metadata, SessionFactoryImplementor sessionFactory, SessionFactoryServiceRegistry serviceRegistry) {
// first, register the JPA-specific persist cascade style
try {
oldPersistCascadeStyle = CascadeStyles.getCascadeStyle("persist");
} catch (Exception e) {
}
CascadeStyles.registerCascadeStyle("persist", new PersistCascadeStyle());
// then prepare listeners
final EventListenerRegistry eventListenerRegistry = serviceRegistry.getService(EventListenerRegistry.class);
eventListenerRegistry.addDuplicationStrategy(JPA_DUPLICATION_STRATEGY);
// op listeners
eventListenerRegistry.setListeners(EventType.AUTO_FLUSH, JpaAutoFlushEventListener.INSTANCE);
eventListenerRegistry.setListeners(EventType.DELETE, new JpaDeleteEventListener());
eventListenerRegistry.setListeners(EventType.FLUSH_ENTITY, new JpaFlushEntityEventListener());
eventListenerRegistry.setListeners(EventType.FLUSH, JpaFlushEventListener.INSTANCE);
eventListenerRegistry.setListeners(EventType.MERGE, new JpaMergeEventListener());
eventListenerRegistry.setListeners(EventType.PERSIST, new JpaPersistEventListener());
eventListenerRegistry.setListeners(EventType.PERSIST_ONFLUSH, new JpaPersistOnFlushEventListener());
eventListenerRegistry.setListeners(EventType.SAVE, new JpaSaveEventListener());
eventListenerRegistry.setListeners(EventType.SAVE_UPDATE, new JpaSaveOrUpdateEventListener());
// post op listeners
eventListenerRegistry.prependListeners(EventType.POST_DELETE, new JpaPostDeleteEventListener());
eventListenerRegistry.prependListeners(EventType.POST_INSERT, new JpaPostInsertEventListener());
eventListenerRegistry.prependListeners(EventType.POST_LOAD, new JpaPostLoadEventListener());
eventListenerRegistry.prependListeners(EventType.POST_UPDATE, new JpaPostUpdateEventListener());
final ConfigurationService cfgService = serviceRegistry.getService(ConfigurationService.class);
for (Map.Entry entry : ((Map<?, ?>) cfgService.getSettings()).entrySet()) {
if (!String.class.isInstance(entry.getKey())) {
continue;
}
final String propertyName = (String) entry.getKey();
if (!propertyName.startsWith(AvailableSettings.EVENT_LISTENER_PREFIX)) {
continue;
}
final String eventTypeName = propertyName.substring(AvailableSettings.EVENT_LISTENER_PREFIX.length() + 1);
final EventType eventType = EventType.resolveEventTypeByName(eventTypeName);
final EventListenerGroup eventListenerGroup = eventListenerRegistry.getEventListenerGroup(eventType);
for (String listenerImpl : ((String) entry.getValue()).split(" ,")) {
eventListenerGroup.appendListener(instantiate(listenerImpl, serviceRegistry));
}
}
// handle JPA "entity listener classes"...
final ReflectionManager reflectionManager = ((MetadataImpl) metadata).getMetadataBuildingOptions().getReflectionManager();
this.callbackRegistry = new CallbackRegistryImpl();
this.jpaListenerFactory = ListenerFactoryBuilder.buildListenerFactory(sessionFactory.getSessionFactoryOptions());
this.callbackBuilder = new CallbackBuilderLegacyImpl(jpaListenerFactory, reflectionManager);
for (PersistentClass persistentClass : metadata.getEntityBindings()) {
if (persistentClass.getClassName() == null) {
// we can have non java class persisted by hibernate
continue;
}
callbackBuilder.buildCallbacksForEntity(persistentClass.getClassName(), callbackRegistry);
}
for (EventType eventType : EventType.values()) {
final EventListenerGroup eventListenerGroup = eventListenerRegistry.getEventListenerGroup(eventType);
for (Object listener : eventListenerGroup.listeners()) {
if (CallbackRegistryConsumer.class.isInstance(listener)) {
((CallbackRegistryConsumer) listener).injectCallbackRegistry(callbackRegistry);
}
}
}
}
use of org.hibernate.event.service.spi.EventListenerRegistry in project hibernate-orm by hibernate.
the class TypeSafeActivator method applyCallbackListeners.
@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;
}
final ConfigurationService cfgService = activationContext.getServiceRegistry().getService(ConfigurationService.class);
final ClassLoaderService classLoaderService = activationContext.getServiceRegistry().getService(ClassLoaderService.class);
// asks for it
if (cfgService.getSettings().get(Environment.CHECK_NULLABILITY) == null) {
activationContext.getSessionFactory().getSessionFactoryOptions().setCheckNullability(false);
}
final BeanValidationEventListener listener = new BeanValidationEventListener(validatorFactory, cfgService.getSettings(), classLoaderService);
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(cfgService.getSettings(), classLoaderService);
}
use of org.hibernate.event.service.spi.EventListenerRegistry in project hibernate-orm by hibernate.
the class JaccIntegrator method doIntegration.
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());
}
Aggregations