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.11.2.4 2005/06/07 17:27:05 arminw Exp $
034: */
035: public class PoolConfiguration extends Properties implements
036: Serializable, AttributeContainer {
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: this .setMaxActive(DEFAULT_MAX_ACTIVE);
079: this .setMaxIdle(DEFAULT_MAX_IDLE);
080: this .setMinIdle(DEFAULT_MIN_IDLE);
081: this .setMaxWait(DEFAULT_MAX_WAIT);
082: this .setWhenExhaustedAction(DEFAULT_WHEN_EXHAUSTED_ACTION);
083: this .setTestOnBorrow(DEFAULT_TEST_ON_BORROW);
084: this .setTestOnReturn(DEFAULT_TEST_ON_RETURN);
085: this .setTestWhileIdle(DEFAULT_TEST_WHILE_IDLE);
086: this
087: .setMinEvictableIdleTimeMillis(DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS);
088: this
089: .setTimeBetweenEvictionRunsMillis(DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS);
090: this
091: .setNumTestsPerEvictionRun(DEFAULT_NUM_TESTS_PER_EVICTION_RUN);
092: this .setLogAbandoned(DEFAULT_LOG_ABANDONED);
093: this .setRemoveAbandoned(DEFAULT_REMOVE_ABANDONED);
094: this
095: .setRemoveAbandonedTimeout(DEFAULT_REMOVE_ABANDONED_TIMEOUT);
096: }
097:
098: public PoolConfiguration(Properties properties) {
099: this ();
100: this .putAll(properties);
101: }
102:
103: /**
104: * Returns an {@link org.apache.commons.pool.impl.GenericObjectPool.Config} object
105: * configurated with the properties extracted from the this instance.
106: * Use this to configurate a pool implementation using
107: * {@link org.apache.commons.pool.impl.GenericObjectPool}.
108: */
109: public GenericObjectPool.Config getObjectPoolConfig() {
110: GenericObjectPool.Config conf = new GenericObjectPool.Config();
111: conf.maxActive = getMaxActive();
112: conf.maxIdle = getMaxIdle();
113: conf.minIdle = getMinIdle();
114: conf.maxWait = getMaxWait();
115: conf.minEvictableIdleTimeMillis = getMinEvictableIdleTimeMillis();
116: conf.numTestsPerEvictionRun = getNumTestsPerEvictionRun();
117: conf.testOnBorrow = isTestOnBorrow();
118: conf.testOnReturn = isTestOnReturn();
119: conf.testWhileIdle = isTestWhileIdle();
120: conf.timeBetweenEvictionRunsMillis = getTimeBetweenEvictionRunsMillis();
121: conf.whenExhaustedAction = getWhenExhaustedAction();
122: return conf;
123: }
124:
125: /**
126: * Returns an {@link org.apache.commons.pool.impl.GenericKeyedObjectPool.Config} object
127: * configurated with the properties extracted from the this instance.
128: * Use this to configurate a pool implementation using
129: * {@link org.apache.commons.pool.impl.GenericKeyedObjectPool}.
130: */
131: public GenericKeyedObjectPool.Config getKeyedObjectPoolConfig() {
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: AbandonedConfig conf = new AbandonedConfig();
148: conf.setRemoveAbandoned(isRemoveAbandoned());
149: conf.setRemoveAbandonedTimeout(getRemoveAbandonedTimeout());
150: conf.setLogAbandoned(isLogAbandoned());
151: return conf;
152: }
153:
154: public void addAttribute(String attributeName, String attributeValue) {
155: setProperty(attributeName, attributeValue);
156: }
157:
158: public String getAttribute(String key) {
159: return getAttribute(key, null);
160: }
161:
162: public String getAttribute(String attributeName, String defaultValue) {
163: final String result = getProperty(attributeName);
164: return result == null ? defaultValue : result;
165: }
166:
167: public boolean isLogAbandoned() {
168: return Boolean.valueOf(getProperty(LOG_ABANDONED))
169: .booleanValue();
170: }
171:
172: public void setLogAbandoned(boolean logAbandoned) {
173: this .setProperty(LOG_ABANDONED, BooleanUtils
174: .toStringTrueFalse(logAbandoned));
175: }
176:
177: public boolean isRemoveAbandoned() {
178: return Boolean.valueOf(getProperty(REMOVE_ABANDONED))
179: .booleanValue();
180: }
181:
182: public void setRemoveAbandoned(boolean removeAbandoned) {
183: this .setProperty(REMOVE_ABANDONED, BooleanUtils
184: .toStringTrueFalse(removeAbandoned));
185: }
186:
187: public int getRemoveAbandonedTimeout() {
188: return Integer.parseInt(getProperty(REMOVE_ABANDONED_TIMEOUT));
189: }
190:
191: public void setRemoveAbandonedTimeout(int removeAbandonedTimeout) {
192: this .setProperty(REMOVE_ABANDONED_TIMEOUT, Integer
193: .toString(removeAbandonedTimeout));
194: }
195:
196: public String getValidationQuery() {
197: String result = getProperty(VALIDATION_QUERY);
198: return StringUtils.isEmpty(result) ? null : result;
199: }
200:
201: public void setValidationQuery(String validationQuery) {
202: setProperty(VALIDATION_QUERY, validationQuery);
203: }
204:
205: public int getMaxActive() {
206: return Integer.parseInt(getProperty(MAX_ACTIVE));
207: }
208:
209: public void setMaxActive(int maxActive) {
210: this .setProperty(MAX_ACTIVE, Integer.toString(maxActive));
211: }
212:
213: public int getMaxIdle() {
214: return Integer.parseInt(getProperty(MAX_IDLE));
215: }
216:
217: public void setMaxIdle(int maxIdle) {
218: this .setProperty(MAX_IDLE, Integer.toString(maxIdle));
219: }
220:
221: public int getMinIdle() {
222: return Integer.parseInt(getProperty(MIN_IDLE));
223: }
224:
225: public void setMinIdle(int minIdle) {
226: this .setProperty(MIN_IDLE, Integer.toString(minIdle));
227: }
228:
229: public long getMaxWait() {
230: return Long.parseLong(getProperty(MAX_WAIT));
231: }
232:
233: public void setMaxWait(long maxWait) {
234: this .setProperty(MAX_WAIT, Long.toString(maxWait));
235: }
236:
237: public byte getWhenExhaustedAction() {
238: return new Byte(getProperty(WHEN_EXHAUSTED_ACTION)).byteValue();
239: }
240:
241: public void setWhenExhaustedAction(byte whenExhaustedAction) {
242: this .setProperty(WHEN_EXHAUSTED_ACTION, Byte
243: .toString(whenExhaustedAction));
244: }
245:
246: public boolean isTestOnBorrow() {
247: return Boolean.valueOf(getProperty(TEST_ON_BORROW))
248: .booleanValue();
249: }
250:
251: public void setTestOnBorrow(boolean testOnBorrow) {
252: this .setProperty(TEST_ON_BORROW, BooleanUtils
253: .toStringTrueFalse(testOnBorrow));
254: }
255:
256: public boolean isTestOnReturn() {
257: return Boolean.valueOf(getProperty(TEST_ON_RETURN))
258: .booleanValue();
259: }
260:
261: public void setTestOnReturn(boolean testOnReturn) {
262: this .setProperty(TEST_ON_RETURN, BooleanUtils
263: .toStringTrueFalse(testOnReturn));
264: }
265:
266: public boolean isTestWhileIdle() {
267: return Boolean.valueOf(getProperty(TEST_WHILE_IDLE))
268: .booleanValue();
269: }
270:
271: public void setTestWhileIdle(boolean testWhileIdle) {
272: this .setProperty(TEST_WHILE_IDLE, BooleanUtils
273: .toStringTrueFalse(testWhileIdle));
274: }
275:
276: public long getMinEvictableIdleTimeMillis() {
277: return Long
278: .parseLong(getProperty(MIN_EVICTABLE_IDLE_TIME_MILLIS));
279: }
280:
281: public void setMinEvictableIdleTimeMillis(
282: long minEvictableIdleTimeMillis) {
283: this .setProperty(MIN_EVICTABLE_IDLE_TIME_MILLIS, Long
284: .toString(minEvictableIdleTimeMillis));
285: }
286:
287: public long getTimeBetweenEvictionRunsMillis() {
288: return Long
289: .parseLong(getProperty(TIME_BETWEEN_EVICTION_RUNS_MILLIS));
290: }
291:
292: public void setTimeBetweenEvictionRunsMillis(
293: long timeBetweenEvictionRunsMillis) {
294: this .setProperty(TIME_BETWEEN_EVICTION_RUNS_MILLIS, Long
295: .toString(timeBetweenEvictionRunsMillis));
296: }
297:
298: public int getNumTestsPerEvictionRun() {
299: return Integer
300: .parseInt(getProperty(NUM_TESTS_PER_EVICTION_RUN));
301: }
302:
303: public void setNumTestsPerEvictionRun(int numTestsPerEvictionRun) {
304: this.setProperty(NUM_TESTS_PER_EVICTION_RUN, Integer
305: .toString(numTestsPerEvictionRun));
306: }
307:
308: }
|