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 javax.persistence.EntityManager;
025    
026    import org.granite.logging.Logger;
027    import org.granite.tide.data.JPAPersistenceAdapter;
028    import org.granite.tide.data.TidePersistenceAdapter;
029    import org.granite.tide.data.TidePersistenceAdapterFactory;
030    import org.granite.util.TypeUtil;
031    import org.springframework.orm.hibernate3.HibernateTransactionManager;
032    import org.springframework.orm.jpa.EntityManagerFactoryUtils;
033    import org.springframework.orm.jpa.JpaTransactionManager;
034    import org.springframework.transaction.PlatformTransactionManager;
035    
036    
037    /**
038     *      Implementation of TidePersistenceAdapterFactory for Spring
039     *  Detects the platform transaction manager and creates Hibernate or JPA persistence adapters accordingly
040     *  
041     *      @author William Drai
042     */
043    public class SpringPersistenceAdapterFactory implements TidePersistenceAdapterFactory {
044        
045            private static final Logger log = Logger.getLogger(SpringPersistenceAdapterFactory.class);
046            
047            private final PlatformTransactionManager transactionManager;
048            
049            public SpringPersistenceAdapterFactory(PlatformTransactionManager transactionManager) {
050                    this.transactionManager = transactionManager;
051            }
052            
053            public TidePersistenceAdapter newPersistenceAdapter() {
054                    if (transactionManager instanceof HibernateTransactionManager) {
055                            try {
056                                    // Use reflection to avoid runtime dependency on Hibernate
057                                    Object sf = transactionManager.getClass().getMethod("getSessionFactory").invoke(transactionManager);
058                                    Class<?> sfClass = TypeUtil.forName("org.hibernate.SessionFactory");
059                                    return (TidePersistenceAdapter)TypeUtil.newInstance("org.granite.tide.hibernate.HibernatePersistenceAdapter",
060                                                    new Class<?>[] { sfClass }, new Object[] { sf });
061                            }
062                            catch (Exception e) {
063                                    log.error("Could not setup Hibernate persistence adapter, incremental changes disabled. Check that granite-hibernate.jar is present in the classpath.");
064                                    return null;
065                            }
066                    }
067                    else if (transactionManager instanceof JpaTransactionManager) {
068                            EntityManager em = EntityManagerFactoryUtils.getTransactionalEntityManager(((JpaTransactionManager)transactionManager).getEntityManagerFactory());
069                            return new JPAPersistenceAdapter(em);
070                    }
071                    
072                    log.error("Unsupported Spring TransactionManager, incremental change s disabled. You may ");
073                    return null;
074            }
075    }