jvanzyl     02/01/14 21:02:32

  Added:       src/java/org/apache/stratum/pool AbstractPoolable.java
                        BoundedBuffer.java DefaultPoolManager.java
                        PoolManager.java Poolable.java
  Log:
  - pool package taken from illka's original code
  
  Revision  Changes    Path
  1.1                  
jakarta-turbine-stratum/src/java/org/apache/stratum/pool/AbstractPoolable.java
  
  Index: AbstractPoolable.java
  ===================================================================
  package org.apache.stratum.pool;
  
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache Turbine" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache Turbine", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  import org.apache.stratum.pool.Poolable;
  
  /**
   * A support class for recyclable objects implementing default methods.
   *
   * @author <a href="mailto:[EMAIL PROTECTED]";>Ilkka Priha</a>
   * @author <a href="mailto:[EMAIL PROTECTED]";>Jason van Zyl</a>
   * @version $Id: AbstractPoolable.java,v 1.1 2002/01/15 05:02:31 jvanzyl Exp $
   */
  public abstract class AbstractPoolable
      implements Poolable
  {
      /**
       * The disposed flag.
       */
      private boolean disposed;
  
      /**
       * Constructs a new recyclable support and calls the default
       * recycle method.
       */
      protected void Poolable()
      {
          recycle();
      }
  
      /**
       * Recycles the object by removing its disposed flag.
       */
      public void recycle()
      {
          disposed = false;
      }
  
      /**
       * Disposes the object by setting its disposed flag.
       */
      public void dispose()
      {
          disposed = true;
      }
  
      /**
       * Checks whether the object is disposed.
       *
       * @return true, if the object is disposed.
       */
      public boolean isDisposed()
      {
          return disposed;
      }
  
      /**
       * A convenience method allowing a clever recyclable object
       * to put itself into a pool for recycling.
       *
       * @return true, if disposal was accepted by the pool.
       */
      protected boolean doDispose()
      {
          try
          {
              // There could be more than one pool so
              // this is not correct.
              //return TurbinePool.putInstance(this);
              return true;
          }
          catch (RuntimeException x)
          {
              return false;
          }
      }
  }
  
  
  
  1.1                  
jakarta-turbine-stratum/src/java/org/apache/stratum/pool/BoundedBuffer.java
  
  Index: BoundedBuffer.java
  ===================================================================
  package org.apache.stratum.pool;
  
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache Turbine" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache Turbine", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  /**
   * Efficient array-based bounded buffer class.
   * Adapted from CPJ, chapter 8, which describes design.
   * Originally written by Doug Lea and released into the public domain.
   * <p>[<a 
href="http://gee.cs.oswego.edu/dl/classes/EDU/oswego/cs/dl/util/concurrent/intro.html";>
 Introduction to this package. </a>] <p>
   *
   * @author <a href="mailto:[EMAIL PROTECTED]";>Ilkka Priha</a>
   * @version $Id: BoundedBuffer.java,v 1.1 2002/01/15 05:02:31 jvanzyl Exp $
   */
  public class BoundedBuffer
  {
      /**
       * The default capacity.
       */
      public static final int DEFAULT_CAPACITY = 1024;
  
      protected final Object[]  array_;      // the elements
  
      protected int takePtr_ = 0;            // circular indices
      protected int putPtr_ = 0;
  
      protected int usedSlots_ = 0;          // length
      protected int emptySlots_;             // capacity - length
  
      /**
       * Creates a buffer with the given capacity.
       *
       * @param capacity the capacity.
       * @throws IllegalArgumentException if capacity less or equal to zero.
       */
      public BoundedBuffer(int capacity)
                           throws IllegalArgumentException
      {
          if (capacity <= 0)
          {
             throw new IllegalArgumentException();
          }           
  
          array_ = new Object[capacity];
          emptySlots_ = capacity;
      }
  
      /**
       * Creates a buffer with the default capacity
       */
      public BoundedBuffer()
      {
          this(DEFAULT_CAPACITY);
      }
  
      /**
       * Returns the number of elements in the buffer.
       * This is only a snapshot value, that may change
       * immediately after returning.
       *
       * @return the size.
       */
      public synchronized int size()
      {
          return usedSlots_;
      }
  
      /**
       * Returns the capacity of the buffer.
       *
       * @return the capacity.
       */
      public int capacity()
      {
          return array_.length;
      }
  
      /**
       * Peeks, but does not remove the top item from the buffer.
       *
       * @return the object or null.
       */
      public synchronized Object peek()
      {
          if (usedSlots_ > 0)
          {
              return array_[takePtr_];
          }            
          else
          {
              return null;
          }            
      }
  
      /**
       * Puts an item in the buffer only if there is capacity available.
       *
       * @param item the item to be inserted.
       * @return true if accepted, else false.
       */
      public synchronized boolean offer(Object x)
      {
          if (x == null)
          {
              throw new IllegalArgumentException();
          }            
  
          if (emptySlots_ > 0)
          {
              --emptySlots_;
              array_[putPtr_] = x;
              if (++putPtr_ >= array_.length)
              {
                  putPtr_ = 0;
              }                
              usedSlots_++;
              return true;
          }
          else
          {
              return false;
          }            
      }
  
      /**
       * Polls and removes the top item from the buffer if one is available.
       *
       * @return the oldest item from the buffer, or null if the buffer is empty.
       */
      public synchronized Object poll()
      {
          if (usedSlots_ > 0)
          {
              --usedSlots_;
              Object old = array_[takePtr_];
              array_[takePtr_] = null;
              if (++takePtr_ >= array_.length)
                  takePtr_ = 0;
              emptySlots_++;
              return old;
          }
          else
          {
              return null;
          }            
      }
  }
  
  
  
  1.1                  
