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            if (transactionContextManager == null) {
055                // Instanciate the transaction context manager
056                this.transactionContextManager = new TransactionContextManager(getTransactionManager(), getXidImporter());
057            }
058            return transactionContextManager;
059        }
060    
061        public Class getObjectType() {
062            return TransactionContextManager.class;
063        }
064    
065        public boolean isSingleton() {
066            return true;
067        }
068    
069            public void setApplicationContext(ApplicationContext applicationContext) {
070                    this.applicationContext = applicationContext;
071            }
072    
073        public ExtendedTransactionManager getTransactionManager() throws XAException {
074                    if (transactionManager == null) {
075                            Map map = applicationContext.getBeansOfType(ExtendedTransactionManager.class);
076                            if (map.size() > 1) {
077                                    throw new IllegalStateException("only one ExtendedTransactionManager can be registered");
078                            } else if (map.size() == 1) {
079                                    transactionManager = (ExtendedTransactionManager) map.values().iterator().next();
080                            } else {
081                        transactionManager = new TransactionManagerImpl(getDefaultTransactionTimeoutSeconds(), getTransactionLog(), getResourceManagers());
082                            }
083                    }
084            return transactionManager;
085        }
086    
087        public void setTransactionManager(ExtendedTransactionManager transactionManager) {
088            this.transactionManager = transactionManager;
089        }
090    
091        public XidImporter getXidImporter() throws XAException {
092                    if (xidImporter == null) {
093                            if (getTransactionManager() instanceof XidImporter) {
094                                    xidImporter = (XidImporter) getTransactionManager(); 
095                            } else {
096                                    Map map = applicationContext.getBeansOfType(XidImporter.class);
097                                    if (map.size() > 1) {
098                                            throw new IllegalStateException("only one XidImporter can be registered");
099                                    } 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    }