View Javadoc

1   /***
2    * 
3    * Copyright 2005 LogicBlaze, Inc. http://www.logicblaze.com
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 java.lang.reflect.Method;
21  
22  import javax.jms.MessageListener;
23  import javax.resource.spi.LocalTransaction;
24  import javax.resource.spi.UnavailableException;
25  import javax.resource.spi.endpoint.MessageEndpoint;
26  import javax.resource.spi.endpoint.MessageEndpointFactory;
27  import javax.transaction.TransactionManager;
28  import javax.transaction.xa.XAResource;
29  
30  import org.apache.geronimo.transaction.manager.NamedXAResource;
31  import org.apache.geronimo.transaction.manager.WrapperNamedXAResource;
32  
33  /***
34   * @version $Revision: 1.5 $
35   */
36  public abstract class EndpointFactorySupport implements MessageEndpointFactory {
37      protected TransactionManager transactionManager;
38      private String name;
39  
40      public MessageEndpoint createEndpoint(XAResource xaResource) throws UnavailableException {
41          MessageListener messageListener = createMessageListener();
42          xaResource = wrapXAResource(xaResource);
43          if (transactionManager != null) {
44              return new XAEndpoint(messageListener, xaResource, transactionManager);
45          }
46          else if (xaResource instanceof LocalTransaction) {
47              return new LocalTransactionEndpoint(messageListener, (LocalTransaction) xaResource);
48          }
49          return new AcknowledgeEndpoint(messageListener);
50      }
51  
52      public String toString() {
53          return super.toString() + "[transactionManager=" + transactionManager + "]";
54      }
55  
56      // Properties
57      //-------------------------------------------------------------------------
58      public boolean isDeliveryTransacted(Method method) throws NoSuchMethodException {
59          return transactionManager != null;
60      }
61  
62      public TransactionManager getTransactionManager() {
63          return transactionManager;
64      }
65  
66      public void setTransactionManager(TransactionManager transactionManager) {
67          this.transactionManager = transactionManager;
68      }
69  
70      public String getName() {
71          return name;
72      }
73  
74      public void setName(String name) {
75          this.name = name;
76      }
77  
78      // Implementation methods
79      //-------------------------------------------------------------------------
80      protected abstract MessageListener createMessageListener() throws UnavailableException;
81  
82      /***
83       * {@link XAResource} instances must be named to support recovery, so either pass
84       * {@link NamedXAResource} instances through or wrap with the Spring name.
85       *
86       * @param xaResource
87       * @return the wrapped XAResource instance
88       */
89      protected XAResource wrapXAResource(XAResource xaResource) {
90          String name = getName();
91          if (xaResource instanceof NamedXAResource || name == null) {
92              return xaResource;
93          }
94          return new WrapperNamedXAResource(xaResource, name);
95      }
96  
97  }