1 25 package org.objectweb.joram.client.connector; 26 27 import org.objectweb.joram.client.jms.FactoryParameters; 28 import org.objectweb.joram.client.jms.ha.local.XAHALocalConnectionFactory; 29 import org.objectweb.joram.client.jms.ha.tcp.HATcpConnectionFactory; 30 import org.objectweb.joram.client.jms.ha.tcp.XAHATcpConnectionFactory; 31 import org.objectweb.joram.client.jms.local.XALocalConnectionFactory; 32 import org.objectweb.joram.client.jms.tcp.TcpConnectionFactory; 33 import org.objectweb.joram.client.jms.tcp.XATcpConnectionFactory; 34 35 import javax.jms.ConnectionFactory ; 36 import javax.jms.JMSException ; 37 import javax.jms.JMSSecurityException ; 38 import javax.jms.IllegalStateException ; 39 import javax.jms.XAConnection ; 40 import javax.jms.XAConnectionFactory ; 41 import javax.jms.XAQueueConnection ; 42 import javax.jms.XATopicConnection ; 43 import javax.naming.StringRefAddr ; 44 import javax.naming.Reference ; 45 import javax.resource.ResourceException ; 46 import javax.resource.spi.CommException ; 47 import javax.resource.spi.ConnectionManager ; 48 import javax.resource.spi.ConnectionRequestInfo ; 49 import javax.resource.spi.ManagedConnection ; 50 import javax.resource.spi.ResourceAdapter ; 51 import javax.resource.spi.SecurityException ; 52 import javax.security.auth.Subject ; 53 54 import java.io.PrintWriter ; 55 import java.util.Iterator ; 56 import java.util.Set ; 57 import java.util.Vector ; 58 59 import org.objectweb.util.monolog.api.BasicLevel; 60 61 65 public class ManagedConnectionFactoryImpl 66 implements javax.resource.spi.ManagedConnectionFactory , 67 javax.resource.spi.ResourceAdapterAssociation , 68 javax.resource.spi.ValidatingManagedConnectionFactory , 69 java.io.Serializable 70 { 71 72 private transient Vector connections = null; 73 74 protected transient PrintWriter out = null; 75 76 77 transient JoramAdapter ra = null; 78 79 80 boolean collocated; 81 82 83 boolean isHa; 84 85 86 String hostName; 87 88 int serverPort; 89 90 91 String userName = "anonymous"; 92 93 String password = "anonymous"; 94 95 100 public int connectingTimer = 0; 101 106 public int txPendingTimer = 0; 107 113 public int cnxPendingTimer = 0; 114 115 120 public boolean asyncSend; 121 122 129 public boolean multiThreadSync; 130 131 137 public int multiThreadSyncDelay = -1; 138 139 142 public ManagedConnectionFactoryImpl() 143 {} 144 145 public int getConnectingTimer() { 146 return connectingTimer; 147 } 148 149 public int getCnxPendingTimer() { 150 return cnxPendingTimer; 151 } 152 153 public int getTxPendingTimer() { 154 return txPendingTimer; 155 } 156 157 protected void setParameters(Object factory) { 158 FactoryParameters fp = null; 159 if (factory instanceof org.objectweb.joram.client.jms.ConnectionFactory) { 160 org.objectweb.joram.client.jms.ConnectionFactory f = 161 (org.objectweb.joram.client.jms.ConnectionFactory) factory; 162 fp = f.getParameters(); 163 } else if (factory instanceof org.objectweb.joram.client.jms.XAConnectionFactory) { 164 org.objectweb.joram.client.jms.XAConnectionFactory f = 165 (org.objectweb.joram.client.jms.XAConnectionFactory) factory; 166 fp = f.getParameters(); 167 } 168 if (fp != null) { 169 fp.connectingTimer = connectingTimer; 170 fp.cnxPendingTimer = cnxPendingTimer; 171 fp.txPendingTimer = txPendingTimer; 172 if (asyncSend) { 173 fp.asyncSend = asyncSend; 174 } 175 if (multiThreadSync) { 176 fp.multiThreadSync = multiThreadSync; 177 } 178 if (multiThreadSyncDelay > 0) { 179 fp.multiThreadSyncDelay = multiThreadSyncDelay; 180 } 181 } 182 } 183 184 192 public Object createConnectionFactory(ConnectionManager cxManager) 193 throws ResourceException { 194 if (AdapterTracing.dbgAdapter.isLoggable(BasicLevel.DEBUG)) 195 AdapterTracing.dbgAdapter.log(BasicLevel.DEBUG, this + " createConnectionFactory(" + cxManager + ")"); 196 197 return new OutboundConnectionFactory(this, cxManager); 198 } 199 200 206 public Object createConnectionFactory() throws ResourceException { 207 if (AdapterTracing.dbgAdapter.isLoggable(BasicLevel.DEBUG)) 208 AdapterTracing.dbgAdapter.log(BasicLevel.DEBUG, this + " createConnectionFactory()"); 209 210 OutboundConnectionFactory factory = 211 new OutboundConnectionFactory(this, null); 212 213 Reference ref = 214 new Reference (factory.getClass().getName(), 215 "org.objectweb.joram.client.connector.ObjectFactoryImpl", 216 null); 217 ref.add(new StringRefAddr ("hostName", hostName)); 218 ref.add(new StringRefAddr ("serverPort", "" + serverPort)); 219 ref.add(new StringRefAddr ("userName", userName)); 220 ref.add(new StringRefAddr ("password", password)); 221 222 factory.setReference(ref); 223 return factory; 224 } 225 226 242 public ManagedConnection 243 createManagedConnection(Subject subject, 244 ConnectionRequestInfo cxRequest) 245 throws ResourceException { 246 if (AdapterTracing.dbgAdapter.isLoggable(BasicLevel.DEBUG)) 247 AdapterTracing.dbgAdapter.log(BasicLevel.DEBUG, 248 this + " createManagedConnection(" + subject + 249 ", " + cxRequest + ")"); 250 251 String userName; 252 String password; 253 254 String hostName = this.hostName; 255 int serverPort = this.serverPort; 256 257 if (cxRequest == null) { 260 userName = this.userName; 261 password = this.password; 262 } 263 else { 264 if (! (cxRequest instanceof ConnectionRequest)) { 265 if (out != null) 266 out.print("Provided ConnectionRequestInfo instance is not a JORAM object."); 267 throw new ResourceException ("Provided ConnectionRequestInfo instance " 268 + "is not a JORAM object."); 269 } 270 271 userName = ((ConnectionRequest) cxRequest).getUserName(); 272 password = ((ConnectionRequest) cxRequest).getPassword(); 273 } 274 275 XAConnectionFactory factory; 276 XAConnection cnx = null; 277 278 if (collocated) { 279 hostName = "localhost"; 280 serverPort = -1; 281 } 282 283 if (isHa) { 284 if (collocated) { 285 factory = XAHALocalConnectionFactory.create(); 286 } else { 287 String urlHa = "hajoram://" + hostName + ":" + serverPort; 288 factory = XAHATcpConnectionFactory.create(urlHa); 289 } 290 } else { 291 if (collocated) { 292 factory = XALocalConnectionFactory.create(); 293 } else { 294 factory = XATcpConnectionFactory.create(hostName, serverPort); 295 } 296 } 297 298 setParameters(factory); 299 300 try { 301 cnx = factory.createXAConnection(userName, password); 302 303 if (AdapterTracing.dbgAdapter.isLoggable(BasicLevel.DEBUG)) 304 AdapterTracing.dbgAdapter.log(BasicLevel.DEBUG, 305 this + " createManagedConnection cnx = " + cnx); 306 } catch (IllegalStateException exc) { 307 if (out != null) 308 out.print("Could not access the JORAM server: " + exc); 309 throw new CommException ("Could not access the JORAM server: " + exc); 310 } catch (JMSSecurityException exc) { 311 if (out != null) 312 out.print("Invalid user identification: " + exc); 313 throw new SecurityException ("Invalid user identification: " + exc); 314 } catch (JMSException exc) { 315 if (out != null) 316 out.print("Failed connecting process: " + exc); 317 throw new ResourceException ("Failed connecting process: " + exc); 318 } 319 320 ManagedConnection managedCx = new ManagedConnectionImpl(ra, 321 cnx, 322 hostName, 323 serverPort, 324 userName); 325 managedCx.setLogWriter(out); 326 327 if (AdapterTracing.dbgAdapter.isLoggable(BasicLevel.DEBUG)) 328 AdapterTracing.dbgAdapter.log(BasicLevel.DEBUG, 329 this + " createManagedConnection managedCx = " + managedCx); 330 return managedCx; 331 } 332 333 344 public ManagedConnection 345 matchManagedConnections(Set connectionSet, 346 Subject subject, 347 ConnectionRequestInfo cxRequest) 348 throws ResourceException { 349 if (AdapterTracing.dbgAdapter.isLoggable(BasicLevel.DEBUG)) 350 AdapterTracing.dbgAdapter.log(BasicLevel.DEBUG, 351 this + " matchManagedConnections(" + connectionSet + 352 ", " + subject + ", " + cxRequest + ")"); 353 354 String userName; 355 356 String mode = "Unified"; 358 359 if (cxRequest == null) 360 userName = this.userName; 361 else { 362 if (! (cxRequest instanceof ConnectionRequest)) { 363 out.print("Provided ConnectionRequestInfo instance is not a JORAM object."); 364 throw new ResourceException ("Provided ConnectionRequestInfo instance " 365 + "is not a JORAM object."); 366 } 367 368 if (cxRequest instanceof QueueConnectionRequest) 369 mode = "PTP"; 370 else if (cxRequest instanceof TopicConnectionRequest) 371 mode = "PubSub"; 372 373 userName = ((ConnectionRequest) cxRequest).getUserName(); 374 } 375 376 ManagedConnectionImpl managedCx = null; 377 boolean matching = false; 378 379 Iterator it = connectionSet.iterator(); 380 381 String hostName = this.hostName; 382 int serverPort = this.serverPort; 383 384 if (collocated) { 385 hostName = "localhost"; 386 serverPort = -1; 387 } 388 389 390 while (! matching && it.hasNext()) { 391 try { 392 managedCx = (ManagedConnectionImpl) it.next(); 393 394 matching = 395 managedCx.matches(hostName, serverPort, userName, mode); 396 } 397 catch (ClassCastException exc) { 398 } 399 } 400 401 if (matching) { 402 if (AdapterTracing.dbgAdapter.isLoggable(BasicLevel.DEBUG)) 403 AdapterTracing.dbgAdapter.log(BasicLevel.DEBUG, 404 this + " matchManagedConnections managedCx = " + managedCx); 405 managedCx.setLogWriter(out); 406 return managedCx; 407 } 408 409 return null; 410 } 411 412 413 414 418 public void setLogWriter(PrintWriter out) throws ResourceException 419 { 420 this.out = out; 421 } 422 423 427 public PrintWriter getLogWriter() throws ResourceException 428 { 429 return out; 430 } 431 432 433 public int hashCode() 434 { 435 return ("Unified:" 436 + hostName 437 + ":" 438 + serverPort 439 + "-" 440 + userName).hashCode(); 441 } 442 443 444 public boolean equals(Object o) 445 { 446 if (! (o instanceof ManagedConnectionFactoryImpl) 447 || o instanceof ManagedQueueConnectionFactoryImpl 448 || o instanceof ManagedTopicConnectionFactoryImpl) 449 return false; 450 451 ManagedConnectionFactoryImpl other = (ManagedConnectionFactoryImpl) o; 452 453 boolean res = 454 hostName.equals(other.hostName) 455 && serverPort == other.serverPort 456 && userName.equals(other.userName); 457 458 if (AdapterTracing.dbgAdapter.isLoggable(BasicLevel.DEBUG)) 459 AdapterTracing.dbgAdapter.log(BasicLevel.DEBUG, 460 this + " equals " + res); 461 return res; 462 } 463 464 465 public ResourceAdapter getResourceAdapter() { 466 if (AdapterTracing.dbgAdapter.isLoggable(BasicLevel.DEBUG)) 467 AdapterTracing.dbgAdapter.log(BasicLevel.DEBUG, 468 this + " getResourceAdapter() = " + ra); 469 return ra; 470 } 471 472 477 public void setResourceAdapter(ResourceAdapter ra) 478 throws ResourceException { 479 if (AdapterTracing.dbgAdapter.isLoggable(BasicLevel.DEBUG)) 480 AdapterTracing.dbgAdapter.log(BasicLevel.DEBUG, 481 this + " setResourceAdapter(" + ra + ")"); 482 483 if (this.ra != null) { 484 out.print("ResourceAdapter instance already associated."); 485 throw new javax.resource.spi.IllegalStateException ("ResourceAdapter " 486 + "instance " 487 + "already " 488 + "associated."); 489 } 490 491 if (! (ra instanceof JoramAdapter)) { 492 out.print("Provided ResourceAdapter is not a JORAM ResourceAdapter object: " 493 + ra.getClass().getName()); 494 throw new ResourceException ("Provided ResourceAdapter is not a JORAM " 495 + "ResourceAdapter object: " 496 + ra.getClass().getName()); 497 } 498 499 this.ra = (JoramAdapter) ra; 500 collocated = ((JoramAdapter) ra).collocated; 501 isHa = ((JoramAdapter) ra).isHa; 502 hostName = ((JoramAdapter) ra).hostName; 503 serverPort = ((JoramAdapter) ra).serverPort; 504 connectingTimer = ((JoramAdapter) ra).connectingTimer; 505 txPendingTimer = ((JoramAdapter) ra).txPendingTimer; 506 cnxPendingTimer = ((JoramAdapter) ra).cnxPendingTimer; 507 asyncSend = ((JoramAdapter) ra).asyncSend; 508 multiThreadSync = ((JoramAdapter) ra).multiThreadSync; 509 multiThreadSyncDelay = ((JoramAdapter) ra).multiThreadSyncDelay; 510 511 if (AdapterTracing.dbgAdapter.isLoggable(BasicLevel.DEBUG)) 512 AdapterTracing.dbgAdapter.log(BasicLevel.DEBUG, 513 this + " setResourceAdapter collocated = " + collocated + 514 ", isHa = " + isHa + 515 ", hostName = " + hostName + 516 ", serverPort = " + serverPort + 517 ", connectingTimer = " + connectingTimer + 518 ", txPendingTimer = " + txPendingTimer + 519 ", cnxPendingTimer = " + cnxPendingTimer); 520 } 521 522 525 public Set getInvalidConnections(Set connectionSet) 526 throws ResourceException { 527 if (AdapterTracing.dbgAdapter.isLoggable(BasicLevel.DEBUG)) 528 AdapterTracing.dbgAdapter.log(BasicLevel.DEBUG, 529 this + " getInvalidConnections(" + connectionSet + ")"); 530 531 Iterator it = connectionSet.iterator(); 532 ManagedConnectionImpl managedCx; 533 534 while (it.hasNext()) { 535 try { 536 managedCx = (ManagedConnectionImpl) it.next(); 537 if (managedCx.isValid()) 538 connectionSet.remove(managedCx); 539 } 540 catch (ClassCastException exc) {} 541 } 542 543 return connectionSet; 544 } 545 546 547 private void readObject(java.io.ObjectInputStream in) 548 throws java.io.IOException , ClassNotFoundException 549 { 550 in.defaultReadObject(); 551 connections = new Vector (); 552 } 553 554 558 public void setCollocated(java.lang.Boolean collocated) 559 { 560 this.collocated = collocated.booleanValue(); 561 } 562 563 public void setHostName(java.lang.String hostName) 564 { 565 this.hostName = hostName; 566 } 567 568 public void setServerPort(java.lang.Integer serverPort) 569 { 570 this.serverPort = serverPort.intValue(); 571 } 572 573 public void setUserName(java.lang.String userName) 574 { 575 this.userName = userName; 576 } 577 578 public void setPassword(java.lang.String password) 579 { 580 this.password = password; 581 } 582 583 public java.lang.Boolean getCollocated() 584 { 585 return new Boolean (collocated); 586 } 587 588 public java.lang.String getHostName() 589 { 590 return hostName; 591 } 592 593 public java.lang.Integer getServerPort() 594 { 595 return new Integer (serverPort); 596 } 597 598 public java.lang.String getUserName() 599 { 600 return userName; 601 } 602 603 public java.lang.String getPassword() 604 { 605 return password; 606 } 607 } 608 | Popular Tags |