You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@turbine.apache.org by he...@apache.org on 2003/04/10 00:12:41 UTC

cvs commit: jakarta-turbine-2/src/java/org/apache/turbine/services/pool PoolService.java TurbinePoolService.java

henning     2003/04/09 15:12:37

  Modified:    src/java/org/apache/turbine/services/pool PoolService.java
                        TurbinePoolService.java
  Log:
  Rework the pool service to decouple it from the factory (it no longer
  extends the factory but wraps a factory object.
  
  Add debugging code to be able to simulate pool filling without actual
  pressure from the application (the pool is filled up to half its capacity
  before recycling starts). Finds lots of bugs. Controlled by the
  
  services.PoolService.pool.debug = true
  
  property in TR.props
  
  reworked the init code to be cleaner and less error prone (parse
  configuration only at init time)
  
  Revision  Changes    Path
  1.7       +87 -7     jakarta-turbine-2/src/java/org/apache/turbine/services/pool/PoolService.java
  
  Index: PoolService.java
  ===================================================================
  RCS file: /home/cvs/jakarta-turbine-2/src/java/org/apache/turbine/services/pool/PoolService.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- PoolService.java	7 Apr 2003 15:34:53 -0000	1.6
  +++ PoolService.java	9 Apr 2003 22:12:35 -0000	1.7
  @@ -54,7 +54,7 @@
    * <http://www.apache.org/>.
    */
   
  -import org.apache.turbine.services.factory.FactoryService;
  +import org.apache.turbine.services.Service;
   import org.apache.turbine.util.TurbineException;
   
   /**
  @@ -72,17 +72,96 @@
    * @author <a href="mailto:ilkka.priha@simsoft.fi">Ilkka Priha</a>
    * @version $Id$
    */
  -public interface PoolService extends FactoryService
  +public interface PoolService
  +        extends Service
   {
  +    /** The key under which this service is stored in TurbineServices. */
  +    String SERVICE_NAME = "PoolService";
  +    
  +    /** The default pool capacity. */
  +    int DEFAULT_POOL_CAPACITY = 128;
  +    
  +    /** The name of the pool capacity property */
  +    String POOL_CAPACITY_KEY = "pool.capacity";
  +    
  +    /** Are we running in debug mode? */
  +    String POOL_DEBUG_KEY = "pool.debug";
  +
  +    /** Default Value for debug mode */
  +    boolean POOL_DEBUG_DEFAULT = false;
  +
       /**
  -     * The key under which this service is stored in TurbineServices.
  +     * Gets an instance of a named class.
  +     *
  +     * @param className the name of the class.
  +     * @return the instance.
  +     * @throws TurbineException if instantiation fails.
        */
  -    String SERVICE_NAME = "PoolService";
  +    Object getInstance(String className)
  +            throws TurbineException;
   
       /**
  -     * The default pool capacity.
  +     * Gets an instance of a named class using a specified class loader.
  +     *
  +     * <p>Class loaders are supported only if the isLoaderSupported
  +     * method returns true. Otherwise the loader parameter is ignored.
  +     *
  +     * @param className the name of the class.
  +     * @param loader the class loader.
  +     * @return the instance.
  +     * @throws TurbineException if instantiation fails.
        */
  -    int DEFAULT_POOL_CAPACITY = 128;
  +    Object getInstance(String className,
  +            ClassLoader loader)
  +            throws TurbineException;
  +
  +    /**
  +     * Gets an instance of a named class.
  +     * 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 TurbineException if instantiation fails.
  +     */
  +    Object getInstance(String className,
  +            Object[] params,
  +            String[] signature)
  +            throws TurbineException;
  +
  +    /**
  +     * Gets an instance of a named class using a specified class loader.
  +     * Parameters for its constructor are given as an array of objects,
  +     * primitive types must be wrapped with a corresponding class.
  +     *
  +     * <p>Class loaders are supported only if the isLoaderSupported
  +     * method returns true. Otherwise the loader parameter is ignored.
  +     *
  +     * @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 TurbineException if instantiation fails.
  +     */
  +    Object getInstance(String className,
  +            ClassLoader loader,
  +            Object[] params,
  +            String[] signature)
  +            throws TurbineException;
  +
  +    /**
  +     * 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 TurbineException if test fails.
  +     * @deprecated Use TurbineFactory.isLoaderSupported(className)
  +     */
  +    boolean isLoaderSupported(String className)
  +            throws TurbineException;
   
       /**
        * Gets an instance of a specified class either from the pool
  @@ -156,4 +235,5 @@
        * Clears all instances from the pool.
        */
       void clearPool();
  +
   }
  
  
  
  1.8       +95 -87    jakarta-turbine-2/src/java/org/apache/turbine/services/pool/TurbinePoolService.java
  
  Index: TurbinePoolService.java
  ===================================================================
  RCS file: /home/cvs/jakarta-turbine-2/src/java/org/apache/turbine/services/pool/TurbinePoolService.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- TurbinePoolService.java	7 Apr 2003 15:30:51 -0000	1.7
  +++ TurbinePoolService.java	9 Apr 2003 22:12:36 -0000	1.8
  @@ -62,10 +62,14 @@
   
   import org.apache.commons.configuration.Configuration;
   
  +import org.apache.commons.logging.Log;
  +import org.apache.commons.logging.LogFactory;
  +
   import org.apache.turbine.services.InitializationException;
  +import org.apache.turbine.services.TurbineBaseService;
   import org.apache.turbine.services.TurbineServices;
   import org.apache.turbine.services.factory.FactoryService;
  -import org.apache.turbine.services.factory.TurbineFactoryService;
  +import org.apache.turbine.services.factory.TurbineFactory;
   import org.apache.turbine.util.TurbineException;
   import org.apache.turbine.util.pool.ArrayCtorRecyclable;
   import org.apache.turbine.util.pool.BoundedBuffer;
  @@ -83,16 +87,21 @@
    * 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:hps@intermeta.de">Henning P. Schmiedehausen</a>
    * @version $Id$
    */
   public class TurbinePoolService
  -        extends TurbineFactoryService
  +        extends TurbineBaseService
           implements PoolService
   {
  -    /**
  -     * The property specifying the pool capacity.
  -     */
  -    public static final String POOL_CAPACITY = "pool.capacity";
  +    /** Are we currently debugging the pool recycling? */
  +    private boolean debugPool = false;
  +
  +    /** Internal Reference to the Factory */
  +    private FactoryService factoryService;
  +
  +    /** Logging */
  +    private static Log log = LogFactory.getLog(TurbinePoolService.class);
   
       /**
        * An inner class for class specific pools.
  @@ -104,14 +113,10 @@
            */
           private class Recycler
           {
  -            /**
  -             * The method.
  -             */
  +            /** The recycle method. */
               private final Method recycle;
   
  -            /**
  -             * The signature.
  -             */
  +            /** The signature. */
               private final String[] signature;
   
               /**
  @@ -123,7 +128,8 @@
               public Recycler(Method rec, String[] sign)
               {
                   recycle = rec;
  -                signature = (sign != null) && (sign.length > 0) ? sign : null;
  +                signature = ((sign != null) && (sign.length > 0))
  +                        ? sign : null;
               }
   
               /**
  @@ -165,23 +171,17 @@
               }
           }
   
  -        /**
  -         * A buffer for class instances.
  -         */
  +        /** A buffer for class instances. */
           private BoundedBuffer pool;
   
  -        /**
  -         * A flag to determine if a more efficient recycler is implemented.
  -         */
  +        /** A flag to determine if a more efficient recycler is implemented. */
           private boolean arrayCtorRecyclable;
   
  -        /**
  -         * A cache for recycling methods.
  -         */
  +        /** A cache for recycling methods. */
           private ArrayList recyclers;
   
           /**
  -         * Contructs a new pool buffer with a specific capacity.
  +         * Constructs a new pool buffer with a specific capacity.
            *
            * @param capacity a capacity.
            */
  @@ -209,6 +209,16 @@
           public Object poll(Object[] params, String[] signature)
                   throws TurbineException
           {
  +            // If we're debugging the recycling code, we want different
  +            // objects to be used when pulling from the pool. Ensure that
  +            // each pool fills up at least to half its capacity.
  +            if (debugPool && (pool.size() < (pool.capacity() / 2)))
  +            {
  +                log.debug("Size: " + pool.size() 
  +                        + ", capacity: " + pool.capacity());
  +                return null;
  +            }
  +
               Object instance = pool.poll();
               if (instance != null)
               {
  @@ -234,11 +244,11 @@
                                       {
                                           Class clazz = instance.getClass();
                                           recycle = clazz.getMethod("recycle",
  -                                                TurbinePoolService.this.getSignature(
  +                                                factoryService.getSignature(
                                                           clazz, params, signature));
  -                                        ArrayList cache = recyclers != null ?
  -                                                (ArrayList) recyclers.clone() :
  -                                                new ArrayList();
  +                                        ArrayList cache = recyclers != null
  +                                                ? (ArrayList) recyclers.clone()
  +                                                : new ArrayList();
                                           cache.add(
                                                   new Recycler(recycle, signature));
                                           recyclers = cache;
  @@ -356,23 +366,32 @@
               throws InitializationException
       {
           Configuration conf = getConfiguration();
  -        if (conf != null)
  +
  +        int capacity = conf.getInt(POOL_CAPACITY_KEY,
  +                DEFAULT_POOL_CAPACITY);
  +
  +        if (capacity <= 0)
           {
  -            try
  -            {
  -                int capacity = conf.getInt(POOL_CAPACITY, DEFAULT_POOL_CAPACITY);
  -                if (capacity <= 0)
  -                {
  -                    throw new IllegalArgumentException("Capacity must be >0");
  -                }
  -                poolCapacity = capacity;
  -            }
  -            catch (Exception x)
  -            {
  -                throw new InitializationException(
  -                        "Failed to initialize TurbinePoolService", x);
  -            }
  +            throw new IllegalArgumentException("Capacity must be >0");
  +        }
  +        poolCapacity = capacity;
  +
  +        debugPool = conf.getBoolean(POOL_DEBUG_KEY,
  +                POOL_DEBUG_DEFAULT);
  +        
  +        if (debugPool)
  +        {
  +            log.info("Activated Pool Debugging!");
  +        }
  +
  +        factoryService = TurbineFactory.getService();
  +        
  +        if (factoryService == null)
  +        {
  +            throw new InitializationException("Factory Service is not configured"
  +                    + " but required for the Pool Service!");
           }
  +        
           setInit(true);
       }
   
  @@ -388,8 +407,9 @@
               throws TurbineException
       {
           Object instance = pollInstance(className, null, null);
  -        return instance == null ?
  -                getFactory().getInstance(className) : instance;
  +        return (instance == null)
  +                ? factoryService.getInstance(className)
  +                : instance;
       }
   
       /**
  @@ -407,8 +427,9 @@
               throws TurbineException
       {
           Object instance = pollInstance(className, null, null);
  -        return instance == null ?
  -                getFactory().getInstance(className, loader) : instance;
  +        return (instance == null)
  +                ? factoryService.getInstance(className, loader)
  +                : instance;
       }
   
       /**
  @@ -430,8 +451,9 @@
               throws TurbineException
       {
           Object instance = pollInstance(className, params, signature);
  -        return instance == null ?
  -                getFactory().getInstance(className, params, signature) : instance;
  +        return (instance == null)
  +                ? factoryService.getInstance(className, params, signature)
  +                : instance;
       }
   
       /**
  @@ -455,8 +477,9 @@
               throws TurbineException
       {
           Object instance = pollInstance(className, params, signature);
  -        return instance == null ?
  -                getFactory().getInstance(className, loader, params, signature) : instance;
  +        return (instance == null)
  +                ? factoryService.getInstance(className, loader, params, signature)
  +                : instance;
       }
   
       /**
  @@ -465,11 +488,12 @@
        * @param className the name of the class.
        * @return true if class loaders are supported, false otherwise.
        * @throws TurbineException if test fails.
  +     * @deprecated Use TurbineFactory.isLoaderSupported(className);
        */
       public boolean isLoaderSupported(String className)
               throws TurbineException
       {
  -        return getFactory().isLoaderSupported(className);
  +        return factoryService.isLoaderSupported(className);
       }
   
       /**
  @@ -484,8 +508,9 @@
               throws TurbineException
       {
           Object instance = pollInstance(clazz.getName(), null, null);
  -        return instance == null ?
  -                super.getInstance(clazz) : instance;
  +        return (instance == null)
  +                ? factoryService.getInstance(clazz.getName()) 
  +                : instance;
       }
   
       /**
  @@ -504,8 +529,9 @@
               throws TurbineException
       {
           Object instance = pollInstance(clazz.getName(), params, signature);
  -        return instance == null ?
  -                super.getInstance(clazz, params, signature) : instance;
  +        return (instance == null)
  +                ? factoryService.getInstance(clazz.getName(), params, signature)
  +                : instance;
       }
   
       /**
  @@ -555,23 +581,13 @@
           if (pool == null)
           {
               /* Check class specific capacity. */
  -            int capacity = poolCapacity;
  +            int capacity;
  +
               Configuration conf = getConfiguration();
  -            if (conf != null)
  -            {
  -                try
  -                {
  -                    capacity = conf.getInt(
  -                            POOL_CAPACITY + '.' + className, poolCapacity);
  -                    if (capacity <= 0)
  -                    {
  -                        capacity = poolCapacity;
  -                    }
  -                }
  -                catch (Exception x)
  -                {
  -                }
  -            }
  +            capacity = conf.getInt(
  +                    POOL_CAPACITY_KEY + '.' + className,
  +                    poolCapacity);
  +            capacity = (capacity <= 0) ? poolCapacity : capacity;
               return capacity;
           }
           else
  @@ -591,8 +607,11 @@
                               int capacity)
       {
           HashMap repository = poolRepository;
  -        repository = repository != null ?
  -                (HashMap) repository.clone() : new HashMap();
  +        repository = (repository != null)
  +                ? (HashMap) repository.clone() : new HashMap();
  +        
  +        capacity = (capacity <= 0) ? poolCapacity : capacity;
  +
           repository.put(className, new PoolBuffer(capacity));
           poolRepository = repository;
       }
  @@ -605,7 +624,7 @@
       public int getSize(String className)
       {
           PoolBuffer pool = (PoolBuffer) poolRepository.get(className);
  -        return pool != null ? pool.size() : 0;
  +        return (pool != null) ? pool.size() : 0;
       }
   
       /**
  @@ -647,17 +666,6 @@
               throws TurbineException
       {
           PoolBuffer pool = (PoolBuffer) poolRepository.get(className);
  -        return pool != null ? pool.poll(params, signature) : null;
  -    }
  -
  -    /**
  -     * Gets the factory service.
  -     *
  -     * @return the factory service.
  -     */
  -    private FactoryService getFactory()
  -    {
  -        return (FactoryService) TurbineServices.
  -                getInstance().getService(FactoryService.SERVICE_NAME);
  +        return (pool != null) ? pool.poll(params, signature) : null;
       }
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: turbine-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: turbine-dev-help@jakarta.apache.org