📄 genericpool.java
字号:
/* * PoolMan Java Object Pooling and Caching Library * Copyright (C) 1999-2001 The Code Studio * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * The full license is located at the root of this distribution * in the LICENSE file. */package com.codestudio.util;import java.lang.reflect.Constructor;/** * GenericPool allows any object to be pooled through the PoolManager. * By passing a classname and optionally some constructor params, any * Java object can be pooled here and accessed through a PoolManager. * */public class GenericPool extends ObjectPool { private GenericPoolMetaData info; protected Class otype; protected Object[] oparams; protected Constructor con; /** * Create a pool of Objects in which the Objects are created using * the default empty constructor. */ public GenericPool(PoolMetaData metad) { super(metad); this.oparams = null; this.con = null; this.info = (GenericPoolMetaData) metad; try { init(); } catch (Exception e) { log("GenericPool: Exception while initializing", e); } } /** * Create a pool of Objects in which the Objects are created using * a specific constructor with the specified params. */ public GenericPool(GenericPoolMetaData metad, String classname, Object[] params) { this(metad); this.oparams = params; this.con = figureConstructor(); } /** * Set the constructor's params */ public synchronized void setParams(Object[] p) { this.oparams = p; this.con = figureConstructor(); } /** * Get the constructor's params */ public synchronized Object[] getParams() { return this.oparams; } protected Constructor figureConstructor() { if (this.oparams == null) return null; Class[] paramscl = new Class[this.oparams.length]; for (int i = 0; i < oparams.length; i++) { paramscl[i] = oparams[i].getClass(); } try { Class clazz = Class.forName(info.getObjectType()); return clazz.getConstructor(paramscl); } catch (ClassNotFoundException cnf) { log("ERROR: GenericPool of " + info.getObjectType() + " cannot be created, that Class cannot be found"); } catch (NoSuchMethodException ne) { log("ERROR: In GenericPool: No such constructor with the specified params:"); } catch (SecurityException se) { log("ERROR: GenericPool: Security Exception:"); } return null; } protected Object create() throws Exception { String otype = info.getObjectType(); if (otype == null) throw new ClassNotFoundException(); Class clazz = Class.forName(otype); Object o = null; if (this.con == null) { o = clazz.newInstance(); return o; } else { o = this.con.newInstance(this.oparams); } return o; } public Object requestObject() { try { return super.checkOut(); } catch (Exception e) { System.out.println(e); return null; } } public void returnObject(Object o) { super.checkIn(o); } protected boolean validate(Object o) { return (o != null); } protected void expire(Object o) { o = null; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -