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.springframework.beans.factory.BeanFactory;
023    import org.springframework.beans.factory.BeanFactoryAware;
024    import org.springframework.beans.factory.DisposableBean;
025    import org.springframework.beans.factory.InitializingBean;
026    
027    import javax.resource.spi.ActivationSpec;
028    import javax.resource.spi.BootstrapContext;
029    import javax.resource.spi.ResourceAdapter;
030    import javax.resource.spi.endpoint.MessageEndpointFactory;
031    import javax.transaction.TransactionManager;
032    
033    /**
034     * Represents a connector in the JCA container - which represents
035     * a single activation specification on a resource adapter
036     *
037     * @version $Revision: 1.1.1.1 $
038     */
039    public class JCAConnector implements InitializingBean, DisposableBean, BeanFactoryAware {
040        private static final transient Log log = LogFactory.getLog(JCAConnector.class);
041    
042        private ActivationSpec activationSpec;
043        private BootstrapContext bootstrapContext;
044        private MessageEndpointFactory endpointFactory;
045        private ResourceAdapter resourceAdapter;
046        private String ref;
047        private TransactionManager transactionManager;
048        private BeanFactory beanFactory;
049    
050        public JCAConnector() {
051        }
052    
053        public JCAConnector(BootstrapContext bootstrapContext, ResourceAdapter resourceAdapter) {
054            this.bootstrapContext = bootstrapContext;
055            this.resourceAdapter = resourceAdapter;
056        }
057    
058        public void afterPropertiesSet() throws Exception {
059            if (activationSpec == null) {
060                throw new IllegalArgumentException("activationSpec must be set");
061            }
062    
063            ResourceAdapter temp = activationSpec.getResourceAdapter();
064            if (temp == null && resourceAdapter != null) {
065                activationSpec.setResourceAdapter(resourceAdapter);
066            }
067            else if (resourceAdapter == null) {
068                resourceAdapter = activationSpec.getResourceAdapter();
069                if (resourceAdapter == null) {
070                    throw new IllegalArgumentException("resourceAdapter property must be set on the activationSpec object");
071                }
072            }
073            if (bootstrapContext == null) {
074                throw new IllegalArgumentException("bootstrapContext must be set");
075            }
076            if (endpointFactory == null) {
077                if (ref == null) {
078                    throw new IllegalArgumentException("either the endpointFactory or ref properties must be set");
079                }
080                if (transactionManager != null) {
081                    endpointFactory = new DefaultEndpointFactory(beanFactory, ref, transactionManager);
082                }
083                else {
084                    // TODO should we have some way of finding a ManagedConnection or other local transaction hook?
085                    endpointFactory = new DefaultEndpointFactory(beanFactory, ref);
086                }
087            }
088            log.info("Activating endpoint for activationSpec: " + activationSpec + " using endpointFactory: " + endpointFactory);
089            resourceAdapter.endpointActivation(endpointFactory, activationSpec);
090        }
091    
092        public void destroy() throws Exception {
093            if (resourceAdapter != null && activationSpec != null) {
094                resourceAdapter.endpointDeactivation(endpointFactory, activationSpec);
095            }
096        }
097    
098        // Properties
099        //-------------------------------------------------------------------------
100        public ActivationSpec getActivationSpec() {
101            return activationSpec;
102        }
103    
104        public void setActivationSpec(ActivationSpec activationSpec) {
105            this.activationSpec = activationSpec;
106        }
107    
108        /**
109         * Returns the name of the MessageListener POJO in Spring
110         */
111        public String getRef() {
112            return ref;
113        }
114    
115        /**
116         * Sets the name of the MessageListener POJO in Spring
117         */
118        public void setRef(String ref) {
119            this.ref = ref;
120        }
121    
122        public MessageEndpointFactory getEndpointFactory() {
123            return endpointFactory;
124        }
125    
126        public void setEndpointFactory(MessageEndpointFactory endpointFactory) {
127            this.endpointFactory = endpointFactory;
128        }
129    
130        public BootstrapContext getBootstrapContext() {
131            return bootstrapContext;
132        }
133    
134        public void setBootstrapContext(BootstrapContext bootstrapContext) {
135            this.bootstrapContext = bootstrapContext;
136        }
137    
138        public ResourceAdapter getResourceAdapter() {
139            return resourceAdapter;
140        }
141    
142        public void setResourceAdapter(ResourceAdapter resourceAdapter) {
143            this.resourceAdapter = resourceAdapter;
144        }
145    
146        public TransactionManager getTransactionManager() {
147            return transactionManager;
148        }
149    
150        public void setTransactionManager(TransactionManager transactionManager) {
151            this.transactionManager = transactionManager;
152        }
153    
154        public BeanFactory getBeanFactory() {
155            return beanFactory;
156        }
157    
158        public void setBeanFactory(BeanFactory beanFactory) {
159            this.beanFactory = beanFactory;
160        }
161    }