KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > broker > util > pooling > PoolConfiguration


1 package org.apache.ojb.broker.util.pooling;
2
3 /* Copyright 2002-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 import java.io.Serializable JavaDoc;
19 import java.util.Properties JavaDoc;
20
21 import org.apache.commons.dbcp.AbandonedConfig;
22 import org.apache.commons.pool.impl.GenericKeyedObjectPool;
23 import org.apache.commons.pool.impl.GenericObjectPool;
24 import org.apache.commons.lang.StringUtils;
25 import org.apache.commons.lang.BooleanUtils;
26 import org.apache.ojb.broker.metadata.AttributeContainer;
27
28 /**
29  * Encapsulates configuration properties for
30  * implementations using {@link org.apache.commons.pool.ObjectPool}.
31  *
32  * @author <a HREF="mailto:armin@codeAuLait.de">Armin Waibel</a>
33  * @version $Id: PoolConfiguration.java,v 1.11.2.4 2005/06/07 17:27:05 arminw Exp $
34  */

35 public class PoolConfiguration extends Properties JavaDoc implements Serializable JavaDoc, AttributeContainer
36 {
37
38     private static final long serialVersionUID = -3850488378321541047L;
39
40     //*****************************************************
41
// constants
42
//*****************************************************
43
public static final String JavaDoc MAX_ACTIVE = "maxActive";
44     public static final String JavaDoc MAX_IDLE = "maxIdle";
45     public static final String JavaDoc MIN_IDLE = "minIdle";
46     public static final String JavaDoc MAX_WAIT = "maxWait";
47     public static final String JavaDoc WHEN_EXHAUSTED_ACTION = "whenExhaustedAction";
48     public static final String JavaDoc TEST_ON_BORROW = "testOnBorrow";
49     public static final String JavaDoc TEST_ON_RETURN = "testOnReturn";
50     public static final String JavaDoc TEST_WHILE_IDLE = "testWhileIdle";
51     public static final String JavaDoc TIME_BETWEEN_EVICTION_RUNS_MILLIS = "timeBetweenEvictionRunsMillis";
52     public static final String JavaDoc NUM_TESTS_PER_EVICTION_RUN = "numTestsPerEvictionRun";
53     public static final String JavaDoc MIN_EVICTABLE_IDLE_TIME_MILLIS = "minEvictableIdleTimeMillis";
54     public static final String JavaDoc LOG_ABANDONED = "logAbandoned";
55     public static final String JavaDoc REMOVE_ABANDONED = "removeAbandoned";
56     public static final String JavaDoc REMOVE_ABANDONED_TIMEOUT = "removeAbandonedTimeout";
57     public static final String JavaDoc VALIDATION_QUERY = "validationQuery";
58
59     //*****************************************************
60
// used default values
61
//*****************************************************
62
public static final int DEFAULT_MAX_ACTIVE = 21;
63     public static final int DEFAULT_MAX_IDLE = -1;
64     public static final int DEFAULT_MIN_IDLE = 0;
65     public static final long DEFAULT_MAX_WAIT = 5000;
66     public static final byte DEFAULT_WHEN_EXHAUSTED_ACTION = GenericObjectPool.WHEN_EXHAUSTED_FAIL;
67     public static final boolean DEFAULT_TEST_ON_BORROW = true;
68     public static final boolean DEFAULT_TEST_ON_RETURN = false;
69     public static final boolean DEFAULT_TEST_WHILE_IDLE = false;
70     public static final long DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS = -1L;
71     public static final int DEFAULT_NUM_TESTS_PER_EVICTION_RUN = 10;
72     public static final long DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS = 1000 * 60 * 10;
73     public static final boolean DEFAULT_LOG_ABANDONED = false;
74     public static final boolean DEFAULT_REMOVE_ABANDONED = false;
75     public static final int DEFAULT_REMOVE_ABANDONED_TIMEOUT = 300;
76
77     public PoolConfiguration()
78     {
79         this.setMaxActive(DEFAULT_MAX_ACTIVE);
80         this.setMaxIdle(DEFAULT_MAX_IDLE);
81         this.setMinIdle(DEFAULT_MIN_IDLE);
82         this.setMaxWait(DEFAULT_MAX_WAIT);
83         this.setWhenExhaustedAction(DEFAULT_WHEN_EXHAUSTED_ACTION);
84         this.setTestOnBorrow(DEFAULT_TEST_ON_BORROW);
85         this.setTestOnReturn(DEFAULT_TEST_ON_RETURN);
86         this.setTestWhileIdle(DEFAULT_TEST_WHILE_IDLE);
87         this.setMinEvictableIdleTimeMillis(DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS);
88         this.setTimeBetweenEvictionRunsMillis(DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS);
89         this.setNumTestsPerEvictionRun(DEFAULT_NUM_TESTS_PER_EVICTION_RUN);
90         this.setLogAbandoned(DEFAULT_LOG_ABANDONED);
91         this.setRemoveAbandoned(DEFAULT_REMOVE_ABANDONED);
92         this.setRemoveAbandonedTimeout(DEFAULT_REMOVE_ABANDONED_TIMEOUT);
93     }
94
95     public PoolConfiguration(Properties JavaDoc properties)
96     {
97         this();
98         this.putAll(properties);
99     }
100
101     /**
102      * Returns an {@link org.apache.commons.pool.impl.GenericObjectPool.Config} object
103      * configurated with the properties extracted from the this instance.
104      * Use this to configurate a pool implementation using
105      * {@link org.apache.commons.pool.impl.GenericObjectPool}.
106      */

107     public GenericObjectPool.Config getObjectPoolConfig()
108     {
109         GenericObjectPool.Config conf = new GenericObjectPool.Config();
110         conf.maxActive = getMaxActive();
111         conf.maxIdle = getMaxIdle();
112         conf.minIdle = getMinIdle();
113         conf.maxWait = getMaxWait();
114         conf.minEvictableIdleTimeMillis = getMinEvictableIdleTimeMillis();
115         conf.numTestsPerEvictionRun = getNumTestsPerEvictionRun();
116         conf.testOnBorrow = isTestOnBorrow();
117         conf.testOnReturn = isTestOnReturn();
118         conf.testWhileIdle = isTestWhileIdle();
119         conf.timeBetweenEvictionRunsMillis = getTimeBetweenEvictionRunsMillis();
120         conf.whenExhaustedAction = getWhenExhaustedAction();
121         return conf;
122     }
123
124     /**
125      * Returns an {@link org.apache.commons.pool.impl.GenericKeyedObjectPool.Config} object
126      * configurated with the properties extracted from the this instance.
127      * Use this to configurate a pool implementation using
128      * {@link org.apache.commons.pool.impl.GenericKeyedObjectPool}.
129      */

130     public GenericKeyedObjectPool.Config getKeyedObjectPoolConfig()
131     {
132         GenericKeyedObjectPool.Config conf = new GenericKeyedObjectPool.Config();
133         conf.maxActive = getMaxActive();
134         conf.maxIdle = getMaxIdle();
135         conf.maxWait = getMaxWait();
136         conf.minEvictableIdleTimeMillis = getMinEvictableIdleTimeMillis();
137         conf.numTestsPerEvictionRun = getNumTestsPerEvictionRun();
138         conf.testOnBorrow = isTestOnBorrow();
139         conf.testOnReturn = isTestOnReturn();
140         conf.testWhileIdle = isTestWhileIdle();
141         conf.timeBetweenEvictionRunsMillis = getTimeBetweenEvictionRunsMillis();
142         conf.whenExhaustedAction = getWhenExhaustedAction();
143         return conf;
144     }
145
146     public AbandonedConfig getAbandonedConfig()
147     {
148         AbandonedConfig conf = new AbandonedConfig();
149         conf.setRemoveAbandoned(isRemoveAbandoned());
150         conf.setRemoveAbandonedTimeout(getRemoveAbandonedTimeout());
151         conf.setLogAbandoned(isLogAbandoned());
152         return conf;
153     }
154
155     public void addAttribute(String JavaDoc attributeName, String JavaDoc attributeValue)
156     {
157         setProperty(attributeName, attributeValue);
158     }
159
160     public String JavaDoc getAttribute(String JavaDoc key)
161     {
162         return getAttribute(key, null);
163     }
164
165     public String JavaDoc getAttribute(String JavaDoc attributeName, String JavaDoc defaultValue)
166     {
167         final String JavaDoc result = getProperty(attributeName);
168         return result == null ? defaultValue : result;
169     }
170
171     public boolean isLogAbandoned()
172     {
173         return Boolean.valueOf(getProperty(LOG_ABANDONED)).booleanValue();
174     }
175
176     public void setLogAbandoned(boolean logAbandoned)
177     {
178         this.setProperty(LOG_ABANDONED, BooleanUtils.toStringTrueFalse(logAbandoned));
179     }
180
181     public boolean isRemoveAbandoned()
182     {
183         return Boolean.valueOf(getProperty(REMOVE_ABANDONED)).booleanValue();
184     }
185
186     public void setRemoveAbandoned(boolean removeAbandoned)
187     {
188         this.setProperty(REMOVE_ABANDONED, BooleanUtils.toStringTrueFalse(removeAbandoned));
189     }
190
191     public int getRemoveAbandonedTimeout()
192     {
193         return Integer.parseInt(getProperty(REMOVE_ABANDONED_TIMEOUT));
194     }
195
196     public void setRemoveAbandonedTimeout(int removeAbandonedTimeout)
197     {
198         this.setProperty(REMOVE_ABANDONED_TIMEOUT, Integer.toString(removeAbandonedTimeout));
199     }
200
201     public String JavaDoc getValidationQuery()
202     {
203         String JavaDoc result = getProperty(VALIDATION_QUERY);
204         return StringUtils.isEmpty(result) ? null : result;
205     }
206
207     public void setValidationQuery(String JavaDoc validationQuery)
208     {
209         setProperty(VALIDATION_QUERY, validationQuery);
210     }
211
212     public int getMaxActive()
213     {
214         return Integer.parseInt(getProperty(MAX_ACTIVE));
215     }
216
217     public void setMaxActive(int maxActive)
218     {
219         this.setProperty(MAX_ACTIVE, Integer.toString(maxActive));
220     }
221
222     public int getMaxIdle()
223     {
224         return Integer.parseInt(getProperty(MAX_IDLE));
225     }
226
227     public void setMaxIdle(int maxIdle)
228     {
229         this.setProperty(MAX_IDLE, Integer.toString(maxIdle));
230     }
231
232     public int getMinIdle()
233     {
234         return Integer.parseInt(getProperty(MIN_IDLE));
235     }
236
237     public void setMinIdle(int minIdle)
238     {
239         this.setProperty(MIN_IDLE, Integer.toString(minIdle));
240     }
241
242     public long getMaxWait()
243     {
244         return Long.parseLong(getProperty(MAX_WAIT));
245     }
246
247     public void setMaxWait(long maxWait)
248     {
249         this.setProperty(MAX_WAIT, Long.toString(maxWait));
250     }
251
252
253     public byte getWhenExhaustedAction()
254     {
255         return new Byte JavaDoc(getProperty(WHEN_EXHAUSTED_ACTION)).byteValue();
256     }
257
258     public void setWhenExhaustedAction(byte whenExhaustedAction)
259     {
260         this.setProperty(WHEN_EXHAUSTED_ACTION, Byte.toString(whenExhaustedAction));
261     }
262
263
264     public boolean isTestOnBorrow()
265     {
266         return Boolean.valueOf(getProperty(TEST_ON_BORROW)).booleanValue();
267     }
268
269     public void setTestOnBorrow(boolean testOnBorrow)
270     {
271         this.setProperty(TEST_ON_BORROW, BooleanUtils.toStringTrueFalse(testOnBorrow));
272     }
273
274
275     public boolean isTestOnReturn()
276     {
277         return Boolean.valueOf(getProperty(TEST_ON_RETURN)).booleanValue();
278     }
279
280     public void setTestOnReturn(boolean testOnReturn)
281     {
282         this.setProperty(TEST_ON_RETURN, BooleanUtils.toStringTrueFalse(testOnReturn));
283     }
284
285
286     public boolean isTestWhileIdle()
287     {
288         return Boolean.valueOf(getProperty(TEST_WHILE_IDLE)).booleanValue();
289     }
290
291     public void setTestWhileIdle(boolean testWhileIdle)
292     {
293         this.setProperty(TEST_WHILE_IDLE, BooleanUtils.toStringTrueFalse(testWhileIdle));
294     }
295
296
297     public long getMinEvictableIdleTimeMillis()
298     {
299         return Long.parseLong(getProperty(MIN_EVICTABLE_IDLE_TIME_MILLIS));
300     }
301
302     public void setMinEvictableIdleTimeMillis(long minEvictableIdleTimeMillis)
303     {
304         this.setProperty(MIN_EVICTABLE_IDLE_TIME_MILLIS, Long.toString(minEvictableIdleTimeMillis));
305     }
306
307
308     public long getTimeBetweenEvictionRunsMillis()
309     {
310         return Long.parseLong(getProperty(TIME_BETWEEN_EVICTION_RUNS_MILLIS));
311     }
312
313     public void setTimeBetweenEvictionRunsMillis(long timeBetweenEvictionRunsMillis)
314     {
315         this.setProperty(TIME_BETWEEN_EVICTION_RUNS_MILLIS, Long.toString(timeBetweenEvictionRunsMillis));
316     }
317
318
319     public int getNumTestsPerEvictionRun()
320     {
321         return Integer.parseInt(getProperty(NUM_TESTS_PER_EVICTION_RUN));
322     }
323
324     public void setNumTestsPerEvictionRun(int numTestsPerEvictionRun)
325     {
326         this.setProperty(NUM_TESTS_PER_EVICTION_RUN, Integer.toString(numTestsPerEvictionRun));
327     }
328
329 }
330
331
Popular Tags