View Javadoc

1   /***
2    * 
3    * Copyright 2005 LogicBlaze, Inc.
4    * 
5    * Licensed under the Apache License, Version 2.0 (the "License"); 
6    * you may not use this file except in compliance with the License. 
7    * You may obtain a copy of the License at 
8    * 
9    * http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS, 
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
14   * See the License for the specific language governing permissions and 
15   * limitations under the License. 
16   * 
17   **/
18  package org.jencks;
19  
20  import javax.jms.MessageListener;
21  import javax.resource.spi.LocalTransaction;
22  import javax.resource.spi.UnavailableException;
23  import javax.transaction.TransactionManager;
24  import javax.transaction.xa.XAResource;
25  
26  import org.springframework.beans.BeansException;
27  import org.springframework.beans.factory.BeanFactory;
28  import org.springframework.beans.factory.BeanFactoryAware;
29  import org.springframework.beans.factory.InitializingBean;
30  
31  /***
32   * A factory of {@link javax.resource.spi.endpoint.MessageEndpoint} instances, either using XA transactions,
33   * Local (JMS) transactions or regular JMS message acknowledgements.
34   * <p/>
35   * To use XA you must set the transactionManager property via the
36   * {@link #setTransactionManager(javax.transaction.TransactionManager)}
37   * method.
38   * <p/>
39   * To use a local JMS transaction, then the XAResouce object passed in the {@link #createEndpoint(XAResource)}
40   * call must implement {@link LocalTransaction}.
41   *
42   * @version $Revision: 1.5 $
43   */
44  public class DefaultEndpointFactory extends EndpointFactorySupport implements InitializingBean, BeanFactoryAware {
45      private BeanFactory beanFactory;
46      private String ref;
47  
48      public DefaultEndpointFactory() {
49      }
50  
51      public DefaultEndpointFactory(BeanFactory beanFactory, String ref) {
52          this.beanFactory = beanFactory;
53          this.ref = ref;
54      }
55  
56      public DefaultEndpointFactory(BeanFactory beanFactory, String ref, TransactionManager transactionManager, String name) {
57          this.beanFactory = beanFactory;
58          this.ref = ref;
59          this.transactionManager = transactionManager;
60          setName(name);
61      }
62  
63      public void afterPropertiesSet() throws Exception {
64          if (ref == null) {
65              throw new IllegalArgumentException("ref property must be set");
66          }
67      }
68  
69      // Properties
70      //-------------------------------------------------------------------------
71      public String getRef() {
72          return ref;
73      }
74  
75      public void setRef(String ref) {
76          this.ref = ref;
77      }
78  
79      public BeanFactory getBeanFactory() {
80          return beanFactory;
81      }
82  
83      public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
84          this.beanFactory = beanFactory;
85      }
86  
87      // Implementation methods
88      //-------------------------------------------------------------------------
89      protected MessageListener createMessageListener() throws UnavailableException {
90          MessageListener messageListener = (MessageListener) beanFactory.getBean(ref, MessageListener.class);
91          if (messageListener == null) {
92              throw new UnavailableException("No MessageListener bean available for reference name: " + ref);
93          }
94          return messageListener;
95      }
96  }