001: package example;
002:
003: import java.io.*;
004: import java.util.*;
005:
006: import javax.resource.spi.ResourceAdapter;
007:
008: import javax.resource.spi.ManagedConnectionFactory;
009: import javax.resource.spi.ManagedConnection;
010: import javax.resource.spi.ConnectionManager;
011:
012: import javax.resource.spi.ManagedConnectionMetaData;
013: import javax.resource.spi.ConnectionRequestInfo;
014:
015: import javax.security.auth.Subject;
016:
017: /**
018: * Main interface between Resin and the connector. It's the
019: * top-level SPI class for creating the SPI ManagedConnections.
020: *
021: * The resource configuration in Resin's web.xml will use bean-style
022: * configuration to configure the ManagecConnectionFactory.
023: */
024: public class ManagedConnectionFactoryImpl implements
025: ManagedConnectionFactory {
026: private String _name;
027:
028: // A counter for the example to keep track of the underlying connections.
029: // Each ManagedConnectionImpl will get its own id.
030: private int _mcCount;
031:
032: // A counter for the example to keep track of the user connections.
033: // Each ConnectionImpl will get its own id.
034: private int _cCount;
035:
036: /**
037: * Sets the name for the connector
038: */
039: public void setName(String name) {
040: _name = name;
041: }
042:
043: /**
044: * Creates the application's view of the connection factory.
045: *
046: * The ConnectionFactory is the equivalent of the JDBC DataSource.
047: * Applications will use the ConnectionFactory to create connections.
048: *
049: * The connector can use any API that makes sense for it. JDBC
050: * connectors will return a DataSource. JMS connectors will return
051: * SessionFactory, etc.
052: *
053: * @param manager ConnectionManager provided by Resin gives access to
054: * some application server resources.
055: */
056: public Object createConnectionFactory(ConnectionManager manager) {
057: return new ConnectionFactoryImpl(this , manager);
058: }
059:
060: /**
061: * Creates the SPI-side of a connection, like the <code>XAConnection</code> of
062: * JDBC. Resin will use the returned <code>ManagedConnection</code>
063: * to create the application connections, manage transactions,
064: * and manage the pool.
065: *
066: * The <code>ConnectionRequestInfo</code> is not used in this example.
067: * When needed, the <code>ConnectionFactoryImpl</code> will create a
068: * <code>ConnectionRequestInfo</code> and pass it to Resin with
069: * the <code>allocateConnection</code> call.
070: *
071: * @param subject security identifier of the application requesting
072: * the connection.
073: * @param reqInfo connection-specific configuration information
074: */
075: public ManagedConnection createManagedConnection(Subject subject,
076: ConnectionRequestInfo reqInfo) {
077: return new ManagedConnectionImpl(_name + "-" + _mcCount++, this );
078: }
079:
080: /**
081: * A connection pool method which lets the connector choose which
082: * idle connection are allowed to be reused for a request.
083: * It returns a connection from the set matching the subject and request
084: * info.
085: *
086: * Many connectors can just return the first connection, if it doesn't
087: * matter which connection is used. However, the pool might contain
088: * connections with different configurations and subjects. This method
089: * lets the connector return a connection that properly matches the
090: * request.
091: *
092: * @param set Resin's current pool of idle connections
093: * @param subject the application id asking for a connection
094: * @param reqInfo connector-specific information used to configure
095: * the connection
096: *
097: * @return a connection matching the subject and reqInfo requirements or
098: * null if none match
099: */
100: public ManagedConnection matchManagedConnections(Set set,
101: Subject subject, ConnectionRequestInfo reqInfo) {
102: Iterator iter = set.iterator();
103:
104: while (iter.hasNext()) {
105: ManagedConnectionImpl mConn = (ManagedConnectionImpl) iter
106: .next();
107:
108: // In this example, all connections are equivalent
109: return mConn;
110: }
111:
112: return null;
113: }
114:
115: /**
116: * This connection factory does not have a separate resource adapter.
117: *
118: * More complicated connection factories will have a
119: * separate ResourceAdapter object to share state among multiple
120: * connection factories and to manage threads, etc, using
121: * the application server.
122: */
123: public void setResourceAdapter(ResourceAdapter ra) {
124: }
125:
126: /**
127: * This connection factory does not have a separate resource adapter.
128: *
129: * More complicated connection factories will have a
130: * separate ResourceAdapter object to share state among multiple
131: * connection factories and to manage threads, etc, using
132: * the application server.
133: */
134: public ResourceAdapter getResourceAdapter() {
135: return null;
136: }
137:
138: /**
139: * createConnectionFactory with no arguments is for a connection factory
140: * outside of an application server. Although most connection factories
141: * will implement it, Resin never uses it.
142: */
143: public Object createConnectionFactory() {
144: throw new UnsupportedOperationException();
145: }
146:
147: /**
148: * Logging should use JDK 1.4 java.util.logging.
149: */
150: public PrintWriter getLogWriter() {
151: return null;
152: }
153:
154: /**
155: * Logging should use JDK 1.4 java.util.logging.
156: */
157: public void setLogWriter(PrintWriter out) {
158: }
159:
160: /**
161: * Returns the connection name.
162: */
163: public String generateConnectionName() {
164: return _name + "-" + _cCount++ + "-conn";
165: }
166:
167: public String toString() {
168: return "ManagedConnectionFactoryImpl[" + _name + "]";
169: }
170: }
|