001/*
002  GRANITE DATA SERVICES
003  Copyright (C) 2011 GRANITE DATA SERVICES S.A.S.
004
005  This file is part of Granite Data Services.
006
007  Granite Data Services is free software; you can redistribute it and/or modify
008  it under the terms of the GNU Library General Public License as published by
009  the Free Software Foundation; either version 2 of the License, or (at your
010  option) any later version.
011
012  Granite Data Services is distributed in the hope that it will be useful, but
013  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
015  for more details.
016
017  You should have received a copy of the GNU Library General Public License
018  along with this library; if not, see <http://www.gnu.org/licenses/>.
019*/
020
021package org.granite.tide.spring;
022
023import org.granite.logging.Logger;
024import org.granite.tide.TidePersistenceManager;
025import org.granite.tide.TideTransactionManager;
026import org.granite.tide.data.AbstractTidePersistenceManager;
027import org.granite.tide.data.JDOPersistenceManager;
028import org.granite.tide.data.NoPersistenceManager;
029import org.granite.util.TypeUtil;
030import org.springframework.orm.hibernate3.HibernateTransactionManager;
031import org.springframework.orm.jdo.JdoTransactionManager;
032import org.springframework.orm.jpa.JpaTransactionManager;
033import org.springframework.transaction.PlatformTransactionManager;
034
035
036/**
037 *      Responsible for attaching a session with the persistence mangager
038 *      @author Cameron Ingram
039 *      @author William Dra???
040 *
041 */
042public class SpringPersistenceManager implements TidePersistenceManager {
043    
044        private static final Logger log = Logger.getLogger(SpringPersistenceManager.class);
045        
046        private TidePersistenceManager pm;
047        
048        
049        public SpringPersistenceManager(PlatformTransactionManager transactionManager) {
050                TideTransactionManager tm = new SpringTransactionManager(transactionManager);
051                if (transactionManager instanceof JpaTransactionManager) {
052                        pm = new SpringJPAPersistenceManager((JpaTransactionManager)transactionManager, tm);
053                        return;
054                }
055                
056                if (transactionManager instanceof JdoTransactionManager) {
057                        pm = new JDOPersistenceManager(((JdoTransactionManager)transactionManager).getPersistenceManagerFactory(), tm);
058                        return;
059                }
060                
061                // Check for Hibernate 3
062                if (transactionManager instanceof HibernateTransactionManager) {
063                        try {
064                                Object sf = transactionManager.getClass().getMethod("getSessionFactory").invoke(transactionManager);
065                                Class<?> sfClass = TypeUtil.forName("org.hibernate.SessionFactory");
066                                pm = (TidePersistenceManager)TypeUtil.newInstance("org.granite.tide.hibernate.HibernatePersistenceManager", 
067                                                new Class<?>[] { sfClass, TideTransactionManager.class }, new Object[] { sf, tm });
068                        }
069                        catch (Exception e) {
070                                log.error("Could not setup Hibernate 3 persistence manager, lazy-loading disabled. Check that granite-hibernate.jar is present in the classpath.");
071                                pm = new NoPersistenceManager();
072                        }
073                        return;
074                }
075                
076                // Check for Hibernate 4
077                try {
078                        Class<?> hibernate4TM = TypeUtil.forName("org.springframework.orm.hibernate4.HibernateTransactionManager");
079                        if (hibernate4TM.isInstance(transactionManager)) {
080                                try {
081                                        Object sf = transactionManager.getClass().getMethod("getSessionFactory").invoke(transactionManager);
082                                        Class<?> sfClass = TypeUtil.forName("org.hibernate.SessionFactory");
083                                        pm = (TidePersistenceManager)TypeUtil.newInstance("org.granite.tide.hibernate4.HibernatePersistenceManager", 
084                                                        new Class<?>[] { sfClass, TideTransactionManager.class }, new Object[] { sf, tm });
085                                }
086                                catch (Exception e) {
087                                        log.error("Could not setup Hibernate 4 persistence manager, lazy-loading disabled. Check that granite-hibernate4.jar is present in the classpath.");
088                                        pm = new NoPersistenceManager();
089                                }
090                                return;
091                        }
092                }
093                catch (ClassNotFoundException e) {
094                        // Hibernate 4 not installed
095                }
096                
097                log.error("Unsupported Spring TransactionManager, lazy-loading disabled");
098                pm = new NoPersistenceManager();
099        }
100
101
102        public Object attachEntity(Object entity, String[] propertyNames) {
103                if (pm instanceof AbstractTidePersistenceManager)
104                        return ((AbstractTidePersistenceManager)pm).attachEntity(this, entity, propertyNames);
105                return pm.attachEntity(entity, propertyNames);
106        }
107
108}