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.1 $
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 return transactionManager;
47 }
48
49 public Class getObjectType() {
50 return GeronimoTransactionManager.class;
51 }
52
53 public boolean isSingleton() {
54 return true;
55 }
56
57 public void afterPropertiesSet() throws Exception {
58 transactionManager = new GeronimoTransactionManager(getTransactionContextManager());
59 }
60
61 public void setApplicationContext(ApplicationContext applicationContext) {
62 this.applicationContext = applicationContext;
63 }
64
65 public TransactionContextManager getTransactionContextManager() {
66 if (transactionContextManager == null) {
67 Map map = applicationContext.getBeansOfType(TransactionContextManager.class);
68 if (map.size() == 1) {
69 transactionContextManager = (TransactionContextManager) map.values().iterator().next();
70 } else {
71 throw new IllegalStateException("no TransactionContextManager is registered");
72 }
73 }
74 return transactionContextManager;
75 }
76
77 public void setTransactionContextManager(TransactionContextManager transactionContextManager) {
78 this.transactionContextManager = transactionContextManager;
79 }
80
81 }