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 java.util.Collection;
021    import java.util.Map;
022    
023    import javax.resource.spi.BootstrapContext;
024    import javax.resource.spi.work.WorkManager;
025    import javax.transaction.xa.XAException;
026    
027    import org.apache.geronimo.connector.work.GeronimoWorkManager;
028    import org.apache.geronimo.transaction.ExtendedTransactionManager;
029    import org.apache.geronimo.transaction.context.TransactionContextManager;
030    import org.apache.geronimo.transaction.log.UnrecoverableLog;
031    import org.apache.geronimo.transaction.manager.TransactionLog;
032    import org.apache.geronimo.transaction.manager.TransactionManagerImpl;
033    import org.apache.geronimo.transaction.manager.XidImporter;
034    import org.springframework.beans.factory.FactoryBean;
035    import org.springframework.beans.factory.InitializingBean;
036    import org.springframework.context.ApplicationContext;
037    import org.springframework.context.ApplicationContextAware;
038    
039    /**
040     * A Spring {@link FactoryBean} for creating a {@link BootstrapContext} for the JCA container
041     * with the {@link WorkManager} and {@link ExtendedTransactionManager}.
042     *
043     * @version $Revision: 1.6 $
044     */
045    public class WorkManagerFactoryBean implements FactoryBean, InitializingBean, ApplicationContextAware {
046    
047            private ApplicationContext applicationContext;
048        private GeronimoWorkManager workManager;
049        private TransactionContextManager transactionContextManager;
050        private int threadPoolSize = 30;
051        private ExtendedTransactionManager transactionManager;
052        private XidImporter xidImporter;
053        private int defaultTransactionTimeoutSeconds = 600;
054        private TransactionLog transactionLog;
055        private Collection resourceManagers;
056    
057        public Object getObject() throws Exception {
058            return getWorkManager();
059        }
060    
061        public Class getObjectType() {
062            return WorkManager.class;
063        }
064    
065        public boolean isSingleton() {
066            return true;
067        }
068        
069        public void setApplicationContext(ApplicationContext applicationContext) {
070            this.applicationContext = applicationContext;
071        }
072    
073        public void afterPropertiesSet() throws Exception {
074        }
075    
076        public GeronimoWorkManager getWorkManager() throws Exception {
077            if (workManager == null) {
078                workManager = createWorkManager();
079                workManager.doStart();
080            }
081            return workManager;
082        }
083    
084        public TransactionContextManager getTransactionContextManager() throws XAException {
085                    if (transactionContextManager == null && applicationContext != null) {
086                            Map map = applicationContext.getBeansOfType(TransactionContextManager.class);
087                            if (map.size() > 1) {
088                                    throw new IllegalStateException("only one TransactionContextManager can be registered");
089                            } else if (map.size() == 1) {
090                                    transactionContextManager = (TransactionContextManager) map.values().iterator().next();
091                            }
092                    }
093                    if (transactionContextManager == null) {
094                transactionContextManager = createTransactionContextManager();
095                    }
096            return transactionContextManager;
097        }
098    
099        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        // Implementation methods
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    }