cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cziege...@apache.org
Subject svn commit: r149543 - in cocoon/trunk/src/core/java/org/apache/cocoon/core/container/handler: AbstractComponentHandler.java NewPoolableComponentHandler.java NonThreadSafePoolableComponentHandler.java PoolableComponentHandler.java
Date Wed, 02 Feb 2005 18:38:46 GMT
Author: cziegeler
Date: Wed Feb  2 10:38:45 2005
New Revision: 149543

URL: http://svn.apache.org/viewcvs?view=rev&rev=149543
Log:
Rename handler and subclass to avoid code duplication

Added:
    cocoon/trunk/src/core/java/org/apache/cocoon/core/container/handler/NonThreadSafePoolableComponentHandler.java
      - copied, changed from r149541, cocoon/trunk/src/core/java/org/apache/cocoon/core/container/handler/PoolableComponentHandler.java
Removed:
    cocoon/trunk/src/core/java/org/apache/cocoon/core/container/handler/PoolableComponentHandler.java
Modified:
    cocoon/trunk/src/core/java/org/apache/cocoon/core/container/handler/AbstractComponentHandler.java
    cocoon/trunk/src/core/java/org/apache/cocoon/core/container/handler/NewPoolableComponentHandler.java

Modified: cocoon/trunk/src/core/java/org/apache/cocoon/core/container/handler/AbstractComponentHandler.java
URL: http://svn.apache.org/viewcvs/cocoon/trunk/src/core/java/org/apache/cocoon/core/container/handler/AbstractComponentHandler.java?view=diff&r1=149542&r2=149543
==============================================================================
--- cocoon/trunk/src/core/java/org/apache/cocoon/core/container/handler/AbstractComponentHandler.java
(original)
+++ cocoon/trunk/src/core/java/org/apache/cocoon/core/container/handler/AbstractComponentHandler.java
Wed Feb  2 10:38:45 2005
@@ -131,7 +131,7 @@
         }
 
         if( info.getModel() == ComponentInfo.MODEL_POOLED )  {
-            handler = new PoolableComponentHandler( info, componentEnv.logger, factory, info.getConfiguration()
);
+            handler = new NonThreadSafePoolableComponentHandler( info, componentEnv.logger,
factory, info.getConfiguration() );
         } else if( info.getModel() == ComponentInfo.MODEL_SINGLETON ) {
             handler = new ThreadSafeComponentHandler( info, componentEnv.logger, factory
);
         } else {

Modified: cocoon/trunk/src/core/java/org/apache/cocoon/core/container/handler/NewPoolableComponentHandler.java
URL: http://svn.apache.org/viewcvs/cocoon/trunk/src/core/java/org/apache/cocoon/core/container/handler/NewPoolableComponentHandler.java?view=diff&r1=149542&r2=149543
==============================================================================
--- cocoon/trunk/src/core/java/org/apache/cocoon/core/container/handler/NewPoolableComponentHandler.java
(original)
+++ cocoon/trunk/src/core/java/org/apache/cocoon/core/container/handler/NewPoolableComponentHandler.java
Wed Feb  2 10:38:45 2005
@@ -21,8 +21,6 @@
 import java.lang.reflect.Method;
 import java.lang.reflect.Proxy;
 import java.util.HashSet;
-import java.util.Iterator;
-import java.util.LinkedList;
 import java.util.Set;
 
 import org.apache.avalon.framework.configuration.Configuration;
@@ -61,35 +59,7 @@
  * @version CVS $Id$
  */
 public class NewPoolableComponentHandler
-extends AbstractFactoryHandler {
-    
-    /** The default max size of the pool */
-    public static final int DEFAULT_MAX_POOL_SIZE = 8;
-
-    /**
-     * Object used to synchronize access to the get and put methods
-     */
-    protected final Object semaphore = new Object();
-
-    /**
-     * The maximum size of the pool.
-     */
-    private final int max;
-
-    /**
-     * List of the Poolable instances which are available for use.
-     */
-    private LinkedList ready;
-
-    /**
-     * Store the size of the ready list to optimize operations which require this value.
-     */
-    private int readySize;
-
-    /**
-     * Total number of Poolable instances in the pool
-     */
-    private int size;
+extends NonThreadSafePoolableComponentHandler {
     
     /**
      * Create a PoolableComponentHandler which manages a pool of Components
@@ -104,90 +74,9 @@
                                      final ComponentFactory factory,
                                      final Configuration config )
     throws Exception {
-        super(info, logger, factory);
-
-        final int poolMax = config.getAttributeAsInteger( "pool-max", DEFAULT_MAX_POOL_SIZE
);
-        this.max = ( poolMax <= 0 ? Integer.MAX_VALUE : poolMax );
-
-        // Create the pool lists.
-        this.ready = new LinkedList();
-    }
-
-    /**
-     * Dispose of the ComponentHandler and any associated Pools and Factories.
-     */
-    public void dispose() {
-        super.dispose();
-
-        // Any Poolables in the m_ready list need to be disposed of
-        synchronized( this.semaphore ) {
-            // Remove objects in the ready list.
-            for( Iterator iter = this.ready.iterator(); iter.hasNext(); ) {
-                Object poolable = iter.next();
-                iter.remove();
-                this.readySize--;
-                this.permanentlyRemovePoolable( poolable );
-            }
-
-            if( ( this.size > 0 ) && this.logger.isDebugEnabled() ) {
-                this.logger.debug( "There were " + this.size
-                                   + " outstanding objects when the pool was disposed." );
-            }
-        }
+        super(info, logger, factory, config);
     }
     
-    /**
-     * Permanently removes a poolable from the pool's active list and
-     *  destroys it so that it will not ever be reused.
-     * <p>
-     * This method is only called by threads that have m_semaphore locked.
-     */
-    protected void permanentlyRemovePoolable( Object poolable ) {
-        this.size--;
-        this.decommission( poolable );
-    }
-
-    /**
-     * Gets a Poolable from the pool.  If there is room in the pool, a new Poolable will
be
-     *  created.  Depending on the parameters to the constructor, the method may block or
throw
-     *  an exception if a Poolable is not available on the pool.
-     *
-     * @return Always returns a Poolable.  Contract requires that put must always be called
with
-     *  the Poolable returned.
-     * @throws Exception An exception may be thrown as described above or if there is an
exception
-     *  thrown by the ObjectFactory's newInstance() method.
-     */
-    protected Object getFromPool() throws Exception {
-        Object poolable;
-        synchronized( this.semaphore ) {
-            // Look for a Poolable at the end of the m_ready list
-            if( this.readySize > 0 ){
-                // A poolable is ready and waiting in the pool
-                poolable = this.ready.removeLast();
-                this.readySize--;
-            } else {
-                // Create a new poolable.  May throw an exception if the poolable can not
be
-                //  instantiated.
-                poolable = this.factory.newInstance();
-                this.size++;
-
-                if( this.logger.isDebugEnabled() ) {
-                    this.logger.debug( "Created a new " + poolable.getClass().getName()
-                                       + " from the object factory." );
-                }
-            }
-        }
-
-        this.factory.exitingPool(poolable);
-        
-        if( this.logger.isDebugEnabled() ) {
-            this.logger.debug( "Got a " + poolable.getClass().getName() + " from the pool."
);
-        }
-
-        return poolable;
-    }
-
-    
     /* (non-Javadoc)
      * @see org.apache.cocoon.core.container.handler.AbstractComponentHandler#doGet()
      */
@@ -202,51 +91,6 @@
         // nothing to do
     }
 
-    /**
-     * Returns a poolable to the pool 
-     *
-     * @param poolable Poolable to return to the pool.
-     */
-    protected void putIntoPool( final Object poolable ) {
-        try {
-            this.factory.enteringPool(poolable);
-        } catch (Exception ignore) {
-            this.logger.warn("Exception during putting component back into the pool.", ignore);
-        }
-
-        synchronized( this.semaphore ) {
-            if( this.size <= this.max ) {
-                if( this.disposed ) {
-                    // The pool has already been disposed.
-                    if( this.logger.isDebugEnabled() ) {
-                        this.logger.debug( "Put called for a " + poolable.getClass().getName()
-                                           + " after the pool was disposed." );
-                    }
-
-                    this.permanentlyRemovePoolable( poolable );
-                } else {
-                    // There is room in the pool to keep this poolable.
-                    if( this.logger.isDebugEnabled() ) {
-                        this.logger.debug( "Put a " + poolable.getClass().getName()
-                                           + " back into the pool." );
-                    }
-
-                    this.ready.addLast( poolable );
-                    this.readySize++;
-
-                }
-            } else {
-                // More Poolables were created than can be held in the pool, so remove.
-                if( this.logger.isDebugEnabled() ) {
-                    this.logger.debug( "No room to put a " + poolable.getClass().getName()
-                                       + " back into the pool, so remove it." );
-                }
-
-                this.permanentlyRemovePoolable( poolable );
-            }
-        }
-    }
-    
     protected void doInitialize() {
         // nothing to do here
     }

Copied: cocoon/trunk/src/core/java/org/apache/cocoon/core/container/handler/NonThreadSafePoolableComponentHandler.java
(from r149541, cocoon/trunk/src/core/java/org/apache/cocoon/core/container/handler/PoolableComponentHandler.java)
URL: http://svn.apache.org/viewcvs/cocoon/trunk/src/core/java/org/apache/cocoon/core/container/handler/NonThreadSafePoolableComponentHandler.java?view=diff&rev=149543&p1=cocoon/trunk/src/core/java/org/apache/cocoon/core/container/handler/PoolableComponentHandler.java&r1=149541&p2=cocoon/trunk/src/core/java/org/apache/cocoon/core/container/handler/NonThreadSafePoolableComponentHandler.java&r2=149543
==============================================================================
--- cocoon/trunk/src/core/java/org/apache/cocoon/core/container/handler/PoolableComponentHandler.java
(original)
+++ cocoon/trunk/src/core/java/org/apache/cocoon/core/container/handler/NonThreadSafePoolableComponentHandler.java
Wed Feb  2 10:38:45 2005
@@ -53,7 +53,7 @@
  *
  * @version CVS $Id$
  */
-public class PoolableComponentHandler
+public class NonThreadSafePoolableComponentHandler
 extends AbstractFactoryHandler {
     
     /** The default max size of the pool */
@@ -92,7 +92,7 @@
      *                managed by the ComponentHandler.
      * @param config The configuration to use to configure the pool.
      */
-    public PoolableComponentHandler( final ComponentInfo info,
+    public NonThreadSafePoolableComponentHandler( final ComponentInfo info,
                                      final Logger logger,
                                      final ComponentFactory factory,
                                      final Configuration config )
@@ -150,7 +150,7 @@
      * @throws Exception An exception may be thrown as described above or if there is an
exception
      *  thrown by the ObjectFactory's newInstance() method.
      */
-    protected Object doGet() throws Exception {
+    protected Object getFromPool() throws Exception {
         Object poolable;
         synchronized( this.semaphore ) {
             // Look for a Poolable at the end of the m_ready list
@@ -185,7 +185,7 @@
      *
      * @param poolable Poolable to return to the pool.
      */
-    protected void doPut( final Object poolable ) {
+    protected void putIntoPool( final Object poolable ) {
         try {
             this.factory.enteringPool(poolable);
         } catch (Exception ignore) {
@@ -227,5 +227,20 @@
     
     protected void doInitialize() {
         // nothing to do here
+    }
+    
+    
+    /* (non-Javadoc)
+     * @see org.apache.cocoon.core.container.handler.AbstractComponentHandler#doGet()
+     */
+    protected Object doGet() throws Exception {
+        return this.getFromPool();
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.cocoon.core.container.handler.AbstractComponentHandler#doPut(java.lang.Object)
+     */
+    protected void doPut(Object component) throws Exception {
+        this.putIntoPool(component);
     }
 }



Mime
View raw message