KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jca > UserPoolItem


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.jca;
31
32 import com.caucho.log.Log;
33 import com.caucho.util.L10N;
34
35 import javax.resource.ResourceException JavaDoc;
36 import javax.resource.spi.ConnectionEvent JavaDoc;
37 import javax.resource.spi.ConnectionRequestInfo JavaDoc;
38 import javax.resource.spi.ManagedConnectionFactory JavaDoc;
39 import javax.security.auth.Subject JavaDoc;
40 import javax.transaction.RollbackException JavaDoc;
41 import java.util.logging.Logger JavaDoc;
42
43 /**
44  * Pool item representing the user connection.
45  *
46  * The UserPoolItem is responsible for the lifecycle of an
47  * associated PoolItem (ownPoolItem).
48  *
49  * The UserPoolItem also has the current XA PoolItem, which may be
50  * shared with other UserPoolItems.
51  *
52  * When the XA completes, the UserPoolItem restores
53  * its current XA PoolItem to the ownPoolItem.
54  */

55 class UserPoolItem {
56   private static final L10N L = new L10N(UserPoolItem.class);
57   private static final Logger JavaDoc log = Log.open(UserPoolItem.class);
58
59   private ConnectionPool _cm;
60   private String JavaDoc _id;
61
62   // the pool item associated with this connection
63
private PoolItem _ownPoolItem;
64
65   private ManagedConnectionFactory JavaDoc _mcf;
66   private Subject JavaDoc _subject;
67   private ConnectionRequestInfo JavaDoc _info;
68
69   // The owning transaction
70
private UserTransactionImpl _transaction;
71   
72   // The head PoolItem for the transaction
73
private PoolItem _sharePoolItem;
74   
75   // The next shared UserPoolItem in the chain
76
private UserPoolItem _shareNext;
77   // The previous shared UserPoolItem in the chain
78
private UserPoolItem _sharePrev;
79
80   private Object JavaDoc _userConn;
81   
82   private boolean _hasConnectionError;
83
84   private IllegalStateException JavaDoc _allocationStackTrace;
85
86   public UserPoolItem(ConnectionPool cm)
87   {
88     _cm = cm;
89     _id = _cm.generateId();
90
91     _transaction = _cm.getTransaction();
92
93     if (cm.getSaveAllocationStackTrace()) {
94       _allocationStackTrace = new IllegalStateException JavaDoc(L.l("unclosed connection: " + this + " was allocated at"));
95       _allocationStackTrace.fillInStackTrace();
96     }
97   }
98
99   public UserPoolItem(ConnectionPool cm, PoolItem poolItem)
100   {
101     this(cm);
102
103     _ownPoolItem = poolItem;
104     _sharePoolItem = poolItem;
105   }
106
107   /**
108    * Sets the managed connection factory
109    */

110   public void setManagedConnectionFactory(ManagedConnectionFactory JavaDoc mcf)
111   {
112     _mcf = mcf;
113   }
114
115   /**
116    * Gets the managed connection factory
117    */

118   public ManagedConnectionFactory JavaDoc getManagedConnectionFactory()
119   {
120     return _mcf;
121   }
122
123   /**
124    * Sets the subject.
125    */

126   public void setSubject(Subject JavaDoc subject)
127   {
128     _subject = subject;
129   }
130
131   /**
132    * Sets the subject.
133    */

134   public Subject JavaDoc getSubject()
135   {
136     return _subject;
137   }
138
139   /**
140    * Sets the info.
141    */

142   public void setInfo(ConnectionRequestInfo JavaDoc info)
143   {
144     _info = info;
145   }
146
147   /**
148    * Gets the info.
149    */

150   public ConnectionRequestInfo JavaDoc getInfo()
151   {
152     return _info;
153   }
154
155   /**
156    * Returns true if the connection is active.
157    */

158   public boolean isActive()
159   {
160     return _userConn != null;
161   }
162   
163   /**
164    * Notifies that a connection error has occurred.
165    */

166   public void connectionErrorOccurred(ConnectionEvent JavaDoc event)
167   {
168     _hasConnectionError = true;
169   }
170
171   /**
172    * Returns true if there was a connection error.
173    */

174   public boolean isConnectionError()
175   {
176     return _hasConnectionError;
177   }
178
179   /**
180    * Returns the allocation stack trace.
181    */

182   public IllegalStateException JavaDoc getAllocationStackTrace()
183   {
184     return _allocationStackTrace;
185   }
186
187   /**
188    * Enables saving of the allocation stack traces.
189    */

190   public void setSaveAllocationStackTrace(boolean enable)
191   {
192     _cm.setSaveAllocationStackTrace(enable);
193   }
194   
195   /**
196    * Returns the pool item.
197    */

198   public PoolItem getOwnPoolItem()
199   {
200     return _ownPoolItem;
201   }
202   
203   /**
204    * Returns the xa item.
205    */

206   public PoolItem getXAPoolItem()
207   {
208     return _sharePoolItem;
209   }
210
211   /**
212    * Gets the user connection.
213    */

214   public Object JavaDoc getUserConnection()
215   {
216     return _userConn;
217   }
218
219   /**
220    * Gets the user connection.
221    */

222   Object JavaDoc allocateUserConnection()
223     throws ResourceException JavaDoc
224   {
225     if (_userConn == null)
226       _userConn = _sharePoolItem.allocateConnection();
227     
228     return _userConn;
229   }
230
231   /**
232    * Sets the user connection.
233    */

234   public void setUserConnection(Object JavaDoc userConn)
235   {
236     _userConn = userConn;
237   }
238
239   /**
240    * Returns the next share.
241    */

242   UserPoolItem getShareNext()
243   {
244     return _shareNext;
245   }
246
247   /**
248    * Associates the UserPoolItem with a pool item
249    */

250   void associatePoolItem(PoolItem poolItem)
251   {
252     if (_ownPoolItem != null)
253       throw new IllegalStateException JavaDoc(L.l("associating with old pool item."));
254     
255     _ownPoolItem = poolItem;
256
257     if (_sharePoolItem != null)
258       removeFromShareList();
259
260     addToShareList(poolItem);
261   }
262
263   /**
264    * Associates the UserPoolItem with a pool item
265    */

266   void associate(PoolItem poolItem,
267          ManagedConnectionFactory JavaDoc mcf,
268          Subject JavaDoc subject,
269          ConnectionRequestInfo JavaDoc info)
270   {
271     if (_sharePoolItem != null)
272       removeFromShareList();
273
274     _mcf = mcf;
275     _subject = subject;
276     _info = info;
277
278     addToShareList(poolItem);
279
280     if (_transaction != null) {
281       try {
282     _transaction.enlistResource(this);
283       } catch (RollbackException JavaDoc e) {
284     removeFromShareList();
285     
286     poolItem.toIdle();
287     
288     throw new RuntimeException JavaDoc(e);
289       } catch (Throwable JavaDoc e) {
290     removeFromShareList();
291
292     poolItem.setConnectionError();
293     poolItem.toIdle();
294     
295     throw new RuntimeException JavaDoc(e);
296       }
297     }
298   }
299
300   /**
301    * Reassociates with the an own pool item.
302    */

303   void reassociatePoolItem()
304     throws ResourceException JavaDoc
305   {
306     if (_ownPoolItem == null) {
307       UserPoolItem item = _cm.allocatePool(_mcf, _subject, _info, this);
308
309       assert(item == this);
310       
311       _ownPoolItem = item.getOwnPoolItem();
312     }
313
314     if (_sharePoolItem != null)
315       removeFromShareList();
316
317     addToShareList(_ownPoolItem);
318   }
319
320   /**
321    * Close the connection, called from UserTransactionImpl.
322    */

323   void abortConnection()
324     throws ResourceException JavaDoc
325   {
326     PoolItem poolItem = _ownPoolItem;
327     _ownPoolItem = null;
328
329     removeFromShareList();
330
331     if (poolItem != null)
332       poolItem.abortConnection();
333   }
334
335   /**
336    * Closes the item.
337    */

338   void close()
339   {
340     PoolItem ownPoolItem = _ownPoolItem;
341     _ownPoolItem = null;
342     
343     _userConn = null;
344
345     if (_transaction != null)
346       _transaction.delistResource(this);
347
348     removeFromShareList();
349
350     if (ownPoolItem != null)
351       ownPoolItem.toIdle();
352   }
353
354   /**
355    * Removes from the current list.
356    */

357   void removeFromShareList()
358   {
359     PoolItem poolItem = _sharePoolItem;
360     _sharePoolItem = null;
361     
362     if (poolItem == null)
363       return;
364
365     synchronized (poolItem._shareLock) {
366       UserPoolItem prev = _sharePrev;
367       UserPoolItem next = _shareNext;
368       
369       _sharePrev = null;
370       _shareNext = null;
371
372       if (prev != null)
373     prev._shareNext = next;
374       else
375     poolItem._shareHead = next;
376
377       if (next != null)
378     next._sharePrev = prev;
379     }
380   }
381
382   /**
383    * Removes from the current list.
384    */

385   void addToShareList(PoolItem poolItem)
386   {
387     if (_sharePoolItem != null)
388       throw new IllegalStateException JavaDoc();
389
390     synchronized (poolItem._shareLock) {
391       _sharePoolItem = poolItem;
392       
393       _sharePrev = null;
394       _shareNext = poolItem._shareHead;
395
396       if (poolItem._shareHead != null)
397     poolItem._shareHead._sharePrev = this;
398
399       poolItem._shareHead = this;
400     }
401   }
402
403   /**
404    * Returns true for a closed connection.
405    */

406   boolean isClosed()
407   {
408     return _sharePoolItem == null;
409   }
410
411   // XAResource stuff
412

413   public String JavaDoc toString()
414   {
415     return "UserPoolItem[" + _cm.getName() + "," + _id + "]";
416   }
417 }
418
Popular Tags