001: package example;
002:
003: import java.io.*;
004: import java.util.*;
005:
006: import javax.resource.spi.ManagedConnection;
007: import javax.resource.spi.ConnectionRequestInfo;
008: import javax.resource.spi.ConnectionEventListener;
009: import javax.resource.spi.ConnectionEvent;
010:
011: import javax.resource.spi.ManagedConnectionMetaData;
012: import javax.resource.spi.LocalTransaction;
013:
014: import javax.transaction.xa.XAResource;
015:
016: import javax.security.auth.Subject;
017:
018: /**
019: * ManagedConnectionImpl represents the underlying (SPI) connection to the
020: * resource. Resin will manage this ManagedConnection in its pool.
021: *
022: * The user will see the ConnectionImpl facade and may not even know
023: * that the ManagedConnectionImpl exists.
024: */
025: public class ManagedConnectionImpl implements ManagedConnection {
026: private ManagedConnectionFactoryImpl _factory;
027:
028: // Identifier for the ManagedConnectionImpl
029: private String _name;
030:
031: // Resin needs to listen for close events
032: private ArrayList _listeners = new ArrayList();
033:
034: /**
035: * Creates a new ManagedConnection with its id.
036: * ManagedConnectionFactoryImpl will create the ManagedConnectionImpl
037: * in response to a Resin request.
038: */
039: ManagedConnectionImpl(String name,
040: ManagedConnectionFactoryImpl factory) {
041: _name = name;
042: _factory = factory;
043: }
044:
045: /**
046: * Creates a new application connection. The application connection
047: * will be in use until its <code>close()</code> method is called.
048: * It's <code>close</code> method will call the ConnectionEventListener
049: * registered with this ManagedConnectionImpl instance.
050: *
051: * It is important for the connection's close to be called and for the
052: * connection to call the close listeners, because Resin needs to
053: * know when the application is done with the connection and Resin
054: * can return the ManagedConnection to the pool.
055: *
056: * @param subject the subject for the connection
057: * @param info the connection information for the connection
058: *
059: * @return the application-view of the connection
060: */
061: public Object getConnection(Subject subject,
062: ConnectionRequestInfo info) {
063: return new ConnectionImpl(_factory.generateConnectionName(),
064: this );
065: }
066:
067: /**
068: * XXX:???
069: *
070: * In some cases, Resin can associate an old application connection
071: * with the ManagedConnection. (I'm not sure when this can happen.)
072: *
073: * @param conn the application view of the connection
074: */
075: public void associateConnection(Object conn) {
076: }
077:
078: /**
079: * Resin will register a listener with the ManagedConnection so it
080: * will know when a connection closes or has a fatal error.
081: *
082: * @param listener Resin's listener to receive notice of a close.
083: */
084: public void addConnectionEventListener(
085: ConnectionEventListener listener) {
086: _listeners.add(listener);
087: }
088:
089: /**
090: * Resin can remove it's listener when it removes the managed connection
091: * from the pool.
092: *
093: * @param listener Resin's listener to receive notice of a close.
094: */
095: public void removeConnectionEventListener(
096: ConnectionEventListener listener) {
097: _listeners.remove(listener);
098: }
099:
100: /**
101: * Implementation method to allow the <code>ConnectionImpl</code> to
102: * trigger Resin's listeners when the connection closes.
103: *
104: * @param conn the user connection which is closing.
105: */
106: void close(ConnectionImpl conn) {
107: ConnectionEvent evt;
108: evt = new ConnectionEvent(this ,
109: ConnectionEvent.CONNECTION_CLOSED);
110:
111: for (int i = 0; i < _listeners.size(); i++) {
112: ConnectionEventListener listener;
113: listener = (ConnectionEventListener) _listeners.get(i);
114:
115: listener.connectionClosed(evt);
116: }
117: }
118:
119: /**
120: * This example isn't returning any meta data. In general,
121: * providing the meta data is nice for the applications.
122: */
123: public ManagedConnectionMetaData getMetaData() {
124: return null;
125: }
126:
127: /**
128: * Transaction-aware resources will return the XAResource for the
129: * managed connection.
130: */
131: public XAResource getXAResource() {
132: return null;
133: }
134:
135: /**
136: * Transaction-aware resources will return the LocalTransaction for the
137: * managed connection. LocalTransaction is a lightweight interface
138: * for transactions that don't need the full XA transactions.
139: */
140: public LocalTransaction getLocalTransaction() {
141: return null;
142: }
143:
144: /**
145: * Called when Resin returns a connection to the idle pool.
146: */
147: public void cleanup() {
148: }
149:
150: /**
151: * Called when Resin is closing the connection. Any sockets, etc,
152: * would be closed here.
153: */
154: public void destroy() {
155: }
156:
157: /**
158: * Logging should use JDK 1.4 java.util.logging.
159: */
160: public PrintWriter getLogWriter() {
161: return null;
162: }
163:
164: /**
165: * Logging should use JDK 1.4 java.util.logging.
166: */
167: public void setLogWriter(PrintWriter log) {
168: }
169:
170: public String toString() {
171: return "ManagedConnectionImpl[" + _name + "]";
172: }
173: }
|