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.Collection;
21 import java.util.Map;
22
23 import javax.resource.spi.BootstrapContext;
24 import javax.resource.spi.work.WorkManager;
25 import javax.transaction.xa.XAException;
26
27 import org.apache.geronimo.connector.work.GeronimoWorkManager;
28 import org.apache.geronimo.transaction.ExtendedTransactionManager;
29 import org.apache.geronimo.transaction.context.TransactionContextManager;
30 import org.apache.geronimo.transaction.log.UnrecoverableLog;
31 import org.apache.geronimo.transaction.manager.TransactionLog;
32 import org.apache.geronimo.transaction.manager.TransactionManagerImpl;
33 import org.apache.geronimo.transaction.manager.XidImporter;
34 import org.springframework.beans.factory.FactoryBean;
35 import org.springframework.beans.factory.InitializingBean;
36 import org.springframework.context.ApplicationContext;
37 import org.springframework.context.ApplicationContextAware;
38
39 /***
40 * A Spring {@link FactoryBean} for creating a {@link BootstrapContext} for the JCA container
41 * with the {@link WorkManager} and {@link ExtendedTransactionManager}.
42 *
43 * @version $Revision: 1.6 $
44 */
45 public class WorkManagerFactoryBean implements FactoryBean, InitializingBean, ApplicationContextAware {
46
47 private ApplicationContext applicationContext;
48 private GeronimoWorkManager workManager;
49 private TransactionContextManager transactionContextManager;
50 private int threadPoolSize = 30;
51 private ExtendedTransactionManager transactionManager;
52 private XidImporter xidImporter;
53 private int defaultTransactionTimeoutSeconds = 600;
54 private TransactionLog transactionLog;
55 private Collection resourceManagers;
56
57 public Object getObject() throws Exception {
58 return getWorkManager();
59 }
60
61 public Class getObjectType() {
62 return WorkManager.class;
63 }
64
65 public boolean isSingleton() {
66 return true;
67 }
68
69 public void setApplicationContext(ApplicationContext applicationContext) {
70 this.applicationContext = applicationContext;
71 }
72
73 public void afterPropertiesSet() throws Exception {
74 }
75
76 public GeronimoWorkManager getWorkManager() throws Exception {
77 if (workManager == null) {
78 workManager = createWorkManager();
79 workManager.doStart();
80 }
81 return workManager;
82 }
83
84 public TransactionContextManager getTransactionContextManager() throws XAException {
85 if (transactionContextManager == null && applicationContext != null) {
86 Map map = applicationContext.getBeansOfType(TransactionContextManager.class);
87 if (map.size() > 1) {
88 throw new IllegalStateException("only one TransactionContextManager can be registered");
89 } else if (map.size() == 1) {
90 transactionContextManager = (TransactionContextManager) map.values().iterator().next();
91 }
92 }
93 if (transactionContextManager == null) {
94 transactionContextManager = createTransactionContextManager();
95 }
96 return transactionContextManager;
97 }
98
99 public void setTransactionContextManager(TransactionContextManager transactionContextManager) {
100 this.transactionContextManager = transactionContextManager;
101 }
102
103 public int getThreadPoolSize() {
104 return threadPoolSize;
105 }
106
107 public void setThreadPoolSize(int threadPoolSize) {
108 this.threadPoolSize = threadPoolSize;
109 }
110
111 public ExtendedTransactionManager getTransactionManager() throws XAException {
112 if (transactionManager == null && applicationContext != null) {
113 Map map = applicationContext.getBeansOfType(ExtendedTransactionManager.class);
114 if (map.size() > 1) {
115 throw new IllegalStateException("only one ExtendedTransactionManager can be registered");
116 } else if (map.size() == 1) {
117 transactionManager = (ExtendedTransactionManager) map.values().iterator().next();
118 }
119 }
120 if (transactionManager == null) {
121 transactionManager = new TransactionManagerImpl(getDefaultTransactionTimeoutSeconds(), getTransactionLog(), getResourceManagers());
122 }
123 return transactionManager;
124 }
125
126 public void setTransactionManager(ExtendedTransactionManager transactionManager) {
127 this.transactionManager = transactionManager;
128 }
129
130 public XidImporter getXidImporter() {
131 if (xidImporter == null && transactionManager instanceof XidImporter) {
132 xidImporter = (XidImporter) transactionManager;
133 }
134 return xidImporter;
135 }
136
137 public void setXidImporter(XidImporter xidImporter) {
138 this.xidImporter = xidImporter;
139 }
140
141 public int getDefaultTransactionTimeoutSeconds() {
142 return defaultTransactionTimeoutSeconds;
143 }
144
145 public void setDefaultTransactionTimeoutSeconds(int defaultTransactionTimeoutSeconds) {
146 this.defaultTransactionTimeoutSeconds = defaultTransactionTimeoutSeconds;
147 }
148
149 public TransactionLog getTransactionLog() {
150 if (transactionLog == null) {
151 transactionLog = new UnrecoverableLog();
152 }
153 return transactionLog;
154 }
155
156 public void setTransactionLog(TransactionLog transactionLog) {
157 this.transactionLog = transactionLog;
158 }
159
160 public Collection getResourceManagers() {
161 return resourceManagers;
162 }
163
164 public void setResourceManagers(Collection resourceManagers) {
165 this.resourceManagers = resourceManagers;
166 }
167
168
169
170 protected TransactionContextManager createTransactionContextManager() throws XAException {
171 return new TransactionContextManager(getTransactionManager(), getXidImporter());
172 }
173
174 protected GeronimoWorkManager createWorkManager() throws XAException {
175 return new GeronimoWorkManager(getThreadPoolSize(), getTransactionContextManager());
176 }
177 }