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