KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portlet > forums > helper > AbstractPool


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Forums JBoss Portlet *
6  * *
7  * Distributable under GPL license. *
8  * See terms of license at gnu.org. *
9  * *
10  *****************************************/

11 package org.jboss.portlet.forums.helper;
12
13
14 /**
15  * A simple object pool designed for extensibility.
16  * There is no resizer thread so far.
17  *
18  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
19  * @version $Revision: 1.1.1.1 $
20  */

21 public abstract class AbstractPool
22 {
23    // Attributes ----------------------------------------------------
24

25    /** The lock. */
26    private final Object JavaDoc lock = new Object JavaDoc();
27
28    /** Pool stack. */
29    private Object JavaDoc[] stack;
30
31    /** Pool index. */
32    private int index;
33
34    // Constructors --------------------------------------------------
35
protected AbstractPool()
36    {
37    }
38
39    // Public --------------------------------------------------------
40

41    /**
42     * Returns an object instance. If the Pool is empty
43     * a new object will be created.
44     *
45     * @return A pooled instance.
46     */

47    public Object JavaDoc acquire()
48    {
49       // The instance we return
50
Object JavaDoc instance = null;
51
52       // Synchronize the stack to keep a consistent state
53
synchronized (lock)
54       {
55          if (index > -1)
56          {
57             instance = stack[index];
58             stack[index--] = null;
59          }
60       }
61
62       // If no instance where available create one
63
if (instance == null)
64       {
65          instance = _create();
66       }
67
68       // Set the object acquired
69
_acquire(instance);
70
71       // Finally return the instance
72
return instance;
73    }
74
75    /**
76     * Release an object and pools it if the pool is
77     * not full, otherwise discard it.
78     *
79     * @param instance the released instance.
80     */

81    public void release(Object JavaDoc instance)
82    {
83       if (instance == null)
84       {
85          // This is a bug from the user of that pool
86
// That should not release a null object
87
return;
88       }
89
90       // First release the instance
91
_release(instance);
92
93       // Keep object state consistent
94
synchronized (lock)
95       {
96          if (index < (stack.length - 1))
97          {
98             stack[++index] = instance;
99             instance = null;
100          }
101       }
102
103       // If the instance is not null it is not in the
104
// pool anymore, so destroy it
105
if (instance != null)
106       {
107          _destroy(instance);
108       }
109    }
110
111    // Protected -----------------------------------------------------
112

113    /**
114     * Initialize the pool.
115     *
116     * @param maxSize the max size of object in the pool
117     * @param initialSize the initial size. A negative value is
118     * considered as zero, a value greater than maxSize is
119     * considered as maxSize.
120     */

121    protected void initialize(int maxSize,
122                              int initialSize)
123    {
124       // Arguments check
125
if (maxSize < 1)
126       {
127          throw new IllegalArgumentException JavaDoc("maxSize must be greater than 0");
128       }
129
130       initialSize = (initialSize < 0) ? 0 : initialSize;
131       initialSize = (initialSize > maxSize) ? maxSize : initialSize;
132
133       // Initialization
134
stack = new Object JavaDoc[maxSize];
135       this.index = -1;
136       while (initialSize-- > 0)
137       {
138          stack[++index] = _create();
139       }
140    }
141
142    /**
143     * The method does nothing bu default,
144     * called when the client release an instance.
145     */

146    protected void _destroy(Object JavaDoc instance)
147    {
148    }
149
150    /**
151     * The method does nothing bu default,
152     * called when the client release an instance.
153     */

154    protected void _acquire(Object JavaDoc instance)
155    {
156    }
157
158    /**
159     * The method does nothing bu default,
160     * called when the client release an instance.
161     */

162    protected void _release(Object JavaDoc instance)
163    {
164    }
165
166    // Abstract ------------------------------------------------------
167

168    /**
169     * Called when the pool need a new instance.
170     */

171    protected abstract Object JavaDoc _create();
172 }
Popular Tags