1 /***
2 *
3 * Copyright 2005 LogicBlaze, Inc. http://www.logicblaze.com
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 **/
18 package org.jencks.factory;
19
20 import java.util.Map;
21
22 import org.apache.geronimo.transaction.context.GeronimoTransactionManager;
23 import org.apache.geronimo.transaction.context.TransactionContextManager;
24 import org.springframework.beans.factory.FactoryBean;
25 import org.springframework.beans.factory.InitializingBean;
26 import org.springframework.context.ApplicationContext;
27 import org.springframework.context.ApplicationContextAware;
28
29 /***
30 * This FactoryBean creates and configures the Geronimo implementation
31 * of the UserTransaction and TransactionManager interfaces.
32 * <p/>
33 * This factory is based on the Geronimo Transaction Context Manager.
34 *
35 * @version $Revision: 1.2 $
36 * @author <a href="mailto:gnt@codehaus.org">Guillaume Nodet</a>
37 * @see org.apache.geronimo.transaction.context.GeronimoTransactionManager
38 */
39 public class GeronimoTransactionManagerFactoryBean implements FactoryBean, InitializingBean, ApplicationContextAware {
40
41 private GeronimoTransactionManager transactionManager;
42 private ApplicationContext applicationContext;
43 private TransactionContextManager transactionContextManager;
44
45 public Object getObject() throws Exception {
46 if (transactionManager == null) {
47 transactionManager = new GeronimoTransactionManager(getTransactionContextManager());
48 }
49 return transactionManager;
50 }
51
52 public Class getObjectType() {
53 return GeronimoTransactionManager.class;
54 }
55
56 public boolean isSingleton() {
57 return true;
58 }
59
60 public void afterPropertiesSet() throws Exception {
61 }
62
63 public void setApplicationContext(ApplicationContext applicationContext) {
64 this.applicationContext = applicationContext;
65 }
66
67 public TransactionContextManager getTransactionContextManager() {
68 if (transactionContextManager == null) {
69 Map map = applicationContext.getBeansOfType(TransactionContextManager.class);
70 if (map.size() == 1) {
71 transactionContextManager = (TransactionContextManager) map.values().iterator().next();
72 } else {
73 throw new IllegalStateException("no TransactionContextManager is registered");
74 }
75 }
76 return transactionContextManager;
77 }
78
79 public void setTransactionContextManager(TransactionContextManager transactionContextManager) {
80 this.transactionContextManager = transactionContextManager;
81 }
82
83 }