Java tutorial
/* * 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 com.sky.projects.pool; import java.io.Closeable; import java.io.Serializable; import org.apache.commons.pool2.PooledObjectFactory; import org.apache.commons.pool2.impl.GenericObjectPool; import org.apache.commons.pool2.impl.GenericObjectPoolConfig; /** * * * @author zealot * * @param <T> */ @SuppressWarnings("serial") public abstract class PoolBase<T> implements Closeable, Serializable { protected GenericObjectPool<T> internalPool; public PoolBase() { } public PoolBase(final GenericObjectPoolConfig poolConfig, PooledObjectFactory<T> factory) { this.initPool(poolConfig, factory); } /** * ? * * @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); } /** * ? */ protected void destroy() { this.close(); } /** * * * @return */ protected T getResource() throws ConnectionException { try { return internalPool.borrowObject(); } catch (Exception e) { throw new ConnectionException("Could not get a resource from the pool", e); } } /** * * * @param resource */ protected void returnResource(final T resource) throws ConnectionException { if (null != resource) try { internalPool.returnObject(resource); } catch (Exception e) { throw new ConnectionException("Could not return the resource to the pool", e); } } /** * * * @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); } } /** * * * @return */ public int getNumActive() { return isInactived() ? -1 : this.internalPool.getNumActive(); } /** * * * @return */ public int getNumIdle() { return isInactived() ? -1 : this.internalPool.getNumIdle(); } /** * * * @return */ public int getNumWaiters() { return isInactived() ? -1 : this.internalPool.getNumWaiters(); } /** * ? * * @return */ public long getMeanBorrowWaitTimeMillis() { return isInactived() ? -1 : this.internalPool.getMeanBorrowWaitTimeMillis(); } /** * * * @return */ public long getMaxBorrowWaitTimeMillis() { return isInactived() ? -1 : this.internalPool.getMaxBorrowWaitTimeMillis(); } /** * ? * * @return */ public boolean isClosed() throws ConnectionException { try { return this.internalPool.isClosed(); } catch (Exception e) { throw new ConnectionException("Could not check closed from the pool", e); } } /** * ? * * @return */ private boolean isInactived() throws ConnectionException { try { return this.internalPool == null || this.internalPool.isClosed(); } catch (Exception e) { throw new ConnectionException("Could not check inactived from the pool", e); } } /** * * * @param count */ protected void addObjects(final int count) throws ConnectionException { try { for (int i = 0; i < count; i++) { this.internalPool.addObject(); } } catch (Exception e) { throw new ConnectionException("Error trying to add idle objects", e); } } /** * */ public void clear() throws ConnectionException { try { this.internalPool.clear(); } catch (Exception e) { throw new ConnectionException("Could not clear the pool", e); } } /** * */ public void close() throws ConnectionException { try { this.internalPool.close(); } catch (Exception e) { throw new ConnectionException("Could not destroy the pool", e); } } }