001 /* 002 * Copyright 2007-2008 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package org.kuali.rice.test; 017 018 import org.kuali.rice.core.api.lifecycle.Lifecycle; 019 import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader; 020 import org.springframework.transaction.PlatformTransactionManager; 021 import org.springframework.transaction.TransactionStatus; 022 import org.springframework.transaction.support.DefaultTransactionDefinition; 023 024 /** 025 * A lifecycle for testing with database transactional rollback. 026 * @author Kuali Rice Team (rice.collab@kuali.org) 027 */ 028 public class TransactionalLifecycle implements Lifecycle { 029 030 private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger 031 .getLogger(TransactionalLifecycle.class); 032 033 /** 034 * Name of the transaction manager to pull from the GlobalResourceLoader. 035 * This will most likely be a Spring bean name. 036 */ 037 public static final String DEFAULT_TRANSACTION_MANAGER_NAME = "transactionManager"; 038 039 private String transactionManagerName; 040 private PlatformTransactionManager transactionManager; 041 042 public TransactionalLifecycle(String transactionManagerName) { 043 this.transactionManagerName = transactionManagerName; 044 } 045 046 public TransactionalLifecycle() { 047 this(DEFAULT_TRANSACTION_MANAGER_NAME); 048 } 049 050 public void setTransactionManager(PlatformTransactionManager transactionManager) { 051 this.transactionManager = transactionManager; 052 } 053 054 /** 055 * Returns the PlatformTransactionManager configured on this lifecycle. 056 * If none defined, pulls the transaction manager out of the GlobalResourceLoader 057 * @return the transaction manager in the GlobalResourceLoader 058 */ 059 private PlatformTransactionManager getTransactionManager() { 060 if (transactionManager == null) { 061 transactionManager = (PlatformTransactionManager) GlobalResourceLoader.getService(transactionManagerName); 062 } 063 return transactionManager; 064 } 065 066 private boolean started; 067 private TransactionStatus TRANSACTION_STATUS; 068 069 public boolean isStarted() { 070 return started; 071 } 072 073 public void start() throws Exception { 074 LOG.info("Starting a transaction from TransactionalLifecycle..."); 075 DefaultTransactionDefinition defaultTransactionDefinition = new DefaultTransactionDefinition(); 076 defaultTransactionDefinition.setTimeout(3600); 077 TRANSACTION_STATUS = getTransactionManager().getTransaction(defaultTransactionDefinition); 078 started = true; 079 } 080 081 public void stop() throws Exception { 082 LOG.info("...rolling back transaction from TransactionalLifecycle."); 083 getTransactionManager().rollback(TRANSACTION_STATUS); 084 started = false; 085 } 086 087 }