directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r159172 - in directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/util/pools: ./ GlobalPoolManager.java IPool.java IPoolable.java LocalPoolManager.java PoolEnum.java PoolException.java PoolManager.java PoolObject.java PoolStackFullException.java PoolStackResizeException.java
Date Sun, 27 Mar 2005 18:42:59 GMT
Author: elecharny
Date: Sun Mar 27 10:42:57 2005
New Revision: 159172

URL: http://svn.apache.org/viewcvs?view=rev&rev=159172
Log:
A first working version of the new ASN.1 LDAP decoder.

Added:
    directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/util/pools/
    directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/util/pools/GlobalPoolManager.java
    directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/util/pools/IPool.java
    directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/util/pools/IPoolable.java
    directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/util/pools/LocalPoolManager.java
    directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/util/pools/PoolEnum.java
    directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/util/pools/PoolException.java
    directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/util/pools/PoolManager.java
    directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/util/pools/PoolObject.java
    directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/util/pools/PoolStackFullException.java
    directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/util/pools/PoolStackResizeException.java

Added: directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/util/pools/GlobalPoolManager.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/util/pools/GlobalPoolManager.java?view=auto&rev=159172
==============================================================================
--- directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/util/pools/GlobalPoolManager.java (added)
+++ directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/util/pools/GlobalPoolManager.java Sun Mar 27 10:42:57 2005
@@ -0,0 +1,248 @@
+/*
+ *   Copyright 2005 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+package org.apache.asn1.util.pools;
+
+import org.apache.asn1.ber.tlv.TLVFactory;
+import org.apache.asn1.ldap.codec.LdapMessageContainerFactory;
+import org.apache.asn1.ldap.pojo.factories.BindRequestPOJOFactory;
+import org.apache.asn1.ldap.pojo.factories.BindResponsePOJOFactory;
+import org.apache.asn1.ldap.pojo.factories.LdapMessagePOJOFactory;
+import org.apache.asn1.ldap.pojo.factories.SaslAuthenticationPOJOFactory;
+import org.apache.asn1.ldap.pojo.factories.SimpleAuthenticationPOJOFactory;
+import org.apache.asn1.util.ObjectFactory;
+
+import org.apache.log4j.Logger;
+
+
+/**
+ * The GlobalPoolManager is used to manage synchronized pools. It creates 
+ * object on demand, and make them available to LocalPoolManager. 
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class GlobalPoolManager extends PoolManager
+{
+    //~ Static fields/initializers -----------------------------------------------------------------
+
+    /** Class Logger */
+    protected static Logger log = Logger.getLogger( GlobalPoolManager.class );
+
+    /** Speed up logging */
+    protected static final boolean DEBUG = log.isDebugEnabled();
+
+    /** Array of pools */
+    private static GlobalPool[] pools = new GlobalPool[PoolEnum.POOL_NUMBER];
+
+    /** Initialize the Global Pools. This method is a singleton */
+    private static GlobalPoolManager instance = new GlobalPoolManager();
+
+    //~ Constructors -------------------------------------------------------------------------------
+
+    /**
+     * Creates a new GlobalPoolManager object. It's aprivate, the class
+     * is a singleton.
+     */
+    private GlobalPoolManager()
+    {
+
+        pools[PoolEnum.TLV_POOL]                     = new GlobalPool( new TLVFactory(), 1024,
+                10240 );
+        
+        pools[PoolEnum.SIMPLE_AUTH_POJO_POOL]        = new GlobalPool(
+                new SimpleAuthenticationPOJOFactory(), 128, 1024 );
+        
+        pools[PoolEnum.SASL_AUTH_POJO_POOL]          = new GlobalPool(
+                new SaslAuthenticationPOJOFactory(), 128, 1024 );
+        
+        pools[PoolEnum.BIND_RESPONSE_POJO_POOL]      = new GlobalPool(
+                new BindResponsePOJOFactory(), 128, 1024 );
+        
+        pools[PoolEnum.BIND_REQUEST_POJO_POOL]       = new GlobalPool( new BindRequestPOJOFactory(),
+                128, 1024 );
+        
+        pools[PoolEnum.LDAP_MESSAGE_POJO_POOL]  = new GlobalPool(
+                new LdapMessagePOJOFactory(), 128, 1024 );
+
+        pools[PoolEnum.LDAP_MESSAGE_CONTAINER_POOL]  = new GlobalPool(
+                new LdapMessageContainerFactory(), 128, 1024 );
+
+    }
+
+    //~ Methods ------------------------------------------------------------------------------------
+
+    /**
+     * Allocate an object and associate it to its pool.
+     *
+     * @param poolId The associated pool ID
+     *
+     * @return An allocated PoolObject
+     *
+     * @throws PoolException Thrown if no more objects are available 
+     */
+    public static PoolObject allocate( int poolId ) throws PoolException
+    {
+
+        GlobalPool gp     = pools[poolId];
+        PoolObject object = ( PoolObject ) pools[poolId].allocate();
+
+        object.setPoolId( poolId );
+
+        return object;
+    }
+
+    /**
+     * Free an object 
+     *
+     * @param object The object to be freed
+     */
+    public static void free( PoolObject object )
+    {
+        pools[object.getPoolId()].free( object );
+    }
+
+    /**
+     * Drain a localPool and put discarded objects back to the GlobalPool.
+     *
+     * @param stack The localPool to drain
+     * @param number Number of objects to transfer.
+     */
+    public static void drain( InnerStack stack, int number )
+    {
+
+        int poolId = ( ( PoolObject ) stack.peek() ).getPoolId();
+
+        synchronized ( instance )
+        {
+
+            for ( int i = 0; i < number; i++ )
+            {
+                pools[poolId].free( ( PoolObject ) stack.pop() );
+            }
+        }
+    }
+
+    //~ Inner Classes ------------------------------------------------------------------------------
+
+    /**
+     * This inner class implement the pool behaviour.
+     */
+    private class GlobalPool extends InnerStack
+    {
+        //~ Instance fields ------------------------------------------------------------------------
+
+        /** The maximum number of objects that can be created */
+        private int maxCapacity;
+
+        /** The current created object number */
+        private int currentObjectNumber = 0;
+
+        /** The object factory to be used to create objects */
+        private ObjectFactory objectFactory;
+
+        //~ Constructors ---------------------------------------------------------------------------
+
+        /**
+         * Creates a new GlobalPool object.
+         *
+         * @param objectFactory The object factory to create objects
+         * @param initialCapacity The initial size of the pool
+         * @param maxCapacity 
+         */
+        private GlobalPool(  ObjectFactory objectFactory, int initialCapacity, int maxCapacity )
+        {
+            super(  initialCapacity, maxCapacity );
+            this.objectFactory  = objectFactory;
+            this.maxCapacity    = maxCapacity;
+            currentObjectNumber = 0;
+        }
+
+        //~ Methods --------------------------------------------------------------------------------
+
+        /**
+         * Allocate an object, either poping it from the global pool or by
+         * creating it with the ObjectFactory 
+         *
+         * @return An object
+         *
+         * @throws PoolException if  
+         */
+        private synchronized PoolObject allocate() throws PoolException
+        {
+
+            if ( isEmpty() )
+            {
+
+                if ( currentObjectNumber == maxCapacity )
+                {
+                    log.error( "Cannot allocate another object, maximum reached" );
+                    throw new PoolException( "Maximum number of object created reached" );
+                }
+                else
+                {
+
+                    PoolObject object = objectFactory.makeObject();
+
+                    if ( DEBUG )
+                    {
+                        log.debug( "Creating globally : " + object.toString() );
+                    }
+
+                    currentObjectNumber++;
+
+                    return object;
+                }
+            }
+            else
+            {
+
+                PoolObject object = ( PoolObject ) pop();
+
+                if ( DEBUG )
+                {
+                    log.debug( "Allocating globally : " + object.toString() );
+                }
+
+                return object;
+            }
+        }
+
+        /**
+         * Free an object. It will be put back in the global pool.
+         *
+         * @param object The object to free
+         */
+        private synchronized void free( PoolObject object )
+        {
+
+            if ( DEBUG )
+            {
+                log.debug( "freeing : " + object.toString() );
+            }
+
+            try
+            {
+                push( object );
+            }
+            catch ( PoolStackFullException sfe )
+            {
+
+                // Too many objects in the stack. We have to free the object
+                object = null;
+            }
+        }
+    }
+}

