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