org.darkphoenixs.pool.PoolBase.java Source code

Java tutorial

Introduction

Here is the source code for org.darkphoenixs.pool.PoolBase.java

Source

/*
 * Copyright 2015-2016 Dark Phoenixs (Open-Source Organization).
 *
 * 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.
 */
package org.darkphoenixs.pool;

import org.apache.commons.pool2.PooledObjectFactory;
import org.apache.commons.pool2.impl.GenericObjectPool;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;

import java.io.Closeable;
import java.io.Serializable;

/**
 * <p>Title: PoolBase</p>
 * <p>Description: </p>
 *
 * @author Victor
 * @version 1.0
 * @see Closeable
 * @see Serializable
 * @since 2015919
 */
public abstract class PoolBase<T> implements Closeable, Serializable {

    /**
     * serialVersionUID
     */
    private static final long serialVersionUID = 536428799879058482L;

    /**
     * internalPool
     */
    protected GenericObjectPool<T> internalPool;

    /**
     * <p>Title: ConnectionPoolBase</p>
     * <p>Description: </p>
     */
    public PoolBase() {
    }

    /**
     * <p>Title: ConnectionPoolBase</p>
     * <p>Description: </p>
     *
     * @param poolConfig ?
     * @param factory    
     */
    public PoolBase(final GenericObjectPoolConfig poolConfig, PooledObjectFactory<T> factory) {
        this.initPool(poolConfig, factory);
    }

    /**
     * <p>Title: initPool</p>
     * <p>Description: ?</p>
     *
     * @param poolConfig ?
     * @param factory    
     */
    protected void initPool(final GenericObjectPoolConfig poolConfig, PooledObjectFactory<T> factory) {
        if (this.internalPool != null)
            this.destroy();

        this.internalPool = new GenericObjectPool<T>(factory, poolConfig);
    }

    /**
     * <p>Title: destroy</p>
     * <p>Description: ?</p>
     */
    protected void destroy() {
        this.close();
    }

    /**
     * <p>Title: getResource</p>
     * <p>Description: </p>
     *
     * @return 
     */
    protected T getResource() {
        try {
            return internalPool.borrowObject();
        } catch (Exception e) {
            throw new ConnectionException("Could not get a resource from the pool", e);
        }
    }

    /**
     * <p>Title: returnResource</p>
     * <p>Description: </p>
     *
     * @param resource 
     */
    protected void returnResource(final T resource) {
        if (null != resource)
            try {
                internalPool.returnObject(resource);
            } catch (Exception e) {
                throw new ConnectionException("Could not return the resource to the pool", e);
            }
    }

    /**
     * <p>Title: invalidateResource</p>
     * <p>Description: </p>
     *
     * @param resource 
     */
    protected void invalidateResource(final T resource) {
        if (null != resource)
            try {
                internalPool.invalidateObject(resource);
            } catch (Exception e) {
                throw new ConnectionException("Could not invalidate the resource to the pool", e);
            }
    }

    /**
     * <p>Title: getNumActive</p>
     * <p>Description: </p>
     *
     * @return 
     */
    public int getNumActive() {
        if (isInactived()) {
            return -1;
        }

        return this.internalPool.getNumActive();
    }

    /**
     * <p>Title: getNumIdle</p>
     * <p>Description: </p>
     *
     * @return 
     */
    public int getNumIdle() {
        if (isInactived()) {
            return -1;
        }

        return this.internalPool.getNumIdle();
    }

    /**
     * <p>Title: getNumWaiters</p>
     * <p>Description: </p>
     *
     * @return 
     */
    public int getNumWaiters() {
        if (isInactived()) {
            return -1;
        }

        return this.internalPool.getNumWaiters();
    }

    /**
     * <p>Title: getMeanBorrowWaitTimeMillis</p>
     * <p>Description: ?</p>
     *
     * @return ?
     */
    public long getMeanBorrowWaitTimeMillis() {
        if (isInactived()) {
            return -1;
        }

        return this.internalPool.getMeanBorrowWaitTimeMillis();
    }

    /**
     * <p>Title: getMaxBorrowWaitTimeMillis</p>
     * <p>Description: </p>
     *
     * @return 
     */
    public long getMaxBorrowWaitTimeMillis() {
        if (isInactived()) {
            return -1;
        }

        return this.internalPool.getMaxBorrowWaitTimeMillis();
    }

    /**
     * <p>Title: isClosed</p>
     * <p>Description: ?</p>
     *
     * @return ?
     */
    public boolean isClosed() {
        try {
            return this.internalPool.isClosed();
        } catch (Exception e) {
            throw new ConnectionException("Could not check closed from the pool", e);
        }
    }

    /**
     * <p>Title: isInactived</p>
     * <p>Description: ?</p>
     *
     * @return ?
     */
    private boolean isInactived() {
        try {
            return this.internalPool == null || this.internalPool.isClosed();
        } catch (Exception e) {
            throw new ConnectionException("Could not check inactived from the pool", e);
        }
    }

    /**
     * <p>Title: addObjects</p>
     * <p>Description: </p>
     *
     * @param count ?
     */
    protected void addObjects(final int count) {
        try {
            for (int i = 0; i < count; i++) {
                this.internalPool.addObject();
            }
        } catch (Exception e) {
            throw new ConnectionException("Error trying to add idle objects", e);
        }
    }

    /**
     * <p>Title: clear</p>
     * <p>Description: </p>
     */
    public void clear() {
        try {
            this.internalPool.clear();
        } catch (Exception e) {
            throw new ConnectionException("Could not clear the pool", e);
        }
    }

    /**
     * <p>Title: close</p>
     * <p>Description: </p>
     */
    public void close() {
        try {
            this.internalPool.close();
        } catch (Exception e) {
            throw new ConnectionException("Could not destroy the pool", e);
        }
    }
}