KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > example > ManagedConnectionImpl


1 package example;
2
3 import java.io.*;
4 import java.util.*;
5
6 import javax.resource.spi.ManagedConnection JavaDoc;
7 import javax.resource.spi.ConnectionRequestInfo JavaDoc;
8 import javax.resource.spi.ConnectionEventListener JavaDoc;
9 import javax.resource.spi.ConnectionEvent JavaDoc;
10
11 import javax.resource.spi.ManagedConnectionMetaData JavaDoc;
12 import javax.resource.spi.LocalTransaction JavaDoc;
13
14 import javax.transaction.xa.XAResource JavaDoc;
15
16 import javax.security.auth.Subject JavaDoc;
17
18 /**
19  * ManagedConnectionImpl represents the underlying (SPI) connection to the
20  * resource. Resin will manage this ManagedConnection in its pool.
21  *
22  * The user will see the ConnectionImpl facade and may not even know
23  * that the ManagedConnectionImpl exists.
24  */

25 public class ManagedConnectionImpl implements ManagedConnection JavaDoc {
26   private ManagedConnectionFactoryImpl _factory;
27   
28   // Identifier for the ManagedConnectionImpl
29
private String JavaDoc _name;
30   
31   // Resin needs to listen for close events
32
private ArrayList _listeners = new ArrayList();
33
34   /**
35    * Creates a new ManagedConnection with its id.
36    * ManagedConnectionFactoryImpl will create the ManagedConnectionImpl
37    * in response to a Resin request.
38    */

39   ManagedConnectionImpl(String JavaDoc name, ManagedConnectionFactoryImpl factory)
40   {
41     _name = name;
42     _factory = factory;
43   }
44   
45   /**
46    * Creates a new application connection. The application connection
47    * will be in use until its <code>close()</code> method is called.
48    * It's <code>close</code> method will call the ConnectionEventListener
49    * registered with this ManagedConnectionImpl instance.
50    *
51    * It is important for the connection's close to be called and for the
52    * connection to call the close listeners, because Resin needs to
53    * know when the application is done with the connection and Resin
54    * can return the ManagedConnection to the pool.
55    *
56    * @param subject the subject for the connection
57    * @param info the connection information for the connection
58    *
59    * @return the application-view of the connection
60    */

61   public Object JavaDoc getConnection(Subject JavaDoc subject, ConnectionRequestInfo JavaDoc info)
62   {
63     return new ConnectionImpl(_factory.generateConnectionName(), this);
64   }
65
66   /**
67    * XXX:???
68    *
69    * In some cases, Resin can associate an old application connection
70    * with the ManagedConnection. (I'm not sure when this can happen.)
71    *
72    * @param conn the application view of the connection
73    */

74   public void associateConnection(Object JavaDoc conn)
75   {
76   }
77
78   /**
79    * Resin will register a listener with the ManagedConnection so it
80    * will know when a connection closes or has a fatal error.
81    *
82    * @param listener Resin's listener to receive notice of a close.
83    */

84   public void addConnectionEventListener(ConnectionEventListener JavaDoc listener)
85   {
86     _listeners.add(listener);
87   }
88
89
90   /**
91    * Resin can remove it's listener when it removes the managed connection
92    * from the pool.
93    *
94    * @param listener Resin's listener to receive notice of a close.
95    */

96   public void removeConnectionEventListener(ConnectionEventListener JavaDoc listener)
97   {
98     _listeners.remove(listener);
99   }
100
101   /**
102    * Implementation method to allow the <code>ConnectionImpl</code> to
103    * trigger Resin's listeners when the connection closes.
104    *
105    * @param conn the user connection which is closing.
106    */

107   void close(ConnectionImpl conn)
108   {
109     ConnectionEvent JavaDoc evt;
110     evt = new ConnectionEvent JavaDoc(this, ConnectionEvent.CONNECTION_CLOSED);
111
112     for (int i = 0; i < _listeners.size(); i++) {
113       ConnectionEventListener JavaDoc listener;
114       listener = (ConnectionEventListener JavaDoc) _listeners.get(i);
115
116       listener.connectionClosed(evt);
117     }
118   }
119
120   /**
121    * This example isn't returning any meta data. In general,
122    * providing the meta data is nice for the applications.
123    */

124   public ManagedConnectionMetaData JavaDoc getMetaData()
125   {
126     return null;
127   }
128
129   /**
130    * Transaction-aware resources will return the XAResource for the
131    * managed connection.
132    */

133   public XAResource JavaDoc getXAResource()
134   {
135     return null;
136   }
137
138   /**
139    * Transaction-aware resources will return the LocalTransaction for the
140    * managed connection. LocalTransaction is a lightweight interface
141    * for transactions that don't need the full XA transactions.
142    */

143   public LocalTransaction JavaDoc getLocalTransaction()
144   {
145     return null;
146   }
147
148   /**
149    * Called when Resin returns a connection to the idle pool.
150    */

151   public void cleanup()
152   {
153   }
154
155   /**
156    * Called when Resin is closing the connection. Any sockets, etc,
157    * would be closed here.
158    */

159   public void destroy()
160   {
161   }
162
163   /**
164    * Logging should use JDK 1.4 java.util.logging.
165    */

166   public PrintWriter getLogWriter()
167   {
168     return null;
169   }
170
171   /**
172    * Logging should use JDK 1.4 java.util.logging.
173    */

174   public void setLogWriter(PrintWriter log)
175   {
176   }
177
178   public String JavaDoc toString()
179   {
180     return "ManagedConnectionImpl[" + _name + "]";
181   }
182 }
183
Popular Tags