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