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     */
022    package org.granite.tide.spring;
023    
024    import org.granite.tide.data.DataContext;
025    import org.granite.tide.data.DataEnabled.PublishMode;
026    import org.granite.tide.data.TideSynchronizationManager;
027    import org.hibernate.Session;
028    import org.hibernate.action.spi.BeforeTransactionCompletionProcess;
029    import org.hibernate.engine.spi.SessionImplementor;
030    import org.hibernate.internal.SessionImpl;
031    import org.springframework.orm.hibernate4.SessionHolder;
032    
033    /**
034     * Responsible for registering a publishing queued process on the Hibernate 4 session
035     * @author william
036     */
037    public class Hibernate4SynchronizationManager implements TideSynchronizationManager {
038            
039            public boolean registerSynchronization(Object resource, boolean shouldRemoveContextAtEnd) {
040                    Session session = null;
041                    if (resource instanceof Session)
042                            session = (Session)resource;
043                    else if (resource instanceof SessionHolder)
044                            session = ((SessionHolder)resource).getSession();
045                    else
046                            throw new IllegalArgumentException("Unknow resource for registering synchronization " + resource);
047                    
048                    ((SessionImpl)session).getActionQueue().registerProcess(new HibernateDataPublishingProcess(shouldRemoveContextAtEnd));
049                    return true;
050            }
051            
052            private static class HibernateDataPublishingProcess implements BeforeTransactionCompletionProcess {
053                    
054                    private boolean removeContext;
055                    
056                    public HibernateDataPublishingProcess(boolean removeContext) {
057                            this.removeContext = removeContext;
058                    }
059                    
060                    public void doBeforeTransactionCompletion(SessionImplementor session) {
061                            DataContext.publish(PublishMode.ON_COMMIT);
062                            if (removeContext)
063                                    DataContext.remove();
064                    }
065            }
066    
067    }