Added: directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/util/pools/IPool.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/util/pools/IPool.java?view=auto&rev=159172
==============================================================================
--- directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/util/pools/IPool.java (added)
+++ directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/util/pools/IPool.java Sun Mar 27 10:42:57 2005
@@ -0,0 +1,66 @@
+/*
+ *   Copyright 2005 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+package org.apache.asn1.util.pools;
+
+/**
+ * An interface to be implemented by objects pool. It list all
+ * the different type of pool we can handle.
+ * 
+ * To add a new type of pool, one has to declare a new constant
+ * in this interface and assign it a new number.
+ *  
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public interface IPool
+{
+    //~ Methods ------------------------------------------------------------------------------------
+
+    /**
+     * Allocate an typed object 
+     *
+     * @return A typed object
+     *
+     * @throws PoolException If the pool is full
+     */
+    PoolObject allocate() throws PoolException;
+
+    /**
+     * Free an object 
+     *
+     * @param object The object to free
+     */
+    void free( PoolObject object );
+
+    /**
+     * Set the strategy to Quadratic
+     * @param increment Increment size to be used by the underlying stack
+    */
+    void setStrategyQuadratic( int increment );
+
+    /**
+     * Set the strategy to Linear
+     * @param increment Increment size to be used by the underlying stack
+    */
+    void setStrategyLinear( int increment );
+
+    /**
+     * Set the strategy to Constant
+     *
+     * @param increment Increment size to be used by the underlying stack
+     */
+    void setStrategyConstant( int increment );
+}

