001/** 002 * GRANITE DATA SERVICES 003 * Copyright (C) 2006-2013 GRANITE DATA SERVICES S.A.S. 004 * 005 * This file is part of the Granite Data Services Platform. 006 * 007 * Granite Data Services is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * Granite Data Services is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 015 * General Public License for more details. 016 * 017 * You should have received a copy of the GNU Lesser General Public 018 * License along with this library; if not, write to the Free Software 019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 020 * USA, or see <http://www.gnu.org/licenses/>. 021 */ 022package org.granite.tide.hibernate4; 023 024import org.hibernate.cfg.Configuration; 025import org.hibernate.engine.spi.SessionFactoryImplementor; 026import org.hibernate.event.service.spi.DuplicationStrategy; 027import org.hibernate.event.service.spi.EventListenerRegistry; 028import org.hibernate.event.spi.EventType; 029import org.hibernate.integrator.spi.Integrator; 030import org.hibernate.metamodel.source.MetadataImplementor; 031import org.hibernate.service.spi.SessionFactoryServiceRegistry; 032 033 034public class Hibernate4Integrator implements Integrator { 035 036 public void integrate(Configuration configuration, SessionFactoryImplementor sessionFactory, SessionFactoryServiceRegistry serviceRegistry) { 037 final EventListenerRegistry eventListenerRegistry = serviceRegistry.getService(EventListenerRegistry.class); 038 039 try { 040 HibernateDataPublishListener listener = new HibernateDataPublishListener(); 041 eventListenerRegistry.getEventListenerGroup(EventType.POST_INSERT).appendListener(listener); 042 eventListenerRegistry.getEventListenerGroup(EventType.POST_UPDATE).appendListener(listener); 043 eventListenerRegistry.getEventListenerGroup(EventType.POST_DELETE).appendListener(listener); 044 045 eventListenerRegistry.getEventListenerGroup(EventType.PERSIST).addDuplicationStrategy(new OverrideStrategy()); 046 eventListenerRegistry.getEventListenerGroup(EventType.PERSIST).appendListener(new HibernatePersistListener()); 047 eventListenerRegistry.getEventListenerGroup(EventType.PERSIST_ONFLUSH).addDuplicationStrategy(new OverrideStrategy()); 048 eventListenerRegistry.getEventListenerGroup(EventType.PERSIST_ONFLUSH).appendListener(new HibernatePersistOnFlushListener()); 049 eventListenerRegistry.getEventListenerGroup(EventType.SAVE_UPDATE).addDuplicationStrategy(new OverrideStrategy()); 050 eventListenerRegistry.getEventListenerGroup(EventType.SAVE_UPDATE).appendListener(new HibernateSaveOrUpdateListener()); 051 eventListenerRegistry.getEventListenerGroup(EventType.MERGE).addDuplicationStrategy(new OverrideStrategy()); 052 eventListenerRegistry.getEventListenerGroup(EventType.MERGE).appendListener(new HibernateMergeListener()); 053 eventListenerRegistry.getEventListenerGroup(EventType.DELETE).addDuplicationStrategy(new OverrideStrategy()); 054 eventListenerRegistry.getEventListenerGroup(EventType.DELETE).appendListener(new HibernateDeleteListener()); 055 eventListenerRegistry.getEventListenerGroup(EventType.LOCK).addDuplicationStrategy(new OverrideStrategy()); 056 eventListenerRegistry.getEventListenerGroup(EventType.LOCK).appendListener(new HibernateLockListener()); 057 eventListenerRegistry.getEventListenerGroup(EventType.AUTO_FLUSH).addDuplicationStrategy(new OverrideStrategy()); 058 eventListenerRegistry.getEventListenerGroup(EventType.AUTO_FLUSH).appendListener(new HibernateAutoFlushListener()); 059 eventListenerRegistry.getEventListenerGroup(EventType.FLUSH).addDuplicationStrategy(new OverrideStrategy()); 060 eventListenerRegistry.getEventListenerGroup(EventType.FLUSH).appendListener(new HibernateFlushListener()); 061 } 062 catch (Exception e) { 063 throw new RuntimeException("Could not setup Hibernate 4 listeners", e); 064 } 065 } 066 067 public void integrate(MetadataImplementor configuration, SessionFactoryImplementor sessionFactory, SessionFactoryServiceRegistry serviceRegistry) { 068 } 069 070 public void disintegrate(SessionFactoryImplementor sessionFactory, SessionFactoryServiceRegistry serviceRegistry) { 071 } 072 073 public static class OverrideStrategy implements DuplicationStrategy { 074 075 public boolean areMatch(Object listener, Object original) { 076 for (Class<?> spiInterface : original.getClass().getInterfaces()) { 077 if (spiInterface.getName().startsWith("org.hibernate.event.spi.")) 078 return spiInterface.isInstance(listener); 079 } 080 return false; 081 } 082 083 public Action getAction() { 084 return Action.REPLACE_ORIGINAL; 085 } 086 087 } 088}