001    /**
002     * 
003     * Copyright 2005 LogicBlaze, Inc. http://www.logicblaze.com
004     * 
005     * Licensed under the Apache License, Version 2.0 (the "License"); 
006     * you may not use this file except in compliance with the License. 
007     * You may obtain a copy of the License at 
008     * 
009     * http://www.apache.org/licenses/LICENSE-2.0
010     * 
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS, 
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
014     * See the License for the specific language governing permissions and 
015     * limitations under the License. 
016     * 
017     **/
018    package org.jencks.factory;
019    
020    import org.apache.geronimo.connector.BootstrapContextImpl;
021    import org.apache.geronimo.connector.work.GeronimoWorkManager;
022    import org.apache.geronimo.gbean.ReferenceCollection;
023    import org.apache.geronimo.transaction.ExtendedTransactionManager;
024    import org.apache.geronimo.transaction.context.TransactionContextManager;
025    import org.apache.geronimo.transaction.log.UnrecoverableLog;
026    import org.apache.geronimo.transaction.manager.TransactionLog;
027    import org.apache.geronimo.transaction.manager.TransactionManagerImpl;
028    import org.apache.geronimo.transaction.manager.XidImporter;
029    import org.springframework.beans.factory.FactoryBean;
030    import org.springframework.beans.factory.InitializingBean;
031    
032    import javax.resource.spi.BootstrapContext;
033    import javax.resource.spi.work.WorkManager;
034    import javax.transaction.xa.XAException;
035    
036    /**
037     * A Spring {@link FactoryBean} for creating a {@link BootstrapContext} for the JCA container
038     * with the {@link WorkManager} and {@link ExtendedTransactionManager}.
039     *
040     * @version $Revision: 1.2 $
041     */
042    public class WorkManagerFactoryBean implements FactoryBean, InitializingBean {
043    
044        private GeronimoWorkManager workManager;
045        private TransactionContextManager transactionContextManager;
046        private int threadPoolSize = 30;
047        private ExtendedTransactionManager transactionManager;
048        private XidImporter xidImporter;
049        private int defaultTransactionTimeoutSeconds = 600;
050        private TransactionLog transactionLog;
051        private ReferenceCollection resourceManagers;
052    
053        public Object getObject() throws Exception {
054            return workManager;
055        }
056    
057        public Class getObjectType() {
058            return WorkManager.class;
059        }
060    
061        public boolean isSingleton() {
062            return true;
063        }
064    
065        public void afterPropertiesSet() throws Exception {
066            workManager = createWorkManager();
067            workManager.doStart();
068        }
069    
070        public GeronimoWorkManager getWorkManager() throws Exception {
071            if (workManager == null) {
072                afterPropertiesSet();
073            }
074            return workManager;
075        }
076    
077        public TransactionContextManager getTransactionContextManager() throws XAException {
078            if (transactionContextManager == null) {
079                transactionContextManager = createTransactionContextManager();
080            }
081            return transactionContextManager;
082        }
083    
084        public void setTransactionContextManager(TransactionContextManager transactionContextManager) {
085            this.transactionContextManager = transactionContextManager;
086        }
087    
088        public int getThreadPoolSize() {
089            return threadPoolSize;
090        }
091    
092        public void setThreadPoolSize(int threadPoolSize) {
093            this.threadPoolSize = threadPoolSize;
094        }
095    
096        public ExtendedTransactionManager getTransactionManager() throws XAException {
097            if (transactionManager == null) {
098                transactionManager = new TransactionManagerImpl(getDefaultTransactionTimeoutSeconds(), getTransactionLog(), getResourceManagers());
099            }
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        // Implementation methods
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    }