Added: directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/util/pools/IPoolable.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/util/pools/IPoolable.java?view=auto&rev=159172
==============================================================================
--- directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/util/pools/IPoolable.java (added)
+++ directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/util/pools/IPoolable.java Sun Mar 27 10:42:57 2005
@@ -0,0 +1,62 @@
+/*
+ *   Copyright 2005 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+package org.apache.asn1.util.pools;
+
+/**
+ * An interface to be implemented by each object that are stored in stacks
+ * when they have been allocated. 
+ * 
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public interface IPoolable
+{
+    //~ Methods ------------------------------------------------------------------------------------
+
+    /**
+     * Associate this object with a pool
+     *
+     * @param pool The pool to which this object belongs
+     */
+    void setPool( IPool pool );
+
+    /**
+     * Get the pool in which this object is stored
+     *
+     * @return The object's pool
+     */
+    IPool getPool();
+
+    /**
+     * Get the pool ID associated with this object
+     *
+     * @return DOCUMENT ME!
+     */
+    int getPoolId();
+
+    /**
+     * Set the pool ID of this object
+     *
+     * @param poolId The pool ID
+     */
+    void setPoolId( int poolId );
+
+    /**
+     * Free the current object by pushing it back to a stack
+     */
+    void free();
+}

Added: directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/util/pools/LocalPoolManager.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/util/pools/LocalPoolManager.java?view=auto&rev=159172
==============================================================================
--- directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/util/pools/LocalPoolManager.java (added)
+++ directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/util/pools/LocalPoolManager.java Sun Mar 27 10:42:57 2005
@@ -0,0 +1,236 @@
+/*
+ *   Copyright 2005 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+package org.apache.asn1.util.pools;
+
+
+import org.apache.log4j.Logger;
+
+
+/**
+ * This class implement a local Object pool manager. It is not  a thread-safe class
+ * so it can't be used in a multi-threaded environment. Its only purpose is to offer
+ * a way to manage objects avoiding allocations inside a single thread. If an object 
+ * is needed and if the pool is empty, then a new object will be created by a global
+ * IPool manager, which is synchronized.
+ * 
+ * This pool manager won't keep allocated objets indefinitely. Two limits are used to
+ * manage objects :
+ * - High Level : it gives the maximum number of free objects in the pool. If 
+ * this limit reached, objetcs are suppressed from this pool and sent back to the
+ * global pool, making them available to the others threads.
+ *  
+ * -  Low Level : it gives the minimum number of free objects to keep in the pool. If
+ * the High limit is reached and objects are discarded from the pool, then the process
+ * will stop when this lower limit is reached. 
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class LocalPoolManager extends PoolManager
+{
+    //~ Static fields/initializers -----------------------------------------------------------------
+
+    /** Class logger */
+    protected static Logger log = Logger.getLogger( LocalPoolManager.class );
+
+    /** To speed up the login */
+    protected static final boolean DEBUG = log.isDebugEnabled();
+
+    //~ Instance fields ----------------------------------------------------------------------------
+
+    /** Array of pools */
+    private LocalPool[] pools = new LocalPool[PoolEnum.POOL_NUMBER];
+
+    //~ Constructors -------------------------------------------------------------------------------
+
+    /**
+     * Creates a new LocalPoolManager object. All the objects that could be allocated are stored in 
+     * local pools.
+     */
+    public LocalPoolManager()
+    {
+
+        // We have to initialize the pools.
+        // Tag and TLV pool will be bigger than others, because PDU may contain many of them
+        // for only one POJO generated.
+        //
+        // TODO It has to be improved. Picocontainer?
+        pools[PoolEnum.TLV_POOL]                     = new LocalPool( PoolEnum.TLV_POOL, 10240,
+                1024 );
+        
+        pools[PoolEnum.SIMPLE_AUTH_POJO_POOL]        = new LocalPool(
+                PoolEnum.SIMPLE_AUTH_POJO_POOL, 1024, 1024 );
+        
+        pools[PoolEnum.SASL_AUTH_POJO_POOL]          = new LocalPool( PoolEnum.SASL_AUTH_POJO_POOL,
+                1024, 1024 );
+        
+        pools[PoolEnum.BIND_RESPONSE_POJO_POOL]      = new LocalPool(
+                PoolEnum.BIND_RESPONSE_POJO_POOL, 1024, 1024 );
+        
+        pools[PoolEnum.BIND_REQUEST_POJO_POOL]       = new LocalPool(
+                PoolEnum.BIND_REQUEST_POJO_POOL, 1024, 1024 );
+
+        pools[PoolEnum.LDAP_MESSAGE_CONTAINER_POOL] = new LocalPool(
+                PoolEnum.LDAP_MESSAGE_CONTAINER_POOL, 1024, 1024 );
+
+        pools[PoolEnum.LDAP_MESSAGE_POJO_POOL] = new LocalPool(
+                PoolEnum.LDAP_MESSAGE_POJO_POOL, 1024, 1024 );
+    }
+
+    //~ Methods ------------------------------------------------------------------------------------
+
+    /**
+     * Allocate an object by asking its associated pool to deliver one.
+     *
+     * @param poolId The pool identifier. Each object has its own pool.
+     *
+     * @return An object newly created or available in the pool.
+     */
+    public PoolObject allocate( int poolId ) throws PoolException
+    {
+
+        PoolObject object = ( PoolObject ) pools[poolId].allocate();
+
+        return object;
+    }
+
+    /**
+     * Free the current object. This object is supposed to be "clean", i.e
+     * it as been reseted to its initial values. It will be put back to its
+     * pool.
+     * 
+     * Be aware that this object could be resued by another task in the same
+     * thread, or if it is discarded from the local stack, it could be used 
+     * by another thread. 
+     *
+     * @param object The object to be freed.
+     */
+    public void free( PoolObject object ) throws PoolStackFullException
+    {
+        pools[object.getPoolId()].free( object );
+    }
+
+    //~ Inner Classes ------------------------------------------------------------------------------
+
+    /**
+     * This inner class implements the LocalPool used by the pool manager.
+     *
+     */
+    private class LocalPool extends InnerStack implements IPool
+    {
+        //~ Instance fields ------------------------------------------------------------------------
+
+        /** The pool ID */
+        private int poolId;
+
+        //~ Constructors ---------------------------------------------------------------------------
+
+        /**
+         * Creates a new LocalPool object.
+         *
+         * @param poolId Identify the object's type that is stored in the pool.
+         * @param capacity Capacity of the pool
+         * @param increment Increment size of the pool
+        */
+        private LocalPool(  int poolId, int capacity, int increment )
+        {
+            super(  capacity, 10 * capacity );
+            this.poolId = poolId;
+            setStrategyLinear( increment );
+        }
+
+        //~ Methods --------------------------------------------------------------------------------
+
+        /**
+         * Allocate an object, possibly asking a new created one to the global pool
+         *
+         * @return An typed object  
+         */
+        public PoolObject allocate() throws PoolException
+        {
+
+            if ( isEmpty() )
+            {
+
+                // No more object available in the local pool? Ask for
+                // a new one from the - synchronized - GlobalPool
+                try
+                {
+
+                    PoolObject object = GlobalPoolManager.allocate( poolId );
+
+                    if ( DEBUG )
+                    {
+                        log.debug( "Creating locally : " + object.toString() );
+                    }
+
+                    object.setPool( this );
+
+                    return object;
+
+                }
+                catch ( PoolException lpe )
+                {
+                    log.error(
+                        "Cannot allocate a " + PoolEnum.getType( poolId ) + " object, reason : " +
+                        lpe.getMessage() );
+                    throw new PoolException( "Cannot allocate an object" );
+                }
+            }
+            else
+            {
+
+                PoolObject object = ( PoolObject ) pop();
+
+                if ( DEBUG )
+                {
+                    log.debug( "Allocating : " + object.toString() );
+                }
+
+                return object;
+            }
+        }
+
+        /**
+         * Free an object by pushing it to its pool. If the
+         * stack is full, we will suppress objects from the local stack
+         * and put them back in the global stack. Half of the available
+         * objects will be deleted.
+         *
+         * @param object The object to free
+         */
+        public void free( PoolObject object )
+        {
+
+            if ( DEBUG )
+            {
+                log.debug( "Freeing : " + object.toString() );
+            }
+
+            try
+            {
+                push( object );
+            }
+            catch ( PoolStackFullException psfe )
+            {
+
+                // The stack is full.We have to send back some
+                // objects to the global pool. Let's release half of the objects.
+                GlobalPoolManager.drain( this, getSize() / 2 );
+            }
+        }
+    }
+}

