KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > example > ManagedConnectionFactoryImpl


1 package example;
2
3 import java.io.*;
4 import java.util.*;
5
6 import javax.resource.spi.ResourceAdapter JavaDoc;
7
8 import javax.resource.spi.ManagedConnectionFactory JavaDoc;
9 import javax.resource.spi.ManagedConnection JavaDoc;
10 import javax.resource.spi.ConnectionManager JavaDoc;
11
12 import javax.resource.spi.ManagedConnectionMetaData JavaDoc;
13 import javax.resource.spi.ConnectionRequestInfo JavaDoc;
14
15 import javax.security.auth.Subject JavaDoc;
16
17 /**
18  * Main interface between Resin and the connector. It's the
19  * top-level SPI class for creating the SPI ManagedConnections.
20  *
21  * The resource configuration in Resin's web.xml will use bean-style
22  * configuration to configure the ManagecConnectionFactory.
23  */

24 public class ManagedConnectionFactoryImpl implements ManagedConnectionFactory JavaDoc {
25   private String JavaDoc _name;
26
27   // A counter for the example to keep track of the underlying connections.
28
// Each ManagedConnectionImpl will get its own id.
29
private int _mcCount;
30   
31   // A counter for the example to keep track of the user connections.
32
// Each ConnectionImpl will get its own id.
33
private int _cCount;
34
35   /**
36    * Sets the name for the connector
37    */

38   public void setName(String JavaDoc name)
39   {
40     _name = name;
41   }
42
43   /**
44    * Creates the application's view of the connection factory.
45    *
46    * The ConnectionFactory is the equivalent of the JDBC DataSource.
47    * Applications will use the ConnectionFactory to create connections.
48    *
49    * The connector can use any API that makes sense for it. JDBC
50    * connectors will return a DataSource. JMS connectors will return
51    * SessionFactory, etc.
52    *
53    * @param manager ConnectionManager provided by Resin gives access to
54    * some application server resources.
55    */

56   public Object JavaDoc createConnectionFactory(ConnectionManager JavaDoc manager)
57   {
58     return new ConnectionFactoryImpl(this, manager);
59   }
60
61   /**
62    * Creates the SPI-side of a connection, like the <code>XAConnection</code> of
63    * JDBC. Resin will use the returned <code>ManagedConnection</code>
64    * to create the application connections, manage transactions,
65    * and manage the pool.
66    *
67    * The <code>ConnectionRequestInfo</code> is not used in this example.
68    * When needed, the <code>ConnectionFactoryImpl</code> will create a
69    * <code>ConnectionRequestInfo</code> and pass it to Resin with
70    * the <code>allocateConnection</code> call.
71    *
72    * @param subject security identifier of the application requesting
73    * the connection.
74    * @param reqInfo connection-specific configuration information
75    */

76   public ManagedConnection JavaDoc
77     createManagedConnection(Subject JavaDoc subject, ConnectionRequestInfo JavaDoc reqInfo)
78   {
79     return new ManagedConnectionImpl(_name + "-" + _mcCount++, this);
80   }
81
82   /**
83    * A connection pool method which lets the connector choose which
84    * idle connection are allowed to be reused for a request.
85    * It returns a connection from the set matching the subject and request
86    * info.
87    *
88    * Many connectors can just return the first connection, if it doesn't
89    * matter which connection is used. However, the pool might contain
90    * connections with different configurations and subjects. This method
91    * lets the connector return a connection that properly matches the
92    * request.
93    *
94    * @param set Resin's current pool of idle connections
95    * @param subject the application id asking for a connection
96    * @param reqInfo connector-specific information used to configure
97    * the connection
98    *
99    * @return a connection matching the subject and reqInfo requirements or
100    * null if none match
101    */

102   public ManagedConnection JavaDoc
103     matchManagedConnections(Set set,
104                 Subject JavaDoc subject,
105                 ConnectionRequestInfo JavaDoc reqInfo)
106   {
107     Iterator iter = set.iterator();
108
109     while (iter.hasNext()) {
110       ManagedConnectionImpl mConn = (ManagedConnectionImpl) iter.next();
111
112       // In this example, all connections are equivalent
113
return mConn;
114     }
115
116     return null;
117   }
118
119   /**
120    * This connection factory does not have a separate resource adapter.
121    *
122    * More complicated connection factories will have a
123    * separate ResourceAdapter object to share state among multiple
124    * connection factories and to manage threads, etc, using
125    * the application server.
126    */

127   public void setResourceAdapter(ResourceAdapter JavaDoc ra)
128   {
129   }
130
131   /**
132    * This connection factory does not have a separate resource adapter.
133    *
134    * More complicated connection factories will have a
135    * separate ResourceAdapter object to share state among multiple
136    * connection factories and to manage threads, etc, using
137    * the application server.
138    */

139   public ResourceAdapter JavaDoc getResourceAdapter()
140   {
141     return null;
142   }
143
144   /**
145    * createConnectionFactory with no arguments is for a connection factory
146    * outside of an application server. Although most connection factories
147    * will implement it, Resin never uses it.
148    */

149   public Object JavaDoc createConnectionFactory()
150   {
151     throw new UnsupportedOperationException JavaDoc();
152   }
153
154   /**
155    * Logging should use JDK 1.4 java.util.logging.
156    */

157   public PrintWriter getLogWriter()
158   {
159     return null;
160   }
161
162   /**
163    * Logging should use JDK 1.4 java.util.logging.
164    */

165   public void setLogWriter(PrintWriter out)
166   {
167   }
168
169   /**
170    * Returns the connection name.
171    */

172   public String JavaDoc generateConnectionName()
173   {
174     return _name + "-" + _cCount++ + "-conn";
175   }
176
177   public String JavaDoc toString()
178   {
179     return "ManagedConnectionFactoryImpl[" + _name + "]";
180   }
181 }
182
Popular Tags