001    /**
002     * 
003     * Copyright 2005 LogicBlaze, Inc.
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.jms.MessageListener;
021    import javax.resource.spi.LocalTransaction;
022    import javax.resource.spi.UnavailableException;
023    import javax.transaction.TransactionManager;
024    import javax.transaction.xa.XAResource;
025    
026    import org.springframework.beans.BeansException;
027    import org.springframework.beans.factory.BeanFactory;
028    import org.springframework.beans.factory.BeanFactoryAware;
029    import org.springframework.beans.factory.InitializingBean;
030    
031    /**
032     * A factory of {@link javax.resource.spi.endpoint.MessageEndpoint} instances, either using XA transactions,
033     * Local (JMS) transactions or regular JMS message acknowledgements.
034     * <p/>
035     * To use XA you must set the transactionManager property via the
036     * {@link #setTransactionManager(javax.transaction.TransactionManager)}
037     * method.
038     * <p/>
039     * To use a local JMS transaction, then the XAResouce object passed in the {@link #createEndpoint(XAResource)}
040     * call must implement {@link LocalTransaction}.
041     *
042     * @version $Revision: 1.5 $
043     */
044    public class DefaultEndpointFactory extends EndpointFactorySupport implements InitializingBean, BeanFactoryAware {
045        private BeanFactory beanFactory;
046        private String ref;
047    
048        public DefaultEndpointFactory() {
049        }
050    
051        public DefaultEndpointFactory(BeanFactory beanFactory, String ref) {
052            this.beanFactory = beanFactory;
053            this.ref = ref;
054        }
055    
056        public DefaultEndpointFactory(BeanFactory beanFactory, String ref, TransactionManager transactionManager, String name) {
057            this.beanFactory = beanFactory;
058            this.ref = ref;
059            this.transactionManager = transactionManager;
060            setName(name);
061        }
062    
063        public void afterPropertiesSet() throws Exception {
064            if (ref == null) {
065                throw new IllegalArgumentException("ref property must be set");
066            }
067        }
068    
069        // Properties
070        //-------------------------------------------------------------------------
071        public String getRef() {
072            return ref;
073        }
074    
075        public void setRef(String ref) {
076            this.ref = ref;
077        }
078    
079        public BeanFactory getBeanFactory() {
080            return beanFactory;
081        }
082    
083        public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
084            this.beanFactory = beanFactory;
085        }
086    
087        // Implementation methods
088        //-------------------------------------------------------------------------
089        protected MessageListener createMessageListener() throws UnavailableException {
090            MessageListener messageListener = (MessageListener) beanFactory.getBean(ref, MessageListener.class);
091            if (messageListener == null) {
092                throw new UnavailableException("No MessageListener bean available for reference name: " + ref);
093            }
094            return messageListener;
095        }
096    }