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 org.springframework.beans.BeansException;
021    import org.springframework.beans.factory.BeanFactory;
022    import org.springframework.beans.factory.BeanFactoryAware;
023    import org.springframework.beans.factory.InitializingBean;
024    
025    import javax.jms.MessageListener;
026    import javax.resource.spi.UnavailableException;
027    import javax.resource.spi.LocalTransaction;
028    import javax.transaction.TransactionManager;
029    import javax.transaction.xa.XAResource;
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.2 $
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) {
057            this.beanFactory = beanFactory;
058            this.ref = ref;
059            this.transactionManager = transactionManager;
060        }
061    
062        public void afterPropertiesSet() throws Exception {
063            if (ref == null) {
064                throw new IllegalArgumentException("ref property must be set");
065            }
066        }
067    
068        // Properties
069        //-------------------------------------------------------------------------
070        public String getRef() {
071            return ref;
072        }
073    
074        public void setRef(String ref) {
075            this.ref = ref;
076        }
077    
078        public BeanFactory getBeanFactory() {
079            return beanFactory;
080        }
081    
082        public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
083            this.beanFactory = beanFactory;
084        }
085    
086        // Implementation methods
087        //-------------------------------------------------------------------------
088        protected MessageListener createMessageListener() throws UnavailableException {
089            MessageListener messageListener = (MessageListener) beanFactory.getBean(ref, MessageListener.class);
090            if (messageListener == null) {
091                throw new UnavailableException("No MessageListener bean available for reference name: " + ref);
092            }
093            return messageListener;
094        }
095    }