01: /*
02: * Copyright 2007 The Kuali Foundation
03: *
04: * Licensed under the Educational Community License, Version 1.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.opensource.org/licenses/ecl1.php
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package org.kuali.rice.jta;
17:
18: import javax.naming.NamingException;
19: import javax.transaction.TransactionManager;
20:
21: import org.apache.commons.lang.StringUtils;
22: import org.kuali.rice.config.Config;
23: import org.kuali.rice.config.ConfigurationException;
24: import org.kuali.rice.core.Core;
25: import org.springframework.beans.factory.FactoryBean;
26: import org.springframework.jndi.JndiTemplate;
27:
28: /**
29: * Factory bean that supplies a TransactionManager object from the the current context
30: * (i.e. plugin, embedding webapp) Config object map if defined therein (under the Config.TRANSACTION_MANAGER_OBJ key),
31: * from JNDI if {@link Config#TRANSACTION_MANAGER_JNDI} is defined,
32: * or from a default declaratively assigned in containing bean factory.
33: *
34: * @author Kuali Rice Team (kuali-rice@googlegroups.com)
35: */
36: public class TransactionManagerFactoryBean implements FactoryBean {
37:
38: private TransactionManager defaultTransactionManager;
39: private JndiTemplate jndiTemplate;
40:
41: public Object getObject() throws Exception {
42: TransactionManager transactionManager = (TransactionManager) Core
43: .getCurrentContextConfig().getObject(
44: Config.TRANSACTION_MANAGER_OBJ);
45: if (transactionManager == null) {
46: String transactionManagerJndiName = Core
47: .getCurrentContextConfig().getProperty(
48: Config.TRANSACTION_MANAGER_JNDI);
49: if (!StringUtils.isEmpty(transactionManagerJndiName)) {
50: if (this .jndiTemplate == null) {
51: this .jndiTemplate = new JndiTemplate();
52: }
53: try {
54: transactionManager = (TransactionManager) this .jndiTemplate
55: .lookup(transactionManagerJndiName,
56: TransactionManager.class);
57: } catch (NamingException e) {
58: throw new ConfigurationException(
59: "Could not locate the TransactionManager at the given JNDI location: '"
60: + transactionManagerJndiName + "'",
61: e);
62: }
63: }
64:
65: }
66: if (transactionManager != null) {
67: return transactionManager;
68: }
69: return this .defaultTransactionManager;
70: }
71:
72: public Class getObjectType() {
73: return TransactionManager.class;
74: }
75:
76: public boolean isSingleton() {
77: return true;
78: }
79:
80: public void setDefaultTransactionManager(
81: TransactionManager transactionManager) {
82: this .defaultTransactionManager = transactionManager;
83: }
84:
85: public JndiTemplate getJndiTemplate() {
86: return this .jndiTemplate;
87: }
88:
89: public void setJndiTemplate(JndiTemplate jndiTemplate) {
90: this.jndiTemplate = jndiTemplate;
91: }
92:
93: }
|