commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rwaldh...@apache.org
Subject cvs commit: jakarta-commons/pool/src/java/org/apache/commons/pool KeyedObjectPool.java BaseKeyedObjectPool.java
Date Thu, 24 Apr 2003 18:07:10 GMT
rwaldhoff    2003/04/24 11:07:10

  Modified:    pool/src/java/org/apache/commons/pool/impl
                        StackKeyedObjectPool.java
                        GenericKeyedObjectPool.java
               pool/src/test/org/apache/commons/pool
                        TestKeyedObjectPool.java
               pool/src/java/org/apache/commons/pool KeyedObjectPool.java
                        BaseKeyedObjectPool.java
  Log:
  add optional KeyedObjectPool.addObject(key) method and test
  refactor extract getActiveCount(key), incrementActiveCount(key) and decrementActiveCount(key)
methods in GenericKeyedObjectPool and StackKeyedObjectPool
  
  Revision  Changes    Path
  1.10      +51 -26    jakarta-commons/pool/src/java/org/apache/commons/pool/impl/StackKeyedObjectPool.java
  
  Index: StackKeyedObjectPool.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/pool/src/java/org/apache/commons/pool/impl/StackKeyedObjectPool.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- StackKeyedObjectPool.java	14 Mar 2003 00:12:48 -0000	1.9
  +++ StackKeyedObjectPool.java	24 Apr 2003 18:07:09 -0000	1.10
  @@ -172,7 +172,6 @@
               stack = new Stack();
               stack.ensureCapacity( _initSleepingCapacity > _maxSleeping ? _maxSleeping
: _initSleepingCapacity);
               _pools.put(key,stack);
  -            _activeCount.put(key,new Integer(0));
           }
           try {
               obj = stack.pop();
  @@ -187,21 +186,18 @@
           if(null != obj && null != _factory) {
               _factory.activateObject(key,obj);
           }
  -        _totActive++;
  -        Integer old = (Integer)(_activeCount.get(key));
  -        _activeCount.put(key,new Integer(old.intValue() + 1));
  +        incrementActiveCount(key);
           return obj;
       }
   
       public synchronized void returnObject(Object key, Object obj) throws Exception {
  -        _totActive--;
  +        decrementActiveCount(key);
           if(null == _factory || _factory.validateObject(key,obj)) {
               Stack stack = (Stack)(_pools.get(key));
               if(null == stack) {
                   stack = new Stack();
                   stack.ensureCapacity( _initSleepingCapacity > _maxSleeping ? _maxSleeping
: _initSleepingCapacity);
                   _pools.put(key,stack);
  -                _activeCount.put(key,new Integer(1));
               }
               if(null != _factory) {
                   try {
  @@ -224,20 +220,24 @@
                   _factory.destroyObject(key,obj);
               }
           }
  -        Integer old = (Integer)(_activeCount.get(key));
  -        _activeCount.put(key,new Integer(old.intValue() - 1));
       }
   
       public synchronized void invalidateObject(Object key, Object obj) throws Exception
{
  -        _totActive--;
  -        Integer old = (Integer)(_activeCount.get(key));
  -        _activeCount.put(key,new Integer(old.intValue() - 1));
  +        decrementActiveCount(key);
           if(null != _factory) {
               _factory.destroyObject(key,obj);
           }
           notifyAll(); // _totalActive has changed
       }
   
  +    public void addObject(Object key) throws Exception {
  +        Object obj = _factory.makeObject(key);
  +        synchronized(this) {
  +            incrementActiveCount(key); // returnObject will decrement this
  +            returnObject(key,obj);
  +        }
  +    }
  +
       public int getNumIdle() {
           return _totIdle;
       }
  @@ -247,13 +247,7 @@
       }
   
       public int getNumActive(Object key) {
  -        try {
  -            return ((Integer)_activeCount.get(key)).intValue();
  -        } catch(NoSuchElementException e) {
  -            return 0;
  -        } catch(NullPointerException e) {
  -            return 0;
  -        }
  +        return getActiveCount(key);
       }
   
       public synchronized int getNumIdle(Object key) {
  @@ -281,7 +275,7 @@
           destroyStack(key,stack);
       }
   
  -    private synchronized void destroyStack(Object key,Stack stack) {
  +    private synchronized void destroyStack(Object key, Stack stack) {
           if(null == stack) {
               return;
           } else {
  @@ -296,7 +290,7 @@
                   }
               }
               _totIdle -= stack.size();
  -            _activeCount.put(key,new Integer(0));
  +            _activeCount.remove(key);
               stack.clear();
           }
       }
  @@ -315,14 +309,14 @@
           return buf.toString();
       }
   
  -    synchronized public void close() throws Exception {
  +    public synchronized void close() throws Exception {
           clear();
           _pools = null;
           _factory = null;
           _activeCount = null;
       }
   
  -    synchronized public void setFactory(KeyedPoolableObjectFactory factory) throws IllegalStateException
{
  +    public synchronized void setFactory(KeyedPoolableObjectFactory factory) throws IllegalStateException
{
           if(0 < getNumActive()) {
               throw new IllegalStateException("Objects are already active");
           } else {
  @@ -331,6 +325,37 @@
           }
       }
   
  +    private int getActiveCount(Object key) {
  +        try {
  +            return ((Integer)_activeCount.get(key)).intValue();
  +        } catch(NoSuchElementException e) {
  +            return 0;
  +        } catch(NullPointerException e) {
  +            return 0;
  +        }
  +    }
  +
  +    private void incrementActiveCount(Object key) {
  +        _totActive++;
  +        Integer old = (Integer)(_activeCount.get(key));
  +        if(null == old) { 
  +            _activeCount.put(key,new Integer(1));
  +        } else {
  +            _activeCount.put(key,new Integer(old.intValue() + 1));
  +        }
  +    }
  +
  +    private void decrementActiveCount(Object key) {
  +        _totActive--;
  +        Integer active = (Integer)(_activeCount.get(key));
  +        if(null == active) {
  +            // do nothing, either null or zero is OK
  +        } else if(active.intValue() <= 1) {
  +            _activeCount.remove(key);
  +        } else {
  +            _activeCount.put(key, new Integer(active.intValue() - 1));
  +        }
  +    }
   
       /** The default cap on the number of "sleeping" instances in the pool. */
       protected static final int DEFAULT_MAX_SLEEPING  = 8;
  
  
  
  1.17      +51 -44    jakarta-commons/pool/src/java/org/apache/commons/pool/impl/GenericKeyedObjectPool.java
  
  Index: GenericKeyedObjectPool.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/pool/src/java/org/apache/commons/pool/impl/GenericKeyedObjectPool.java,v
  retrieving revision 1.16
  retrieving revision 1.17
  diff -u -r1.16 -r1.17
  --- GenericKeyedObjectPool.java	18 Apr 2003 21:36:51 -0000	1.16
  +++ GenericKeyedObjectPool.java	24 Apr 2003 18:07:09 -0000	1.17
  @@ -750,11 +750,7 @@
               if(null == pair) {
                   // check if we can create one
                   // (note we know that the num sleeping is 0, else we wouldn't be here)
  -                int active = 0;
  -                Integer act = (Integer)(_activeMap.get(key));
  -                if(null != act) {
  -                    active = act.intValue();
  -                }
  +                int active = getActiveCount(key);
                   if(_maxActive <= 0 || active < _maxActive) {
                       Object obj = _factory.makeObject(key);
                       pair = new ObjectTimestampPair(obj);
  @@ -795,13 +791,7 @@
                       throw new NoSuchElementException("Could not create a validated object");
                   } // else keep looping
               } else {
  -                Integer active = (Integer)(_activeMap.get(key));
  -                if(null == active) {
  -                    _activeMap.put(key,new Integer(1));
  -                } else {
  -                    _activeMap.put(key,new Integer(active.intValue() + 1));
  -                }
  -                _totalActive++;
  +                incrementActiveCount(key);
                   return pair.value;
               }
           }
  @@ -854,11 +844,7 @@
       }
   
       public synchronized int getNumActive(Object key) {
  -        try {
  -            return((Integer)(_activeMap.get(key))).intValue();
  -        } catch(Exception e) {
  -            return 0;
  -        }
  +        return getActiveCount(key);
       }
   
       public synchronized int getNumIdle(Object key) {
  @@ -898,18 +884,8 @@
                   _poolMap.put(key, pool);
                   _poolList.add(key);
               }
  -            // subtract one from the total and keyed active counts
  -            _totalActive--;
  -            Integer active = (Integer)(_activeMap.get(key));
  -            if(null == active) {
  -                // do nothing, either null or zero is OK
  -            } else if(active.intValue() <= 1) {
  -                _activeMap.remove(key);
  -            } else {
  -                _activeMap.put(key, new Integer(active.intValue() - 1));
  -            }
  -            // if there's no space in the pool, flag the object
  -            // for destruction
  +            decrementActiveCount(key);
  +            // if there's no space in the pool, flag the object for destruction
               // else if we passivated succesfully, return it to the pool
               if(_maxIdle >= 0 && (pool.size() >= _maxIdle)) {
                   shouldDestroy = true;
  @@ -930,20 +906,20 @@
       }
   
       public synchronized void invalidateObject(Object key, Object obj) throws Exception
{
  -        _totalActive--;
  -        Integer active = (Integer)(_activeMap.get(key));
  -        if(null == active) {
  -            // do nothing, either null or zero is OK
  -        } else if(active.intValue() <= 1) {
  -            _activeMap.remove(key);
  -        } else {
  -            _activeMap.put(key, new Integer(active.intValue() - 1));
  -        }
  +        decrementActiveCount(key);
           _factory.destroyObject(key,obj);
           notifyAll(); // _totalActive has changed
       }
   
  -    synchronized public void close() throws Exception {
  +    public void addObject(Object key) throws Exception {
  +        Object obj = _factory.makeObject(key);
  +        synchronized(this) {
  +            incrementActiveCount(key); // returnObject will decrement this
  +            returnObject(key,obj);
  +        }
  +    }
  +
  +    public synchronized void close() throws Exception {
           clear();
           _poolList = null;
           _poolMap = null;
  @@ -1057,7 +1033,7 @@
           }
       }
   
  -    //--- package methods --------------------------------------------
  +    //--- package and private methods -----------------------------------------
   
       synchronized String debugInfo() {
           StringBuffer buf = new StringBuffer();
  @@ -1076,6 +1052,37 @@
           } else {
               return(int)(Math.ceil((double)_totalIdle/Math.abs((double)_numTestsPerEvictionRun)));
           }
  +    }
  +
  +    private void incrementActiveCount(Object key) {
  +        _totalActive++;
  +        Integer active = (Integer)(_activeMap.get(key));
  +        if(null == active) {
  +            _activeMap.put(key,new Integer(1));
  +        } else {
  +            _activeMap.put(key,new Integer(active.intValue() + 1));
  +        }
  +    }
  +
  +    private void decrementActiveCount(Object key) {
  +        _totalActive--;
  +        Integer active = (Integer)(_activeMap.get(key));
  +        if(null == active) {
  +            // do nothing, either null or zero is OK
  +        } else if(active.intValue() <= 1) {
  +            _activeMap.remove(key);
  +        } else {
  +            _activeMap.put(key, new Integer(active.intValue() - 1));
  +        }
  +    }
  +
  +    private int getActiveCount(Object key) {
  +        int active = 0;
  +        Integer act = (Integer)(_activeMap.get(key));
  +        if(null != act) {
  +            active = act.intValue();
  +        }
  +        return active;
       }
   
       //--- inner classes ----------------------------------------------
  
  
  
  1.5       +37 -4     jakarta-commons/pool/src/test/org/apache/commons/pool/TestKeyedObjectPool.java
  
  Index: TestKeyedObjectPool.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/pool/src/test/org/apache/commons/pool/TestKeyedObjectPool.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- TestKeyedObjectPool.java	24 Apr 2003 17:44:08 -0000	1.4
  +++ TestKeyedObjectPool.java	24 Apr 2003 18:07:10 -0000	1.5
  @@ -267,5 +267,38 @@
           assertEquals(0,_pool.getNumIdle(keya));
       }
   
  +    public void testBaseAddObject() throws Exception {
  +        try {
  +            _pool = makeEmptyPool(3);
  +        } catch(IllegalArgumentException e) {
  +            return; // skip this test if unsupported
  +        }
  +        Object key = makeKey(0);
  +        try {
  +            assertEquals(0,_pool.getNumIdle());
  +            assertEquals(0,_pool.getNumActive());
  +            assertEquals(0,_pool.getNumIdle(key));
  +            assertEquals(0,_pool.getNumActive(key));
  +            _pool.addObject(key);
  +            assertEquals(1,_pool.getNumIdle());
  +            assertEquals(0,_pool.getNumActive());
  +            assertEquals(1,_pool.getNumIdle(key));
  +            assertEquals(0,_pool.getNumActive(key));
  +            Object obj = _pool.borrowObject(key);
  +            assertEquals(getNthObject(key,0),obj);
  +            assertEquals(0,_pool.getNumIdle());
  +            assertEquals(1,_pool.getNumActive());
  +            assertEquals(0,_pool.getNumIdle(key));
  +            assertEquals(1,_pool.getNumActive(key));
  +            _pool.returnObject(key,obj);
  +            assertEquals(1,_pool.getNumIdle());
  +            assertEquals(0,_pool.getNumActive());
  +            assertEquals(1,_pool.getNumIdle(key));
  +            assertEquals(0,_pool.getNumActive(key));
  +        } catch(UnsupportedOperationException e) {
  +            return; // skip this test if one of those calls is unsupported
  +        }
  +    }
  +
       private KeyedObjectPool _pool = null;
   }
  
  
  
  1.10      +12 -6     jakarta-commons/pool/src/java/org/apache/commons/pool/KeyedObjectPool.java
  
  Index: KeyedObjectPool.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/pool/src/java/org/apache/commons/pool/KeyedObjectPool.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- KeyedObjectPool.java	24 Apr 2003 01:34:45 -0000	1.9
  +++ KeyedObjectPool.java	24 Apr 2003 18:07:10 -0000	1.10
  @@ -91,8 +91,6 @@
    * {@link ObjectPool pools}).
    * </p>
    *
  - * @TODO add optional addObject(Object key) method here and to implementations
  - * 
    * @author Rodney Waldhoff
    * @version $Revision$ $Date$
    *
  @@ -149,6 +147,14 @@
        * @param obj a {@link #borrowObject borrowed} instance to be returned.
        */
       void invalidateObject(Object key, Object obj) throws Exception;
  +
  +    /**
  +     * Create an object using my {@link #setFactory factory} or other
  +     * implementation dependent mechanism, and place it into the pool.
  +     * addObject() is useful for "pre-loading" a pool with idle objects.
  +     * (Optional operation).
  +     */
  +    void addObject(Object key) throws Exception;
   
       /**
        * Returns the number of instances
  
  
  
  1.6       +11 -4     jakarta-commons/pool/src/java/org/apache/commons/pool/BaseKeyedObjectPool.java
  
  Index: BaseKeyedObjectPool.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/pool/src/java/org/apache/commons/pool/BaseKeyedObjectPool.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- BaseKeyedObjectPool.java	25 Feb 2003 23:38:20 -0000	1.5
  +++ BaseKeyedObjectPool.java	24 Apr 2003 18:07:10 -0000	1.6
  @@ -77,6 +77,13 @@
       /**
        * Not supported in this base implementation.
        */
  +    public void addObject(Object key) throws Exception, UnsupportedOperationException {
  +        throw new UnsupportedOperationException();
  +    }
  +
  +    /**
  +     * Not supported in this base implementation.
  +     */
       public int getNumIdle(Object key) throws UnsupportedOperationException {
           throw new UnsupportedOperationException();
       }
  
  
  

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


Mime
View raw message