KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > StrictMaxPool


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.ejb3;
23
24 import java.util.LinkedList JavaDoc;
25 import javax.ejb.EJBException JavaDoc;
26 import org.jboss.logging.Logger;
27 import EDU.oswego.cs.dl.util.concurrent.FIFOSemaphore;
28
29 /**
30  * @author <a HREF="mailto:kabir.khan@jboss.org">Kabir Khan</a>
31  * @version $Revision: 54985 $
32  */

33 public class StrictMaxPool
34         extends AbstractPool
35 {
36    // Constants -----------------------------------------------------
37
public static final int DEFAULT_MAX_SIZE = 30;
38    public static final long DEFAULT_TIMEOUT = Long.MAX_VALUE;
39
40    // Attributes ----------------------------------------------------
41
/**
42     * A FIFO semaphore that is set when the strict max size behavior is in effect.
43     * When set, only maxSize instances may be active and any attempt to get an
44     * instance will block until an instance is freed.
45     */

46    private FIFOSemaphore strictMaxSize;
47    /**
48     * The time in milliseconds to wait for the strictMaxSize semaphore.
49     */

50    private long strictTimeout;
51
52    /**
53     * The pool data structure
54     */

55    protected LinkedList JavaDoc pool = new LinkedList JavaDoc();
56    /**
57     * The maximum number of instances allowed in the pool
58     */

59    protected int maxSize = 30;
60
61    Logger log = Logger.getLogger(StrictMaxPool.class);
62
63
64    // Static --------------------------------------------------------
65

66    // Constructors --------------------------------------------------
67

68    // Public --------------------------------------------------------
69

70    /**
71     * super.initialize() must have been called in advance
72     */

73    public void initialize(Container container, Class JavaDoc contextClass, Class JavaDoc beanClass, int maxSize, long timeout)
74    {
75       super.initialize(container, contextClass, beanClass, maxSize, timeout);
76       this.maxSize = maxSize;
77       this.strictMaxSize = new FIFOSemaphore(maxSize);
78       this.strictTimeout = timeout;
79    }
80    
81    public void setMaxSize(int maxSize)
82    {
83       this.maxSize = maxSize;
84       this.strictMaxSize = new FIFOSemaphore(maxSize);
85    }
86
87    /**
88     * Get an instance without identity.
89     * Can be used by finders,create-methods, and activation
90     *
91     * @return Context /w instance
92     */

93    public BeanContext get()
94    {
95       boolean trace = log.isTraceEnabled();
96       if (trace)
97          log.trace("Get instance " + this + "#" + pool.size() + "#" + container.getBeanClass());
98
99       // Block until an instance is available
100
try
101       {
102          boolean acquired = strictMaxSize.attempt(strictTimeout);
103          if (trace)
104             log.trace("Acquired(" + acquired + ") strictMaxSize semaphore, remaining=" + strictMaxSize.permits());
105          if (acquired == false)
106             throw new EJBException JavaDoc("Failed to acquire the pool semaphore, strictTimeout=" + strictTimeout);
107       }
108       catch (InterruptedException JavaDoc e)
109       {
110          throw new EJBException JavaDoc("Pool strictMaxSize semaphore was interrupted");
111       }
112
113       synchronized (pool)
114       {
115          if (!pool.isEmpty())
116          {
117             return (BeanContext) pool.removeFirst();
118          }
119       }
120
121       // Pool is empty, create an instance
122
return create();
123    }
124
125    public BeanContext get(Class JavaDoc[] initTypes, Object JavaDoc[] initValues)
126    {
127       boolean trace = log.isTraceEnabled();
128       if (trace)
129          log.trace("Get instance " + this + "#" + pool.size() + "#" + container.getBeanClass());
130
131       // Block until an instance is available
132
try
133       {
134          boolean acquired = strictMaxSize.attempt(strictTimeout);
135          if (trace)
136             log.trace("Acquired(" + acquired + ") strictMaxSize semaphore, remaining=" + strictMaxSize.permits());
137          if (acquired == false)
138             throw new EJBException JavaDoc("Failed to acquire the pool semaphore, strictTimeout=" + strictTimeout);
139       }
140       catch (InterruptedException JavaDoc e)
141       {
142          throw new EJBException JavaDoc("Pool strictMaxSize semaphore was interrupted");
143       }
144
145       synchronized (pool)
146       {
147          if (!pool.isEmpty())
148          {
149             return (BeanContext) pool.removeFirst();
150          }
151       }
152
153       // Pool is empty, create an instance
154
return create(initTypes, initValues);
155    }
156
157    /**
158     * Return an instance after invocation.
159     * <p/>
160     * Called in 2 cases:
161     * a) Done with finder method
162     * b) Just removed
163     *
164     * @param ctx
165     */

166    public void release(BeanContext ctx)
167    {
168       if (log.isTraceEnabled())
169       {
170          String JavaDoc msg = pool.size() + "/" + maxSize + " Free instance:" + this
171                       + "#" + container.getBeanClass();
172          log.trace(msg);
173       }
174
175       try
176       {
177          // Add the unused context back into the pool
178
boolean removeIt = false;
179          synchronized (pool)
180          {
181             if (pool.size() < maxSize)
182             {
183                pool.addFirst(ctx);
184             }
185             else
186             {
187                removeIt = true;
188             }
189          }
190          if (removeIt) remove(ctx);
191          // If we block when maxSize instances are in use, invoke release on strictMaxSize
192
strictMaxSize.release();
193       }
194       catch (Exception JavaDoc ignored)
195       {
196       }
197    }
198
199    public void discard(BeanContext ctx)
200    {
201       if (log.isTraceEnabled())
202       {
203          String JavaDoc msg = "Discard instance:" + this + "#" + ctx
204                       + "#" + container.getBeanClass();
205          log.trace(msg);
206       }
207
208       // If we block when maxSize instances are in use, invoke release on strictMaxSize
209
strictMaxSize.release();
210
211       // Throw away, unsetContext()
212
super.discard(ctx);
213    }
214
215    // Package protected ---------------------------------------------
216

217    // Protected -----------------------------------------------------
218
protected void destroy() throws Exception JavaDoc
219    {
220       freeAll();
221       this.container = null;
222    }
223
224    // Private -------------------------------------------------------
225

226    /**
227     * At undeployment we want to free completely the pool.
228     */

229    private void freeAll()
230    {
231       LinkedList JavaDoc clone = (LinkedList JavaDoc) pool.clone();
232       for (int i = 0; i < clone.size(); i++)
233       {
234          BeanContext bc = (BeanContext) clone.get(i);
235          // Clear TX so that still TX entity pools get killed as well
236
discard(bc);
237       }
238       pool.clear();
239    }
240
241    // Inner classes -------------------------------------------------
242

243 }
244
Popular Tags