View Javadoc

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.5 $
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 workManager;
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          workManager = createWorkManager();
75          workManager.doStart();
76      }
77  
78      public GeronimoWorkManager getWorkManager() throws Exception {
79          if (workManager == null) {
80              afterPropertiesSet();
81          }
82          return workManager;
83      }
84  
85      public TransactionContextManager getTransactionContextManager() throws XAException {
86  		if (transactionContextManager == null && applicationContext != null) {
87  			Map map = applicationContext.getBeansOfType(TransactionContextManager.class);
88  			if (map.size() > 1) {
89  				throw new IllegalStateException("only one TransactionContextManager can be registered");
90  			} else if (map.size() == 1) {
91  				transactionContextManager = (TransactionContextManager) map.values().iterator().next();
92  			}
93  		}
94  		if (transactionContextManager == null) {
95              transactionContextManager = createTransactionContextManager();
96  		}
97          return transactionContextManager;
98      }
99  
100     public void setTransactionContextManager(TransactionContextManager transactionContextManager) {
101         this.transactionContextManager = transactionContextManager;
102     }
103 
104     public int getThreadPoolSize() {
105         return threadPoolSize;
106     }
107 
108     public void setThreadPoolSize(int threadPoolSize) {
109         this.threadPoolSize = threadPoolSize;
110     }
111 
112     public ExtendedTransactionManager getTransactionManager() throws XAException {
113 		if (transactionManager == null && applicationContext != null) {
114 			Map map = applicationContext.getBeansOfType(ExtendedTransactionManager.class);
115 			if (map.size() > 1) {
116 				throw new IllegalStateException("only one ExtendedTransactionManager can be registered");
117 			} else if (map.size() == 1) {
118 				transactionManager = (ExtendedTransactionManager) map.values().iterator().next();
119 			}
120 		}
121 		if (transactionManager == null) {
122             transactionManager = new TransactionManagerImpl(getDefaultTransactionTimeoutSeconds(), getTransactionLog(), getResourceManagers());
123 		}
124         return transactionManager;
125     }
126 
127     public void setTransactionManager(ExtendedTransactionManager transactionManager) {
128         this.transactionManager = transactionManager;
129     }
130 
131     public XidImporter getXidImporter() {
132         if (xidImporter == null && transactionManager instanceof XidImporter) {
133             xidImporter = (XidImporter) transactionManager;
134         }
135         return xidImporter;
136     }
137 
138     public void setXidImporter(XidImporter xidImporter) {
139         this.xidImporter = xidImporter;
140     }
141 
142     public int getDefaultTransactionTimeoutSeconds() {
143         return defaultTransactionTimeoutSeconds;
144     }
145 
146     public void setDefaultTransactionTimeoutSeconds(int defaultTransactionTimeoutSeconds) {
147         this.defaultTransactionTimeoutSeconds = defaultTransactionTimeoutSeconds;
148     }
149 
150     public TransactionLog getTransactionLog() {
151         if (transactionLog == null) {
152             transactionLog = new UnrecoverableLog();
153         }
154         return transactionLog;
155     }
156 
157     public void setTransactionLog(TransactionLog transactionLog) {
158         this.transactionLog = transactionLog;
159     }
160 
161     public Collection getResourceManagers() {
162         return resourceManagers;
163     }
164 
165     public void setResourceManagers(Collection resourceManagers) {
166         this.resourceManagers = resourceManagers;
167     }
168 
169     // Implementation methods
170     //-------------------------------------------------------------------------
171     protected TransactionContextManager createTransactionContextManager() throws XAException {
172         return new TransactionContextManager(getTransactionManager(), getXidImporter());
173     }
174 
175     protected GeronimoWorkManager createWorkManager() throws XAException {
176         return new GeronimoWorkManager(getThreadPoolSize(), getTransactionContextManager());
177     }
178 }