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    
021    package org.granite.tide.spring;
022    
023    import org.granite.logging.Logger;
024    import org.granite.tide.TidePersistenceManager;
025    import org.granite.tide.TideTransactionManager;
026    import org.granite.tide.data.AbstractTidePersistenceManager;
027    import org.granite.tide.data.JPAPersistenceManager;
028    import org.granite.tide.data.NoPersistenceManager;
029    import org.granite.util.TypeUtil;
030    import org.springframework.orm.hibernate3.HibernateTransactionManager;
031    import org.springframework.orm.jpa.JpaTransactionManager;
032    import org.springframework.transaction.PlatformTransactionManager;
033    
034    
035    /**
036     *      Responsible for attaching a session with the persistence mangager
037     *      @author Cameron Ingram
038     *      @author William Dra�
039     *
040     */
041    public class SpringPersistenceManager implements TidePersistenceManager {
042        
043            private static final Logger log = Logger.getLogger(SpringPersistenceManager.class);
044            
045            private TidePersistenceManager pm;
046            
047            
048            public SpringPersistenceManager(PlatformTransactionManager transactionManager) {
049                    TideTransactionManager tm = new SpringTransactionManager(transactionManager);
050                    if (transactionManager instanceof HibernateTransactionManager) {
051                            try {
052                                    Object sf = transactionManager.getClass().getMethod("getSessionFactory").invoke(transactionManager);
053                                    Class<?> sfClass = TypeUtil.forName("org.hibernate.SessionFactory");
054                                    pm = (TidePersistenceManager)TypeUtil.newInstance("org.granite.tide.hibernate.HibernatePersistenceManager", 
055                                                    new Class<?>[] { sfClass, TideTransactionManager.class }, new Object[] { sf, tm });
056                            }
057                            catch (Exception e) {
058                                    log.error("Could not setup Hibernate persistence manager, lazy-loading disabled. Check that granite-hibernate.jar is present in the classpath.");
059                                    pm = new NoPersistenceManager();
060                            }
061                    }
062                    else if (transactionManager instanceof JpaTransactionManager) {
063                            pm = new JPAPersistenceManager(((JpaTransactionManager)transactionManager).getEntityManagerFactory(), tm);
064                    }
065                    else {
066                            log.error("Unsupported Spring TransactionManager, lazy-loading disabled");
067                            pm = new NoPersistenceManager();
068                    }
069            }
070    
071    
072            public Object attachEntity(Object entity, String[] propertyNames) {
073                    if (pm instanceof AbstractTidePersistenceManager)
074                            return ((AbstractTidePersistenceManager)pm).attachEntity(this, entity, propertyNames);
075                    return pm.attachEntity(entity, propertyNames);
076            }
077    
078    }