001    package org.apache.ojb.broker.util.pooling;
002    
003    /* Copyright 2002-2005 The Apache Software Foundation
004     *
005     * Licensed under the Apache License, Version 2.0 (the "License");
006     * you may not use this file except in compliance with the License.
007     * You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    
018    import java.io.Serializable;
019    import java.util.Properties;
020    
021    import org.apache.commons.dbcp.AbandonedConfig;
022    import org.apache.commons.pool.impl.GenericKeyedObjectPool;
023    import org.apache.commons.pool.impl.GenericObjectPool;
024    import org.apache.commons.lang.StringUtils;
025    import org.apache.commons.lang.BooleanUtils;
026    import org.apache.ojb.broker.metadata.AttributeContainer;
027    
028    /**
029     * Encapsulates configuration properties for
030     * implementations using {@link org.apache.commons.pool.ObjectPool}.
031     *
032     * @author <a href="mailto:armin@codeAuLait.de">Armin Waibel</a>
033     * @version $Id: PoolConfiguration.java,v 1.1 2007-08-24 22:17:42 ewestfal Exp $
034     */
035    public class PoolConfiguration extends Properties implements Serializable, AttributeContainer
036    {
037    
038            private static final long serialVersionUID = -3850488378321541047L;
039    
040        //*****************************************************
041        // constants
042        //*****************************************************
043        public static final String MAX_ACTIVE = "maxActive";
044        public static final String MAX_IDLE = "maxIdle";
045        public static final String MIN_IDLE = "minIdle";
046        public static final String MAX_WAIT = "maxWait";
047        public static final String WHEN_EXHAUSTED_ACTION = "whenExhaustedAction";
048        public static final String TEST_ON_BORROW = "testOnBorrow";
049        public static final String TEST_ON_RETURN = "testOnReturn";
050        public static final String TEST_WHILE_IDLE = "testWhileIdle";
051        public static final String TIME_BETWEEN_EVICTION_RUNS_MILLIS = "timeBetweenEvictionRunsMillis";
052        public static final String NUM_TESTS_PER_EVICTION_RUN = "numTestsPerEvictionRun";
053        public static final String MIN_EVICTABLE_IDLE_TIME_MILLIS = "minEvictableIdleTimeMillis";
054        public static final String LOG_ABANDONED = "logAbandoned";
055        public static final String REMOVE_ABANDONED = "removeAbandoned";
056        public static final String REMOVE_ABANDONED_TIMEOUT = "removeAbandonedTimeout";
057        public static final String VALIDATION_QUERY = "validationQuery";
058    
059        //*****************************************************
060        // used default values
061        //*****************************************************
062        public static final int DEFAULT_MAX_ACTIVE = 21;
063        public static final int DEFAULT_MAX_IDLE = -1;
064        public static final int DEFAULT_MIN_IDLE = 0;
065        public static final long DEFAULT_MAX_WAIT = 5000;
066        public static final byte DEFAULT_WHEN_EXHAUSTED_ACTION = GenericObjectPool.WHEN_EXHAUSTED_FAIL;
067        public static final boolean DEFAULT_TEST_ON_BORROW = true;
068        public static final boolean DEFAULT_TEST_ON_RETURN = false;
069        public static final boolean DEFAULT_TEST_WHILE_IDLE = false;
070        public static final long DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS = -1L;
071        public static final int DEFAULT_NUM_TESTS_PER_EVICTION_RUN = 10;
072        public static final long DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS = 1000 * 60 * 10;
073        public static final boolean DEFAULT_LOG_ABANDONED = false;
074        public static final boolean DEFAULT_REMOVE_ABANDONED = false;
075        public static final int DEFAULT_REMOVE_ABANDONED_TIMEOUT = 300;
076    
077        public PoolConfiguration()
078        {
079            this.setMaxActive(DEFAULT_MAX_ACTIVE);
080            this.setMaxIdle(DEFAULT_MAX_IDLE);
081            this.setMinIdle(DEFAULT_MIN_IDLE);
082            this.setMaxWait(DEFAULT_MAX_WAIT);
083            this.setWhenExhaustedAction(DEFAULT_WHEN_EXHAUSTED_ACTION);
084            this.setTestOnBorrow(DEFAULT_TEST_ON_BORROW);
085            this.setTestOnReturn(DEFAULT_TEST_ON_RETURN);
086            this.setTestWhileIdle(DEFAULT_TEST_WHILE_IDLE);
087            this.setMinEvictableIdleTimeMillis(DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS);
088            this.setTimeBetweenEvictionRunsMillis(DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS);
089            this.setNumTestsPerEvictionRun(DEFAULT_NUM_TESTS_PER_EVICTION_RUN);
090            this.setLogAbandoned(DEFAULT_LOG_ABANDONED);
091            this.setRemoveAbandoned(DEFAULT_REMOVE_ABANDONED);
092            this.setRemoveAbandonedTimeout(DEFAULT_REMOVE_ABANDONED_TIMEOUT);
093        }
094    
095        public PoolConfiguration(Properties properties)
096        {
097            this();
098            this.putAll(properties);
099        }
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 attributeName, String attributeValue)
156        {
157            setProperty(attributeName, attributeValue);
158        }
159    
160        public String getAttribute(String key)
161        {
162            return getAttribute(key, null);
163        }
164    
165        public String getAttribute(String attributeName, String defaultValue)
166        {
167            final String 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 getValidationQuery()
202        {
203            String result = getProperty(VALIDATION_QUERY);
204            return StringUtils.isEmpty(result) ? null : result;
205        }
206    
207        public void setValidationQuery(String 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(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