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.1 $
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 return transactionManager;
047 }
048
049 public Class getObjectType() {
050 return GeronimoTransactionManager.class;
051 }
052
053 public boolean isSingleton() {
054 return true;
055 }
056
057 public void afterPropertiesSet() throws Exception {
058 transactionManager = new GeronimoTransactionManager(getTransactionContextManager());
059 }
060
061 public void setApplicationContext(ApplicationContext applicationContext) {
062 this.applicationContext = applicationContext;
063 }
064
065 public TransactionContextManager getTransactionContextManager() {
066 if (transactionContextManager == null) {
067 Map map = applicationContext.getBeansOfType(TransactionContextManager.class);
068 if (map.size() == 1) {
069 transactionContextManager = (TransactionContextManager) map.values().iterator().next();
070 } else {
071 throw new IllegalStateException("no TransactionContextManager is registered");
072 }
073 }
074 return transactionContextManager;
075 }
076
077 public void setTransactionContextManager(TransactionContextManager transactionContextManager) {
078 this.transactionContextManager = transactionContextManager;
079 }
080
081 }