Package org.apache.ojb.broker.util.pooling

Source Code of org.apache.ojb.broker.util.pooling.PoolConfiguration

package org.apache.ojb.broker.util.pooling;

/* Copyright 2002-2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import java.io.Serializable;
import java.util.Properties;

import org.apache.commons.dbcp.AbandonedConfig;
import org.apache.commons.pool.impl.GenericKeyedObjectPool;
import org.apache.commons.pool.impl.GenericObjectPool;


/**
* Encapsulates configuration properties for
* implementations using {@link org.apache.commons.pool.ObjectPool}.
*
* @author <a href="mailto:armin@codeAuLait.de">Armin Waibel</a>
* @version $Id: PoolConfiguration.java,v 1.11 2004/06/23 12:34:12 arminw Exp $
*/
public class PoolConfiguration extends Properties implements Serializable
{
  private static final long serialVersionUID = -3850488378321541047L;
    public static final String EMPTY = "";
    //*****************************************************
    // constants
    //*****************************************************
    public static final String MAX_ACTIVE = "maxActive";
    public static final String MAX_IDLE = "maxIdle";
    public static final String MAX_WAIT = "maxWait";
    public static final String WHEN_EXHAUSTED_ACTION = "whenExhaustedAction";
    public static final String TEST_ON_BORROW = "testOnBorrow";
    public static final String TEST_ON_RETURN = "testOnReturn";
    public static final String TEST_WHILE_IDLE = "testWhileIdle";
    public static final String TIME_BETWEEN_EVICTION_RUNS_MILLIS = "timeBetweenEvictionRunsMillis";
    public static final String NUM_TESTS_PER_EVICTION_RUN = "numTestsPerEvictionRun";
    public static final String MIN_EVICTABLE_IDLE_TIME_MILLIS = "minEvictableIdleTimeMillis";
    public static final String LOG_ABANDONED = "logAbandoned";
    public static final String REMOVE_ABANDONED = "removeAbandoned";
    public static final String REMOVE_ABANDONED_TIMEOUT = "removeAbandonedTimeout";
    public static final String VALIDATION_QUERY = "validationQuery";


    //*****************************************************
    // used default values
    //*****************************************************
    public static final int DEFAULT_MAX_ACTIVE = 21;
    public static final int DEFAULT_MAX_IDLE = -1;
    public static final long DEFAULT_MAX_WAIT = 5000;
    public static final byte DEFAULT_WHEN_EXHAUSTED_ACTION = GenericObjectPool.WHEN_EXHAUSTED_FAIL;
    public static final boolean DEFAULT_TEST_ON_BORROW = true;
    public static final boolean DEFAULT_TEST_ON_RETURN = false;
    public static final boolean DEFAULT_TEST_WHILE_IDLE = false;
    public static final long DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS = -1L;
    public static final int DEFAULT_NUM_TESTS_PER_EVICTION_RUN = 10;
    public static final long DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS = 1000 * 60 * 10;
    public static final boolean DEFAULT_LOG_ABANDONED = false;
    public static final boolean DEFAULT_REMOVE_ABANDONED = false;
    public static final int DEFAULT_REMOVE_ABANDONED_TIMEOUT = 300;

    public PoolConfiguration()
    {
        this.setMaxActive(DEFAULT_MAX_ACTIVE);
        this.setMaxIdle(DEFAULT_MAX_IDLE);
        this.setMaxWait(DEFAULT_MAX_WAIT);
        this.setWhenExhaustedAction(DEFAULT_WHEN_EXHAUSTED_ACTION);
        this.setTestOnBorrow(DEFAULT_TEST_ON_BORROW);
        this.setTestOnReturn(DEFAULT_TEST_ON_RETURN);
        this.setTestWhileIdle(DEFAULT_TEST_WHILE_IDLE);
        this.setMinEvictableIdleTimeMillis(DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS);
        this.setTimeBetweenEvictionRunsMillis(DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS);
        this.setNumTestsPerEvictionRun(DEFAULT_NUM_TESTS_PER_EVICTION_RUN);
        this.setLogAbandoned(DEFAULT_LOG_ABANDONED);
        this.setRemoveAbandoned(DEFAULT_REMOVE_ABANDONED);
        this.setRemoveAbandonedTimeout(DEFAULT_REMOVE_ABANDONED_TIMEOUT);
    }

