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 org.springframework.beans.BeansException;
21  import org.springframework.beans.factory.BeanFactory;
22  import org.springframework.beans.factory.BeanFactoryAware;
23  import org.springframework.beans.factory.InitializingBean;
24  
25  import javax.jms.MessageListener;
26  import javax.resource.spi.UnavailableException;
27  import javax.resource.spi.LocalTransaction;
28  import javax.transaction.TransactionManager;
29  import javax.transaction.xa.XAResource;
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.2 $
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) {
57          this.beanFactory = beanFactory;
58          this.ref = ref;
59          this.transactionManager = transactionManager;
60      }
61  
62      public void afterPropertiesSet() throws Exception {
63          if (ref == null) {
64              throw new IllegalArgumentException("ref property must be set");
65          }
66      }
67  
68      // Properties
69      //-------------------------------------------------------------------------
70      public String getRef() {
71          return ref;
72      }
73  
74      public void setRef(String ref) {
75          this.ref = ref;
76      }
77  
78      public BeanFactory getBeanFactory() {
79          return beanFactory;
80      }
81  
82      public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
83          this.beanFactory = beanFactory;
84      }
85  
86      // Implementation methods
87      //-------------------------------------------------------------------------
88      protected MessageListener createMessageListener() throws UnavailableException {
89          MessageListener messageListener = (MessageListener) beanFactory.getBean(ref, MessageListener.class);
90          if (messageListener == null) {
91              throw new UnavailableException("No MessageListener bean available for reference name: " + ref);
92          }
93          return messageListener;
94      }
95  }