1 10 11 package org.mule.ra; 12 13 import java.io.PrintWriter ; 14 import java.util.ArrayList ; 15 import java.util.HashSet ; 16 import java.util.Iterator ; 17 import java.util.List ; 18 import java.util.Set ; 19 20 import javax.resource.NotSupportedException ; 21 import javax.resource.ResourceException ; 22 import javax.resource.spi.ConnectionEvent ; 23 import javax.resource.spi.ConnectionEventListener ; 24 import javax.resource.spi.ConnectionRequestInfo ; 25 import javax.resource.spi.ManagedConnection ; 26 import javax.resource.spi.ManagedConnectionMetaData ; 27 import javax.resource.spi.security.PasswordCredential ; 28 import javax.security.auth.Subject ; 29 import javax.transaction.xa.XAResource ; 30 31 import org.mule.config.i18n.Message; 32 import org.mule.config.i18n.Messages; 33 import org.mule.impl.security.MuleCredentials; 34 35 38 public class MuleManagedConnection implements ManagedConnection 39 { 40 private MuleManagedConnectionFactory mcf; 41 private List listeners = new ArrayList (); 42 private Set connectionSet; 43 private PrintWriter logWriter; 44 private boolean destroyed; 45 46 private PasswordCredential passCred; 47 48 56 57 MuleManagedConnection(MuleManagedConnectionFactory mcf, 58 Subject subject, 59 ConnectionRequestInfo cxRequestInfo) throws ResourceException 60 { 61 this.mcf = mcf; 62 63 this.passCred = RaHelper.getPasswordCredential(mcf, subject, cxRequestInfo); 66 67 connectionSet = new HashSet (); 68 } 69 70 80 81 public Object getConnection(Subject subject, ConnectionRequestInfo connectionRequestInfo) 82 throws ResourceException 83 { 84 85 checkIfDestroyed(); 86 87 PasswordCredential pc = RaHelper.getPasswordCredential(mcf, subject, connectionRequestInfo); 88 89 if (!passCred.equals(pc)) 90 { 91 throw new javax.resource.spi.SecurityException (new Message(Messages.AUTH_DENIED_ON_ENDPOINT_X, 93 this).getMessage()); 94 } 95 96 String user; 97 String password; 98 MuleConnectionRequestInfo info = (MuleConnectionRequestInfo)connectionRequestInfo; 99 100 user = info.getUserName(); 101 password = info.getPassword(); 102 if (user == null) 103 { 104 user = mcf.getUsername(); 106 password = mcf.getPassword(); 107 } 108 MuleCredentials creds = null; 109 if (user != null) 110 { 111 if (password == null) 112 { 113 password = ""; 114 } 115 creds = new MuleCredentials(user, password.toCharArray()); 116 } 117 118 MuleConnection connection = new DefaultMuleConnection(this, info.getManager(), creds); 119 addConnection(connection); 120 return connection; 121 } 122 123 128 129 public void destroy() throws ResourceException 130 { 131 if (destroyed) 132 { 133 return; 134 } 135 destroyed = true; 136 137 invalidateConnections(); 138 } 139 140 147 148 public void cleanup() throws ResourceException 149 { 150 checkIfDestroyed(); 151 152 invalidateConnections(); 153 } 154 155 private void invalidateConnections() 156 { 157 Iterator it = connectionSet.iterator(); 158 while (it.hasNext()) 159 { 160 DefaultMuleConnection connection = (DefaultMuleConnection)it.next(); 161 connection.invalidate(); 162 } 163 connectionSet.clear(); 164 } 165 166 174 175 public void associateConnection(Object connection) throws ResourceException 176 { 177 checkIfDestroyed(); 178 179 if (connection instanceof MuleConnection) 180 { 181 MuleConnection cnn = (MuleConnection)connection; 182 cnn.associateConnection(this); 183 } 184 else 185 { 186 throw new IllegalStateException (new Message(Messages.OBJECT_X_MARKED_INVALID, 187 DefaultMuleConnection.class.getName() + ": " 188 + (connection == null ? "null" : connection.getClass().getName())).toString()); 189 } 190 } 191 192 200 201 public void addConnectionEventListener(ConnectionEventListener listener) 202 { 203 listeners.add(listener); 204 } 205 206 212 213 public void removeConnectionEventListener(ConnectionEventListener listener) 214 { 215 listeners.remove(listener); 216 } 217 218 228 public XAResource getXAResource() throws ResourceException 230 { 231 throw new NotSupportedException ("getXAResource"); 232 } 233 234 243 244 public javax.resource.spi.LocalTransaction getLocalTransaction() throws ResourceException 245 { 246 throw new NotSupportedException ("getLocalTransaction"); 247 } 248 249 258 259 public ManagedConnectionMetaData getMetaData() throws ResourceException 260 { 261 checkIfDestroyed(); 262 return new MuleManagedConnectionMetaData(this); 263 } 264 265 273 274 public void setLogWriter(PrintWriter out) throws ResourceException 275 { 276 this.logWriter = out; 277 } 278 279 286 287 public PrintWriter getLogWriter() throws ResourceException 288 { 289 return logWriter; 290 } 291 292 297 298 public String getUsername() 299 { 300 if (passCred != null) 301 { 302 return passCred.getUserName(); 303 } 304 else 305 { 306 return null; 307 } 308 } 309 310 315 316 public PasswordCredential getPasswordCredential() 317 { 318 return passCred; 319 } 320 321 326 327 public void addConnection(MuleConnection connection) 328 { 329 connectionSet.add(connection); 330 } 331 332 337 338 private void checkIfDestroyed() throws ResourceException 339 { 340 if (destroyed) 341 { 342 throw new ResourceException ( 343 new Message(Messages.X_IS_DISPOSED, "MuleManagedConnection").toString()); 344 } 345 } 346 347 353 354 public void removeConnection(MuleConnection connection) 355 { 356 connectionSet.remove(connection); 357 } 358 359 364 365 boolean isDestroyed() 366 { 367 return destroyed; 368 } 369 370 376 377 public MuleManagedConnectionFactory getManagedConnectionFactory() 378 { 379 return this.mcf; 380 } 381 382 void fireBeginEvent() 383 { 384 ConnectionEvent event = new ConnectionEvent (MuleManagedConnection.this, 385 ConnectionEvent.LOCAL_TRANSACTION_STARTED); 386 Iterator iterator = listeners.iterator(); 387 while (iterator.hasNext()) 388 { 389 ConnectionEventListener l = (ConnectionEventListener )iterator.next(); 390 l.localTransactionStarted(event); 391 } 392 } 393 394 void fireCommitEvent() 395 { 396 ConnectionEvent event = new ConnectionEvent (MuleManagedConnection.this, 397 ConnectionEvent.LOCAL_TRANSACTION_COMMITTED); 398 Iterator iterator = listeners.iterator(); 399 while (iterator.hasNext()) 400 { 401 ConnectionEventListener l = (ConnectionEventListener )iterator.next(); 402 l.localTransactionCommitted(event); 403 } 404 } 405 406 void fireRollbackEvent() 407 { 408 ConnectionEvent event = new ConnectionEvent (MuleManagedConnection.this, 409 ConnectionEvent.LOCAL_TRANSACTION_ROLLEDBACK); 410 Iterator iterator = listeners.iterator(); 411 while (iterator.hasNext()) 412 { 413 ConnectionEventListener l = (ConnectionEventListener )iterator.next(); 414 l.localTransactionRolledback(event); 415 } 416 } 417 418 void fireCloseEvent(MuleConnection connection) 419 { 420 ConnectionEvent event = new ConnectionEvent (MuleManagedConnection.this, 421 ConnectionEvent.CONNECTION_CLOSED); 422 event.setConnectionHandle(connection); 423 424 Iterator iterator = listeners.iterator(); 425 while (iterator.hasNext()) 426 { 427 ConnectionEventListener l = (ConnectionEventListener )iterator.next(); 428 l.connectionClosed(event); 429 } 430 } 431 432 void fireErrorOccurredEvent(Exception error) 433 { 434 ConnectionEvent event = new ConnectionEvent (MuleManagedConnection.this, 435 ConnectionEvent.CONNECTION_ERROR_OCCURRED, error); 436 Iterator iterator = listeners.iterator(); 437 while (iterator.hasNext()) 438 { 439 ConnectionEventListener l = (ConnectionEventListener )iterator.next(); 440 l.connectionErrorOccurred(event); 441 } 442 } 443 444 } 445 | Popular Tags |