1 46 47 package org.mr.ra; 48 49 import java.io.Serializable ; 50 import java.util.HashMap ; 51 52 import javax.jms.Connection ; 53 import javax.jms.JMSException ; 54 import javax.jms.XAConnection ; 55 import javax.jms.XASession ; 56 import javax.resource.NotSupportedException ; 57 import javax.resource.ResourceException ; 58 import javax.resource.spi.ActivationSpec ; 59 import javax.resource.spi.BootstrapContext ; 60 import javax.resource.spi.ResourceAdapter ; 61 import javax.resource.spi.ResourceAdapterInternalException ; 62 import javax.resource.spi.endpoint.MessageEndpointFactory ; 63 import javax.transaction.xa.XAResource ; 64 65 import org.apache.commons.logging.Log; 66 import org.apache.commons.logging.LogFactory; 67 import org.mr.api.jms.MantaConnection; 68 import org.mr.api.jms.MantaConnectionFactory; 69 import org.mr.api.jms.MantaXAConnection; 70 import org.mr.api.jms.MantaXAConnectionFactory; 71 import org.mr.ra.inbound.ActivationSpecImpl; 72 import org.mr.ra.inbound.EndpointKey; 73 import org.mr.ra.inbound.MantaAsfEndpointWorker; 74 import org.mr.ra.inbound.MantaBaseEndpointWorker; 75 78 85 public class ResourceAdapterImpl 86 implements ResourceAdapter , Serializable 87 { 88 private static final long serialVersionUID = 3257291318350000180L; 89 private static final Log log = LogFactory.getLog(ResourceAdapterImpl.class); 90 91 93 95 private BootstrapContext bootContext = null; 96 private HashMap endpointWorkers = new HashMap (); 97 final private ConnectionRequestInfoImpl info; 99 private HashMap connectionFactoryMap = new HashMap (1); 102 103 104 public ResourceAdapterImpl() { 105 info = new ConnectionRequestInfoImpl(); 106 } 107 108 109 111 114 public void start(BootstrapContext bootstrapContext) throws ResourceAdapterInternalException { 115 this.bootContext = bootstrapContext; 116 } 117 118 119 122 public void stop() { 123 this.bootContext = null; 124 } 125 126 127 128 132 public void endpointActivation(MessageEndpointFactory endpointFactory, 133 ActivationSpec activationSpec) 134 throws ResourceException { 135 136 138 if (activationSpec.getResourceAdapter() != this) { 140 throw new ResourceException ("Activation spec not initialized with this ResourceAdapter instance"); 141 } 142 143 if (activationSpec.getClass().equals(ActivationSpecImpl.class)) { 145 EndpointKey key = new EndpointKey(endpointFactory, (ActivationSpecImpl) activationSpec); 146 147 if (endpointWorkers.containsKey(key)) { 151 throw new IllegalStateException ("Endpoint previously activated"); 152 } 153 154 MantaBaseEndpointWorker worker = new MantaAsfEndpointWorker(this, key); 156 endpointWorkers.put(key, worker); 157 worker.start(); 158 } else { 159 throw new NotSupportedException ("That type of ActicationSpec not supported: " + activationSpec.getClass()); 160 } 161 } 162 163 167 public void endpointDeactivation(MessageEndpointFactory endpointFactory, ActivationSpec activationSpec) { 168 169 if (activationSpec.getClass().equals(ActivationSpecImpl.class)) { 170 EndpointKey key = new EndpointKey(endpointFactory, (ActivationSpecImpl) activationSpec); 171 MantaBaseEndpointWorker worker = (MantaBaseEndpointWorker) endpointWorkers.remove(key); 172 if (worker == null) { 173 return; 177 } 178 try { 179 worker.stop(); 180 } catch (InterruptedException e) { 181 Thread.currentThread().interrupt(); 186 } 187 } 188 } 189 190 191 192 198 public XAResource [] getXAResources(ActivationSpec [] activationSpecs) throws ResourceException { 199 Connection connection = null; 200 try { 201 connection = makeConnection(); 202 if (connection instanceof XAConnection ) { 203 XASession session = ((XAConnection ) connection).createXASession(); 204 XAResource xaResource = session.getXAResource(); 205 return new XAResource [] { xaResource }; 206 } else { 207 return new XAResource [] {}; 208 } 209 } catch (JMSException e) { 210 throw new ResourceException (e); 211 } finally { 212 try { 213 connection.close(); 214 } catch (Throwable ignore) { 215 } 216 } 217 } 218 219 226 public boolean equals(Object o) { 227 if (this == o) { 228 return true; 229 } 230 if (!(o instanceof ResourceAdapterImpl)) { 231 return false; 232 } 233 return true; 234 } 235 236 237 243 public int hashCode() { 244 return 851; } 246 247 248 254 258 public MantaConnection makeConnection() throws JMSException { 259 return makeConnection(info); 260 } 261 262 265 public MantaXAConnection makeConnection(ConnectionRequestInfoImpl crInfo) throws JMSException { 266 MantaXAConnectionFactory connectionFactory = getConnectionFactory(crInfo); 267 String userName = info.getUserName(); 268 String password = info.getPassword(); 269 MantaXAConnection physicalConnection = (MantaXAConnection) connectionFactory.createXAConnection(userName, password); 270 return physicalConnection; 271 } 272 273 276 public MantaXAConnection makeConnection(ActivationSpecImpl activationSpec) throws JMSException { 277 MantaXAConnectionFactory connectionFactory = getConnectionFactory(info); 279 String userName = defaultValue(activationSpec.getUserName(), info.getUserName()); 280 String password = defaultValue(activationSpec.getPassword(), info.getPassword()); 281 MantaXAConnection physicalConnection = (MantaXAConnection) connectionFactory.createXAConnection(userName, password); 282 if (activationSpec.isDurableSubscription()) { 283 physicalConnection.setClientID(activationSpec.getClientId()); 284 } 285 return physicalConnection; 286 } 287 288 293 private MantaXAConnectionFactory getConnectionFactory(ConnectionRequestInfoImpl crInfo) { 294 if(crInfo == null) { 296 crInfo = info; 297 } 298 299 synchronized(connectionFactoryMap) { 300 if(!(connectionFactoryMap.containsKey(crInfo))) { 301 MantaXAConnectionFactory factory = new MantaXAConnectionFactory(); 302 connectionFactoryMap.put(crInfo, factory); 303 return factory; 304 } 305 } 306 return (MantaXAConnectionFactory)connectionFactoryMap.get(crInfo); 307 } 308 309 310 313 public BootstrapContext getBootstrapContext() { 314 return bootContext; 315 } 316 317 320 public ConnectionRequestInfoImpl getInfo() { 321 return info; 322 } 323 324 325 331 334 public String getPassword() { 335 return emptyToNull(info.getPassword()); 336 } 337 338 341 public void setPassword(String password) { 342 info.setPassword(password); 343 } 344 345 348 public String getUserName() { 349 return emptyToNull(info.getUserName()); 350 } 351 352 355 public void setUserName(String userid) { 356 info.setUserName(userid); 357 } 358 359 360 366 private boolean notEqual(Object o1, Object o2) { 368 return (o1 == null ^ o2 == null) || (o1 != null && !o1.equals(o2)); 369 } 370 371 private String defaultValue(String value, String defaultValue) { 373 if (value != null) 374 return value; 375 return defaultValue; 376 } 377 378 private String emptyToNull(String value) { 380 if (value == null || value.length() == 0) { 381 return null; 382 } 383 return value; 384 } 385 } 386 | Popular Tags |