001 /*
002 * Copyright 2002-2005 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017 package org.jencks.factory;
018
019 import java.util.Collection;
020 import java.util.Map;
021
022 import javax.transaction.xa.XAException;
023
024 import org.apache.geronimo.transaction.ExtendedTransactionManager;
025 import org.apache.geronimo.transaction.context.TransactionContextManager;
026 import org.apache.geronimo.transaction.log.UnrecoverableLog;
027 import org.apache.geronimo.transaction.manager.TransactionLog;
028 import org.apache.geronimo.transaction.manager.TransactionManagerImpl;
029 import org.apache.geronimo.transaction.manager.XidImporter;
030 import org.springframework.beans.factory.FactoryBean;
031 import org.springframework.beans.factory.InitializingBean;
032 import org.springframework.context.ApplicationContext;
033 import org.springframework.context.ApplicationContextAware;
034
035 /**
036 * This FactoryBean creates and configures the TransactionManagerContext
037 * of Geronimo.
038 *
039 * @author Thierry Templier
040 * @see org.apache.geronimo.transaction.log.UnrecoverableLog
041 * @see org.apache.geronimo.transaction.log.HOWLLog
042 */
043 public class TransactionContextManagerFactoryBean implements FactoryBean, InitializingBean, ApplicationContextAware {
044
045 private XidImporter xidImporter;
046 private ExtendedTransactionManager transactionManager;
047 private ApplicationContext applicationContext;
048 private TransactionContextManager transactionContextManager;
049 private int defaultTransactionTimeoutSeconds = 600;
050 private TransactionLog transactionLog;
051 private Collection resourceManagers;
052
053 public Object getObject() throws Exception {
054 return transactionContextManager;
055 }
056
057 public Class getObjectType() {
058 return TransactionContextManager.class;
059 }
060
061 public boolean isSingleton() {
062 return true;
063 }
064
065 public void setApplicationContext(ApplicationContext applicationContext) {
066 this.applicationContext = applicationContext;
067 }
068
069 public ExtendedTransactionManager getTransactionManager() throws XAException {
070 if (transactionManager == null) {
071 Map map = applicationContext.getBeansOfType(ExtendedTransactionManager.class);
072 if (map.size() > 1) {
073 throw new IllegalStateException("only one ExtendedTransactionManager can be registered");
074 } else if (map.size() == 1) {
075 transactionManager = (ExtendedTransactionManager) map.values().iterator().next();
076 } else {
077 transactionManager = new TransactionManagerImpl(getDefaultTransactionTimeoutSeconds(), getTransactionLog(), getResourceManagers());
078 }
079 }
080 return transactionManager;
081 }
082
083 public void setTransactionManager(ExtendedTransactionManager transactionManager) {
084 this.transactionManager = transactionManager;
085 }
086
087 public XidImporter getXidImporter() throws XAException {
088 if (xidImporter == null) {
089 if (getTransactionManager() instanceof XidImporter) {
090 xidImporter = (XidImporter) getTransactionManager();
091 } else {
092 Map map = applicationContext.getBeansOfType(XidImporter.class);
093 if (map.size() > 1) {
094 throw new IllegalStateException("only one XidImporter can be registered");
095 } else if (map.size() == 1) {
096 transactionManager = (ExtendedTransactionManager) map.values().iterator().next();
097 } else {
098 throw new IllegalStateException("no XidImporter is registered");
099 }
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 }