KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > wsmgmt > pool > impl > BoundedPool


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.enterprise.admin.wsmgmt.pool.impl;
24
25 import java.util.Collection JavaDoc;
26 import java.util.Collections JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.ArrayList JavaDoc;
31 import com.sun.enterprise.admin.wsmgmt.pool.spi.Pool;
32
33 /**
34  * Pool used to keep track of SOAP messages. This implementation of
35  * the pool keeps a fixed amount of entries in the pool. If pool
36  * size exceeds the max allowed entry, the oldest entry is removed from
37  * the pool.
38  */

39 public class BoundedPool implements Pool{
40
41     /**
42      * Constructor.
43      *
44      * @param name name of the pool
45      */

46     public BoundedPool(String JavaDoc name) {
47         _name = name;
48         _keyList = Collections.synchronizedList(new ArrayList JavaDoc());
49         _map = new HashMap JavaDoc();
50     }
51
52     /**
53      * Constructor.
54      *
55      * @param name name of the pool
56      * @param size max size of the pool
57      */

58     public BoundedPool(String JavaDoc name, int size) {
59
60         this(name);
61         _maxSizeAllowed = size;
62     }
63
64     /**
65      * Returns the object for the given key.
66      *
67      * @param key key used to put objects in the pool
68      *
69      * @return object for the given key or null
70      */

71     public Object JavaDoc get(Object JavaDoc key) {
72         return _map.get(key);
73     }
74
75     /**
76      * Adds the object to the pool. If pool size is greater
77      * than the max allowed, the oldest entry is removed
78      * from the pool.
79      *
80      * @param key key used to put this object
81      * @param val actual object to be added to the pool
82      *
83      * @return previous value for this key or null
84      */

85     public Object JavaDoc put(Object JavaDoc key, Object JavaDoc val) {
86
87         if ((key == null) && (val == null)) {
88             throw new IllegalArgumentException JavaDoc();
89         }
90
91         Object JavaDoc oldVal = null;
92
93         synchronized (this) {
94             oldVal = _map.put(key, val);
95             _keyList.add(key);
96
97             // remove the extra entry from the pool
98
if (_keyList.size() > _maxSizeAllowed) {
99                 Object JavaDoc rkey = _keyList.remove(0);
100                 assert (rkey != null);
101
102                 Object JavaDoc rval = _map.remove(rkey);
103                 assert (rval != null);
104             }
105         }
106
107         return oldVal;
108     }
109
110     /**
111      * Removes this keyed entry from the pool.
112      *
113      * @param key key of the entry that is targeted to be removed
114      *
115      * @return removed entry or null if key not found
116      */

117     public Object JavaDoc remove(Object JavaDoc key) {
118         Object JavaDoc val = null;
119
120         synchronized (this) {
121             _keyList.remove(key);
122             val = _map.remove(key);
123         }
124
125         return val;
126     }
127
128     /**
129      * Removes all mappings from this pool.
130      */

131     public void clear() {
132         synchronized (this) {
133             _map.clear();
134             _keyList.clear();
135         }
136     }
137
138     /**
139      * Returns the number of mappings in this pool.
140      *
141      * @return current number of mappings in this pool
142      */

143     public int size() {
144         return _map.size();
145     }
146
147     /**
148      * Returns the maximum number of mappings allowed in this pool.
149      *
150      * @return max number of mappings allowed
151      */

152     public int getMaxSize() {
153         return _maxSizeAllowed;
154     }
155
156     /**
157      * Returns true if this pool contains mapping for the specified key.
158      *
159      * @param key the presence of the key to be tested
160      *
161      * @return true if this pool contains mapping for the specified key
162      */

163     public boolean containsKey(Object JavaDoc key) {
164         return _map.containsKey(key);
165     }
166
167     /**
168      * Returns true if this pool contains mapping for the specified value.
169      *
170      * @param val the presence of the value to be tested
171      *
172      * @return true if this pool contains mapping for the specified value
173      */

174     public boolean containsValue(Object JavaDoc val) {
175         return _map.containsValue(val);
176     }
177
178     /**
179      * Returns a collection view of the values contained in this pool.
180      *
181      * @return a collection view of the values contained in this pool
182      */

183     public Collection JavaDoc values() {
184         return _map.values();
185     }
186
187     /**
188      * Resets the max size of the pool. If new size is less than current
189      * size, all extra entries in the pool are removed.
190      *
191      * @param size new max size of the pool
192      */

193     public void resize(int size) {
194
195         if (size < 1) {
196             return;
197         }
198
199         if (size == _maxSizeAllowed) {
200             // do nothing
201
} else if (size > _maxSizeAllowed) {
202             synchronized (this) {
203                 _maxSizeAllowed = size;
204             }
205         } else {
206             // remove the extra entries from the pool
207
synchronized (this) {
208                 int currentSize = _map.size();
209                 int diff = currentSize - size;
210
211                 // remove extra entries if current size is bigger than diff
212
for (int i=0; i<diff; i++) {
213                     Object JavaDoc key = _keyList.get(i);
214                     assert (key != null);
215
216                     if (key != null) {
217                         Object JavaDoc val = _map.remove(key);
218                         assert (val != null);
219                     }
220                 }
221                 for(int i=0; i <diff; i++) {
222                     _keyList.remove(0);
223                 }
224                 _maxSizeAllowed = size;
225             }
226         }
227     }
228
229     /**
230      * Returns the name of the pool.
231      *
232      * @return name of the pool
233      */

234     public String JavaDoc getName() {
235         return _name;
236     }
237
238     // ---- INSTANCE VARIABLES - PRIVATE -------------------------
239
private List JavaDoc _keyList = null;
240     private Map JavaDoc _map = null;
241     private String JavaDoc _name = null;
242     private int _maxSizeAllowed = 25;
243 }
244
Popular Tags