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 org.granite.logging.Logger; 025import org.granite.tide.TidePersistenceManager; 026import org.granite.tide.TideTransactionManager; 027import org.granite.tide.data.AbstractTidePersistenceManager; 028import org.granite.tide.data.JDOPersistenceManager; 029import org.granite.tide.data.NoPersistenceManager; 030import org.granite.util.TypeUtil; 031import org.springframework.orm.hibernate3.HibernateTransactionManager; 032import org.springframework.orm.jdo.JdoTransactionManager; 033import org.springframework.orm.jpa.JpaTransactionManager; 034import org.springframework.transaction.PlatformTransactionManager; 035 036 037/** 038 * Responsible for attaching a session with the persistence mangager 039 * @author Cameron Ingram 040 * @author William Dra� 041 * 042 */ 043public class SpringPersistenceManager implements TidePersistenceManager { 044 045 private static final Logger log = Logger.getLogger(SpringPersistenceManager.class); 046 047 private TidePersistenceManager pm; 048 049 050 public SpringPersistenceManager(PlatformTransactionManager transactionManager) { 051 TideTransactionManager tm = new SpringTransactionManager(transactionManager); 052 if (transactionManager instanceof JpaTransactionManager) { 053 pm = new SpringJPAPersistenceManager((JpaTransactionManager)transactionManager, tm); 054 return; 055 } 056 057 if (transactionManager instanceof JdoTransactionManager) { 058 pm = new JDOPersistenceManager(((JdoTransactionManager)transactionManager).getPersistenceManagerFactory(), tm); 059 return; 060 } 061 062 // Check for Hibernate 3 063 if (transactionManager instanceof HibernateTransactionManager) { 064 try { 065 Object sf = transactionManager.getClass().getMethod("getSessionFactory").invoke(transactionManager); 066 Class<?> sfClass = TypeUtil.forName("org.hibernate.SessionFactory"); 067 pm = (TidePersistenceManager)TypeUtil.newInstance("org.granite.tide.hibernate.HibernatePersistenceManager", 068 new Class<?>[] { sfClass, TideTransactionManager.class }, new Object[] { sf, tm }); 069 } 070 catch (Exception e) { 071 log.error("Could not setup Hibernate 3 persistence manager, lazy-loading disabled. Check that granite-hibernate.jar is present in the classpath."); 072 pm = new NoPersistenceManager(); 073 } 074 return; 075 } 076 077 // Check for Hibernate 4 078 try { 079 Class<?> hibernate4TM = TypeUtil.forName("org.springframework.orm.hibernate4.HibernateTransactionManager"); 080 if (hibernate4TM.isInstance(transactionManager)) { 081 try { 082 Object sf = transactionManager.getClass().getMethod("getSessionFactory").invoke(transactionManager); 083 Class<?> sfClass = TypeUtil.forName("org.hibernate.SessionFactory"); 084 pm = (TidePersistenceManager)TypeUtil.newInstance("org.granite.tide.hibernate4.HibernatePersistenceManager", 085 new Class<?>[] { sfClass, TideTransactionManager.class }, new Object[] { sf, tm }); 086 } 087 catch (Exception e) { 088 log.error("Could not setup Hibernate 4 persistence manager, lazy-loading disabled. Check that granite-hibernate4.jar is present in the classpath."); 089 pm = new NoPersistenceManager(); 090 } 091 return; 092 } 093 } 094 catch (ClassNotFoundException e) { 095 // Hibernate 4 not installed 096 } 097 098 log.error("Unsupported Spring TransactionManager, lazy-loading disabled"); 099 pm = new NoPersistenceManager(); 100 } 101 102 103 public Object attachEntity(Object entity, String[] propertyNames) { 104 if (pm instanceof AbstractTidePersistenceManager) 105 return ((AbstractTidePersistenceManager)pm).attachEntity(this, entity, propertyNames); 106 return pm.attachEntity(entity, propertyNames); 107 } 108 109}