001 /** 002 * 003 * Copyright 2005 LogicBlaze, Inc. http://www.logicblaze.com 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 java.lang.reflect.Method; 021 022 import javax.jms.MessageListener; 023 import javax.resource.spi.LocalTransaction; 024 import javax.resource.spi.UnavailableException; 025 import javax.resource.spi.endpoint.MessageEndpoint; 026 import javax.resource.spi.endpoint.MessageEndpointFactory; 027 import javax.transaction.TransactionManager; 028 import javax.transaction.xa.XAResource; 029 030 import org.apache.geronimo.transaction.manager.NamedXAResource; 031 import org.apache.geronimo.transaction.manager.WrapperNamedXAResource; 032 033 /** 034 * @version $Revision: 1.5 $ 035 */ 036 public abstract class EndpointFactorySupport implements MessageEndpointFactory { 037 protected TransactionManager transactionManager; 038 private String name; 039 040 public MessageEndpoint createEndpoint(XAResource xaResource) throws UnavailableException { 041 MessageListener messageListener = createMessageListener(); 042 xaResource = wrapXAResource(xaResource); 043 if (transactionManager != null) { 044 return new XAEndpoint(messageListener, xaResource, transactionManager); 045 } 046 else if (xaResource instanceof LocalTransaction) { 047 return new LocalTransactionEndpoint(messageListener, (LocalTransaction) xaResource); 048 } 049 return new AcknowledgeEndpoint(messageListener); 050 } 051 052 public String toString() { 053 return super.toString() + "[transactionManager=" + transactionManager + "]"; 054 } 055 056 // Properties 057 //------------------------------------------------------------------------- 058 public boolean isDeliveryTransacted(Method method) throws NoSuchMethodException { 059 return transactionManager != null; 060 } 061 062 public TransactionManager getTransactionManager() { 063 return transactionManager; 064 } 065 066 public void setTransactionManager(TransactionManager transactionManager) { 067 this.transactionManager = transactionManager; 068 } 069 070 public String getName() { 071 return name; 072 } 073 074 public void setName(String name) { 075 this.name = name; 076 } 077 078 // Implementation methods 079 //------------------------------------------------------------------------- 080 protected abstract MessageListener createMessageListener() throws UnavailableException; 081 082 /** 083 * {@link XAResource} instances must be named to support recovery, so either pass 084 * {@link NamedXAResource} instances through or wrap with the Spring name. 085 * 086 * @param xaResource 087 * @return the wrapped XAResource instance 088 */ 089 protected XAResource wrapXAResource(XAResource xaResource) { 090 String name = getName(); 091 if (xaResource instanceof NamedXAResource || name == null) { 092 return xaResource; 093 } 094 return new WrapperNamedXAResource(xaResource, name); 095 } 096 097 }