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          return transactionContextManager;
55      }
56  
57      public Class getObjectType() {
58          return TransactionContextManager.class;
59      }
60  
61      public boolean isSingleton() {
62          return true;
63      }
64  
65  	public void setApplicationContext(ApplicationContext applicationContext) {
66  		this.applicationContext = applicationContext;
67  	}
68  
69      public ExtendedTransactionManager getTransactionManager() throws XAException {
70  		if (transactionManager == null) {
71  			Map map = applicationContext.getBeansOfType(ExtendedTransactionManager.class);
72  			if (map.size() > 1) {
73  				throw new IllegalStateException("only one ExtendedTransactionManager can be registered");
74  			} else if (map.size() == 1) {
75  				transactionManager = (ExtendedTransactionManager) map.values().iterator().next();
76  			} else {
77  	            transactionManager = new TransactionManagerImpl(getDefaultTransactionTimeoutSeconds(), getTransactionLog(), getResourceManagers());
78  			}
79  		}
80          return transactionManager;
81      }
82  
83      public void setTransactionManager(ExtendedTransactionManager transactionManager) {
84          this.transactionManager = transactionManager;
85      }
86  
87      public XidImporter getXidImporter() throws XAException {
88  		if (xidImporter == null) {
89  			if (getTransactionManager() instanceof XidImporter) {
90  				xidImporter = (XidImporter) getTransactionManager(); 
91  			} else {
92  				Map map = applicationContext.getBeansOfType(XidImporter.class);
93  				if (map.size() > 1) {
94  					throw new IllegalStateException("only one XidImporter can be registered");
95  				} else if (map.size() == 1) {
96  					transactionManager = (ExtendedTransactionManager) map.values().iterator().next();
97  				} else {
98  					throw new IllegalStateException("no XidImporter is registered");
99  				}
100 			}
101 		}
102         return xidImporter;
103     }
104     
105     public void setXidImporter(XidImporter xidImporter) {
106     	this.xidImporter = xidImporter;
107     }
108 
109     public int getDefaultTransactionTimeoutSeconds() {
110         return defaultTransactionTimeoutSeconds;
111     }
112 
113     public void setDefaultTransactionTimeoutSeconds(int defaultTransactionTimeoutSeconds) {
114         this.defaultTransactionTimeoutSeconds = defaultTransactionTimeoutSeconds;
115     }
116 
117     public TransactionLog getTransactionLog() {
118         if (transactionLog == null) {
119             transactionLog = new UnrecoverableLog();
120         }
121         return transactionLog;
122     }
123 
124     public void setTransactionLog(TransactionLog transactionLog) {
125         this.transactionLog = transactionLog;
126     }
127 
128     public Collection getResourceManagers() {
129         return resourceManagers;
130     }
131 
132     public void setResourceManagers(Collection resourceManagers) {
133         this.resourceManagers = resourceManagers;
134     }
135 
136     /***
137      * This method initializes the transaction context manager basing on
138      * the Geronimo implementation of the transaction manager and a dedicated
139      * transaction log.
140      */
141     public void afterPropertiesSet() throws Exception {
142         // Instanciate the transaction context manager
143         this.transactionContextManager = new TransactionContextManager(getTransactionManager(), getXidImporter());
144     }
145     
146 
147 }