Added: directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/util/pools/PoolEnum.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/util/pools/PoolEnum.java?view=auto&rev=159172
==============================================================================
--- directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/util/pools/PoolEnum.java (added)
+++ directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/util/pools/PoolEnum.java Sun Mar 27 10:42:57 2005
@@ -0,0 +1,91 @@
+/*
+ *   Copyright 2005 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+package org.apache.asn1.util.pools;
+
+/**
+ * This is where all the different pools are named. To add a new pool, we have to
+ * add an entry to the list, and to modify the getType method.
+ * 
+ * TODO This is ugly. Another way to do it should be implemented, like a picoContainer approach,
+ * where pools are declared outside the code...  
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public final class PoolEnum
+{
+    //~ Instance fields ----------------------------------------------------------------------------
+
+    /** IPool of tags */
+    public static int TAG_POOL = 0;
+
+    /** IPool of TLVs */
+    public static final int TLV_POOL = TAG_POOL++;
+
+    /** IPool of SimpleAuthPOJO objects */
+    public static final int SIMPLE_AUTH_POJO_POOL = TAG_POOL++;
+
+    /** IPool of SaslAuthPOJO objects */
+    public static final int SASL_AUTH_POJO_POOL = TAG_POOL++;
+
+    /** IPool of BindResponsePOJO objects */
+    public static final int BIND_RESPONSE_POJO_POOL = TAG_POOL++;
+
+    /** IPool of BindRequestPOJO objects */
+    public static final int BIND_REQUEST_POJO_POOL = TAG_POOL++;
+
+    /** IPool of LdapMessageContainer objects */
+    public static final int LDAP_MESSAGE_CONTAINER_POOL = TAG_POOL++;
+
+    /** IPool of LdapMessagePOJO objects */
+    public static final int LDAP_MESSAGE_POJO_POOL = TAG_POOL++;
+
+    /** To add a new pool type, change the static name.
+     * Don't forget to add a String in poolStrings !  */
+    // public static final int <ADDED_POOL> = TAG_POOL++;
+
+    /** The maximum number of pool we could have */
+    public static final int POOL_NUMBER = TAG_POOL;
+    
+    private static final String[] poolStrings = new String[]{
+            "TLV",
+            "SimpleAuthPOJO",
+            "SaslAuthPOJO",
+            "BindResponsePOJO",
+            "BindRequestPOJO",
+            "LdapMessageContainer",
+            "LdapMessagePOJO"
+			// Add a new pool right here
+    };
+
+    /**
+     * Get the name of the object stored in this pool 
+     *
+     * @return The object name
+     */
+    public static String getType(int poolId)
+    {
+
+    	if ((poolId > 0) && (poolId < poolStrings.length)) 
+    	{
+    		return poolStrings[poolId];
+        }
+    	else
+    	{
+    		return "UNKNOWN POOL";
+    	}
+    }
+}

