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;
019    
020    import org.apache.commons.logging.Log;
021    import org.apache.commons.logging.LogFactory;
022    import org.jencks.factory.BootstrapContextFactoryBean;
023    import org.springframework.beans.BeansException;
024    import org.springframework.beans.factory.DisposableBean;
025    import org.springframework.beans.factory.InitializingBean;
026    import org.springframework.context.ApplicationContext;
027    import org.springframework.context.ApplicationContextAware;
028    
029    import javax.resource.spi.BootstrapContext;
030    import javax.resource.spi.ResourceAdapter;
031    
032    /**
033     * Represents a base JCA container which has no dependency on Geronimo
034     * and requires a mandatory {@link BootstrapContext} and {@link ResourceAdapter}
035     * properties to be configured.
036     * <p/>
037     * Typically Spring users will use the {@link BootstrapContextFactoryBean} to create
038     * the {@link BootstrapContext} instance, with the work manager and transaction manager.
039     *
040     * @version $Revision: 1.2 $
041     */
042    public class JCAContainer implements InitializingBean, DisposableBean, ApplicationContextAware {
043        private static final transient Log log = LogFactory.getLog(JCAContainer.class);
044        private BootstrapContext bootstrapContext;
045        private ResourceAdapter resourceAdapter;
046            private ApplicationContext applicationContext;
047        private boolean lazyLoad = false;
048        
049        public JCAContainer() {
050        }
051    
052        public JCAConnector addConnector() {
053            return new JCAConnector(getBootstrapContext(), getResourceAdapter());
054        }
055    
056        public void afterPropertiesSet() throws Exception {
057            if (resourceAdapter == null) {
058                throw new IllegalArgumentException("resourceAdapter must be set");
059            }
060            if (bootstrapContext == null) {
061                if (bootstrapContext == null) {
062                    throw new IllegalArgumentException("bootstrapContext must be set");
063                }
064            }
065            resourceAdapter.start(bootstrapContext);
066    
067            // now lets start all of the JCAConnector instances
068            if (!lazyLoad) {
069                    if (applicationContext == null) {
070                        throw new IllegalArgumentException("applicationContext should have been set by Spring");
071                    }
072                    applicationContext.getBeansOfType(JCAConnector.class);
073            }
074    
075            String version = null;
076            Package aPackage = Package.getPackage("org.jencks");
077            if (aPackage != null) {
078                version = aPackage.getImplementationVersion();
079            }
080    
081            log.info("Jencks JCA Container (http://jencks.org/) has started running version: " + version);
082        }
083    
084        public void destroy() throws Exception {
085            if (resourceAdapter != null) {
086                resourceAdapter.stop();
087            }
088        }
089    
090        // Properties
091        //-------------------------------------------------------------------------
092        public ApplicationContext getApplicationContext() {
093            return applicationContext;
094        }
095    
096        public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
097            this.applicationContext = applicationContext;
098        }
099    
100        public ResourceAdapter getResourceAdapter() {
101            return resourceAdapter;
102        }
103    
104        public void setResourceAdapter(ResourceAdapter resourceAdapter) {
105            this.resourceAdapter = resourceAdapter;
106        }
107    
108        public BootstrapContext getBootstrapContext() {
109            return bootstrapContext;
110        }
111    
112        public void setBootstrapContext(BootstrapContext bootstrapContext) {
113            this.bootstrapContext = bootstrapContext;
114        }
115    
116        public boolean isLazyLoad() {
117            return lazyLoad;
118        }
119    
120        public void setLazyLoad(boolean lazyLoad) {
121            this.lazyLoad = lazyLoad;
122        }
123    
124    }