    public PoolConfiguration(Properties properties)
    {
        this();
        this.putAll(properties);
    }

    /**
     * Returns an {@link org.apache.commons.pool.impl.GenericObjectPool.Config} object
     * configurated with the properties extracted from the this instance.
     * Use this to configurate a pool implementation using
     * {@link org.apache.commons.pool.impl.GenericObjectPool}.
     */
    public GenericObjectPool.Config getObjectPoolConfig()
    {
        GenericObjectPool.Config conf = new GenericObjectPool.Config();
        conf.maxActive = getMaxActive();
        conf.maxIdle = getMaxIdle();
        conf.maxWait = getMaxWait();
        conf.minEvictableIdleTimeMillis = getMinEvictableIdleTimeMillis();
        conf.numTestsPerEvictionRun = getNumTestsPerEvictionRun();
        conf.testOnBorrow = isTestOnBorrow();
        conf.testOnReturn = isTestOnReturn();
        conf.testWhileIdle = isTestWhileIdle();
        conf.timeBetweenEvictionRunsMillis = getTimeBetweenEvictionRunsMillis();
        conf.whenExhaustedAction = getWhenExhaustedAction();
        return conf;
    }

    /**
     * Returns an {@link org.apache.commons.pool.impl.GenericKeyedObjectPool.Config} object
     * configurated with the properties extracted from the this instance.
     * Use this to configurate a pool implementation using
     * {@link org.apache.commons.pool.impl.GenericKeyedObjectPool}.
     */
    public GenericKeyedObjectPool.Config getKeyedObjectPoolConfig()
    {
        GenericKeyedObjectPool.Config conf = new GenericKeyedObjectPool.Config();
        conf.maxActive = getMaxActive();
        conf.maxIdle = getMaxIdle();
        conf.maxWait = getMaxWait();
        conf.minEvictableIdleTimeMillis = getMinEvictableIdleTimeMillis();
        conf.numTestsPerEvictionRun = getNumTestsPerEvictionRun();
        conf.testOnBorrow = isTestOnBorrow();
        conf.testOnReturn = isTestOnReturn();
        conf.testWhileIdle = isTestWhileIdle();
        conf.timeBetweenEvictionRunsMillis = getTimeBetweenEvictionRunsMillis();
        conf.whenExhaustedAction = getWhenExhaustedAction();
        return conf;
    }

    public AbandonedConfig getAbandonedConfig()
    {
        AbandonedConfig conf = new AbandonedConfig();
        conf.setLogAbandoned(isLogAbandoned());
        conf.setRemoveAbandoned(isRemoveAbandoned());
        conf.setRemoveAbandonedTimeout(getRemoveAbandonedTimeout());
        return conf;
    }

    public boolean isLogAbandoned()
    {
      return Boolean.valueOf(getProperty(LOG_ABANDONED)).booleanValue();
    }

    public void setLogAbandoned(boolean logAbandoned)
    {
        this.setProperty(LOG_ABANDONED, EMPTY + logAbandoned);
    }

    public boolean isRemoveAbandoned()
    {
        return Boolean.valueOf(getProperty(REMOVE_ABANDONED)).booleanValue();
    }

    public void setRemoveAbandoned(boolean removeAbandoned)
    {
        this.setProperty(REMOVE_ABANDONED, EMPTY + removeAbandoned);
    }

    public int getRemoveAbandonedTimeout()
    {
        return Integer.parseInt(getProperty(REMOVE_ABANDONED_TIMEOUT));
    }