Added: directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/util/pools/PoolException.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/util/pools/PoolException.java?view=auto&rev=159172
==============================================================================
--- directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/util/pools/PoolException.java (added)
+++ directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/util/pools/PoolException.java Sun Mar 27 10:42:57 2005
@@ -0,0 +1,37 @@
+/*
+ *   Copyright 2005 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+package org.apache.asn1.util.pools;
+
+/**
+ * Thrown when a IPool has encountered a failure condition during an allocation. 
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class PoolException extends Exception
+{
+    //~ Constructors -------------------------------------------------------------------------------
+
+    /**
+     * Creates a PoolException
+     * 
+     * @param message A message with meaning to a human
+     */
+    public PoolException(  String message )
+    {
+        super(  message );
+    }
+}

Added: directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/util/pools/PoolManager.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/util/pools/PoolManager.java?view=auto&rev=159172
==============================================================================
--- directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/util/pools/PoolManager.java (added)
+++ directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/util/pools/PoolManager.java Sun Mar 27 10:42:57 2005
@@ -0,0 +1,471 @@
+/*
+ *   Copyright 2005 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+package org.apache.asn1.util.pools;
+
+
+import org.apache.log4j.Logger;
+
+import java.util.EmptyStackException;
+
+
+/**
+ * The pool manager super class. It hide the Stack implementation used by
+ * inherited pool managers 
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class PoolManager
+{
+    //~ Static fields/initializers -----------------------------------------------------------------
+
+    /** The class logger */
+    private static final Logger log = Logger.getLogger( PoolManager.class );
+
+    /** A static to avoid String construction when invocing the logger */
+    private static final boolean DEBUG = log.isDebugEnabled();
+
+    /** A static to avoid String construction when invocing the logger  */
+    private static final boolean INFO = log.isInfoEnabled();
+
+    //~ Inner Classes ------------------------------------------------------------------------------
+
+    /**
+     * A simple stack implementation. &lt;b&gt; It is NOT synchronized. &lt;/b&gt;
+     *
+     * @author <a href="mailto:directory-dev@incubator.apache.org">Apache Directory Project</a>
+     */
+    protected class InnerStack
+    {
+        //~ Static fields/initializers -------------------------------------------------------------
+
+        /** Constant means that the stack will be increased by a constant size
+         * (capacity = N, N + K, N + 2K, N + 3K, ...) 
+         **/
+        private static final int CONSTANT = 0;
+
+        /** Linear means that the stack will be increased by a increasing size
+         * (capacity = N, 2N, 3N, 4N, ...)
+         */
+        private static final int LINEAR = 1;
+
+        /** Geometric means that the stack will be increased by a increased size
+         *  (capacity = 1N, 2N, 4N, 8N ...)
+         **/
+        private static final int QUADRATIC = 2;
+
+        //~ Instance fields ------------------------------------------------------------------------
+
+        /** Elements container */
+        private Object[] elements;
+
+        /** Current stack's capacity */
+        private int capacity;
+
+        /** Max stack capacity */
+        private int maxCapacity;
+
+        /** Size by which the stack will be increased if needed */
+        private int increment;
+
+        /** Current top of the stack. -1 if the stack is empty.*/
+        private int top;
+
+        /** Initial increment size, in case we choose a Linear strategy */
+        private int initialIncrement;
+
+        /** Strategy used to increase the stack capacity */
+        private int strategy = LINEAR;
+
+        //~ Constructors ---------------------------------------------------------------------------
+
+        /**
+         * Creates a new InnerStack object with a default initial capacity of 16 elements,
+         * a default increment of 16, no maximum size, and a Constant strategy.
+         */
+        public InnerStack()
+        {
+            elements         = new Object[16];
+            capacity         = 16;
+            maxCapacity      = 0;
+            increment        = 16;
+            top              = -1;
+            initialIncrement = increment;
+            strategy         = CONSTANT;
+        }
+
+        /**
+         * Creates a new InnerStack object whith an initial capacity.
+         * 
+         * @param capacity Initial capacity of the stack
+        */
+        public InnerStack(  int capacity )
+        {
+            elements         = new Object[capacity];
+            this.capacity    = capacity;
+            maxCapacity      = 0;
+            increment        = 16;
+            top              = -1;
+            initialIncrement = increment;
+            strategy         = CONSTANT;
+        }
+
+        /**
+         * Creates a new InnerStack object whith an initial capacity and a maximum capacity.
+         * 
+         * @param capacity Initial capacity of the stack
+         * @param maxCapacity Maximum stack capacity
+        */
+        public InnerStack(  int capacity, int maxCapacity )
+        {
+            elements         = new Object[capacity];
+            this.capacity    = capacity;
+            increment        = 16;
+            this.maxCapacity = maxCapacity;
+            top              = -1;
+            initialIncrement = increment;
+            strategy         = CONSTANT;
+        }
+
+        //~ Methods --------------------------------------------------------------------------------
+
+        /**
+         * Returns the current stack's size 
+         *
+         * @return  The current stacks size.
+         */
+        public int getSize()
+        {
+
+            return top + 1;
+        }
+
+        /**
+         * Returns the current stack's capacity 
+         *
+         * @return  The current stack's capacity.
+         */
+        public int getCapacity()
+        {
+
+            return capacity;
+        }
+
+        /**
+         * Tells if the stack is empty. 
+         *
+         * @return  wcode>true</code> if the stack is empty.
+         */
+        public boolean isEmpty()
+        {
+
+            return ( top == -1 );
+        }
+
+        /**
+         * Return the top of the stack, without removing it from the stack.   
+         *
+         * @return  The top element, or <code>null</code> if the stack is empty.
+         */
+        public Object peek()
+        {
+
+            if ( top == -1 )
+            {
+
+                return null;
+            }
+
+            return elements[top];
+        }
+
+        /**
+         * Return the top of the stack, and remove the element from it.
+         * Note that the stack element holder is nullified, to avoid a memeory
+         * leak.
+         *
+         * @return  The top element, or <code>null</code> if the stack is empty.
+         * @exception EmptyStackException Thrown if the stack is empty.
+         */
+        public Object pop() throws EmptyStackException
+        {
+
+            if ( top == -1 )
+            {
+                throw new EmptyStackException();
+            }
+
+            Object object = elements[top];
+            elements[top--] = null;
+
+            return object;
+        }
+
+        /**
+         * Add an element on top of the stack. This is not a copy, it's a reference.
+         * 
+         * If the stack is full, we try to increase its capacity using the predefined
+         * strategy (one of Constant, Linear or Quadratic). If the stack's capacity 
+         * reach its limit, an exception is thrown.
+         *
+         * @param  object  The element to put on top of the stack.
+         * @exception PoolStackFullException Thrown if the stack capacity has reached its limit
+         */
+        public void push( Object object ) throws PoolStackFullException
+        {
+
+            if ( top == ( capacity - 1 ) )
+            {
+
+                switch ( strategy )
+                {
+
+                    case QUADRATIC :
+
+                        // Beware that it can lead to an out of memory very quickly !!
+                        increment += increment;
+
+                        break;
+
+                    case LINEAR :
+                        increment += initialIncrement;
+
+                        break;
+
+                    case CONSTANT :
+                        break;
+                }
+
+                int newCapacity = capacity + increment;
+
+                if ( ( newCapacity >= maxCapacity ) && ( maxCapacity != 0 ) )
+                {
+                    // We have reach the limit! we have to check that it's not already
+                    // the case for the current capacity, and if so, throw an exception
+
+                    if ( capacity == maxCapacity )
+                    {
+                        log.error( "The stack cannot be increased, it has reached its limit" );
+                        throw new PoolStackFullException( "InnerStack limit reached" );
+                    }
+                    else
+                    {
+
+                        log.warn( "The stack's capacity limit has been reached." );
+                        newCapacity = maxCapacity;
+                    }
+                }
+
+                if ( INFO )
+                {
+                    log.info(
+                        "InnerStack is increased. Old capacity = " + capacity +
+                        ", new capacity = " +
+                        ( capacity + increment ) );
+                }
+
+                Object[] newElements = new Object[newCapacity];
+                System.arraycopy( elements, 0, newElements, 0, capacity );
+                elements = newElements;
+                capacity = newCapacity;
+            }
+
+            elements[++top] = object;
+        }
+
+        /**
+         * Drain unused elements from the stack resizing it.
+         *
+         * @param newCapacity The new stack's capacity
+         * @exception PoolStackResizeException Thrown if we ask a resize that exceed the maximum capacity
+         */
+        public void resize( int newCapacity ) throws PoolStackResizeException
+        {
+
+            if ( newCapacity < capacity )
+            {
+
+                if ( newCapacity < ( top + 1 ) )
+                {
+
+                    // We are asked to release objects on the stack...
+                    int nbDelete = ( top + 1 ) - newCapacity;
+
+                    for ( int i = 0; i < nbDelete; i++ )
+                    {
+
+                        // we just have to pop objects.
+                        pop();
+                    }
+
+                    capacity = newCapacity;
+                }
+                else
+                {
+
+                    // we just have to change the capacity
+                    capacity = newCapacity;
+                }
+
+
+                // if we drain the stack, we will have to reset the increment
+                increment = initialIncrement;
+            }
+            else
+            {
+                // we have to increase the stack capaity, but we can't
+                // increase it above its maxCapacity
+
+                if ( newCapacity > maxCapacity )
+                {
+                    log.error(
+                        "An attempt to resize (" + newCapacity +
+                        ") a stack above its maximul capacity (" + maxCapacity + ") has been done" );
+                    throw new PoolStackResizeException(
+                        "Cannot resize a stack above its maximum capacity" );
+                }
+                else
+                {
+                    this.capacity = newCapacity;
+                }
+            }
+        }
+
+        /**
+         * Get the current value of the increment used to increase the stack size 
+         *
+         * @return Returns the increment.
+         */
+        public int getIncrement()
+        {
+
+            return increment;
+        }
+
+        /**
+         * Set an increment value.
+         *
+         * @param increment The increment to set.
+         */
+        public void setIncrement( int increment )
+        {
+            this.increment = increment;
+        }
+
+        /**
+         * Get the maximum capacity of the current stack.
+         *
+         * @return Returns the maxCapacity.
+         */
+        public int getMaxCapacity()
+        {
+
+            return maxCapacity;
+        }
+
+        /**
+         * Set a maximum capacity of the current stack.
+         *
+         * @param maxCapacity The maxCapacity to set.
+         */
+        public void setMaxCapacity( int maxCapacity )
+        {
+            this.maxCapacity = maxCapacity;
+        }
+
+        /**
+         * Tells if the current implemented strategy to increase the stack capacity
+         * is Constant.
+         *
+         * @return Returns <code>true</code> if the strategy is constant.
+         */
+        public boolean isStrategyConstant()
+        {
+
+            return strategy == CONSTANT;
+        }
+
+        /**
+         * Tells if the current implemented strategy to increase the stack capacity
+         * is Linear.
+         *
+         * @return Returns <code>true</code> if the strategy is linear.
+         */
+        public boolean isStrategyLinear()
+        {
+
+            return strategy == LINEAR;
+        }
+
+        /**
+         * Tells if the current implemented strategy to increase the stack capacity
+         * is Quadratic.
+         *
+         * @return Returns <code>true</code> if the strategy is quadratic.
+         */
+        public boolean isStrategyQuadratic()
+        {
+
+            return strategy == QUADRATIC;
+        }
+
+        /**
+         * Set a Constant increase strategy. Each time the stack will have to grow, 
+         * a constant number of empty elems will be created. It's slower, but safer
+         * from a memory consomption point of vue.
+         * 
+         * @param increment Initial size of the increment  
+         */
+        public void setStrategyConstant( int increment )
+        {
+            strategy         = CONSTANT;
+            this.increment   = increment;
+            initialIncrement = increment;
+        }
+
+        /**
+         * Set a Constant increase strategy. Each time the stack will have to grow, 
+         * N*size empty elems will be created, where N will be incremented at each 
+         * invocation. It's faster than the constant strategy, but eat much more memory.
+         * 
+         * When setting a Linear strategy, it could be wize to evaluate the estimated
+         * average stack size, and create this stack with this number of elements, then 
+         * knowing the variance could help to set an adapted increment value.  
+         * 
+         * @param increment Initial size of the increment 
+        */
+        public void setStrategyLinear( int increment )
+        {
+            strategy         = LINEAR;
+            this.increment   = increment;
+            initialIncrement = increment;
+        }
+
+        /**
+         * Set a Quadratic increase strategy. This is experimental, and use to compare
+         * results against Constant and Linear startegy. It eats the memory very quickly... 
+         * Set a quadratic strategy
+         * 
+         * @param increment Initial size of the increment 
+         */
+        public void setStrategyQuadratic( int increment )
+        {
+            strategy         = QUADRATIC;
+            this.increment   = increment;
+            initialIncrement = increment;
+        }
+    } // end class InnerStack
+}

