1 package org.apache.ojb.otm.connector; 2 3 17 18 import org.apache.ojb.broker.PBKey; 19 import org.apache.ojb.otm.OTMConnection; 20 import org.apache.ojb.otm.core.Transaction; 21 import org.apache.ojb.otm.core.TransactionException; 22 23 import javax.resource.NotSupportedException ; 24 import javax.resource.ResourceException ; 25 import javax.resource.spi.ConnectionEvent ; 26 import javax.resource.spi.ConnectionEventListener ; 27 import javax.resource.spi.ConnectionRequestInfo ; 28 import javax.resource.spi.LocalTransaction ; 29 import javax.resource.spi.ManagedConnection ; 30 import javax.resource.spi.ManagedConnectionFactory ; 31 import javax.resource.spi.ManagedConnectionMetaData ; 32 import javax.security.auth.Subject ; 33 import javax.transaction.xa.XAResource ; 34 import java.io.PrintWriter ; 35 import java.util.ArrayList ; 36 import java.util.Collection ; 37 import java.util.HashSet ; 38 import java.util.Iterator ; 39 import java.util.Set ; 40 41 45 46 public class OTMJCAManagedConnection implements ManagedConnection , LocalTransaction 47 { 48 private PBKey m_pbKey; 49 private OTMJCAManagedConnectionFactory m_managedConnectionFactory; 50 private PrintWriter m_logWriter; 51 private boolean m_destroyed; 52 private final Set m_handles = new HashSet (); 53 private final Collection m_connectionEventListeners = new ArrayList (); 54 private boolean m_managed = false; 55 58 private OTMConnection m_connection; 59 private Transaction m_tx; 60 61 OTMJCAManagedConnection(ManagedConnectionFactory mcf, OTMConnection conn, PBKey pbKey) 62 { 63 Util.log("In OTMJCAManagedConnection"); 64 m_managedConnectionFactory = (OTMJCAManagedConnectionFactory) mcf; 65 m_pbKey = pbKey; 66 m_connection = conn; 67 } 68 69 73 OTMConnection getConnection() 74 { 75 if (m_connection == null) 76 { 77 OTMConnectionRuntimeException ex = new OTMConnectionRuntimeException("Connection is null."); 78 sendEvents(ConnectionEvent.CONNECTION_ERROR_OCCURRED, ex, null); 79 } 80 return m_connection; 81 } 82 83 public Transaction getTransaction() 84 { 85 return this.m_tx; 86 } 87 88 public void setTransaction(Transaction tx) 89 { 90 if (this.m_tx != null) throw new IllegalStateException ("Connection already has Transaction"); 91 this.m_tx = tx; 92 } 93 94 public String getUserName() 95 { 96 return m_pbKey.getUser(); 97 } 98 99 public Object getConnection(Subject subject, ConnectionRequestInfo connectionRequestInfo) 100 throws ResourceException 101 { 102 Util.log("In OTMJCAManagedConnection.getConnection"); 103 OTMJCAConnection myCon = new OTMJCAConnection(this); 104 synchronized (m_handles) 105 { 106 m_handles.add(myCon); 107 } 108 return myCon; 109 } 110 111 public void destroy() 112 { 113 Util.log("In OTMJCAManagedConnection.destroy"); 114 cleanup(); 115 m_connection.close(); 116 m_destroyed = true; 117 } 118 119 public void cleanup() 120 { 121 Util.log("In OTMJCAManagedConnection.cleanup"); 122 synchronized (m_handles) 123 { 124 for (Iterator i = m_handles.iterator(); i.hasNext();) 125 { 126 OTMJCAConnection lc = (OTMJCAConnection) i.next(); 127 lc.setManagedConnection(null); 128 } 129 m_handles.clear(); 130 } 131 } 132 133 void closeHandle(OTMJCAConnection handle) 134 { 135 synchronized (m_handles) 136 { 137 m_handles.remove(handle); 138 } 139 sendEvents(ConnectionEvent.CONNECTION_CLOSED, null, handle); 140 } 141 142 public void associateConnection(Object connection) 143 { 144 Util.log("In OTMJCAManagedConnection.associateConnection"); 145 if (connection == null) 146 { 147 throw new OTMConnectionRuntimeException("Cannot associate a null connection"); 148 } 149 if (!(connection instanceof OTMJCAConnection)) 150 { 151 throw new OTMConnectionRuntimeException("Cannot associate a connection of type: " + connection.getClass().getName() + " to a handle that manages: " + OTMJCAConnection.class.getName()); 152 } 153 ((OTMJCAConnection) connection).setManagedConnection(this); 154 synchronized (m_handles) 155 { 156 m_handles.add(connection); 157 } 158 } 159 160 public void addConnectionEventListener(ConnectionEventListener cel) 161 { 162 synchronized (m_connectionEventListeners) 163 { 164 m_connectionEventListeners.add(cel); 165 } 166 } 167 168 public void removeConnectionEventListener(ConnectionEventListener cel) 169 { 170 synchronized (m_connectionEventListeners) 171 { 172 m_connectionEventListeners.remove(cel); 173 } 174 } 175 176 public XAResource getXAResource() 177 throws ResourceException 178 { 179 Util.log("In OTMJCAManagedConnection.getXAResource"); 180 throw new NotSupportedException ("public XAResource getXAResource() not supported in this release."); 181 } 182 183 187 public LocalTransaction getLocalTransaction() 188 { 189 Util.log("In OTMJCAManagedConnection.getLocalTransaction"); 190 return this; 191 } 192 193 public ManagedConnectionMetaData getMetaData() 194 throws ResourceException 195 { 196 Util.log("In OTMJCAManagedConnection.getMetaData"); 197 return new OTMConnectionMetaData(this); 198 } 199 200 public void setLogWriter(PrintWriter out) 201 throws ResourceException 202 { 203 Util.log("In OTMJCAManagedConnection.setLogWriter"); 204 m_logWriter = out; 205 } 206 207 public PrintWriter getLogWriter() 208 throws ResourceException 209 { 210 Util.log("In OTMJCAManagedConnection.getLogWriter"); 211 return m_logWriter; 212 } 213 214 boolean isDestroyed() 215 { 216 Util.log("In OTMJCAManagedConnection.isDestroyed"); 217 return m_destroyed; 218 } 219 220 ManagedConnectionFactory getManagedConnectionFactory() 221 { 222 Util.log("In OTMJCAManagedConnection.getManagedConnectionFactory"); 223 return m_managedConnectionFactory; 224 } 225 226 public void begin() throws ResourceException 227 { 228 Util.log("In OTMJCAManagedConnection.begin"); 229 if (!isManaged()) 230 { 231 try 232 { 233 m_tx = m_managedConnectionFactory.getKit().getTransaction(m_connection); 234 m_tx.begin(); 235 setManaged(true); 236 } 237 catch (TransactionException e) 238 { 239 ResourceException ex = new ResourceException (e.getMessage()); 240 sendEvents(ConnectionEvent.CONNECTION_ERROR_OCCURRED, ex, null); 241 throw ex; 242 } 243 } 244 else 245 { 246 ResourceException ex = new ResourceException ("You probably called begin again without calling Commit or Rollback, OTM does not support nested Local Transactions."); 247 sendEvents(ConnectionEvent.CONNECTION_ERROR_OCCURRED, ex, null); 248 throw ex; 249 } 250 } 251 252 public void commit() throws ResourceException 253 { 254 Util.log("In OTMJCAManagedConnection.commit"); 255 if (isManaged()) 256 { 257 try 258 { 259 setManaged(false); 260 m_tx.commit(); 261 } 262 catch (TransactionException e) 263 { 264 m_tx.rollback(); 265 ResourceException ex = new ResourceException (e.getMessage()); 266 sendEvents(ConnectionEvent.CONNECTION_ERROR_OCCURRED, ex, null); 267 throw ex; 268 } 269 } 270 else 271 { 272 ResourceException ex = new ResourceException ("Cannot call commit when you are not in a Local Transaction."); 273 sendEvents(ConnectionEvent.CONNECTION_ERROR_OCCURRED, ex, null); 274 throw ex; 275 } 276 } 277 278 public void rollback() throws ResourceException 279 { 280 Util.log("In OTMJCAManagedConnection.rollback"); 281 if (isManaged()) 282 { 283 try 284 { 285 m_tx.rollback(); 286 setManaged(false); 287 } 288 catch (TransactionException e) 289 { 290 ResourceException ex = new ResourceException (e.getMessage()); 291 sendEvents(ConnectionEvent.CONNECTION_ERROR_OCCURRED, ex, null); 292 throw ex; 293 } 294 } 295 else 296 { 297 ResourceException ex = new ResourceException ("Cannot call rollback when you are not in a Local Transaction."); 298 sendEvents(ConnectionEvent.CONNECTION_ERROR_OCCURRED, ex, null); 299 throw ex; 300 } 301 } 302 303 private boolean isManaged() 304 { 305 return m_managed; 306 } 307 308 private void setManaged(boolean flag) 309 { 310 m_managed = flag; 311 } 312 313 325 void sendEvents(int eventType, Exception ex, Object connectionHandle) 326 { 327 ConnectionEvent ce = null; 328 if (ex == null) 329 { 330 ce = new ConnectionEvent (this, eventType); 331 } 332 else 333 { 334 ce = new ConnectionEvent (this, eventType, ex); 335 } 336 ce.setConnectionHandle(connectionHandle); 337 Collection copy = null; 338 synchronized (m_connectionEventListeners) 339 { 340 copy = new ArrayList (m_connectionEventListeners); 341 } 342 switch (ce.getId()) 343 { 344 case ConnectionEvent.CONNECTION_CLOSED: 345 for (Iterator i = copy.iterator(); i.hasNext();) 346 { 347 ConnectionEventListener cel = (ConnectionEventListener ) i.next(); 348 cel.connectionClosed(ce); 349 } 350 break; 351 case ConnectionEvent.LOCAL_TRANSACTION_STARTED: 352 for (Iterator i = copy.iterator(); i.hasNext();) 353 { 354 ConnectionEventListener cel = (ConnectionEventListener ) i.next(); 355 cel.localTransactionStarted(ce); 356 } 357 break; 358 case ConnectionEvent.LOCAL_TRANSACTION_COMMITTED: 359 for (Iterator i = copy.iterator(); i.hasNext();) 360 { 361 ConnectionEventListener cel = (ConnectionEventListener ) i.next(); 362 cel.localTransactionCommitted(ce); 363 } 364 break; 365 case ConnectionEvent.LOCAL_TRANSACTION_ROLLEDBACK: 366 for (Iterator i = copy.iterator(); i.hasNext();) 367 { 368 ConnectionEventListener cel = (ConnectionEventListener ) i.next(); 369 cel.localTransactionRolledback(ce); 370 } 371 break; 372 case ConnectionEvent.CONNECTION_ERROR_OCCURRED: 373 for (Iterator i = copy.iterator(); i.hasNext();) 374 { 375 ConnectionEventListener cel = (ConnectionEventListener ) i.next(); 376 cel.connectionErrorOccurred(ce); 377 } 378 break; 379 default: 380 throw new IllegalArgumentException ("Illegal eventType: " + ce.getId()); 381 } 382 } 383 } 384 | Popular Tags |