001    /**
002     * 
003     * Copyright 2005 LogicBlaze, Inc. http://www.logicblaze.com
004     * 
005     * Licensed under the Apache License, Version 2.0 (the "License"); 
006     * you may not use this file except in compliance with the License. 
007     * You may obtain a copy of the License at 
008     * 
009     * http://www.apache.org/licenses/LICENSE-2.0
010     * 
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS, 
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
014     * See the License for the specific language governing permissions and 
015     * limitations under the License. 
016     * 
017     **/
018    package org.jencks.factory;
019    
020    import java.util.Map;
021    
022    import org.apache.geronimo.transaction.context.GeronimoTransactionManager;
023    import org.apache.geronimo.transaction.context.TransactionContextManager;
024    import org.springframework.beans.factory.FactoryBean;
025    import org.springframework.beans.factory.InitializingBean;
026    import org.springframework.context.ApplicationContext;
027    import org.springframework.context.ApplicationContextAware;
028    
029    /**
030     * This FactoryBean creates and configures the Geronimo implementation
031     * of the UserTransaction and TransactionManager interfaces.
032     * <p/>
033     * This factory is based on the Geronimo Transaction Context Manager.
034     *
035     * @version $Revision: 1.2 $
036     * @author <a href="mailto:gnt@codehaus.org">Guillaume Nodet</a>
037     * @see org.apache.geronimo.transaction.context.GeronimoTransactionManager
038     */
039    public class GeronimoTransactionManagerFactoryBean implements FactoryBean, InitializingBean, ApplicationContextAware {
040    
041            private GeronimoTransactionManager transactionManager;
042            private ApplicationContext applicationContext;
043        private TransactionContextManager transactionContextManager;
044            
045            public Object getObject() throws Exception {
046                    if (transactionManager == null) {
047                            transactionManager = new GeronimoTransactionManager(getTransactionContextManager());
048                    }
049                    return transactionManager;
050            }
051    
052            public Class getObjectType() {
053                    return GeronimoTransactionManager.class;
054            }
055    
056            public boolean isSingleton() {
057                    return true;
058            }
059    
060            public void afterPropertiesSet() throws Exception {
061            }
062    
063            public void setApplicationContext(ApplicationContext applicationContext) {
064                    this.applicationContext = applicationContext;
065            }
066    
067            public TransactionContextManager getTransactionContextManager() {
068                    if (transactionContextManager == null) {
069                            Map map = applicationContext.getBeansOfType(TransactionContextManager.class);
070                            if (map.size() == 1) {
071                                    transactionContextManager = (TransactionContextManager) map.values().iterator().next();
072                            } else {
073                                    throw new IllegalStateException("no TransactionContextManager is registered");
074                            }
075                    }
076                    return transactionContextManager;
077            }
078    
079            public void setTransactionContextManager(TransactionContextManager transactionContextManager) {
080                    this.transactionContextManager = transactionContextManager;
081            }
082    
083    }