Added: directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/util/pools/PoolObject.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/util/pools/PoolObject.java?view=auto&rev=159172
==============================================================================
--- directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/util/pools/PoolObject.java (added)
+++ directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/util/pools/PoolObject.java Sun Mar 27 10:42:57 2005
@@ -0,0 +1,85 @@
+/*
+ *   Copyright 2005 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+package org.apache.asn1.util.pools;
+
+/**
+ * The super class of every object that can be stored in a IPool
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class PoolObject implements IPoolable
+{
+    //~ Instance fields ----------------------------------------------------------------------------
+
+    /** The pool where objects are stored */
+    private IPool pool;
+
+    /** The pool ID */
+    private int poolId;
+
+    //~ Methods ------------------------------------------------------------------------------------
+
+    /**
+     * Associate a pool to this object
+     *
+     * @param pool The object pool
+     */
+    public void setPool( IPool pool )
+    {
+        this.pool = pool;
+    }
+
+    /**
+     * Get the object's pool
+     *
+     * @return The pool associated with this object
+     */
+    public IPool getPool()
+    {
+
+        return pool;
+    }
+
+    /**
+     * Get the pool ID
+     *
+     * @return The ID of the associated pool
+     */
+    public int getPoolId()
+    {
+
+        return poolId;
+    }
+
+    /**
+     * Set a pool ID
+     *
+     * @param poolId The ID of the associated pool
+     */
+    public void setPoolId( int poolId )
+    {
+        this.poolId = poolId;
+    }
+
+    /**
+     * Free an object by pushing it to its pool.
+     */
+    public void free()
+    {
+        pool.free( this );
+    }
+}

