You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@turbine.apache.org by pa...@apache.org on 2018/12/13 21:29:03 UTC
svn commit: r1848900 - in /turbine/fulcrum/trunk/pool:
src/java/org/apache/fulcrum/pool/ xdocs/
Author: painter
Date: Thu Dec 13 21:29:02 2018
New Revision: 1848900
URL: http://svn.apache.org/viewvc?rev=1848900&view=rev
Log:
Code cleanup, fixed last findbug performance error, updated changes.xml
Modified:
turbine/fulcrum/trunk/pool/src/java/org/apache/fulcrum/pool/DefaultPoolService.java
turbine/fulcrum/trunk/pool/src/java/org/apache/fulcrum/pool/PoolBuffer.java
turbine/fulcrum/trunk/pool/src/java/org/apache/fulcrum/pool/PoolException.java
turbine/fulcrum/trunk/pool/src/java/org/apache/fulcrum/pool/PoolService.java
turbine/fulcrum/trunk/pool/src/java/org/apache/fulcrum/pool/Recyclable.java
turbine/fulcrum/trunk/pool/src/java/org/apache/fulcrum/pool/Recycler.java
turbine/fulcrum/trunk/pool/xdocs/changes.xml
Modified: turbine/fulcrum/trunk/pool/src/java/org/apache/fulcrum/pool/DefaultPoolService.java
URL: http://svn.apache.org/viewvc/turbine/fulcrum/trunk/pool/src/java/org/apache/fulcrum/pool/DefaultPoolService.java?rev=1848900&r1=1848899&r2=1848900&view=diff
==============================================================================
--- turbine/fulcrum/trunk/pool/src/java/org/apache/fulcrum/pool/DefaultPoolService.java (original)
+++ turbine/fulcrum/trunk/pool/src/java/org/apache/fulcrum/pool/DefaultPoolService.java Thu Dec 13 21:29:02 2018
@@ -56,10 +56,12 @@ public class DefaultPoolService extends
* 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.
*/
@@ -326,8 +328,9 @@ public class DefaultPoolService extends
}
// ---------------- Avalon Lifecycle Methods ---------------------
- /**
+ /* (non-Javadoc)
* Avalon component lifecycle method
+ * @see org.apache.avalon.framework.configuration.Configurable#configure(org.apache.avalon.framework.configuration.Configuration)
*/
public void configure(Configuration conf) {
final Configuration capacities = conf.getChild(POOL_CAPACITY, false);
@@ -349,7 +352,7 @@ public class DefaultPoolService extends
if (capacityMap == null) {
capacityMap = new HashMap<String, Integer>();
}
- capacityMap.put(key, new Integer(capacity));
+ capacityMap.put(key, capacity);
}
}
}
Modified: turbine/fulcrum/trunk/pool/src/java/org/apache/fulcrum/pool/PoolBuffer.java
URL: http://svn.apache.org/viewvc/turbine/fulcrum/trunk/pool/src/java/org/apache/fulcrum/pool/PoolBuffer.java?rev=1848900&r1=1848899&r2=1848900&view=diff
==============================================================================
--- turbine/fulcrum/trunk/pool/src/java/org/apache/fulcrum/pool/PoolBuffer.java (original)
+++ turbine/fulcrum/trunk/pool/src/java/org/apache/fulcrum/pool/PoolBuffer.java Thu Dec 13 21:29:02 2018
@@ -21,173 +21,149 @@ package org.apache.fulcrum.pool;
import java.lang.reflect.Method;
import java.util.ArrayList;
-import java.util.Iterator;
import org.apache.fulcrum.factory.FactoryService;
-
/**
* An inner class for class specific pools.
*/
-public class PoolBuffer
-{
- /**
- * A buffer for class instances.
- */
- private BoundedBuffer pool;
- /**
- * A flag to determine if a more efficient recycler is implemented.
- */
- private boolean arrayCtorRecyclable;
- /**
- * A cache for recycling methods.
- */
- private ArrayList<Recycler> recyclers;
- /**
- * Contructs a new pool buffer with a specific capacity.
- *
- * @param capacity a capacity.
- */
- public PoolBuffer(int capacity)
- {
- pool = new BoundedBuffer(capacity);
- }
- /**
- * Tells pool that it contains objects which can be
- * initialized using an Object array.
- *
- * @param isArrayCtor a <code>boolean</code> value
- */
- public void setArrayCtorRecyclable(boolean isArrayCtor)
- {
- arrayCtorRecyclable = isArrayCtor;
- }
- /**
- * Polls for an instance from the pool.
- *
- *
- * @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
- {
- Object instance = pool.poll();
- if (instance != null)
- {
- if (arrayCtorRecyclable)
- {
- ((ArrayCtorRecyclable) instance).recycle(params);
- }
- else if (instance instanceof Recyclable)
- {
- try
- {
- if ((signature != null) && (signature.length > 0))
- {
- /* Get the recycle method from the cache. */
- Method recycle = getRecycle(signature);
- if (recycle == null)
- {
- synchronized (this)
- {
- /* Make a synchronized recheck. */
- recycle = getRecycle(signature);
- if (recycle == null)
- {
- Class<? extends Object> clazz = instance.getClass();
- recycle =
- clazz.getMethod(
- "recycle",
- factoryService.getSignature(clazz, params, signature));
-
- @SuppressWarnings("unchecked")
- ArrayList<Recycler> cache =
- recyclers != null ? (ArrayList<Recycler>) recyclers.clone() : new ArrayList<Recycler>();
- cache.add(new Recycler(recycle, signature));
- recyclers = cache;
- }
- }
- }
- recycle.invoke(instance, params);
- }
- else
- {
- ((Recyclable) instance).recycle();
- }
- }
- catch (Exception x)
- {
- throw new PoolException("Recycling failed for " + instance.getClass().getName(), x);
- }
- }
- }
- return instance;
- }
-
- /**
- * Offers an instance to the pool.
- *
- * @param instance an instance.
- * @return false if failed to dispose
- */
- public boolean offer(Object instance)
- {
- if (instance instanceof Recyclable)
- {
- try
- {
- ((Recyclable) instance).dispose();
- }
- catch (Exception x)
- {
- return false;
- }
- }
- return pool.offer(instance);
- }
- /**
- * Returns the capacity of the pool.
- *
- * @return the capacity.
- */
- public int capacity()
- {
- return pool.capacity();
- }
- /**
- * Returns the size of the pool.
- *
- * @return the size.
- */
- public int size()
- {
- return pool.size();
- }
- /**
- * Returns a cached recycle method
- * corresponding to the given signature.
- *
- * @param signature the signature.
- * @return the recycle method or null.
- */
- private Method getRecycle(String[] signature)
- {
- ArrayList<Recycler> cache = recyclers;
- if (cache != null)
- {
- Method recycle;
- for (Iterator<Recycler> i = cache.iterator(); i.hasNext();)
- {
- recycle = ((Recycler) i.next()).match(signature);
- if (recycle != null)
- {
- return recycle;
- }
- }
- }
- return null;
- }
+public class PoolBuffer {
+
+ /**
+ * A buffer for class instances.
+ */
+ private BoundedBuffer pool;
+
+ /**
+ * A flag to determine if a more efficient recycler is implemented.
+ */
+ private boolean arrayCtorRecyclable;
+
+ /**
+ * A cache for recycling methods.
+ */
+ private ArrayList<Recycler> recyclers;
+
+ /**
+ * Contructs a new pool buffer with a specific capacity.
+ *
+ * @param capacity a capacity.
+ */
+ public PoolBuffer(int capacity) {
+ pool = new BoundedBuffer(capacity);
+ }
+
+ /**
+ * Tells pool that it contains objects which can be initialized using an Object
+ * array.
+ *
+ * @param isArrayCtor a <code>boolean</code> value
+ */
+ public void setArrayCtorRecyclable(boolean isArrayCtor) {
+ arrayCtorRecyclable = isArrayCtor;
+ }
+
+ /**
+ * Polls for an instance from the pool.
+ *
+ *
+ * @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 {
+ Object instance = pool.poll();
+ if (instance != null) {
+ if (arrayCtorRecyclable) {
+ ((ArrayCtorRecyclable) instance).recycle(params);
+ } else if (instance instanceof Recyclable) {
+ try {
+ if (signature != null && signature.length > 0) {
+ /* Get the recycle method from the cache. */
+ Method recycle = getRecycle(signature);
+ if (recycle == null) {
+ synchronized (this) {
+ /* Make a synchronized recheck. */
+ recycle = getRecycle(signature);
+ if (recycle == null) {
+ Class<? extends Object> clazz = instance.getClass();
+ recycle = clazz.getMethod("recycle",
+ factoryService.getSignature(clazz, params, signature));
+
+ @SuppressWarnings("unchecked")
+ ArrayList<Recycler> cache = recyclers != null
+ ? (ArrayList<Recycler>) recyclers.clone()
+ : new ArrayList<Recycler>();
+ cache.add(new Recycler(recycle, signature));
+ recyclers = cache;
+ }
+ }
+ }
+ recycle.invoke(instance, params);
+ } else {
+ ((Recyclable) instance).recycle();
+ }
+ } catch (Exception x) {
+ throw new PoolException("Recycling failed for " + instance.getClass().getName(), x);
+ }
+ }
+ }
+ return instance;
+ }
+
+ /**
+ * Offers an instance to the pool.
+ *
+ * @param instance an instance.
+ * @return false if failed to dispose
+ */
+ public boolean offer(Object instance) {
+ if (instance instanceof Recyclable) {
+ try {
+ ((Recyclable) instance).dispose();
+ } catch (Exception x) {
+ return false;
+ }
+ }
+ return pool.offer(instance);
+ }
+
+ /**
+ * Returns the capacity of the pool.
+ *
+ * @return the capacity.
+ */
+ public int capacity() {
+ return pool.capacity();
+ }
+
+ /**
+ * Returns the size of the pool.
+ *
+ * @return the size.
+ */
+ public int size() {
+ return pool.size();
+ }
+
+ /**
+ * Returns a cached recycle method corresponding to the given signature.
+ *
+ * @param signature the signature.
+ * @return the recycle method or null.
+ */
+ private Method getRecycle(String[] signature) {
+ ArrayList<Recycler> cache = recyclers;
+ if (cache != null) {
+ Method recycle;
+ for (Recycler recycler : cache) {
+ recycle = recycler.match(signature);
+ if (recycle != null)
+ return recycle;
+ }
+ }
+ return null;
+ }
}
Modified: turbine/fulcrum/trunk/pool/src/java/org/apache/fulcrum/pool/PoolException.java
URL: http://svn.apache.org/viewvc/turbine/fulcrum/trunk/pool/src/java/org/apache/fulcrum/pool/PoolException.java?rev=1848900&r1=1848899&r2=1848900&view=diff
==============================================================================
--- turbine/fulcrum/trunk/pool/src/java/org/apache/fulcrum/pool/PoolException.java (original)
+++ turbine/fulcrum/trunk/pool/src/java/org/apache/fulcrum/pool/PoolException.java Thu Dec 13 21:29:02 2018
@@ -25,23 +25,22 @@ package org.apache.fulcrum.pool;
* @author <a href="mailto:epugh@upstate.com">Eric Pugh</a>
* @version $Id$
*/
-public class PoolException extends Exception
-{
- /**
- * Serial number
- */
- private static final long serialVersionUID = 8192045560423973532L;
+public class PoolException extends Exception {
+
+ /**
+ * Serial number
+ */
+ private static final long serialVersionUID = 8192045560423973532L;
- public PoolException(String msg)
- {
- super(msg);
- }
- public PoolException(Exception ex)
- {
- super(ex);
- }
- public PoolException(String msg, Exception ex)
- {
- super(msg, ex);
- }
+ public PoolException(String msg) {
+ super(msg);
+ }
+
+ public PoolException(Exception ex) {
+ super(ex);
+ }
+
+ public PoolException(String msg, Exception ex) {
+ super(msg, ex);
+ }
}
Modified: turbine/fulcrum/trunk/pool/src/java/org/apache/fulcrum/pool/PoolService.java
URL: http://svn.apache.org/viewvc/turbine/fulcrum/trunk/pool/src/java/org/apache/fulcrum/pool/PoolService.java?rev=1848900&r1=1848899&r2=1848900&view=diff
==============================================================================
--- turbine/fulcrum/trunk/pool/src/java/org/apache/fulcrum/pool/PoolService.java (original)
+++ turbine/fulcrum/trunk/pool/src/java/org/apache/fulcrum/pool/PoolService.java Thu Dec 13 21:29:02 2018
@@ -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
@@ -20,108 +19,98 @@ package org.apache.fulcrum.pool;
* under the License.
*/
-
-
/**
- * 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 object will be instantiated
- * from the specified class. If only class name is given, the request
- * to create an instance will be forwarded to the Factory Service.
+ * 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 object
+ * will be instantiated from the specified class. If only class name is given,
+ * the request to create an instance will be forwarded to the Factory Service.
*
- * <p>For objects implementing the Recyclable interface, a recycle
- * method will be called, when they are taken from the pool, and
- * a dispose method, when they are returned to the pool.
+ * <p>
+ * For objects implementing the Recyclable interface, a recycle method will be
+ * called, when they are taken from the pool, and a dispose method, when they
+ * are returned to the pool.
*
* @author <a href="mailto:ilkka.priha@simsoft.fi">Ilkka Priha</a>
* @author <a href="mailto:mcconnell@apache.org">Stephen McConnell</a>
* @version $Id$
*/
-public interface PoolService
-{
- /** Avalon role - used to id the component within the manager */
- String ROLE = PoolService.class.getName();
-
-
- /**
- * The default pool capacity.
- */
- public static final int DEFAULT_POOL_CAPACITY = 128;
-
- /**
- * Gets an instance of a specified class either from the pool
- * or by instantiating from the class if the pool is empty.
- *
- * @param clazz the class.
- * @return the instance.
- * @throws PoolException if recycling fails.
- */
- public Object getInstance(Class clazz)
- throws PoolException;
-
- /**
- * Gets an instance of a specified class either from the pool
- * or by instantiating 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;
-
- /**
- * 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.
- */
- public boolean putInstance(Object instance);
-
- /**
- * 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);
-
- /**
- * 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.
- */
- public void setCapacity(String className,
- int capacity);
-
- /**
- * Gets the current size of the pool for a named class.
- *
- * @param className the name of the class
- * @return the size of the pool for the class
- */
- public int getSize(String className);
-
- /**
- * Clears instances of a named class from the pool.
- *
- * @param className the name of the class.
- */
- public void clearPool(String className);
-
- /**
- * Clears all instances from the pool.
- */
- void clearPool();
+public interface PoolService {
+ /** Avalon role - used to id the component within the manager */
+ String ROLE = PoolService.class.getName();
+
+ /**
+ * The default pool capacity.
+ */
+ public static final int DEFAULT_POOL_CAPACITY = 128;
+
+ /**
+ * Gets an instance of a specified class either from the pool or by
+ * instantiating from the class if the pool is empty.
+ *
+ * @param clazz the class.
+ * @return the instance.
+ * @throws PoolException if recycling fails.
+ */
+ public Object getInstance(Class clazz) throws PoolException;
+
+ /**
+ * Gets an instance of a specified class either from the pool or by
+ * instantiating 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;
+
+ /**
+ * 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.
+ */
+ public boolean putInstance(Object instance);
+
+ /**
+ * 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);
+
+ /**
+ * 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.
+ */
+ public void setCapacity(String className, int capacity);
+
+ /**
+ * Gets the current size of the pool for a named class.
+ *
+ * @param className the name of the class
+ * @return the size of the pool for the class
+ */
+ public int getSize(String className);
+
+ /**
+ * Clears instances of a named class from the pool.
+ *
+ * @param className the name of the class.
+ */
+ public void clearPool(String className);
+
+ /**
+ * Clears all instances from the pool.
+ */
+ void clearPool();
}
Modified: turbine/fulcrum/trunk/pool/src/java/org/apache/fulcrum/pool/Recyclable.java
URL: http://svn.apache.org/viewvc/turbine/fulcrum/trunk/pool/src/java/org/apache/fulcrum/pool/Recyclable.java?rev=1848900&r1=1848899&r2=1848900&view=diff
==============================================================================
--- turbine/fulcrum/trunk/pool/src/java/org/apache/fulcrum/pool/Recyclable.java (original)
+++ turbine/fulcrum/trunk/pool/src/java/org/apache/fulcrum/pool/Recyclable.java Thu Dec 13 21:29:02 2018
@@ -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
@@ -20,38 +19,35 @@ package org.apache.fulcrum.pool;
* under the License.
*/
-
/**
- * An interface for objects that can be pooled and
- * recycled several times by different clients.
+ * An interface for objects that can be pooled and recycled several times by
+ * different clients.
*
* @author <a href="mailto:ilkka.priha@simsoft.fi">Ilkka Priha</a>
* @version $Id$
*/
-public interface Recyclable
-{
- /**
- * Recycles the object for a new client. Recycle methods with
- * parameters must be added to implementing object and they will be
- * automatically called by pool implementations when the object is
- * taken from the pool for a new client. The parameters must
- * correspond to the parameters of the constructors of the object.
- * For new objects, constructors can call their corresponding recycle
- * methods whenever applicable.
- * The recycle methods must call their super.
- */
- public void recycle();
-
- /**
- * Disposes the object after use. The method is called
- * when the object is returned to its pool.
- * The dispose method must call its super.
- */
- public void dispose();
-
- /**
- * Checks whether the recyclable has been disposed.
- * @return true, if the recyclable is disposed.
- */
- public boolean isDisposed();
+public interface Recyclable {
+
+ /**
+ * Recycles the object for a new client. Recycle methods with parameters must be
+ * added to implementing object and they will be automatically called by pool
+ * implementations when the object is taken from the pool for a new client. The
+ * parameters must correspond to the parameters of the constructors of the
+ * object. For new objects, constructors can call their corresponding recycle
+ * methods whenever applicable. The recycle methods must call their super.
+ */
+ public void recycle();
+
+ /**
+ * Disposes the object after use. The method is called when the object is
+ * returned to its pool. The dispose method must call its super.
+ */
+ public void dispose();
+
+ /**
+ * Checks whether the recyclable has been disposed.
+ *
+ * @return true, if the recyclable is disposed.
+ */
+ public boolean isDisposed();
}
Modified: turbine/fulcrum/trunk/pool/src/java/org/apache/fulcrum/pool/Recycler.java
URL: http://svn.apache.org/viewvc/turbine/fulcrum/trunk/pool/src/java/org/apache/fulcrum/pool/Recycler.java?rev=1848900&r1=1848899&r2=1848900&view=diff
==============================================================================
--- turbine/fulcrum/trunk/pool/src/java/org/apache/fulcrum/pool/Recycler.java (original)
+++ turbine/fulcrum/trunk/pool/src/java/org/apache/fulcrum/pool/Recycler.java Thu Dec 13 21:29:02 2018
@@ -20,6 +20,7 @@ package org.apache.fulcrum.pool;
*/
import java.lang.reflect.Method;
+import java.util.Arrays;
/**
* An inner class for cached recycle methods.
@@ -30,10 +31,12 @@ public class Recycler
* The method.
*/
private final Method recycle;
+
/**
* The signature.
*/
private final String[] signature;
+
/**
* Constructs a new recycler.
*
@@ -43,8 +46,9 @@ public class Recycler
public Recycler(Method rec, String[] sign)
{
recycle = rec;
- signature = (sign != null) && (sign.length > 0) ? sign : null;
+ signature = sign != null && sign.length > 0 ? sign : null;
}
+
/**
* Matches the given signature against
* that of the recycle method of this recycler.
@@ -54,31 +58,17 @@ public class Recycler
*/
public Method match(String[] sign)
{
- if ((sign != null) && (sign.length > 0))
- {
- if ((signature != null) && (sign.length == signature.length))
- {
- for (int i = 0; i < signature.length; i++)
- {
- if (!signature[i].equals(sign[i]))
- {
- return null;
- }
- }
- return recycle;
- }
- else
- {
- return null;
- }
- }
- else if (signature == null)
- {
- return recycle;
- }
- else
- {
- return null;
- }
+ if ( signature == null )
+ {
+ return recycle;
+ } else {
+
+ // test if there is a match
+ if ( !Arrays.equals(sign, signature) ) {
+ return null;
+ } else {
+ return recycle;
+ }
+ }
}
}
Modified: turbine/fulcrum/trunk/pool/xdocs/changes.xml
URL: http://svn.apache.org/viewvc/turbine/fulcrum/trunk/pool/xdocs/changes.xml?rev=1848900&r1=1848899&r2=1848900&view=diff
==============================================================================
--- turbine/fulcrum/trunk/pool/xdocs/changes.xml (original)
+++ turbine/fulcrum/trunk/pool/xdocs/changes.xml Thu Dec 13 21:29:02 2018
@@ -24,6 +24,11 @@
</properties>
<body>
+ <release version="1.0.5" date="in SVN">
+ <action dev="painter" type="update">
+ Derive from Turbine parent POM 5
+ </action>
+ </release>
<release version="1.0.4" date="2009-06-21">
<action dev="tv" type="add">
Added a Maven-2 build