1 28 29 package org.apache.commons.transaction.memory.jca; 30 31 import java.io.PrintWriter ; 32 import java.util.ArrayList ; 33 import java.util.List ; 34 import java.util.Iterator ; 35 import java.util.Map ; 36 37 import javax.resource.ResourceException ; 38 import javax.resource.spi.ConnectionEvent ; 39 import javax.resource.spi.ConnectionEventListener ; 40 import javax.resource.spi.ConnectionRequestInfo ; 41 import javax.resource.spi.LocalTransaction ; 42 import javax.resource.spi.ManagedConnection ; 43 import javax.resource.spi.ManagedConnectionMetaData ; 44 import javax.security.auth.Subject ; 45 import javax.transaction.xa.XAResource ; 46 47 import org.apache.commons.transaction.memory.TransactionalMapWrapper; 48 49 54 public class MapManagedConnection implements ManagedConnection { 55 56 MapXAResource xares = null; 57 MapLocalTransaction tx = null; 58 String name = null; 59 TransactionalMapWrapper map = null; 60 61 protected MapConnection connection = null; 62 protected List listeners = new ArrayList (); 63 protected PrintWriter out; 64 65 public MapManagedConnection(ConnectionRequestInfo cxRequestInfo) { 66 name = ((MapConnectionSpec) cxRequestInfo).getName(); 67 68 map = MemoryMapResourceManager.getInstance().lookup(name); 69 xares = new MapXAResource(map); 70 tx = new MapLocalTransaction(map); 71 72 } 73 74 Map getMap() { 75 return map; 76 } 77 78 public void close() { 79 ConnectionEvent event = new ConnectionEvent (this, ConnectionEvent.CONNECTION_CLOSED); 80 for (Iterator it = listeners.iterator(); it.hasNext();) { 81 ((ConnectionEventListener ) it.next()).connectionClosed(event); 82 } 83 connection.invalidate(); 84 connection = null; 85 } 86 87 90 public Object getConnection(Subject subject, ConnectionRequestInfo cxRequestInfo) throws ResourceException { 91 92 if (connection == null) { 93 connection = new MapConnection(this); 94 } 95 return connection; 96 } 97 98 101 public void destroy() throws ResourceException { 102 103 if (connection != null) { 104 connection.invalidate(); 105 connection = null; 106 } 107 108 listeners = null; 109 name = null; 110 map = null; 111 xares = null; 112 tx = null; 113 } 114 115 118 public void cleanup() throws ResourceException { 119 120 if (connection != null) { 121 connection.invalidate(); 122 } 123 } 124 125 128 public void associateConnection(Object connection) throws ResourceException { 129 if (!(connection instanceof MapConnection)) { 130 throw new ResourceException ("Connection is not of type MapConnection"); 131 } 132 133 this.connection = (MapConnection)connection; 134 this.connection.mc = this; 135 } 136 137 140 public void addConnectionEventListener(ConnectionEventListener listener) { 141 142 listeners.add(listener); 143 } 144 145 148 public void removeConnectionEventListener(ConnectionEventListener listener) { 149 150 listeners.remove(listener); 151 } 152 153 156 public XAResource getXAResource() throws ResourceException { 157 return xares; 158 } 159 160 163 public LocalTransaction getLocalTransaction() throws ResourceException { 164 return tx; 165 } 166 167 170 public ManagedConnectionMetaData getMetaData() throws ResourceException { 171 172 return null; 173 } 174 175 178 public void setLogWriter(PrintWriter out) throws ResourceException { 179 this.out = out; 180 xares.setLoggerFacade(out); 181 } 182 183 186 public PrintWriter getLogWriter() throws ResourceException { 187 188 return out; 189 } 190 } 191 | Popular Tags |