KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.jca;
30
31 import com.caucho.log.Log;
32 import com.caucho.util.L10N;
33
34 import java.lang.ref.SoftReference JavaDoc;
35 import java.util.ArrayList JavaDoc;
36 import java.util.logging.Logger JavaDoc;
37
38
39 /**
40  * Manages the connections for a thread. Needed to support the
41  * "cached" connection, i.e. elisting only when work is actually done.
42  */

43 public class ThreadConnectionManager {
44   private static final L10N L = new L10N(ThreadConnectionManager.class);
45   private static final Logger JavaDoc log = Log.open(ThreadConnectionManager.class);
46
47   private static final ThreadLocal JavaDoc<ThreadConnectionManager> _threadManager
48     = new ThreadLocal JavaDoc<ThreadConnectionManager>();
49
50   private ArrayList JavaDoc<SoftReference JavaDoc<PoolItem>> _activeConnections
51     = new ArrayList JavaDoc<SoftReference JavaDoc<PoolItem>>();
52
53   /**
54    * Returns the manager for the current thread.
55    */

56   public static ThreadConnectionManager getThreadManager()
57   {
58     return _threadManager.get();
59   }
60
61   /**
62    * Sets the manager for the current thread.
63    */

64   public static void setThreadManager(ThreadConnectionManager manager)
65   {
66     _threadManager.set(manager);
67   }
68
69   /**
70    * Returns the manager for the current thread, creating if necessary.
71    */

72   public static ThreadConnectionManager createThreadManager()
73   {
74     ThreadConnectionManager cm = _threadManager.get();
75
76     if (cm == null) {
77       cm = new ThreadConnectionManager();
78       _threadManager.set(cm);
79     }
80       
81     return cm;
82   }
83
84   /**
85    * Adds a pooled connection to the current thread.
86    */

87   public static void addConnection(PoolItem conn)
88   {
89     createThreadManager().add(conn);
90   }
91
92   /**
93    * Removes a pooled connection from the current thread.
94    */

95   public static void removeConnection(PoolItem conn)
96   {
97     createThreadManager().remove(conn);
98   }
99
100   /**
101    * Adds the connection.
102    */

103   private void add(PoolItem conn)
104   {
105     _activeConnections.add(new SoftReference JavaDoc<PoolItem>(conn));
106   }
107
108   /**
109    * Removes the connection.
110    */

111   private void remove(PoolItem conn)
112   {
113     for (int i = _activeConnections.size() - 1; i >= 0; i--) {
114       PoolItem aConn = _activeConnections.get(i).get();
115
116       if (aConn == null || aConn == conn)
117     _activeConnections.remove(i);
118     }
119   }
120 }
121
Popular Tags