001    /**
002     * Copyright 2005-2013 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.core.framework.persistence.ojb;
017    
018    import javax.transaction.TransactionManager;
019    
020    import org.apache.ojb.broker.transaction.tm.TransactionManagerFactoryException;
021    import org.kuali.rice.core.api.exception.RiceRuntimeException;
022    
023    /**
024     * <p>An implementation of an OJB TransactionManagerFactory which provides access to Workflow's
025     * JTA UserTransaction.</p>
026     *
027     * <p>If the TransactionManager singleton has been set via {@link #setTransactionManager(TransactionManager)}
028     * then that reference is returned, otherwise the TransactionManager is pulled from Workflow's Spring core
029     * via the SpringServiceLocator.</p>
030     *
031     * <p>When accessed from outside the workflow core (i.e. embedded mode), the transaction manager
032     * singleton MUST explicitly be set - it cannot be resolved through the SpringServiceLocator.</p>
033     *
034     * <p>Note: if OJB is caused to initialize DURING Spring initialization (for example, by programmatically
035     * obtaining the OJB PersistenceBrokerFactory to set the platform attribute of connection descriptors
036     * from within a bean initialized by Spring), the TransactionManager singleton MUST be set beforehand,
037     * otherwise NPE will result from attempting to traverse SpringServiceLocator as the GlobalResourceLoader
038     * will not have been initialized yet).</p>
039     *
040     * <p>This TransactionManagerFactory implementation is specified in OJB via the following
041     * setting the OJB properties:</p>
042     * <blockquote>
043     *   <code>
044     *     JTATransactionManagerClass=org.kuali.rice.core.database.WorkflowTransactionManagerFactory
045     *   </code>
046     * </blockquote>
047     *
048     * @author Kuali Rice Team (rice.collab@kuali.org)
049     */
050    public class TransactionManagerFactory implements org.apache.ojb.broker.transaction.tm.TransactionManagerFactory {
051    
052            private static TransactionManager transactionManager;
053    
054            public TransactionManager getTransactionManager() throws TransactionManagerFactoryException {
055                // return SpringServiceLocator.getJtaTransactionManager().getTransactionManager();
056                    if (transactionManager == null) {
057                            throw new RiceRuntimeException("The JTA Transaction Manager for OJB was not configured properly.");
058                    }
059                    return transactionManager;
060                    // core and plugins
061                    // TODO what to do here
062            //return KSBServiceLocator.getTransactionManager();
063            }
064    
065            public static void setTransactionManager(TransactionManager transactionManager) {
066                    TransactionManagerFactory.transactionManager = transactionManager;
067            }
068    
069    }