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 a single
036     * activation specification on a resource adapter
037     * 
038     * @version $Revision: 1.6 $
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        private JCAContainer jcaContainer;
052    
053        public JCAConnector() {
054        }
055    
056        public JCAConnector(BootstrapContext bootstrapContext, ResourceAdapter resourceAdapter) {
057            this.bootstrapContext = bootstrapContext;
058            this.resourceAdapter = resourceAdapter;
059        }
060    
061        public void afterPropertiesSet() throws Exception {
062            if (activationSpec == null) {
063                throw new IllegalArgumentException("activationSpec must be set");
064            }
065    
066            if (resourceAdapter == null) {
067                resourceAdapter = activationSpec.getResourceAdapter();
068            }
069            if (resourceAdapter == null && jcaContainer != null) {
070                resourceAdapter = jcaContainer.getResourceAdapter();
071            }
072            if (resourceAdapter == null) {
073                throw new IllegalArgumentException("resourceAdapter property must be set on the activationSpec object");
074            }
075            if (activationSpec.getResourceAdapter() == null) {
076                activationSpec.setResourceAdapter(resourceAdapter);
077            }
078    
079            if (bootstrapContext == null && jcaContainer != null) {
080                bootstrapContext = jcaContainer.getBootstrapContext();
081            }
082            if (bootstrapContext == null) {
083                throw new IllegalArgumentException("bootstrapContext must be set");
084            }
085            if (endpointFactory == null) {
086                if (ref == null) {
087                    throw new IllegalArgumentException("either the endpointFactory or ref properties must be set");
088                }
089                if (transactionManager != null) {
090                    endpointFactory = new DefaultEndpointFactory(beanFactory, ref, transactionManager, getName());
091                }
092                else {
093                    // TODO should we have some way of finding a ManagedConnection
094                    // or other local transaction hook?
095                    endpointFactory = new DefaultEndpointFactory(beanFactory, ref);
096                }
097            }
098            log.info("Activating endpoint for activationSpec: " + activationSpec + " using endpointFactory: " + endpointFactory);
099            resourceAdapter.endpointActivation(endpointFactory, activationSpec);
100        }
101    
102        public void destroy() throws Exception {
103            if (resourceAdapter != null && activationSpec != null) {
104                resourceAdapter.endpointDeactivation(endpointFactory, activationSpec);
105            }
106        }
107    
108        // Properties
109        // -------------------------------------------------------------------------
110    
111        public String getName() {
112            return name;
113        }
114    
115        public void setBeanName(String name) {
116            this.name = name;
117        }
118    
119        public ActivationSpec getActivationSpec() {
120            return activationSpec;
121        }
122    
123        public void setActivationSpec(ActivationSpec activationSpec) {
124            this.activationSpec = activationSpec;
125        }
126    
127        /**
128         * Returns the name of the MessageListener POJO in Spring
129         */
130        public String getRef() {
131            return ref;
132        }
133    
134        /**
135         * Sets the name of the MessageListener POJO in Spring
136         */
137        public void setRef(String ref) {
138            this.ref = ref;
139        }
140    
141        public MessageEndpointFactory getEndpointFactory() {
142            return endpointFactory;
143        }
144    
145        public void setEndpointFactory(MessageEndpointFactory endpointFactory) {
146            this.endpointFactory = endpointFactory;
147        }
148    
149        public BootstrapContext getBootstrapContext() {
150            return bootstrapContext;
151        }
152    
153        public void setBootstrapContext(BootstrapContext bootstrapContext) {
154            this.bootstrapContext = bootstrapContext;
155        }
156    
157        public ResourceAdapter getResourceAdapter() {
158            return resourceAdapter;
159        }
160    
161        public void setResourceAdapter(ResourceAdapter resourceAdapter) {
162            this.resourceAdapter = resourceAdapter;
163        }
164    
165        public TransactionManager getTransactionManager() {
166            return transactionManager;
167        }
168    
169        public void setTransactionManager(TransactionManager transactionManager) {
170            this.transactionManager = transactionManager;
171        }
172    
173        public BeanFactory getBeanFactory() {
174            return beanFactory;
175        }
176    
177        public void setBeanFactory(BeanFactory beanFactory) {
178            this.beanFactory = beanFactory;
179        }
180    
181        public JCAContainer getJcaContainer() {
182            return jcaContainer;
183        }
184    
185        public void setJcaContainer(JCAContainer jcaConnector) {
186            this.jcaContainer = jcaConnector;
187        }
188    }