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.UserTransaction;
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 UserTransaction object from the the current context
30: * (i.e. plugin, embedding webapp) Config object map if defined therein (under the Config.USER_TRANSACTION key),
31: * from JNDI if {@link Config#USER_TRANSACTION_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 UserTransactionFactoryBean implements FactoryBean {
37:
38: private UserTransaction defaultUserTransaction;
39: private JndiTemplate jndiTemplate;
40:
41: public Object getObject() throws Exception {
42: UserTransaction userTransaction = (UserTransaction) Core
43: .getCurrentContextConfig().getObject(
44: Config.USER_TRANSACTION_OBJ);
45: if (userTransaction == null) {
46: String userTransactionJndiName = Core
47: .getCurrentContextConfig().getProperty(
48: Config.USER_TRANSACTION_JNDI);
49: if (!StringUtils.isEmpty(userTransactionJndiName)) {
50: if (this .jndiTemplate == null) {
51: this .jndiTemplate = new JndiTemplate();
52: }
53: try {
54: userTransaction = (UserTransaction) this .jndiTemplate
55: .lookup(userTransactionJndiName,
56: UserTransaction.class);
57: } catch (NamingException e) {
58: throw new ConfigurationException(
59: "Could not locate the UserTransaction at the given JNDI location: '"
60: + userTransactionJndiName + "'", e);
61: }
62: }
63:
64: }
65: if (userTransaction != null) {
66: return userTransaction;
67: }
68: return this .defaultUserTransaction;
69: }
70:
71: public Class getObjectType() {
72: return UserTransaction.class;
73: }
74:
75: public boolean isSingleton() {
76: return true;
77: }
78:
79: public void setDefaultUserTransaction(
80: UserTransaction userTransaction) {
81: this .defaultUserTransaction = userTransaction;
82: }
83:
84: public JndiTemplate getJndiTemplate() {
85: return this .jndiTemplate;
86: }
87:
88: public void setJndiTemplate(JndiTemplate jndiTemplate) {
89: this.jndiTemplate = jndiTemplate;
90: }
91:
92: }
|