1 25 26 package org.objectweb.easybeans.container.mdb; 27 28 import java.lang.reflect.InvocationTargetException ; 29 import java.lang.reflect.Method ; 30 import java.util.List ; 31 32 import javax.ejb.ActivationConfigProperty ; 33 import javax.resource.ResourceException ; 34 import javax.resource.spi.ActivationSpec ; 35 import javax.resource.spi.InvalidPropertyException ; 36 import javax.resource.spi.ResourceAdapter ; 37 import javax.resource.spi.UnavailableException ; 38 import javax.resource.spi.endpoint.MessageEndpoint ; 39 import javax.resource.spi.endpoint.MessageEndpointFactory ; 40 import javax.transaction.xa.XAResource ; 41 42 import org.objectweb.easybeans.api.EZBContainer; 43 import org.objectweb.easybeans.api.FactoryException; 44 import org.objectweb.easybeans.api.bean.EasyBeansMDB; 45 import org.objectweb.easybeans.api.pool.PoolException; 46 47 52 public class MDBMessageEndPointFactory extends MDBFactory implements MessageEndpointFactory { 53 54 57 public static final String DEFAULT_ACTIVATION_SPEC_NAME = "joramActivationSpec"; 58 59 63 private ActivationSpec activationSpec = null; 64 65 68 private ResourceAdapter resourceAdapter = null; 69 70 80 public MDBMessageEndPointFactory(final String className, final EZBContainer container, 81 final ActivationSpec activationSpec, final ResourceAdapter resourceAdapter) throws FactoryException { 82 super(className, container); 83 this.activationSpec = activationSpec; 84 this.resourceAdapter = resourceAdapter; 85 } 86 87 91 @Override 92 public void init() throws FactoryException { 93 initActivationSpec(); 94 95 validateActivationSpec(); 96 97 activate(); 98 } 99 100 101 105 private void initActivationSpec() throws FactoryException { 106 List <ActivationConfigProperty > properties = getMessageDrivenInfo().getActivationConfigProperties(); 109 if (properties != null) { 110 for (ActivationConfigProperty property : properties) { 111 String key = property.propertyName(); 113 String value = property.propertyValue(); 115 116 String methodName = "set" + key.substring(0, 1).toUpperCase() + key.substring(1); 118 119 Method m = null; 121 try { 122 m = activationSpec.getClass().getMethod(methodName, new Class [] {String .class}); 123 } catch (SecurityException e) { 124 throw new FactoryException("Cannot get a method named '" + methodName 125 + "' on activation spec object '" + activationSpec + "'.", e); 126 } catch (NoSuchMethodException e) { 127 throw new FactoryException("Cannot get a method named '" + methodName 128 + "' on activation spec object '" + activationSpec + "'.", e); 129 } 130 131 try { 133 m.invoke(activationSpec, value); 134 } catch (IllegalArgumentException e) { 135 throw new FactoryException("Cannot invoke method named '" + methodName + "' with value '" + value 136 + "' on activation spec object '" + activationSpec + "'.", e); 137 } catch (IllegalAccessException e) { 138 throw new FactoryException("Cannot invoke method named '" + methodName + "' with value '" + value 139 + "' on activation spec object '" + activationSpec + "'.", e); 140 } catch (InvocationTargetException e) { 141 throw new FactoryException("Cannot invoke method named '" + methodName + "' with value '" + value 142 + "' on activation spec object '" + activationSpec + "'.", e); 143 } 144 145 } 146 } 147 148 } 149 150 155 private void validateActivationSpec() throws FactoryException { 156 try { 157 activationSpec.validate(); 158 } catch (InvalidPropertyException e) { 159 throw new FactoryException("Cannot validate the validation spec object", e); 160 } 161 } 162 163 168 private void activate() throws FactoryException { 169 try { 170 resourceAdapter.endpointActivation(this, activationSpec); 171 } catch (ResourceException e) { 172 throw new FactoryException( 173 "Cannot activate the activationspec object and us (MessageEndPointFactory) on the resource adapter", 174 e); 175 } 176 } 177 178 188 public MessageEndpoint createEndpoint(final XAResource xaResource) throws UnavailableException { 189 MDBMessageEndPoint messageEndpoint = null; 191 192 EasyBeansMDB easyBeansMDB = null; 194 try { 195 easyBeansMDB = getPool().get(); 196 } catch (PoolException e) { 197 throw new UnavailableException ("Cannot get instance in the pool", e); 198 } 199 200 messageEndpoint = new MDBMessageListenerEndPoint(this, easyBeansMDB); 203 204 messageEndpoint.setXaResource(xaResource); 206 207 return messageEndpoint; 208 } 209 210 214 protected void releaseEndPoint(final MDBMessageEndPoint mdbMessageEndPoint) { 215 try { 217 getPool().release(mdbMessageEndPoint.getEasyBeansMDB()); 218 } catch (PoolException e) { 219 throw new IllegalStateException ("Cannot release the given message end point", e); 220 } 221 } 222 223 236 public boolean isDeliveryTransacted(final Method method) throws NoSuchMethodException { 237 return false; 239 } 240 241 244 @Override 245 public void stop() { 246 super.stop(); 248 249 resourceAdapter.endpointDeactivation(this, activationSpec); 251 252 } 253 } 254 | Popular Tags |