1 25 package org.objectweb.joram.client.connector; 26 27 import org.objectweb.joram.client.jms.ha.local.XAHALocalConnectionFactory; 28 import org.objectweb.joram.client.jms.ha.local.XAQueueHALocalConnectionFactory; 29 import org.objectweb.joram.client.jms.ha.tcp.XAHATcpConnectionFactory; 30 import org.objectweb.joram.client.jms.ha.tcp.XAQueueHATcpConnectionFactory; 31 import org.objectweb.joram.client.jms.local.XALocalConnectionFactory; 32 import org.objectweb.joram.client.jms.local.XAQueueLocalConnectionFactory; 33 import org.objectweb.joram.client.jms.tcp.QueueTcpConnectionFactory; 34 import org.objectweb.joram.client.jms.tcp.TcpConnectionFactory; 35 import org.objectweb.joram.client.jms.tcp.XATcpConnectionFactory; 36 import org.objectweb.joram.client.jms.tcp.XAQueueTcpConnectionFactory; 37 38 import javax.jms.ConnectionFactory ; 39 import javax.jms.JMSException ; 40 import javax.jms.JMSSecurityException ; 41 import javax.jms.IllegalStateException ; 42 import javax.jms.QueueConnectionFactory ; 43 import javax.jms.XAConnection ; 44 import javax.jms.XAQueueConnection ; 45 import javax.jms.XAConnectionFactory ; 46 import javax.jms.XAQueueConnectionFactory ; 47 import javax.naming.StringRefAddr ; 48 import javax.naming.Reference ; 49 import javax.resource.ResourceException ; 50 import javax.resource.spi.CommException ; 51 import javax.resource.spi.ConnectionManager ; 52 import javax.resource.spi.ConnectionRequestInfo ; 53 import javax.resource.spi.ManagedConnection ; 54 import javax.resource.spi.ResourceAdapter ; 55 import javax.resource.spi.SecurityException ; 56 import javax.security.auth.Subject ; 57 58 import java.io.PrintWriter ; 59 import java.util.Iterator ; 60 import java.util.Set ; 61 import java.util.Vector ; 62 63 import org.objectweb.util.monolog.api.BasicLevel; 64 65 69 public class ManagedQueueConnectionFactoryImpl 70 extends ManagedConnectionFactoryImpl 71 implements javax.resource.spi.ManagedConnectionFactory , 72 javax.resource.spi.ResourceAdapterAssociation , 73 javax.resource.spi.ValidatingManagedConnectionFactory , 74 java.io.Serializable 75 { 76 79 public ManagedQueueConnectionFactoryImpl() 80 {} 81 82 83 91 public Object createConnectionFactory(ConnectionManager cxManager) 92 throws ResourceException { 93 if (AdapterTracing.dbgAdapter.isLoggable(BasicLevel.DEBUG)) 94 AdapterTracing.dbgAdapter.log(BasicLevel.DEBUG, this + " createConnectionFactory(" + cxManager + ")"); 95 96 return new OutboundQueueConnectionFactory(this, cxManager); 97 } 98 99 105 public Object createConnectionFactory() 106 throws ResourceException { 107 if (AdapterTracing.dbgAdapter.isLoggable(BasicLevel.DEBUG)) 108 AdapterTracing.dbgAdapter.log(BasicLevel.DEBUG, this + " createConnectionFactory()"); 109 110 OutboundConnectionFactory factory = 111 new OutboundQueueConnectionFactory(this, 112 DefaultConnectionManager.getRef()); 113 114 Reference ref = 115 new Reference (factory.getClass().getName(), 116 "org.objectweb.joram.client.connector.ObjectFactoryImpl", 117 null); 118 ref.add(new StringRefAddr ("hostName", hostName)); 119 ref.add(new StringRefAddr ("serverPort", "" + serverPort)); 120 ref.add(new StringRefAddr ("userName", userName)); 121 ref.add(new StringRefAddr ("password", password)); 122 123 factory.setReference(ref); 124 return factory; 125 } 126 127 143 public ManagedConnection 144 createManagedConnection(Subject subject, 145 ConnectionRequestInfo cxRequest) 146 throws ResourceException { 147 148 if (AdapterTracing.dbgAdapter.isLoggable(BasicLevel.DEBUG)) 149 AdapterTracing.dbgAdapter.log(BasicLevel.DEBUG, 150 this + " createManagedConnection(" + subject + 151 ", " + cxRequest + ")"); 152 153 String userName; 154 String password; 155 156 String hostName = this.hostName; 157 int serverPort = this.serverPort; 158 159 if (cxRequest == null) { 162 userName = this.userName; 163 password = this.password; 164 } 165 else { 166 if (! (cxRequest instanceof ConnectionRequest)) { 167 if (out != null) 168 out.print("Provided ConnectionRequestInfo instance is not a JORAM object."); 169 throw new ResourceException ("Provided ConnectionRequestInfo instance " 170 + "is not a JORAM object."); 171 } 172 173 userName = ((ConnectionRequest) cxRequest).getUserName(); 174 password = ((ConnectionRequest) cxRequest).getPassword(); 175 } 176 177 XAConnection cnx = null; 178 179 if (collocated) { 180 hostName = "localhost"; 181 serverPort = -1; 182 } 183 184 try { 185 186 if (isHa) { 187 if (collocated) { 188 if (cxRequest instanceof QueueConnectionRequest) { 189 XAQueueConnectionFactory factory = XAQueueHALocalConnectionFactory.create(); 190 setParameters(factory); 191 cnx = factory.createXAQueueConnection(userName, password); 192 } else { 193 XAConnectionFactory factory = XAHALocalConnectionFactory.create(); 194 setParameters(factory); 195 cnx = factory.createXAConnection(userName, password); 196 } 197 } else { 198 String urlHa = "hajoram://" + hostName + ":" + serverPort; 199 if (cxRequest instanceof QueueConnectionRequest) { 200 XAQueueConnectionFactory factory = XAQueueHATcpConnectionFactory.create(urlHa); 201 setParameters(factory); 202 cnx = factory.createXAQueueConnection(userName, password); 203 } else { 204 XAConnectionFactory factory = XAHATcpConnectionFactory.create(urlHa); 205 setParameters(factory); 206 cnx = factory.createXAConnection(userName, password); 207 } 208 } 209 } else { 210 if (collocated) { 211 if (cxRequest instanceof QueueConnectionRequest) { 212 XAQueueConnectionFactory factory = XAQueueLocalConnectionFactory.create(); 213 setParameters(factory); 214 cnx = factory.createXAQueueConnection(userName, password); 215 } else { 216 XAConnectionFactory factory = XALocalConnectionFactory.create(); 217 setParameters(factory); 218 cnx = factory.createXAConnection(userName, password); 219 } 220 } else { 221 if (cxRequest instanceof QueueConnectionRequest) { 222 XAQueueConnectionFactory factory = XAQueueTcpConnectionFactory.create(hostName, serverPort); 223 setParameters(factory); 224 cnx = factory.createXAQueueConnection(userName, password); 225 } else { 226 XAConnectionFactory factory = XATcpConnectionFactory.create(hostName, serverPort); 227 setParameters(factory); 228 cnx = factory.createXAConnection(userName, password); 229 } 230 } 231 } 232 233 if (AdapterTracing.dbgAdapter.isLoggable(BasicLevel.DEBUG)) 234 AdapterTracing.dbgAdapter.log(BasicLevel.DEBUG, this + " createManagedConnection cnx = " + cnx); 235 236 } catch (IllegalStateException exc) { 237 if (out != null) 238 out.print("Could not access the JORAM server: " + exc); 239 throw new CommException ("Could not access the JORAM server: " + exc); 240 } catch (JMSSecurityException exc) { 241 if (out != null) 242 out.print("Invalid user identification: " + exc); 243 throw new SecurityException ("Invalid user identification: " + exc); 244 } catch (JMSException exc) { 245 if (out != null) 246 out.print("Failed connecting process: " + exc); 247 throw new ResourceException ("Failed connecting process: " + exc); 248 } 249 250 ManagedConnection managedCx = new ManagedConnectionImpl(ra, 251 cnx, 252 hostName, 253 serverPort, 254 userName); 255 managedCx.setLogWriter(out); 256 257 if (AdapterTracing.dbgAdapter.isLoggable(BasicLevel.DEBUG)) 258 AdapterTracing.dbgAdapter.log(BasicLevel.DEBUG, 259 this + " createManagedConnection managedCx = " + managedCx); 260 261 return managedCx; 262 } 263 264 275 public ManagedConnection 276 matchManagedConnections(Set connectionSet, 277 Subject subject, 278 ConnectionRequestInfo cxRequest) 279 throws ResourceException { 280 281 if (AdapterTracing.dbgAdapter.isLoggable(BasicLevel.DEBUG)) 282 AdapterTracing.dbgAdapter.log(BasicLevel.DEBUG, 283 this + " matchManagedConnections(" + connectionSet + 284 ", " + subject + ", " + cxRequest + ")"); 285 286 String userName; 287 String mode = "Unified"; 288 289 if (cxRequest == null) 291 userName = this.userName; 292 else { 293 if (! (cxRequest instanceof ConnectionRequest)) { 294 if (out != null) 295 out.print("Provided ConnectionRequestInfo instance is not a JORAM object."); 296 throw new ResourceException ("Provided ConnectionRequestInfo instance " 297 + "is not a JORAM object."); 298 } 299 300 userName = ((ConnectionRequest) cxRequest).getUserName(); 301 302 if (cxRequest instanceof QueueConnectionRequest) 303 mode = "PTP"; 304 } 305 306 String hostName = this.hostName; 307 int serverPort = this.serverPort; 308 309 if (collocated) { 310 hostName = "localhost"; 311 serverPort = -1; 312 } 313 314 ManagedConnectionImpl managedCx = null; 315 boolean matching = false; 316 317 Iterator it = connectionSet.iterator(); 318 while (! matching && it.hasNext()) { 319 try { 320 managedCx = (ManagedConnectionImpl) it.next(); 321 matching = managedCx.matches(hostName, serverPort, userName, mode); 322 } 323 catch (ClassCastException exc) {} 324 } 325 326 if (matching) { 327 if (AdapterTracing.dbgAdapter.isLoggable(BasicLevel.DEBUG)) 328 AdapterTracing.dbgAdapter.log(BasicLevel.DEBUG, 329 this + " matchManagedConnections match " + managedCx); 330 managedCx.setLogWriter(out); 331 return managedCx; 332 } 333 return null; 334 } 335 336 337 public int hashCode() 338 { 339 return ("PTP:" 340 + hostName 341 + ":" 342 + serverPort 343 + "-" 344 + userName).hashCode(); 345 } 346 347 348 public boolean equals(Object o) 349 { 350 if (! (o instanceof ManagedQueueConnectionFactoryImpl)) 351 return false; 352 353 ManagedConnectionFactoryImpl other = (ManagedConnectionFactoryImpl) o; 354 355 boolean res = 356 hostName.equals(other.hostName) 357 && serverPort == other.serverPort 358 && userName.equals(other.userName); 359 360 if (AdapterTracing.dbgAdapter.isLoggable(BasicLevel.DEBUG)) 361 AdapterTracing.dbgAdapter.log(BasicLevel.DEBUG, 362 this + " equals = " + res); 363 return res; 364 } 365 } 366 | Popular Tags |