    public void setRemoveAbandonedTimeout(int removeAbandonedTimeout)
    {
        this.setProperty(REMOVE_ABANDONED_TIMEOUT, EMPTY + removeAbandonedTimeout);
    }

    public String getValidationQuery()
    {
        return getProperty(VALIDATION_QUERY);
    }

    public void setValidationQuery(String validationQuery)
    {
        if(validationQuery != null) setProperty(VALIDATION_QUERY, validationQuery);
    }

    public int getMaxActive()
    {
        return Integer.parseInt(getProperty(MAX_ACTIVE));
    }

    public void setMaxActive(int maxActive)
    {
        this.setProperty(MAX_ACTIVE, EMPTY + maxActive);
    }

    public int getMaxIdle()
    {
        return Integer.parseInt(getProperty(MAX_IDLE));
    }

    public void setMaxIdle(int maxIdle)
    {
        this.setProperty(MAX_IDLE, EMPTY + maxIdle);
    }


    public long getMaxWait()
    {
        return Long.parseLong(getProperty(MAX_WAIT));
    }

    public void setMaxWait(long maxWait)
    {
        this.setProperty(MAX_WAIT, EMPTY + maxWait);
    }


    public byte getWhenExhaustedAction()
    {
        return new Byte(getProperty(WHEN_EXHAUSTED_ACTION)).byteValue();
    }

    public void setWhenExhaustedAction(byte whenExhaustedAction)
    {
        this.setProperty(WHEN_EXHAUSTED_ACTION, EMPTY + whenExhaustedAction);
    }


    public boolean isTestOnBorrow()
    {
        return Boolean.valueOf(getProperty(TEST_ON_BORROW)).booleanValue();
    }

    public void setTestOnBorrow(boolean testOnBorrow)
    {
        this.setProperty(TEST_ON_BORROW, EMPTY + testOnBorrow);
    }


    public boolean isTestOnReturn()
    {
        return Boolean.valueOf(getProperty(TEST_ON_RETURN)).booleanValue();
    }

    public void setTestOnReturn(boolean testOnReturn)
    {
        this.setProperty(TEST_ON_RETURN, EMPTY + testOnReturn);
    }


    public boolean isTestWhileIdle()
    {
        return Boolean.valueOf(getProperty(TEST_WHILE_IDLE)).booleanValue();
    }

    public void setTestWhileIdle(boolean testWhileIdle)
    {
        this.setProperty(TEST_WHILE_IDLE, EMPTY + testWhileIdle);
    }


    public long getMinEvictableIdleTimeMillis()
    {
        return Long.parseLong(getProperty(MIN_EVICTABLE_IDLE_TIME_MILLIS));
    }

    public void setMinEvictableIdleTimeMillis(long minEvictableIdleTimeMillis)
    {
        this.setProperty(MIN_EVICTABLE_IDLE_TIME_MILLIS, EMPTY + minEvictableIdleTimeMillis);
    }


    public long getTimeBetweenEvictionRunsMillis()
    {
        return Long.parseLong(getProperty(TIME_BETWEEN_EVICTION_RUNS_MILLIS));
    }

    public void setTimeBetweenEvictionRunsMillis(long timeBetweenEvictionRunsMillis)
    {
        this.setProperty(TIME_BETWEEN_EVICTION_RUNS_MILLIS, EMPTY + timeBetweenEvictionRunsMillis);
    }


    public int getNumTestsPerEvictionRun()
    {
        return Integer.parseInt(getProperty(NUM_TESTS_PER_EVICTION_RUN));
    }

    public void setNumTestsPerEvictionRun(int numTestsPerEvictionRun)
    {
        this.setProperty(NUM_TESTS_PER_EVICTION_RUN, EMPTY + numTestsPerEvictionRun);
    }
}
TOP

Related Classes of org.apache.ojb.broker.util.pooling.PoolConfiguration

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.