This is an automated email from the ASF dual-hosted git repository. gk pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/turbine-fulcrum-pool.git
commit 0fc9ae3941006ceaae780e0cb2e1a5728967eee0 Author: Jeffery Painter <[email protected]> AuthorDate: Mon Nov 5 15:42:25 2018 +0000 Fix/update javadocs for pool and localization for mvn site to build cleanly git-svn-id: https://svn.apache.org/repos/asf/turbine/fulcrum/trunk/pool@1845817 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/fulcrum/pool/ArrayCtorRecyclable.java | 2 + .../org/apache/fulcrum/pool/BoundedBuffer.java | 2 +- .../apache/fulcrum/pool/DefaultPoolService.java | 732 ++++++++++----------- .../apache/fulcrum/pool/InitableRecyclable.java | 9 +- src/java/org/apache/fulcrum/pool/PoolBuffer.java | 16 +- src/java/org/apache/fulcrum/pool/PoolService.java | 4 +- .../org/apache/fulcrum/pool/PoolServiceTest.java | 167 ++--- 7 files changed, 451 insertions(+), 481 deletions(-) diff --git a/src/java/org/apache/fulcrum/pool/ArrayCtorRecyclable.java b/src/java/org/apache/fulcrum/pool/ArrayCtorRecyclable.java index fd335c0..68dbae3 100644 --- a/src/java/org/apache/fulcrum/pool/ArrayCtorRecyclable.java +++ b/src/java/org/apache/fulcrum/pool/ArrayCtorRecyclable.java @@ -35,6 +35,8 @@ public interface ArrayCtorRecyclable extends Recyclable * Recycles the object for a new client. Objects implementing * this interface must also provide a matching constructor. * The recycle methods must call their super. + * + * @param params the parameters to recycle */ public void recycle(Object[] params); } diff --git a/src/java/org/apache/fulcrum/pool/BoundedBuffer.java b/src/java/org/apache/fulcrum/pool/BoundedBuffer.java index 40abd8d..56bfbed 100644 --- a/src/java/org/apache/fulcrum/pool/BoundedBuffer.java +++ b/src/java/org/apache/fulcrum/pool/BoundedBuffer.java @@ -107,7 +107,7 @@ public class BoundedBuffer /** * Puts an item in the buffer only if there is capacity available. * - * @param item the item to be inserted. + * @param x the item to be inserted. * @return true if accepted, else false. */ public synchronized boolean offer(Object x) diff --git a/src/java/org/apache/fulcrum/pool/DefaultPoolService.java b/src/java/org/apache/fulcrum/pool/DefaultPoolService.java index 30b4739..b2a5995 100644 --- a/src/java/org/apache/fulcrum/pool/DefaultPoolService.java +++ b/src/java/org/apache/fulcrum/pool/DefaultPoolService.java @@ -34,402 +34,360 @@ import org.apache.fulcrum.factory.FactoryException; import org.apache.fulcrum.factory.FactoryService; /** - * The Pool Service extends the Factory Service by adding support - * for pooling instantiated objects. When a new instance is - * requested, the service first checks its pool if one is available. - * If the the pool is empty, a new instance will be requested - * from the FactoryService. + * The Pool Service extends the Factory Service by adding support for pooling + * instantiated objects. When a new instance is requested, the service first + * checks its pool if one is available. If the the pool is empty, a new instance + * will be requested from the FactoryService. * - * For objects implementing the Recyclable interface, a recycle - * method will be called, when they taken from the pool, and - * a dispose method, when they are returned to the pool. + * For objects implementing the Recyclable interface, a recycle method will be + * called, when they taken from the pool, and a dispose method, when they are + * returned to the pool. * * @author <a href="mailto:[email protected]">Ilkka Priha</a> * @author <a href="mailto:[email protected]">Stephen McConnell</a> * @version $Id$ * - * @avalon.component name="pool" lifestyle="transient" - * @avalon.service type="org.apache.fulcrum.pool.PoolService" + * avalon.component name="pool" lifestyle="transient" + * avalon.service type="org.apache.fulcrum.pool.PoolService" */ -public class DefaultPoolService extends AbstractLogEnabled implements PoolService, Serviceable, Disposable, Initializable, Configurable -{ - /** - * The property specifying the pool capacity. - */ - public static final String POOL_CAPACITY = "capacity"; - /** - * The default capacity of pools. - */ - private int poolCapacity = DEFAULT_POOL_CAPACITY; - /** - * The pool repository, one pool for each class. - */ - private HashMap<String, PoolBuffer> poolRepository = new HashMap<>(); - private Map<String, Integer> capacityMap; - private FactoryService factoryService; - private ServiceManager manager; +public class DefaultPoolService extends AbstractLogEnabled + implements PoolService, Serviceable, Disposable, Initializable, Configurable { + /** + * The property specifying the pool capacity. + */ + public static final String POOL_CAPACITY = "capacity"; + /** + * The default capacity of pools. + */ + private int poolCapacity = DEFAULT_POOL_CAPACITY; + /** + * The pool repository, one pool for each class. + */ + private HashMap<String, PoolBuffer> poolRepository = new HashMap<>(); + private Map<String, Integer> capacityMap; + private FactoryService factoryService; + private ServiceManager manager; - /** - * Gets an instance of a named class either from the pool - * or by calling the Factory Service if the pool is empty. - * - * @param className the name of the class. - * @return the instance. - * @throws PoolException if recycling fails. - */ - public Object getInstance(String className) throws PoolException - { - try - { - Object instance = pollInstance(className, null, null); - return instance == null ? getFactory().getInstance(className) : instance; - } - catch (FactoryException fe) - { - throw new PoolException(fe); - } - } - /** - * Gets an instance of a named class either from the pool - * or by calling the Factory Service if the pool is empty. - * The specified class loader will be passed to the Factory Service. - * - * @param className the name of the class. - * @param loader the class loader. - * @return the instance. - * @throws PoolException if recycling fails. - */ - public Object getInstance(String className, ClassLoader loader) throws PoolException - { - try - { - Object instance = pollInstance(className, null, null); - return instance == null ? getFactory().getInstance(className, loader) : instance; - } - catch (FactoryException fe) - { - throw new PoolException(fe); - } - } - /** - * Gets an instance of a named class either from the pool - * or by calling the Factory Service if the pool is empty. - * Parameters for its constructor are given as an array of objects, - * primitive types must be wrapped with a corresponding class. - * - * @param className the name of the class. - * @param loader the class loader. - * @param params an array containing the parameters of the constructor. - * @param signature an array containing the signature of the constructor. - * @return the instance. - * @throws PoolException if recycling fails. - */ - public Object getInstance(String className, Object[] params, String[] signature) throws PoolException - { - try - { - Object instance = pollInstance(className, params, signature); - return instance == null ? getFactory().getInstance(className, params, signature) : instance; - } - catch (FactoryException fe) - { - throw new PoolException(fe); - } - } - /** - * Gets an instance of a named class either from the pool - * or by calling the Factory Service if the pool is empty. - * Parameters for its constructor are given as an array of objects, - * primitive types must be wrapped with a corresponding class. - * The specified class loader will be passed to the Factory Service. - * - * @param className the name of the class. - * @param loader the class loader. - * @param params an array containing the parameters of the constructor. - * @param signature an array containing the signature of the constructor. - * @return the instance. - * @throws PoolException if recycling fails. - */ - public Object getInstance(String className, ClassLoader loader, Object[] params, String[] signature) - throws PoolException - { - try - { - Object instance = pollInstance(className, params, signature); - return instance == null ? getFactory().getInstance(className, loader, params, signature) : instance; - } - catch (FactoryException fe) - { - throw new PoolException(fe); - } - } - /** - * Tests if specified class loaders are supported for a named class. - * - * @param className the name of the class. - * @return true if class loaders are supported, false otherwise. - * @throws PoolException if test fails. - */ - public boolean isLoaderSupported(String className) throws FactoryException - { - return getFactory().isLoaderSupported(className); - } - /** - * Gets an instance of a specified class either from the pool - * or by instatiating from the class if the pool is empty. - * - * @param clazz the class. - * @return the instance. - * @throws PoolException if recycling fails. - */ - @SuppressWarnings("unchecked") - public Object getInstance(Class clazz) throws PoolException - { - try - { - Object instance = pollInstance(clazz.getName(), null, null); - return instance == null ? factoryService.getInstance(clazz) : instance; - } - catch (FactoryException fe) - { - throw new PoolException(fe); - } - } - /** - * Gets an instance of a specified class either from the pool - * or by instatiating from the class if the pool is empty. - * - * @todo There is a whacky .toString() on the clazzz, but otherwise it - * won't compile.. - * @param clazz the class. - * @param params an array containing the parameters of the constructor. - * @param signature an array containing the signature of the constructor. - * @return the instance. - * @throws PoolException if recycling fails. - */ - public Object getInstance(Class clazz, Object params[], String signature[]) throws PoolException - { - try - { - Object instance = pollInstance(clazz.getName(), params, signature); - //FactoryService fs = getFactory(); - return instance == null ? getFactory().getInstance(clazz.toString(), params, signature) : instance; - } - catch (FactoryException fe) - { - throw new PoolException(fe); - } - } - /** - * Puts a used object back to the pool. Objects implementing - * the Recyclable interface can provide a recycle method to - * be called when they are reused and a dispose method to be - * called when they are returned to the pool. - * - * @param instance the object instance to recycle. - * @return true if the instance was accepted. - */ - @SuppressWarnings("unchecked") - public boolean putInstance(Object instance) - { - if (instance != null) - { - HashMap<String, PoolBuffer> repository = poolRepository; - String className = instance.getClass().getName(); - PoolBuffer pool = (PoolBuffer) repository.get(className); - if (pool == null) - { - pool = new PoolBuffer(getCapacity(className)); - repository = (HashMap<String, PoolBuffer>) repository.clone(); - repository.put(className, pool); - poolRepository = repository; - if (instance instanceof ArrayCtorRecyclable) - { - pool.setArrayCtorRecyclable(true); - } - } - return pool.offer(instance); - } - else - { - return false; - } - } - /** - * Gets the capacity of the pool for a named class. - * - * @param className the name of the class. - */ - public int getCapacity(String className) - { - PoolBuffer pool = (PoolBuffer) poolRepository.get(className); - if (pool == null) - { - /* Check class specific capacity. */ - int capacity = poolCapacity; - if (capacityMap != null) - { - Integer cap = (Integer) capacityMap.get(className); - if (cap != null) - { - capacity = cap.intValue(); - } - } - return capacity; - } - else - { - return pool.capacity(); - } - } - /** - * Sets the capacity of the pool for a named class. - * Note that the pool will be cleared after the change. - * - * @param className the name of the class. - * @param capacity the new capacity. - */ - @SuppressWarnings("unchecked") - public void setCapacity(String className, int capacity) - { - HashMap<String, PoolBuffer> repository = poolRepository; - repository = repository != null ? (HashMap<String, PoolBuffer>) repository.clone() : new HashMap<String, PoolBuffer>(); - repository.put(className, new PoolBuffer(capacity)); - poolRepository = repository; - } - /** - * Gets the current size of the pool for a named class. - * - * @param className the name of the class. - */ - public int getSize(String className) - { - PoolBuffer pool = (PoolBuffer) poolRepository.get(className); - return pool != null ? pool.size() : 0; - } - /** - * Clears instances of a named class from the pool. - * - * @param className the name of the class. - */ - @SuppressWarnings("unchecked") - public void clearPool(String className) - { - HashMap<String, PoolBuffer> repository = poolRepository; - if (repository.get(className) != null) - { - repository = (HashMap<String, PoolBuffer>) repository.clone(); - repository.remove(className); - poolRepository = repository; - } - } - /** - * Clears all instances from the pool. - */ - public void clearPool() - { - poolRepository = new HashMap<String, PoolBuffer>(); - } - /** - * Polls and recycles an object of the named class from the pool. - * - * @param className the name of the class. - * @param params an array containing the parameters of the constructor. - * @param signature an array containing the signature of the constructor. - * @return the object or null. - * @throws PoolException if recycling fails. - */ - private Object pollInstance(String className, Object[] params, String[] signature) throws PoolException - { - PoolBuffer pool = (PoolBuffer) poolRepository.get(className); - return pool != null ? pool.poll(params, signature, factoryService) : null; - } - /** - * Gets the factory service. - * - * @return the factory service. - */ - protected FactoryService getFactory() - { - return factoryService; - } - - // ---------------- Avalon Lifecycle Methods --------------------- - /** - * Avalon component lifecycle method - */ - public void configure(Configuration conf) - { - final Configuration capacities = conf.getChild(POOL_CAPACITY, false); - if (capacities != null) - { - Configuration defaultConf = capacities.getChild("default"); - int capacity = defaultConf.getValueAsInteger(DEFAULT_POOL_CAPACITY); - if (capacity <= 0) - { - throw new IllegalArgumentException("Capacity must be >0"); - } - poolCapacity = capacity; - Configuration[] nameVal = capacities.getChildren(); - for (int i = 0; i < nameVal.length; i++) - { - String key = nameVal[i].getName(); - if (!"default".equals(key)) - { - capacity = nameVal[i].getValueAsInteger(poolCapacity); - if (capacity < 0) - { - capacity = poolCapacity; - } - if (capacityMap == null) - { - capacityMap = new HashMap<String, Integer>(); - } - capacityMap.put(key, new Integer(capacity)); - } - } - } - } + /** + * Gets an instance of a named class either from the pool or by calling the + * Factory Service if the pool is empty. + * + * @param className the name of the class. + * @return the instance. + * @throws PoolException if recycling fails. + */ + public Object getInstance(String className) throws PoolException { + try { + Object instance = pollInstance(className, null, null); + return instance == null ? getFactory().getInstance(className) : instance; + } catch (FactoryException fe) { + throw new PoolException(fe); + } + } - /** - * Avalon component lifecycle method - * @avalon.dependency type="org.apache.fulcrum.factory.FactoryService" - */ - public void service(ServiceManager manager) - { - this.manager = manager; - } + /** + * Gets an instance of a named class either from the pool or by calling the + * Factory Service if the pool is empty. The specified class loader will be + * passed to the Factory Service. + * + * @param className the name of the class. + * @param loader the class loader. + * @return the instance. + * @throws PoolException if recycling fails. + */ + public Object getInstance(String className, ClassLoader loader) throws PoolException { + try { + Object instance = pollInstance(className, null, null); + return instance == null ? getFactory().getInstance(className, loader) : instance; + } catch (FactoryException fe) { + throw new PoolException(fe); + } + } - /** - * Avalon component lifecycle method - * Initializes the service by loading default class loaders - * and customized object factories. - * - * @throws InitializationException if initialization fails. - */ - public void initialize() throws Exception - { - try - { - factoryService = (FactoryService) manager.lookup(FactoryService.ROLE); - } - catch (Exception e) - { - throw new Exception( - "DefaultPoolService.initialize: Failed to get a Factory object", e); - } - } + /** + * Gets an instance of a named class either from the pool or by calling the + * Factory Service if the pool is empty. Parameters for its constructor are + * given as an array of objects, primitive types must be wrapped with a + * corresponding class. + * + * @param className the name of the class. + * @param params an array containing the parameters of the constructor. + * @param signature an array containing the signature of the constructor. + * @return the instance. + * @throws PoolException if recycling fails. + */ + public Object getInstance(String className, Object[] params, String[] signature) throws PoolException { + try { + Object instance = pollInstance(className, params, signature); + return instance == null ? getFactory().getInstance(className, params, signature) : instance; + } catch (FactoryException fe) { + throw new PoolException(fe); + } + } - /** - * Avalon component lifecycle method - */ - public void dispose() - { - if (factoryService != null) - { - manager.release(factoryService); - } - factoryService = null; - manager = null; - } + /** + * Gets an instance of a named class either from the pool or by calling the + * Factory Service if the pool is empty. Parameters for its constructor are + * given as an array of objects, primitive types must be wrapped with a + * corresponding class. The specified class loader will be passed to the Factory + * Service. + * + * @param className the name of the class. + * @param loader the class loader. + * @param params an array containing the parameters of the constructor. + * @param signature an array containing the signature of the constructor. + * @return the instance. + * @throws PoolException if recycling fails. + */ + public Object getInstance(String className, ClassLoader loader, Object[] params, String[] signature) + throws PoolException { + try { + Object instance = pollInstance(className, params, signature); + return instance == null ? getFactory().getInstance(className, loader, params, signature) : instance; + } catch (FactoryException fe) { + throw new PoolException(fe); + } + } + + /** + * Tests if specified class loaders are supported for a named class. + * + * @param className the name of the class. + * @return true if class loaders are supported, false otherwise. + * @throws FactoryException if test fails. + */ + public boolean isLoaderSupported(String className) throws FactoryException { + return getFactory().isLoaderSupported(className); + } + + /** + * Gets an instance of a specified class either from the pool or by instatiating + * from the class if the pool is empty. + * + * @param clazz the class. + * @return the instance. + * @throws PoolException if recycling fails. + */ + @SuppressWarnings("unchecked") + public Object getInstance(Class clazz) throws PoolException { + try { + Object instance = pollInstance(clazz.getName(), null, null); + return instance == null ? factoryService.getInstance(clazz) : instance; + } catch (FactoryException fe) { + throw new PoolException(fe); + } + } + + /** + * Gets an instance of a specified class either from the pool or by instatiating + * from the class if the pool is empty. + * + * @param clazz the class. + * @param params an array containing the parameters of the constructor. + * @param signature an array containing the signature of the constructor. + * @return the instance. + * @throws PoolException if recycling fails. + */ + public Object getInstance(Class clazz, Object params[], String signature[]) throws PoolException { + + try { + Object instance = pollInstance(clazz.getName(), params, signature); + // FactoryService fs = getFactory(); + + // TODO There is a whacky .toString() on the clazz object, + // but otherwise it won't compile + return instance == null ? getFactory().getInstance(clazz.toString(), params, signature) : instance; + + } catch (FactoryException fe) { + throw new PoolException(fe); + } + } + + /** + * Puts a used object back to the pool. Objects implementing the Recyclable + * interface can provide a recycle method to be called when they are reused and + * a dispose method to be called when they are returned to the pool. + * + * @param instance the object instance to recycle. + * @return true if the instance was accepted. + */ + @SuppressWarnings("unchecked") + public boolean putInstance(Object instance) { + if (instance != null) { + HashMap<String, PoolBuffer> repository = poolRepository; + String className = instance.getClass().getName(); + PoolBuffer pool = (PoolBuffer) repository.get(className); + if (pool == null) { + pool = new PoolBuffer(getCapacity(className)); + repository = (HashMap<String, PoolBuffer>) repository.clone(); + repository.put(className, pool); + poolRepository = repository; + if (instance instanceof ArrayCtorRecyclable) { + pool.setArrayCtorRecyclable(true); + } + } + return pool.offer(instance); + } else { + return false; + } + } + + /** + * Gets the capacity of the pool for a named class. + * + * @param className the name of the class. + */ + public int getCapacity(String className) { + PoolBuffer pool = (PoolBuffer) poolRepository.get(className); + if (pool == null) { + /* Check class specific capacity. */ + int capacity = poolCapacity; + if (capacityMap != null) { + Integer cap = (Integer) capacityMap.get(className); + if (cap != null) { + capacity = cap.intValue(); + } + } + return capacity; + } else { + return pool.capacity(); + } + } + + /** + * Sets the capacity of the pool for a named class. Note that the pool will be + * cleared after the change. + * + * @param className the name of the class. + * @param capacity the new capacity. + */ + @SuppressWarnings("unchecked") + public void setCapacity(String className, int capacity) { + HashMap<String, PoolBuffer> repository = poolRepository; + repository = repository != null ? (HashMap<String, PoolBuffer>) repository.clone() + : new HashMap<String, PoolBuffer>(); + repository.put(className, new PoolBuffer(capacity)); + poolRepository = repository; + } + + /** + * Gets the current size of the pool for a named class. + * + * @param className the name of the class. + */ + public int getSize(String className) { + PoolBuffer pool = (PoolBuffer) poolRepository.get(className); + return pool != null ? pool.size() : 0; + } + + /** + * Clears instances of a named class from the pool. + * + * @param className the name of the class. + */ + @SuppressWarnings("unchecked") + public void clearPool(String className) { + HashMap<String, PoolBuffer> repository = poolRepository; + if (repository.get(className) != null) { + repository = (HashMap<String, PoolBuffer>) repository.clone(); + repository.remove(className); + poolRepository = repository; + } + } + + /** + * Clears all instances from the pool. + */ + public void clearPool() { + poolRepository = new HashMap<String, PoolBuffer>(); + } + + /** + * Polls and recycles an object of the named class from the pool. + * + * @param className the name of the class. + * @param params an array containing the parameters of the constructor. + * @param signature an array containing the signature of the constructor. + * @return the object or null. + * @throws PoolException if recycling fails. + */ + private Object pollInstance(String className, Object[] params, String[] signature) throws PoolException { + PoolBuffer pool = (PoolBuffer) poolRepository.get(className); + return pool != null ? pool.poll(params, signature, factoryService) : null; + } + + /** + * Gets the factory service. + * + * @return the factory service. + */ + protected FactoryService getFactory() { + return factoryService; + } + + // ---------------- Avalon Lifecycle Methods --------------------- + /** + * Avalon component lifecycle method + */ + public void configure(Configuration conf) { + final Configuration capacities = conf.getChild(POOL_CAPACITY, false); + if (capacities != null) { + Configuration defaultConf = capacities.getChild("default"); + int capacity = defaultConf.getValueAsInteger(DEFAULT_POOL_CAPACITY); + if (capacity <= 0) { + throw new IllegalArgumentException("Capacity must be >0"); + } + poolCapacity = capacity; + Configuration[] nameVal = capacities.getChildren(); + for (int i = 0; i < nameVal.length; i++) { + String key = nameVal[i].getName(); + if (!"default".equals(key)) { + capacity = nameVal[i].getValueAsInteger(poolCapacity); + if (capacity < 0) { + capacity = poolCapacity; + } + if (capacityMap == null) { + capacityMap = new HashMap<String, Integer>(); + } + capacityMap.put(key, new Integer(capacity)); + } + } + } + } + + /** + * Avalon component lifecycle method + * + * {@link org.apache.fulcrum.factory.FactoryService} + * + * @param manager the service manager + */ + public void service(ServiceManager manager) { + this.manager = manager; + } + + /** + * Avalon component lifecycle method Initializes the service by loading default + * class loaders and customized object factories. + * + * @throws Exception if initialization fails. + */ + public void initialize() throws Exception { + try { + factoryService = (FactoryService) manager.lookup(FactoryService.ROLE); + } catch (Exception e) { + throw new Exception("DefaultPoolService.initialize: Failed to get a Factory object", e); + } + } + + /** + * Avalon component lifecycle method + */ + public void dispose() { + if (factoryService != null) { + manager.release(factoryService); + } + factoryService = null; + manager = null; + } } diff --git a/src/java/org/apache/fulcrum/pool/InitableRecyclable.java b/src/java/org/apache/fulcrum/pool/InitableRecyclable.java index f6683a6..7c78ca3 100644 --- a/src/java/org/apache/fulcrum/pool/InitableRecyclable.java +++ b/src/java/org/apache/fulcrum/pool/InitableRecyclable.java @@ -1,6 +1,5 @@ package org.apache.fulcrum.pool; - /* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file @@ -21,13 +20,10 @@ package org.apache.fulcrum.pool; */ - - - /** * An interface for objects that can be pooled and recycled several times * by different clients. Pooled objects that implement this interface - * use no argument ctor and recycle methods. Initialization is taken + * use no argument or recycle methods. Initialization is taken * care of using the init method. This is a way to avoid * introspection/reflection when pooling an object. * @@ -39,6 +35,9 @@ public interface InitableRecyclable extends Recyclable /** * This method should be called after retrieving the object from * the pool. + * + * @param initObj the object to initialize + * @throws PoolException pool exception if failed to initialize */ public void init(Object initObj) throws PoolException; } diff --git a/src/java/org/apache/fulcrum/pool/PoolBuffer.java b/src/java/org/apache/fulcrum/pool/PoolBuffer.java index 1c1e97d..8269fb1 100644 --- a/src/java/org/apache/fulcrum/pool/PoolBuffer.java +++ b/src/java/org/apache/fulcrum/pool/PoolBuffer.java @@ -64,8 +64,12 @@ public class PoolBuffer } /** * Polls for an instance from the pool. - * @param factoryService - * + * + * + * @param params object paramaters + * @param signature signature of the class + * @param factoryService service to add + * @throws PoolException if service failed to be found * @return an instance or null. */ public Object poll(Object[] params, String[] signature, FactoryService factoryService) throws PoolException @@ -98,7 +102,9 @@ public class PoolBuffer clazz.getMethod( "recycle", factoryService.getSignature(clazz, params, signature)); - ArrayList<Recycler> cache = + + @SuppressWarnings("unchecked") + ArrayList<Recycler> cache = recyclers != null ? (ArrayList<Recycler>) recyclers.clone() : new ArrayList<Recycler>(); cache.add(new Recycler(recycle, signature)); recyclers = cache; @@ -120,10 +126,12 @@ public class PoolBuffer } return instance; } + /** * Offers an instance to the pool. - * + * * @param instance an instance. + * @return false if failed to dispose */ public boolean offer(Object instance) { diff --git a/src/java/org/apache/fulcrum/pool/PoolService.java b/src/java/org/apache/fulcrum/pool/PoolService.java index 2c0d654..6e8ec2c 100644 --- a/src/java/org/apache/fulcrum/pool/PoolService.java +++ b/src/java/org/apache/fulcrum/pool/PoolService.java @@ -90,6 +90,7 @@ public interface PoolService * Gets the capacity of the pool for a named class. * * @param className the name of the class. + * @return total capacity */ public int getCapacity(String className); @@ -106,7 +107,8 @@ public interface PoolService /** * Gets the current size of the pool for a named class. * - * @param className the name of the class. + * @param className the name of the class + * @return the size of the pool for the class */ public int getSize(String className); diff --git a/src/test/org/apache/fulcrum/pool/PoolServiceTest.java b/src/test/org/apache/fulcrum/pool/PoolServiceTest.java index 6130e95..5724e96 100644 --- a/src/test/org/apache/fulcrum/pool/PoolServiceTest.java +++ b/src/test/org/apache/fulcrum/pool/PoolServiceTest.java @@ -25,88 +25,89 @@ import org.apache.fulcrum.testcontainer.BaseUnitTest; * @author Eric Pugh * @author <a href="mailto:[email protected]">Stephen McConnell</a> * - * To change the template for this generated type comment go to - * Window>Preferences>Java>Code Generation>Code and Comments */ -public class PoolServiceTest extends BaseUnitTest -{ - private PoolService poolService = null; - /** - * Defines the testcase name for JUnit. - * - * @param name the testcase's name. - */ - public PoolServiceTest(String name) - { - super(name); - } - - public void setUp() throws Exception - { - super.setUp(); - - poolService = (PoolService) this.resolve( PoolService.class.getName() ); - } - - /* - * Class to test for Object getInstance(Class) - */ - public void testGetInstanceClass() throws PoolException - { - Object object = poolService.getInstance(StringBuilder.class); - assertTrue(object instanceof StringBuilder); - - } - - public void testPutInstance() - { - String s = "I am a string"; - assertEquals(0, poolService.getSize("java.lang.String")); - poolService.putInstance(s); - assertEquals(1, poolService.getSize("java.lang.String")); - - } - public void testGetSetCapacity() - { - assertEquals(128, poolService.getCapacity("java.lang.String")); - poolService.setCapacity("java.lang.String", 278); - assertEquals(278, poolService.getCapacity("java.lang.String")); - - } - public void testGetSize() - { - String s = "I am a string"; - assertEquals(0, poolService.getSize("java.lang.String")); - poolService.putInstance(s); - assertEquals(1, poolService.getSize("java.lang.String")); - - } - /* - * Class to test for void clearPool(String) - */ - public void testClearPoolString() - { - String s = "I am a string"; - assertEquals(0, poolService.getSize("java.lang.String")); - poolService.putInstance(s); - assertEquals(1, poolService.getSize("java.lang.String")); - poolService.clearPool("java.lang.String"); - assertEquals(0, poolService.getSize("java.lang.String")); - - } - /* - * Class to test for void clearPool() - */ - public void testClearPool() - { - String s = "I am a string"; - assertEquals(0, poolService.getSize("java.lang.String")); - poolService.putInstance(s); - poolService.putInstance(new Double(32)); - assertEquals(1, poolService.getSize("java.lang.String")); - poolService.clearPool(); - assertEquals(0, poolService.getSize("java.lang.String")); - assertEquals(0, poolService.getSize("java.lang.Double")); - - } +public class PoolServiceTest extends BaseUnitTest { + private PoolService poolService = null; + + /** + * Defines the testcase name for JUnit. + * + * @param name the testcase's name. + */ + public PoolServiceTest(String name) { + super(name); + } + + /** + * Perform pool service setup + * + * @throws Exception generic exception + */ + public void setUp() throws Exception { + super.setUp(); + + poolService = (PoolService) this.resolve(PoolService.class.getName()); + } + + /** + * Class to test for Object getInstance(Class) + * + * @throws PoolException generic exception + */ + public void testGetInstanceClass() throws PoolException { + Object object = poolService.getInstance(StringBuilder.class); + assertTrue(object instanceof StringBuilder); + + } + + public void testPutInstance() { + String s = "I am a string"; + assertEquals(0, poolService.getSize("java.lang.String")); + poolService.putInstance(s); + assertEquals(1, poolService.getSize("java.lang.String")); + + } + + public void testGetSetCapacity() { + assertEquals(128, poolService.getCapacity("java.lang.String")); + poolService.setCapacity("java.lang.String", 278); + assertEquals(278, poolService.getCapacity("java.lang.String")); + + } + + public void testGetSize() { + String s = "I am a string"; + assertEquals(0, poolService.getSize("java.lang.String")); + poolService.putInstance(s); + assertEquals(1, poolService.getSize("java.lang.String")); + + } + + /* + * Class to test for void clearPool(String) + */ + public void testClearPoolString() { + String s = "I am a string"; + assertEquals(0, poolService.getSize("java.lang.String")); + poolService.putInstance(s); + assertEquals(1, poolService.getSize("java.lang.String")); + poolService.clearPool("java.lang.String"); + assertEquals(0, poolService.getSize("java.lang.String")); + + } + + /* + * Class to test for void clearPool() + */ + public void testClearPool() { + String s = "I am a string"; + assertEquals(0, poolService.getSize("java.lang.String")); + poolService.putInstance(s); + poolService.putInstance(new Double(32)); + assertEquals(1, poolService.getSize("java.lang.String")); + poolService.clearPool(); + assertEquals(0, poolService.getSize("java.lang.String")); + assertEquals(0, poolService.getSize("java.lang.Double")); + + } }