Added: directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/util/pools/PoolStackFullException.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/util/pools/PoolStackFullException.java?view=auto&rev=159172
==============================================================================
--- directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/util/pools/PoolStackFullException.java (added)
+++ directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/util/pools/PoolStackFullException.java Sun Mar 27 10:42:57 2005
@@ -0,0 +1,37 @@
+/*
+ *   Copyright 2005 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+package org.apache.asn1.util.pools;
+
+/**
+ * An exception that is raised if the stack is full.
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class PoolStackFullException extends Exception
+{
+    //~ Constructors -------------------------------------------------------------------------------
+
+    /**
+     * Creates a PoolStackFullException
+     * 
+     * @param message A message with meaning to a human
+     */
+    public PoolStackFullException(  String message )
+    {
+        super(  message );
+    }
+}

Added: directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/util/pools/PoolStackResizeException.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/util/pools/PoolStackResizeException.java?view=auto&rev=159172
==============================================================================
--- directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/util/pools/PoolStackResizeException.java (added)
+++ directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/util/pools/PoolStackResizeException.java Sun Mar 27 10:42:57 2005
@@ -0,0 +1,37 @@
+/*
+ *   Copyright 2005 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+package org.apache.asn1.util.pools;
+
+/**
+ * An exception that is raised if the stack resize could not be done.
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class PoolStackResizeException extends Exception
+{
+    //~ Constructors -------------------------------------------------------------------------------
+
+    /**
+     * Creates a PoolStackResizeException
+     * 
+     * @param message A message with meaning to a human
+     */
+    public PoolStackResizeException(  String message )
+    {
+        super(  message );
+    }
+}



Mime
View raw message