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.XATopicHALocalConnectionFactory; 29 import org.objectweb.joram.client.jms.ha.tcp.XAHATcpConnectionFactory; 30 import org.objectweb.joram.client.jms.ha.tcp.XATopicHATcpConnectionFactory; 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.local.XATopicLocalConnectionFactory; 34 import org.objectweb.joram.client.jms.tcp.TcpConnectionFactory; 35 import org.objectweb.joram.client.jms.tcp.TopicTcpConnectionFactory; 36 import org.objectweb.joram.client.jms.tcp.XAQueueTcpConnectionFactory; 37 import org.objectweb.joram.client.jms.tcp.XATcpConnectionFactory; 38 import org.objectweb.joram.client.jms.tcp.XATopicTcpConnectionFactory; 39 40 import javax.jms.ConnectionFactory ; 41 import javax.jms.JMSException ; 42 import javax.jms.JMSSecurityException ; 43 import javax.jms.IllegalStateException ; 44 import javax.jms.TopicConnectionFactory ; 45 import javax.jms.XAConnection ; 46 import javax.jms.XATopicConnection ; 47 import javax.jms.XAConnectionFactory ; 48 import javax.jms.XATopicConnectionFactory ; 49 import javax.naming.StringRefAddr ; 50 import javax.naming.Reference ; 51 import javax.resource.ResourceException ; 52 import javax.resource.spi.CommException ; 53 import javax.resource.spi.ConnectionManager ; 54 import javax.resource.spi.ConnectionRequestInfo ; 55 import javax.resource.spi.ManagedConnection ; 56 import javax.resource.spi.ResourceAdapter ; 57 import javax.resource.spi.SecurityException ; 58 import javax.security.auth.Subject ; 59 60 import java.io.PrintWriter ; 61 import java.util.Iterator ; 62 import java.util.Set ; 63 import java.util.Vector ; 64 65 import org.objectweb.util.monolog.api.BasicLevel; 66 67 71 public class ManagedTopicConnectionFactoryImpl 72 extends ManagedConnectionFactoryImpl 73 implements javax.resource.spi.ManagedConnectionFactory , 74 javax.resource.spi.ResourceAdapterAssociation , 75 javax.resource.spi.ValidatingManagedConnectionFactory , 76 java.io.Serializable 77 { 78 81 public ManagedTopicConnectionFactoryImpl() 82 {} 83 84 85 93 public Object createConnectionFactory(ConnectionManager cxManager) 94 throws ResourceException { 95 if (AdapterTracing.dbgAdapter.isLoggable(BasicLevel.DEBUG)) 96 AdapterTracing.dbgAdapter.log(BasicLevel.DEBUG, 97 this + " createConnectionFactory(" + cxManager + ")"); 98 99 return new OutboundTopicConnectionFactory(this, cxManager); 100 } 101 102 108 public Object createConnectionFactory() 109 throws ResourceException { 110 if (AdapterTracing.dbgAdapter.isLoggable(BasicLevel.DEBUG)) 111 AdapterTracing.dbgAdapter.log(BasicLevel.DEBUG, this + " createConnectionFactory()"); 112 113 OutboundConnectionFactory factory = 114 new OutboundTopicConnectionFactory(this, 115 DefaultConnectionManager.getRef()); 116 117 Reference ref = 118 new Reference (factory.getClass().getName(), 119 "org.objectweb.joram.client.connector.ObjectFactoryImpl", 120 null); 121 ref.add(new StringRefAddr ("hostName", hostName)); 122 ref.add(new StringRefAddr ("serverPort", "" + serverPort)); 123 ref.add(new StringRefAddr ("userName", userName)); 124 ref.add(new StringRefAddr ("password", password)); 125 126 factory.setReference(ref); 127 return factory; 128 } 129 130 146 public ManagedConnection 147 createManagedConnection(Subject subject, 148 ConnectionRequestInfo cxRequest) 149 throws ResourceException { 150 if (AdapterTracing.dbgAdapter.isLoggable(BasicLevel.DEBUG)) 151 AdapterTracing.dbgAdapter.log(BasicLevel.DEBUG, 152 this + " createManagedConnection(" + subject + 153 ", " + cxRequest + ")"); 154 155 String userName; 156 String password; 157 158 String hostName = this.hostName; 159 int serverPort = this.serverPort; 160 161 if (cxRequest == null) { 164 userName = this.userName; 165 password = this.password; 166 } 167 else { 168 if (! (cxRequest instanceof ConnectionRequest)) { 169 if (out != null) 170 out.print("Provided ConnectionRequestInfo instance is not a JORAM object."); 171 throw new ResourceException ("Provided ConnectionRequestInfo instance " 172 + "is not a JORAM object."); 173 } 174 175 userName = ((ConnectionRequest) cxRequest).getUserName(); 176 password = ((ConnectionRequest) cxRequest).getPassword(); 177 } 178 179 XAConnection cnx = null; 180 181 if (collocated) { 182 hostName = "localhost"; 183 serverPort = -1; 184 } 185 186 try { 187 if (isHa) { 188 if (collocated) { 189 if (cxRequest instanceof TopicConnectionRequest) { 190 XATopicConnectionFactory factory = XATopicHALocalConnectionFactory.create(); 191 setParameters(factory); 192 cnx = factory.createXATopicConnection(userName, password); 193 } else { 194 XAConnectionFactory factory = XAHALocalConnectionFactory.create(); 195 setParameters(factory); 196 cnx = factory.createXAConnection(userName, password); 197 } 198 } else { 199 String urlHa = "hajoram://" + hostName + ":" + serverPort; 200 if (cxRequest instanceof TopicConnectionRequest) { 201 XATopicConnectionFactory factory = XATopicHATcpConnectionFactory.create(urlHa); 202 setParameters(factory); 203 cnx = factory.createXATopicConnection(userName, password); 204 } else { 205 XAConnectionFactory factory = XAHATcpConnectionFactory.create(urlHa); 206 setParameters(factory); 207 cnx = factory.createXAConnection(userName, password); 208 } 209 } 210 } else { 211 if (collocated) { 212 if (cxRequest instanceof TopicConnectionRequest) { 213 XATopicConnectionFactory factory = XATopicLocalConnectionFactory.create(); 214 setParameters(factory); 215 cnx = factory.createXATopicConnection(userName, password); 216 } else { 217 XAConnectionFactory factory = XALocalConnectionFactory.create(); 218 setParameters(factory); 219 cnx = factory.createXAConnection(userName, password); 220 } 221 } else { 222 if (cxRequest instanceof TopicConnectionRequest) { 223 XATopicConnectionFactory factory = XATopicTcpConnectionFactory.create(hostName, serverPort); 224 setParameters(factory); 225 cnx = factory.createXATopicConnection(userName, password); 226 } else { 227 XAConnectionFactory factory = XATcpConnectionFactory.create(hostName, serverPort); 228 setParameters(factory); 229 cnx = factory.createXAConnection(userName, password); 230 } 231 } 232 } 233 if (AdapterTracing.dbgAdapter.isLoggable(BasicLevel.DEBUG)) 234 AdapterTracing.dbgAdapter.log(BasicLevel.DEBUG, 235 this + " createManagedConnection cnx = " + cnx); 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 + 285 ", " + cxRequest + ")"); 286 287 String userName; 288 String mode = "Unified"; 289 290 if (cxRequest == null) 292 userName = this.userName; 293 else { 294 if (! (cxRequest instanceof ConnectionRequest)) { 295 if (out != null) 296 out.print("Provided ConnectionRequestInfo instance is not a JORAM object."); 297 throw new ResourceException ("Provided ConnectionRequestInfo instance " 298 + "is not a JORAM object."); 299 } 300 301 userName = ((ConnectionRequest) cxRequest).getUserName(); 302 303 if (cxRequest instanceof TopicConnectionRequest) 304 mode = "PubSub"; 305 } 306 307 String hostName = this.hostName; 308 int serverPort = this.serverPort; 309 310 if (collocated) { 311 hostName = "localhost"; 312 serverPort = -1; 313 } 314 315 ManagedConnectionImpl managedCx = null; 316 boolean matching = false; 317 318 Iterator it = connectionSet.iterator(); 319 while (! matching && it.hasNext()) { 320 try { 321 managedCx = (ManagedConnectionImpl) it.next(); 322 matching = managedCx.matches(hostName, serverPort, userName, mode); 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 ("PubSub:" 340 + hostName 341 + ":" 342 + serverPort 343 + "-" 344 + userName).hashCode(); 345 } 346 347 348 public boolean equals(Object o) 349 { 350 if (! (o instanceof ManagedTopicConnectionFactoryImpl)) 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 |