1 22 package org.jboss.ejb3.mdb.inflow; 23 24 import java.lang.reflect.InvocationHandler ; 25 import java.lang.reflect.Method ; 26 import java.util.Collection ; 27 import java.util.HashMap ; 28 29 import javax.ejb.TransactionManagementType ; 30 import javax.ejb.TransactionAttribute ; 31 import javax.ejb.TransactionAttributeType ; 32 import javax.management.ObjectName ; 33 import javax.management.MBeanServer ; 34 import javax.management.MalformedObjectNameException ; 35 import javax.resource.spi.ActivationSpec ; 36 import javax.resource.spi.UnavailableException ; 37 import javax.resource.spi.endpoint.MessageEndpoint ; 38 import javax.resource.spi.endpoint.MessageEndpointFactory ; 39 import javax.transaction.xa.XAResource ; 40 41 import org.jboss.deployment.DeploymentException; 42 import org.jboss.ejb3.Container; 43 import org.jboss.ejb3.KernelAbstractionFactory; 44 import org.jboss.ejb3.tx.TxUtil; 45 import org.jboss.ejb3.mdb.MessagingContainer; 46 import org.jboss.mx.util.JMXExceptionDecoder; 47 import org.jboss.logging.Logger; 48 49 import EDU.oswego.cs.dl.util.concurrent.SynchronizedInt; 50 51 58 public class JBossMessageEndpointFactory implements MessageEndpointFactory 59 { 60 private static final Logger log = Logger.getLogger(JBossMessageEndpointFactory.class); 61 62 63 protected boolean trace = log.isTraceEnabled(); 64 65 66 protected MessagingContainer container; 67 68 69 protected HashMap properties = new HashMap (); 70 71 72 protected Class messagingTypeClass; 73 74 75 protected String resourceAdapterName; 76 77 protected ObjectName resourceAdapterObjectName; 78 79 80 protected ActivationSpec activationSpec; 81 82 83 protected Class [] interfaces; 84 85 86 protected SynchronizedInt nextProxyId = new SynchronizedInt(0); 87 88 90 91 protected String [] createActivationSpecSig = new String [] 92 { 93 Class .class.getName(), 94 Collection .class.getName() 95 }; 96 97 98 protected String [] activationSig = new String [] 99 { 100 MessageEndpointFactory .class.getName(), 101 ActivationSpec .class.getName() 102 }; 103 104 106 public JBossMessageEndpointFactory() 107 { 108 } 109 110 112 117 public MessagingContainer getContainer() 118 { 119 return container; 120 } 121 122 124 public MessageEndpoint createEndpoint(XAResource resource) throws UnavailableException 125 { 126 trace = log.isTraceEnabled(); 127 128 if (trace) 129 log.trace("createEndpoint " + this + " xaResource=" + resource); 130 131 MessageEndpoint endpoint = createProxy(resource); 132 133 if (trace) 134 log.trace("Created endpoint " + endpoint + " from " + this); 135 136 return endpoint; 137 } 138 139 protected MessageEndpoint createProxy(XAResource resource) 140 { 141 try 142 { 143 Class proxyClass = java.lang.reflect.Proxy.getProxyClass(container.getBeanClass().getClassLoader(), interfaces); 144 145 final Class [] constructorParams = {InvocationHandler .class}; 146 java.lang.reflect.Constructor proxyConstructor = proxyClass.getConstructor(constructorParams); 147 148 MessageInflowLocalProxy proxy = new MessageInflowLocalProxy(container); 149 proxy.setXaResource(resource); 150 proxy.setMessageEndpointFactory(this); 151 152 Object [] args = {proxy}; 153 MessageEndpoint endpoint = (MessageEndpoint )proxyConstructor.newInstance(args); 154 return endpoint; 155 156 } catch (Exception e) 157 { 158 e.printStackTrace(); 159 return null; 160 } 161 } 162 163 public boolean isDeliveryTransacted(Method method) throws NoSuchMethodException 164 { 165 TransactionManagementType mtype = TxUtil.getTransactionManagementType(container); 166 if (mtype == javax.ejb.TransactionManagementType.BEAN) return false; 167 168 169 TransactionAttribute attr = (TransactionAttribute )container.resolveAnnotation(method, TransactionAttribute .class); 170 if (attr == null) 171 { 172 attr =(TransactionAttribute )container.resolveAnnotation(TransactionAttribute .class); 173 } 174 TransactionAttributeType type = TransactionAttributeType.REQUIRED; 175 if (attr != null) type = attr.value(); 176 return type == javax.ejb.TransactionAttributeType.REQUIRED; 177 } 178 179 181 public void start() throws Exception 182 { 183 resolveMessageListener(); 185 resolveResourceAdapterName(); 186 resolveResourceAdapter(); 188 createActivationSpec(); 190 interfaces = new Class [] { MessageEndpoint .class, messagingTypeClass}; 193 activate(); 195 } 196 197 public void stop() throws Exception 198 { 199 deactivate(); 201 } 202 204 209 public void setContainer(final Container container) 210 { 211 this.container = (MessagingContainer) container; 212 } 213 214 216 219 public String toString() 220 { 221 StringBuffer buffer = new StringBuffer (100); 222 buffer.append(super.toString()); 223 buffer.append("{ resourceAdapter=").append(resourceAdapterName); 224 buffer.append(", messagingType=").append(container.getMessagingType()); 225 buffer.append(", ejbName=").append(container.getEjbName()); 226 buffer.append(", activationConfig=").append(properties.values()); 227 buffer.append(", activationSpec=").append(activationSpec); 228 buffer.append("}"); 229 return buffer.toString(); 230 } 231 232 234 239 protected void resolveMessageListener() throws DeploymentException 240 { 241 messagingTypeClass = container.getMessagingType(); 242 } 243 244 250 protected void resolveResourceAdapterName() throws DeploymentException 251 { 252 resourceAdapterName = container.getResourceAdaptorName(); 253 } 254 255 protected void resolveResourceAdapter() 256 { 257 try 258 { 259 resourceAdapterObjectName = new ObjectName ("jboss.jca:service=RARDeployment,name='" + resourceAdapterName + "'"); 260 } 262 catch (MalformedObjectNameException e) 263 { 264 throw new RuntimeException (e); 265 } 266 } 267 268 273 protected void createActivationSpec() throws DeploymentException 274 { 275 properties = new HashMap (container.getActivationConfigProperties()); 276 277 Object [] params = new Object [] 278 { 279 messagingTypeClass, 280 properties.values() 281 }; 282 283 try 284 { 285 activationSpec = (ActivationSpec ) KernelAbstractionFactory.getInstance().invoke(resourceAdapterObjectName, "createActivationSpec", params, createActivationSpecSig); 286 } 287 catch (Throwable t) 288 { 289 t = JMXExceptionDecoder.decode(t); 290 DeploymentException.rethrowAsDeploymentException("Unable to create activation spec ra=" + resourceAdapterObjectName + 291 " messaging-type=" + messagingTypeClass.getName() + " properties=" + container.getActivationConfigProperties(), t); 292 } 293 } 294 295 300 protected void activate() throws DeploymentException 301 { 302 Object [] params = new Object [] { this, activationSpec }; 303 try 304 { 305 KernelAbstractionFactory.getInstance().invoke(resourceAdapterObjectName, "endpointActivation", params, activationSig); 306 } 307 catch (Throwable t) 308 { 309 t = JMXExceptionDecoder.decode(t); 310 DeploymentException.rethrowAsDeploymentException("Endpoint activation failed ra=" + resourceAdapterObjectName + 311 " activationSpec=" + activationSpec, t); 312 } 313 } 314 315 318 protected void deactivate() 319 { 320 Object [] params = new Object [] { this, activationSpec }; 321 try 322 { 323 KernelAbstractionFactory.getInstance().invoke(resourceAdapterObjectName, "endpointDeactivation", params, activationSig); 324 } 325 catch (Throwable t) 326 { 327 t = JMXExceptionDecoder.decode(t); 328 log.warn("Endpoint activation failed ra=" + resourceAdapterObjectName + 329 " activationSpec=" + activationSpec, t); 330 } 331 } 332 } 333 | Popular Tags |