jakarta-turbine-stratum/src/java/org/apache/stratum/pool/DefaultPoolManager.java
  
  Index: DefaultPoolManager.java
  ===================================================================
  package org.apache.stratum.pool;
  
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache Turbine" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache Turbine", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  import java.util.HashMap;
  import java.util.ArrayList;
  import java.util.Iterator;
  import java.lang.reflect.Method;
  import org.apache.stratum.exception.NestableException;
  import org.apache.stratum.factory.DefaultFactoryManager;
  
  /**
   * The PoolManager extends the FactoryManager 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 FactoryManager.
   *
   * <p>For objects implementing the Poolable 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]";>Jason van Zyl</a>
   * @version $Id: DefaultPoolManager.java,v 1.1 2002/01/15 05:02:31 jvanzyl Exp $
   */
  public class DefaultPoolManager
      extends DefaultFactoryManager
      implements PoolManager
  {
      /**
       * The property specifying the pool capacity.
       */
      public static final String POOL_CAPACITY = "pool.capacity";
  
      /**
       * The default capacity of pools.
       */
      private int poolCapacity = DEFAULT_POOL_CAPACITY;
  
      /**
       * The pool repository, one pool for each class.
       */
      private HashMap poolRepository = new HashMap();
  
      /**
       * Constructs a Pool Service.
       */
      public DefaultPoolManager()
      {
      }
  
      /**
       * Initializes the service by setting the pool capacity.
       *
       * @param config initialization configuration.
       * @throws InitializationException if initialization fails.
       */
      public void init()
          throws NestableException
      {
          if (getConfiguration() != null)
          {
              try
              {
                  int capacity =
                      getConfiguration().getInt(POOL_CAPACITY,DEFAULT_POOL_CAPACITY);
  
                  if (capacity <= 0)
                  {
                      throw new IllegalArgumentException("Capacity must be >0");
                  }
                  poolCapacity = capacity;
              }
              catch (Exception x)
              {
                  throw new NestableException(
                      "Failed to initialize TurbinePoolService",x);
              }
          }
      }
  
      /**
       * 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 NestableException if recycling fails.
       */
      public Object getInstance(String className)
          throws NestableException
      {
          Object instance = pollInstance(className,null,null);
          return instance == null ? getInstance(className) : instance;
      }
  
      /**
       * 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 NestableException if recycling fails.
       */
      public Object getInstance(String className,
                                ClassLoader loader)
          throws NestableException
      {
          Object instance = pollInstance(className,null,null);
          return instance == null ? getInstance(className,loader) : instance;
      }
  
      /**
       * 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 NestableException if recycling fails.
       */
      public Object getInstance(String className,
                                Object[] params,
                                String[] signature)
          throws NestableException
      {
          Object instance = pollInstance(className,params,signature);
          return instance == null ? getInstance(className,params,signature) : instance;
      }
  
      /**
       * 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 NestableException if recycling fails.
       */
      public Object getInstance(String className,
                                ClassLoader loader,
                                Object[] params,
                                String[] signature)
          throws NestableException
      {
          Object instance = pollInstance(className,params,signature);
          return instance == null ? getInstance(className,loader,params,signature) : 
instance;
      }
  
      /**
       * 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 NestableException if test fails.
       */
      public boolean isLoaderSupported(String className)
          throws NestableException
      {
          return 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 NestableException if recycling fails.
       */
      public Object getInstance(Class clazz)
          throws NestableException
      {
          Object instance = pollInstance(clazz.getName(),null,null);
          return instance == null ?
              super.getInstance(clazz) : instance;
      }
  
      /**
       * 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 NestableException if recycling fails.
       */
      public Object getInstance(Class clazz,
                                Object params[],
                                String signature[])
          throws NestableException
      {
          Object instance = pollInstance(clazz.getName(),params,signature);
          return instance == null ?
              super.getInstance(clazz,params,signature) : instance;
      }
  
      /**
       * 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)
      {
          if (instance != null)
          {
              HashMap repository = poolRepository;
              String className = instance.getClass().getName();
              PoolBuffer pool = (PoolBuffer) repository.get(className);
              if (pool == null)
              {
                  pool = new PoolBuffer(getCapacity(className));
                  repository = (HashMap) repository.clone();
                  repository.put(className,pool);
                  poolRepository = repository;
              }
              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 (getConfiguration() != null)
              {
                  try
                  {
                      capacity = getConfiguration().getInt(
                          POOL_CAPACITY + '.' + className,poolCapacity);
                      if (capacity <= 0)
                      {
                          capacity = poolCapacity;
                      }
                  }
                  catch (Exception x)
                  {
                  }
              }
              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.
       */
      public void setCapacity(String className,
                              int capacity)
      {
          HashMap repository = poolRepository;
          repository = repository != null ?
              (HashMap) repository.clone() : new HashMap();
          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.
       */
      public void clearPool(String className)
      {
          HashMap repository = poolRepository;
          if (repository.get(className) != null)
          {
              repository = (HashMap) repository.clone();
              repository.remove(className);
              poolRepository = repository;
          }
      }
  
      /**
       * Clears all instances from the pool.
       */
      public void clearPool()
      {
          poolRepository = new HashMap();
      }
  
      /**
       * 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 NestableException if recycling fails.
       */
      private Object pollInstance(String className,
                                  Object[] params,
                                  String[] signature)
          throws NestableException
      {
          PoolBuffer pool = (PoolBuffer) poolRepository.get(className);
          return pool != null ? pool.poll(params,signature) : null;
      }
  
      /**
       * An inner class for class specific pools.
       */
      private class PoolBuffer
      {
          /**
           * An inner class for cached recycle methods.
           */
          private class Recycler
          {
              /**
               * The method.
               */
              private final Method recycle;
  
              /**
               * The signature.
               */
              private final String[] signature;
  
              /**
               * Constructs a new recycler.
               *
               * @param rec the recycle method.
               * @param sign the signature.
               */
              public Recycler(Method rec,
                              String[] sign)
              {
                  recycle = rec;
                  signature = (sign != null) && (sign.length > 0) ? sign : null;
              }
  
              /**
               * Matches the given signature against
               * that of the recycle method of this recycler.
               *
               * @param sign the signature.
               * @return the matching recycle method or null.
               */
              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;
                  }
              }
          }
  
          /**
           * A buffer for class instances.
           */
          private BoundedBuffer pool;
  
          /**
           * A cache for recycling methods.
           */
          private ArrayList recyclers;
  
          /**
           * Contructs a new pool buffer with a specific capacity.
           *
           * @param capacity a capacity.
           */
          public PoolBuffer(int capacity)
          {
              pool = new BoundedBuffer(capacity);
          }
  
          /**
           * Polls for an instance from the pool.
           *
           * @return an instance or null.
           */
          public Object poll(Object[] params,
                             String[] signature)
              throws NestableException
          {
              Object instance = pool.poll();
              if (instance != null)
              {
                  if (instance instanceof Poolable)
                  {
                      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 clazz = instance.getClass();
                                          recycle = clazz.getMethod("recycle",
                                              DefaultPoolManager.this.getSignature(
                                                  clazz,params,signature));
                                          ArrayList cache = recyclers != null ?
                                              (ArrayList) recyclers.clone() :
                                              new ArrayList();
                                          cache.add(
                                              new Recycler(recycle,signature));
                                          recyclers = cache;
                                      }
                                  }
                              }
                              recycle.invoke(instance,params);
                          }
                          else
                          {
                              ((Poolable) instance).recycle();
                          }
                      }
                      catch (Exception x)
                      {
                          throw new NestableException(
                              "Recycling failed for " + 
instance.getClass().getName(),x);
                      }
                  }
              }
              return instance;
          }
  
          /**
           * Offers an instance to the pool.
           *
           * @param instance an instance.
           */
          public boolean offer(Object instance)
          {
              if (instance instanceof Poolable)
              {
                  try
                  {
                      ((Poolable) 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 cache = recyclers;
              if (cache != null)
              {
                  Method recycle;
                  for (Iterator i = cache.iterator(); i.hasNext();)
                  {
                      recycle = ((Recycler) i.next()).match(signature);
                      if (recycle != null)
                      {
                          return recycle;
                      }
                  }
              }
              return null;
          }
      }
  }
  
  
  
  1.1                  
jakarta-turbine-stratum/src/java/org/apache/stratum/pool/PoolManager.java
  
  Index: PoolManager.java
  ===================================================================
  package org.apache.stratum.pool;
  
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache Turbine" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache Turbine", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  import org.apache.stratum.exception.NestableException;
  import org.apache.stratum.factory.FactoryManager;
  
  /**
   * 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 intance 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.
   *
   * @author <a href="mailto:[EMAIL PROTECTED]";>Ilkka Priha</a>
   * @author <a href="mailto:[EMAIL PROTECTED]";>Jason van Zyl</a>
   * @version $Id: PoolManager.java,v 1.1 2002/01/15 05:02:31 jvanzyl Exp $
   */
  public interface PoolManager
      extends FactoryManager
  {
      /**
       * 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 instatiating from the class if the pool is empty.
       *
       * @param clazz the class.
       * @return the instance.
       * @throws NestableException if recycling fails.
       */
      Object getInstance(Class clazz)
          throws NestableException;
  
      /**
       * 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 NestableException if recycling fails.
       */
      Object getInstance(Class clazz,
                                Object params[],
                                String signature[])
          throws NestableException;
  
      /**
       * 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.
       */
      boolean putInstance(Object instance);
  
      /**
       * Gets the capacity of the pool for a named class.
       *
       * @param className the name of the class.
       */
      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.
       */
      void setCapacity(String className,
                              int capacity);
  
      /**
       * Gets the current size of the pool for a named class.
       *
       * @param className the name of the class.
       */
      int getSize(String className);
  
      /**
       * Clears instances of a named class from the pool.
       *
       * @param className the name of the class.
       */
      void clearPool(String className);
  
      /**
       * Clears all instances from the pool.
       */
      void clearPool();
  }
  
  
  
  1.1                  
jakarta-turbine-stratum/src/java/org/apache/stratum/pool/Poolable.java
  
  Index: Poolable.java
  ===================================================================
  package org.apache.stratum.pool;
  
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache Turbine" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache Turbine", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  /**
   * An interface for objects that can be pooled and
   * recycled several times by different clients.
   *
   * @author <a href="mailto:[EMAIL PROTECTED]";>Ilkka Priha</a>
   * @author <a href="mailto:[EMAIL PROTECTED]";>Jason van Zyl</a>
   * @version $Id: Poolable.java,v 1.1 2002/01/15 05:02:31 jvanzyl Exp $
   */
  public interface Poolable
  {
      /**
       * 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.
       */
      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.
       */
      void dispose();
  
      /**
       * Checks whether the recyclable has been disposed.
       * @return true, if the recyclable is disposed.
       */
      boolean isDisposed();
  }
  
  
  

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to