01: /*
02: * Copyright 2005-2006 The Kuali Foundation.
03: *
04: *
05: * Licensed under the Educational Community License, Version 1.0 (the "License");
06: * you may not use this file except in compliance with the License.
07: * You may obtain a copy of the License at
08: *
09: * http://www.opensource.org/licenses/ecl1.php
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package edu.iu.uis.eden.core.dependencylifecycles;
18:
19: import javax.transaction.TransactionManager;
20:
21: import org.apache.commons.lang.StringUtils;
22: import org.kuali.rice.core.Core;
23: import org.kuali.rice.lifecycle.BaseLifecycle;
24: import org.objectweb.jotm.Current;
25: import org.springframework.beans.factory.BeanFactory;
26: import org.springframework.beans.factory.BeanFactoryAware;
27: import org.springframework.context.ApplicationEvent;
28: import org.springframework.context.ApplicationListener;
29: import org.springframework.context.event.ContextClosedEvent;
30: import org.springframework.context.event.ContextRefreshedEvent;
31:
32: import edu.iu.uis.eden.EdenConstants;
33: import edu.iu.uis.eden.KEWServiceLocator;
34: import edu.iu.uis.eden.exception.WorkflowRuntimeException;
35:
36: /**
37: *
38: * @author ewestfal
39: */
40: public class TransactionTimeoutLifecycle extends BaseLifecycle
41: implements ApplicationListener, BeanFactoryAware {
42:
43: private BeanFactory beanFactory;
44:
45: @Override
46: public void start() throws Exception {
47: TransactionManager transactionManager = (TransactionManager) beanFactory
48: .getBean(KEWServiceLocator.JTA_TRANSACTION_MANAGER);
49: if (transactionManager instanceof Current) {
50: Current jotm = (Current) transactionManager;
51: if (!StringUtils.isBlank(Core.getRootConfig()
52: .getTransactionTimeout())) {
53: jotm.setDefaultTimeout(Integer.parseInt(Core
54: .getRootConfig().getTransactionTimeout()));
55: jotm.setTransactionTimeout(jotm.getDefaultTimeout());
56: } else {
57: jotm
58: .setDefaultTimeout(EdenConstants.DEFAULT_TRANSACTION_TIMEOUT_SECONDS);
59: jotm.setTransactionTimeout(jotm.getDefaultTimeout());
60: }
61: }
62: super .start();
63: }
64:
65: /**
66: * Initialize this LifeCycle when spring starts.
67: */
68: public void onApplicationEvent(ApplicationEvent event) {
69: try {
70: if (event instanceof ContextRefreshedEvent) {
71: if (isStarted()) {
72: stop();
73: }
74: start();
75: } else if (event instanceof ContextClosedEvent) {
76: stop();
77: }
78: } catch (Exception e) {
79: if (e instanceof RuntimeException) {
80: throw (RuntimeException) e;
81: }
82: throw new WorkflowRuntimeException(
83: "Failed to handle application context event: "
84: + event, e);
85: }
86: }
87:
88: public void setBeanFactory(BeanFactory beanFactory) {
89: this.beanFactory = beanFactory;
90: }
91:
92: }
|