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.spring;
023
024import java.util.HashMap;
025import java.util.Map;
026
027import org.granite.tide.data.TideSynchronizationManager;
028import org.granite.util.TypeUtil;
029import org.springframework.orm.jpa.EntityManagerHolder;
030
031/**
032 * Responsible for registering a publishing synchronization on the JPA entity manager
033 * Delegated to Hibernate-specific workarounds when necessary
034 * @author william
035 */
036public class JPASynchronizationManager implements TideSynchronizationManager {
037        
038        private Map<String, TideSynchronizationManager> syncsMap = new HashMap<String, TideSynchronizationManager>();
039        
040        public JPASynchronizationManager() {
041                try {
042                        syncsMap.put("org.hibernate.impl.SessionImpl", TypeUtil.newInstance("org.granite.tide.spring.Hibernate3SynchronizationManager", TideSynchronizationManager.class));
043                }
044                catch (Throwable t) {
045                        // Hibernate 3 not found
046                }
047                try {
048                        syncsMap.put("org.hibernate.internal.SessionImpl", TypeUtil.newInstance("org.granite.tide.spring.Hibernate4SynchronizationManager", TideSynchronizationManager.class));
049                }
050                catch (Throwable t) {
051                        // Hibernate 4 not found
052                }
053        }
054        
055        public boolean registerSynchronization(Object resource, boolean shouldRemoveContextAtEnd) {
056                EntityManagerHolder entityManagerHolder = (EntityManagerHolder)resource;
057                Object delegate = entityManagerHolder.getEntityManager().getDelegate();
058                if (syncsMap.containsKey(delegate.getClass().getName()))
059                        return syncsMap.get(delegate.getClass().getName()).registerSynchronization(delegate, shouldRemoveContextAtEnd);
060                
061                return false;
062        }
063}