View Javadoc

1   /*
2    * Copyright 2002-2005 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
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  
17  package org.jencks.factory;
18  
19  import java.util.Collection;
20  import java.util.Map;
21  
22  import javax.transaction.xa.XAException;
23  
24  import org.apache.geronimo.transaction.ExtendedTransactionManager;
25  import org.apache.geronimo.transaction.context.TransactionContextManager;
26  import org.apache.geronimo.transaction.log.UnrecoverableLog;
27  import org.apache.geronimo.transaction.manager.TransactionLog;
28  import org.apache.geronimo.transaction.manager.TransactionManagerImpl;
29  import org.apache.geronimo.transaction.manager.XidImporter;
30  import org.springframework.beans.factory.FactoryBean;
31  import org.springframework.beans.factory.InitializingBean;
32  import org.springframework.context.ApplicationContext;
33  import org.springframework.context.ApplicationContextAware;
34  
35  /***
36   * This FactoryBean creates and configures the TransactionManagerContext
37   * of Geronimo.
38   *
39   * @author Thierry Templier
40   * @see org.apache.geronimo.transaction.log.UnrecoverableLog
41   * @see org.apache.geronimo.transaction.log.HOWLLog
42   */
43  public class TransactionContextManagerFactoryBean implements FactoryBean, InitializingBean, ApplicationContextAware {
44  
45  	private XidImporter xidImporter;
46      private ExtendedTransactionManager transactionManager;
47  	private ApplicationContext applicationContext;
48      private TransactionContextManager transactionContextManager;
49      private int defaultTransactionTimeoutSeconds = 600;
50      private TransactionLog transactionLog;
51      private Collection resourceManagers;
52  
53      public Object getObject() throws Exception {
54      	if (transactionContextManager == null) {
55              // Instanciate the transaction context manager
56              this.transactionContextManager = new TransactionContextManager(getTransactionManager(), getXidImporter());
57      	}
58          return transactionContextManager;
59      }
60  
61      public Class getObjectType() {
62          return TransactionContextManager.class;
63      }
64  
65      public boolean isSingleton() {
66          return true;
67      }
68  
69  	public void setApplicationContext(ApplicationContext applicationContext) {
70  		this.applicationContext = applicationContext;
71  	}
72  
73      public ExtendedTransactionManager getTransactionManager() throws XAException {
74  		if (transactionManager == null) {
75  			Map map = applicationContext.getBeansOfType(ExtendedTransactionManager.class);
76  			if (map.size() > 1) {
77  				throw new IllegalStateException("only one ExtendedTransactionManager can be registered");
78  			} else if (map.size() == 1) {
79  				transactionManager = (ExtendedTransactionManager) map.values().iterator().next();
80  			} else {
81  	            transactionManager = new TransactionManagerImpl(getDefaultTransactionTimeoutSeconds(), getTransactionLog(), getResourceManagers());
82  			}
83  		}
84          return transactionManager;
85      }
86  
87      public void setTransactionManager(ExtendedTransactionManager transactionManager) {
88          this.transactionManager = transactionManager;
89      }
90  
91      public XidImporter getXidImporter() throws XAException {
92  		if (xidImporter == null) {
93  			if (getTransactionManager() instanceof XidImporter) {
94  				xidImporter = (XidImporter) getTransactionManager(); 
95  			} else {
96  				Map map = applicationContext.getBeansOfType(XidImporter.class);
97  				if (map.size() > 1) {
98  					throw new IllegalStateException("only one XidImporter can be registered");
99  				} else if (map.size() == 1) {
100 					transactionManager = (ExtendedTransactionManager) map.values().iterator().next();
101 				} else {
102 					throw new IllegalStateException("no XidImporter is registered");
103 				}
104 			}
105 		}
106         return xidImporter;
107     }
108     
109     public void setXidImporter(XidImporter xidImporter) {
110     	this.xidImporter = xidImporter;
111     }
112 
113     public int getDefaultTransactionTimeoutSeconds() {
114         return defaultTransactionTimeoutSeconds;
115     }
116 
117     public void setDefaultTransactionTimeoutSeconds(int defaultTransactionTimeoutSeconds) {
118         this.defaultTransactionTimeoutSeconds = defaultTransactionTimeoutSeconds;
119     }
120 
121     public TransactionLog getTransactionLog() {
122         if (transactionLog == null) {
123             transactionLog = new UnrecoverableLog();
124         }
125         return transactionLog;
126     }
127 
128     public void setTransactionLog(TransactionLog transactionLog) {
129         this.transactionLog = transactionLog;
130     }
131 
132     public Collection getResourceManagers() {
133         return resourceManagers;
134     }
135 
136     public void setResourceManagers(Collection resourceManagers) {
137         this.resourceManagers = resourceManagers;
138     }
139 
140     /***
141      * This method initializes the transaction context manager basing on
142      * the Geronimo implementation of the transaction manager and a dedicated
143      * transaction log.
144      */
145     public void afterPropertiesSet() throws Exception {
146     }
147     
148 
149 }