View Javadoc

1   /*
2    * Copyright 2005 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at 
7    * 
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software 
11   * distributed under the License is distributed on an "AS IS" BASIS, 
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
13   * See the License for the specific language governing permissions and 
14   * limitations under the License.
15   */
16  
17  package org.apache.jdo.util;
18  
19  import java.util.Stack;
20  
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  
24  /***
25  * A general purpose pooling class.
26  *
27  * @author Dave Bristor
28  */
29  public class Pool {
30      // Objects in the pool
31      private final Stack stack = new Stack();
32  
33      // Size of the pool
34      private final int size;
35  
36      // Number of elements release by Pool for client use
37      private int count = 0;
38      
39      /*** Number of millis to wait for a free entry
40       * Currently fixed; might be made configurable in future.
41       */
42      private int waitMillis = 1000;
43      
44      /*** Number of times to wait for a free entry
45       * Currently fixed; might be made configurable in future.
46       */
47      private int waitNumber = 5;
48  
49      /*** I18N */
50      private final static I18NHelper msg = I18NHelper.getInstance("org.apache.jdo.util.Bundle"); // NOI18N
51  
52      // For debugging TBD!!!
53      static final Log test = LogFactory.getFactory().getInstance(
54          "org.apache.jdo.util"); // NOI18N
55  
56      /***
57       * Constructs a pool that will limit the number of objects which it can
58       * contain.
59       * @param size The maximum number of items that can be put into the pool.
60       */
61      public Pool(int size) {
62          this.size = size;
63      }
64  
65      /***
66       * Puts the given object into the pool, if there the pool has fewer than
67       * the number of elements specifed when created.  If the pool is full,
68       * blocks until an element is removed. 
69       * @param o Object to be put in the pool.
70       * @throws InterruptedException
71       */
72      public synchronized void put(Object o) throws InterruptedException {
73         boolean debug = test.isDebugEnabled();
74          
75         if (debug) {
76             test.debug("Pool.put: " + o); // NOI18N
77         }
78         
79         if (count > size || count < 0) {
80             if (debug) {
81                 test.debug("Pool: count " + count + // NOI18N
82                              " out of range 0-" + size); // NOI18N
83             }
84             throw new RuntimeException(
85                 msg.msg(
86                     "EXC_CountOutOfRange", // NOI18N
87                     new Integer(count).toString(),
88                     new Integer(size).toString()));
89         }
90         
91         if (stack.contains(o)) {
92             if (debug) {
93                 test.debug("Pool: duplicate object"); // NOI18N
94             }
95             throw new RuntimeException(
96                 msg.msg(
97                     "EXC_DuplicateObject", o)); // NOI18N
98         }
99  
100        while (count == size) {
101            if (debug) {
102                test.debug("Pool.put: block"); // NOI18N
103            }
104            wait();
105        }
106        stack.push(o);
107        ++count;
108        notify();
109     }
110 
111     /***
112      * Gets an object from the pool, if one is available.  If an object is not
113      * available, waits until one is.  The waiting is governed by two
114      * variables, which are currently fixed: waitMillis and waitNumber.
115      * If no object is available from the pool within (waitNumber) times
116      * (waitMillis) milliseconds, then a RuntimeException is thrown.
117      * In future, the waitMillis and waitNumber should be configurable.
118      * @return An object from the pool.
119      */
120     public synchronized Object get() throws InterruptedException {
121         boolean debug = test.isDebugEnabled();
122         Object rc = null;
123 
124         if (count > size || count < 0) {
125             if (debug) {
126                 test.debug("Pool: count " + count + // NOI18N
127                            " out of range 0-" + size); // NOI18N
128             }
129             throw new RuntimeException(
130                 msg.msg(
131                     "EXC_CountOutOfRange", // NOI18N
132                     new Integer(count).toString(),
133                     new Integer(size).toString()));
134         }
135 
136         int timeouts = 0;
137         while (count == 0 && timeouts++ < waitNumber) {
138             if (debug) {
139                 test.debug("Pool.get: block " + timeouts); // NOI18N
140             }
141             wait(waitMillis);
142         }
143         if (timeouts >= waitNumber) {
144             throw new RuntimeException(
145                 msg.msg("EXC_PoolGetTimeout")); // NOI18N
146         }
147         rc = stack.pop();
148         --count;
149         notify();
150         if (debug) {
151             test.debug("Pool.get: " + rc); // NOI18N
152         }
153         return rc;
154     }
155 }