1 22 package org.jboss.test.jcaprops.support; 23 24 import java.io.PrintWriter ; 25 import java.util.Iterator ; 26 27 import javax.resource.ResourceException ; 28 import javax.resource.spi.ConnectionEvent ; 29 import javax.resource.spi.ConnectionEventListener ; 30 import javax.resource.spi.ConnectionRequestInfo ; 31 import javax.resource.spi.LocalTransaction ; 32 import javax.resource.spi.ManagedConnection ; 33 import javax.resource.spi.ManagedConnectionMetaData ; 34 import javax.security.auth.Subject ; 35 import javax.transaction.xa.XAResource ; 36 37 import EDU.oswego.cs.dl.util.concurrent.CopyOnWriteArraySet; 38 import EDU.oswego.cs.dl.util.concurrent.SynchronizedBoolean; 39 40 46 public class PropertyTestManagedConnection implements ManagedConnection , LocalTransaction 47 { 48 private SynchronizedBoolean destroyed = new SynchronizedBoolean(false); 49 private CopyOnWriteArraySet connections = new CopyOnWriteArraySet(); 50 private CopyOnWriteArraySet listeners = new CopyOnWriteArraySet(); 51 53 public PropertyTestManagedConnection(PropertyTestManagedConnectionFactory mcf) 54 { 55 } 57 58 public void addConnectionEventListener(ConnectionEventListener listener) 59 { 60 checkDestroyed(); 61 listeners.add(listener); 62 } 63 64 public void associateConnection(Object connection) throws ResourceException 65 { 66 checkDestroyed(); 67 if (connection instanceof PropertyTestConnectionImpl) 68 throw new ResourceException ("Wrong object"); 69 ((PropertyTestConnectionImpl) connection).setManagedConnection(this); 70 connections.add(connection); 71 } 72 73 public void cleanup() throws ResourceException 74 { 75 for (Iterator i = connections.iterator(); i.hasNext();) 76 { 77 PropertyTestConnectionImpl connection = (PropertyTestConnectionImpl) i.next(); 78 connection.setManagedConnection(null); 79 } 80 connections.clear(); 81 } 82 83 public void destroy() throws ResourceException 84 { 85 destroyed.set(true); 86 cleanup(); 87 } 88 89 public Object getConnection(Subject subject, ConnectionRequestInfo cxRequestInfo) throws ResourceException 90 { 91 PropertyTestConnectionImpl connection = new PropertyTestConnectionImpl(); 92 connection.setManagedConnection(this); 93 return connection; 94 } 95 96 public LocalTransaction getLocalTransaction() throws ResourceException 97 { 98 return this; 99 } 100 101 public PrintWriter getLogWriter() throws ResourceException 102 { 103 return null; 104 } 105 106 public ManagedConnectionMetaData getMetaData() throws ResourceException 107 { 108 return null; 109 } 110 111 public XAResource getXAResource() throws ResourceException 112 { 113 return null; 114 } 115 116 public void removeConnectionEventListener(ConnectionEventListener listener) 117 { 118 listeners.remove(listener); 119 } 120 121 public void setLogWriter(PrintWriter out) throws ResourceException 122 { 123 } 124 125 public void begin() throws ResourceException 126 { 127 } 128 129 public void commit() throws ResourceException 130 { 131 } 132 133 public void rollback() throws ResourceException 134 { 135 } 136 137 protected void checkDestroyed() 138 { 139 if (destroyed.get()) 140 throw new IllegalStateException ("Destroyed"); 141 } 142 143 void closeHandle(PropertyTestConnectionImpl connection) 144 { 145 if (destroyed.get()) 146 return; 147 148 connections.remove(connection); 149 150 ConnectionEvent ce = new ConnectionEvent (this, ConnectionEvent.CONNECTION_CLOSED); 151 ce.setConnectionHandle(connection); 152 for (Iterator i = listeners.iterator(); i.hasNext();) 153 { 154 ConnectionEventListener listener = (ConnectionEventListener ) i.next(); 155 listener.connectionClosed(ce); 156 } 157 } 158 } 159 | Popular Tags |