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/impl GenericObjectPool.java
Date Thu, 31 Oct 2002 18:56:11 GMT
rwaldhoff    2002/10/31 10:56:11

  Modified:    pool/src/java/org/apache/commons/pool/impl
                        GenericObjectPool.java
  Log:
  extract evict() from inner class
  make some protected stuff private
  
  Revision  Changes    Path
  1.10      +105 -95   jakarta-commons/pool/src/java/org/apache/commons/pool/impl/GenericObjectPool.java
  
  Index: GenericObjectPool.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/pool/src/java/org/apache/commons/pool/impl/GenericObjectPool.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- GenericObjectPool.java	30 Oct 2002 22:54:41 -0000	1.9
  +++ GenericObjectPool.java	31 Oct 2002 18:56:11 -0000	1.10
  @@ -1,5 +1,5 @@
   /*
  - * $Header$
  + * $Id$
    * $Revision$
    * $Date$
    *
  @@ -7,7 +7,7 @@
    *
    * The Apache Software License, Version 1.1
    *
  - * Copyright (c) 1999-2002 The Apache Software Foundation.  All rights
  + * Copyright (c) 2001-2002 The Apache Software Foundation.  All rights
    * reserved.
    *
    * Redistribution and use in source and binary forms, with or without
  @@ -61,14 +61,13 @@
   
   package org.apache.commons.pool.impl;
   
  -import org.apache.commons.pool.*;
  -import org.apache.commons.collections.CursorableLinkedList;
  -import org.apache.commons.collections.CursorableLinkedList.Cursor;
   import java.util.Iterator;
   import java.util.NoSuchElementException;
  -import java.util.Enumeration;
  -import java.util.EmptyStackException;
  -import java.util.ListIterator;
  +
  +import org.apache.commons.collections.CursorableLinkedList;
  +import org.apache.commons.pool.BaseObjectPool;
  +import org.apache.commons.pool.ObjectPool;
  +import org.apache.commons.pool.PoolableObjectFactory;
   
   /**
    * A configurable {@link ObjectPool} implementation.
  @@ -835,6 +834,10 @@
           clear();
           _pool = null;
           _factory = null;
  +        if(null != _evictionCursor) {
  +            _evictionCursor.close();
  +            _evictionCursor = null;
  +        }
           if(null != _evictor) {
               _evictor.cancel();
               _evictor = null;
  @@ -850,6 +853,66 @@
           }
       }
   
  +    public synchronized void evict() throws Exception {
  +        if(!_pool.isEmpty()) {
  +            if(null == _evictionCursor) {
  +                _evictionCursor = (CursorableLinkedList.Cursor)(_pool.cursor(_pool.size()));
  +            } else if(!_evictionCursor.hasPrevious()) {
  +                _evictionCursor.close();
  +                _evictionCursor = (CursorableLinkedList.Cursor)(_pool.cursor(_pool.size()));
  +            }
  +            for(int i=0,m=getNumTests();i<m;i++) {
  +                if(!_evictionCursor.hasPrevious()) {
  +                    _evictionCursor.close();
  +                    _evictionCursor = (CursorableLinkedList.Cursor)(_pool.cursor(_pool.size()));
  +                } else {
  +                    ObjectTimestampPair pair = (ObjectTimestampPair)(_evictionCursor.previous());
  +                    if(_minEvictableIdleTimeMillis > 0 &&
  +                       System.currentTimeMillis() - pair.tstamp > _minEvictableIdleTimeMillis)
{
  +                       try {
  +                           _evictionCursor.remove();
  +                           _factory.destroyObject(pair.value);
  +                       } catch(Exception e) {
  +                           ; // ignored
  +                       }
  +                    } else if(_testWhileIdle) {
  +                        boolean active = false;
  +                        try {
  +                            _factory.activateObject(pair.value);
  +                            active = true;
  +                        } catch(Exception e) {
  +                            _evictionCursor.remove();
  +                            try {
  +                                _factory.passivateObject(pair.value);
  +                            } catch(Exception ex) {
  +                                ; // ignored
  +                            }
  +                            _factory.destroyObject(pair.value);
  +                        }
  +                        if(active) {
  +                            if(!_factory.validateObject(pair.value)) {
  +                                _evictionCursor.remove();
  +                                try {
  +                                    _factory.passivateObject(pair.value);
  +                                } catch(Exception ex) {
  +                                    ; // ignored
  +                                }
  +                                _factory.destroyObject(pair.value);
  +                            } else {
  +                                try {
  +                                    _factory.passivateObject(pair.value);
  +                                } catch(Exception e) {
  +                                    _evictionCursor.remove();
  +                                    _factory.destroyObject(pair.value);
  +                                }
  +                            }
  +                        }
  +                    }
  +                }
  +            }
  +        }
  +    }
  +    
       //--- package methods --------------------------------------------
   
       synchronized String debugInfo() {
  @@ -866,6 +929,14 @@
           return buf.toString();
       }
   
  +    private int getNumTests() {
  +        if(_numTestsPerEvictionRun >= 0) {
  +            return _numTestsPerEvictionRun;
  +        } else {
  +            return(int)(Math.ceil((double)_pool.size()/Math.abs((double)_numTestsPerEvictionRun)));
  +        }
  +    }
  +
       //--- inner classes ----------------------------------------------
   
       /**
  @@ -898,7 +969,6 @@
           }
   
           public void run() {
  -            CursorableLinkedList.Cursor cursor = null;
               while(!_cancelled) {
                   long sleeptime = 0L;
                   synchronized(GenericObjectPool.this) {
  @@ -910,81 +980,19 @@
                       ; // ignored
                   }
                   try {
  -                    synchronized(GenericObjectPool.this) {
  -                        if(!_pool.isEmpty()) {
  -                            if(null == cursor) {
  -                                cursor = (CursorableLinkedList.Cursor)(_pool.cursor(_pool.size()));
  -                            } else if(!cursor.hasPrevious()) {
  -                                cursor.close();
  -                                cursor = (CursorableLinkedList.Cursor)(_pool.cursor(_pool.size()));
  -                            }
  -                            for(int i=0,m=getNumTests();i<m;i++) {
  -                                if(!cursor.hasPrevious()) {
  -                                    cursor.close();
  -                                    cursor = (CursorableLinkedList.Cursor)(_pool.cursor(_pool.size()));
  -                                } else {
  -                                    ObjectTimestampPair pair = (ObjectTimestampPair)(cursor.previous());
  -                                    if(_minEvictableIdleTimeMillis > 0 &&
  -                                       System.currentTimeMillis() - pair.tstamp > _minEvictableIdleTimeMillis)
{
  -                                       try {
  -                                           cursor.remove();
  -                                           _factory.destroyObject(pair.value);
  -                                       } catch(Exception e) {
  -                                           ; // ignored
  -                                       }
  -                                    } else if(_testWhileIdle) {
  -                                        boolean active = false;
  -                                        try {
  -                                            _factory.activateObject(pair.value);
  -                                            active = true;
  -                                        } catch(Exception e) {
  -                                            cursor.remove();
  -                                            try {
  -                                                _factory.passivateObject(pair.value);
  -                                            } catch(Exception ex) {
  -                                                ; // ignored
  -                                            }
  -                                            _factory.destroyObject(pair.value);
  -                                        }
  -                                        if(active) {
  -                                            if(!_factory.validateObject(pair.value)) {
  -                                                cursor.remove();
  -                                                try {
  -                                                    _factory.passivateObject(pair.value);
  -                                                } catch(Exception ex) {
  -                                                    ; // ignored
  -                                                }
  -                                                _factory.destroyObject(pair.value);
  -                                            } else {
  -                                                try {
  -                                                    _factory.passivateObject(pair.value);
  -                                                } catch(Exception e) {
  -                                                    cursor.remove();
  -                                                    _factory.destroyObject(pair.value);
  -                                                }
  -                                            }
  -                                        }
  -                                    }
  -                                }
  -                            }
  -                        }
  -                    }
  +                    evict();
                   } catch(Exception e) {
                       // ignored
                   }
               }
  -            if(null != cursor) {
  -                cursor.close();
  +            synchronized(GenericObjectPool.this) {
  +                if(null != _evictionCursor) {
  +                    _evictionCursor.close();
  +                    _evictionCursor = null;
  +                }
               }
           }
   
  -        private int getNumTests() {
  -            if(_numTestsPerEvictionRun >= 0) {
  -                return _numTestsPerEvictionRun;
  -            } else {
  -                return(int)(Math.ceil((double)_pool.size()/Math.abs((double)_numTestsPerEvictionRun)));
  -            }
  -        }
       }
   
       /**
  @@ -1006,21 +1014,21 @@
           public long minEvictableIdleTimeMillis = GenericObjectPool.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS;
       }
   
  -    //--- protected attributes ---------------------------------------
  +    //--- private attributes ---------------------------------------
   
       /**
        * The cap on the number of idle instances in the pool.
        * @see #setMaxIdle
        * @see #getMaxIdle
        */
  -    protected int _maxIdle = DEFAULT_MAX_IDLE;
  +    private int _maxIdle = DEFAULT_MAX_IDLE;
   
       /**
        * The cap on the total number of active instances from the pool.
        * @see #setMaxActive
        * @see #getMaxActive
        */
  -    protected int _maxActive = DEFAULT_MAX_ACTIVE;
  +    private int _maxActive = DEFAULT_MAX_ACTIVE;
   
       /**
        * The maximum amount of time (in millis) the
  @@ -1038,7 +1046,7 @@
        * @see #setWhenExhaustedAction
        * @see #getWhenExhaustedAction
        */
  -    protected long _maxWait = DEFAULT_MAX_WAIT;
  +    private long _maxWait = DEFAULT_MAX_WAIT;
   
       /**
        * The action to take when the {@link #borrowObject} method
  @@ -1052,7 +1060,7 @@
        * @see #setWhenExhaustedAction
        * @see #getWhenExhaustedAction
        */
  -    protected byte _whenExhaustedAction = DEFAULT_WHEN_EXHAUSTED_ACTION;
  +    private byte _whenExhaustedAction = DEFAULT_WHEN_EXHAUSTED_ACTION;
   
       /**
        * When <tt>true</tt>, objects will be
  @@ -1065,7 +1073,7 @@
        * @see #setTestOnBorrow
        * @see #getTestOnBorrow
        */
  -    protected boolean _testOnBorrow = DEFAULT_TEST_ON_BORROW;
  +    private boolean _testOnBorrow = DEFAULT_TEST_ON_BORROW;
   
       /**
        * When <tt>true</tt>, objects will be
  @@ -1076,7 +1084,7 @@
        * @see #getTestOnReturn
        * @see #setTestOnReturn
        */
  -    protected boolean _testOnReturn = DEFAULT_TEST_ON_RETURN;
  +    private boolean _testOnReturn = DEFAULT_TEST_ON_RETURN;
   
       /**
        * When <tt>true</tt>, objects will be
  @@ -1089,7 +1097,7 @@
        * @see #getTimeBetweenEvictionRunsMillis
        * @see #setTimeBetweenEvictionRunsMillis
        */
  -    protected boolean _testWhileIdle = DEFAULT_TEST_WHILE_IDLE;
  +    private boolean _testWhileIdle = DEFAULT_TEST_WHILE_IDLE;
   
       /**
        * The number of milliseconds to sleep between runs of the
  @@ -1100,7 +1108,7 @@
        * @see #setTimeBetweenEvictionRunsMillis
        * @see #getTimeBetweenEvictionRunsMillis
        */
  -    protected long _timeBetweenEvictionRunsMillis = DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS;
  +    private long _timeBetweenEvictionRunsMillis = DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS;
   
       /**
        * The number of objects to examine during each run of the
  @@ -1115,7 +1123,7 @@
        * @see #getTimeBetweenEvictionRunsMillis
        * @see #setTimeBetweenEvictionRunsMillis
        */
  -    protected int _numTestsPerEvictionRun =  DEFAULT_NUM_TESTS_PER_EVICTION_RUN;
  +    private int _numTestsPerEvictionRun =  DEFAULT_NUM_TESTS_PER_EVICTION_RUN;
   
       /**
        * The minimum amount of time an object may sit idle in the pool
  @@ -1129,22 +1137,24 @@
        * @see #getTimeBetweenEvictionRunsMillis
        * @see #setTimeBetweenEvictionRunsMillis
        */
  -    protected long _minEvictableIdleTimeMillis = DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS;
  +    private long _minEvictableIdleTimeMillis = DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS;
   
       /** My pool. */
  -    protected CursorableLinkedList _pool = null;
  +    private CursorableLinkedList _pool = null;
   
       /** My {@link PoolableObjectFactory}. */
  -    protected PoolableObjectFactory _factory = null;
  +    private PoolableObjectFactory _factory = null;
   
       /**
        * The number of objects {@link #borrowObject} borrowed
        * from the pool, but not yet returned.
        */
  -    protected int _numActive = 0;
  +    private int _numActive = 0;
   
       /**
        * My idle object eviction thread, if any.
        */
  -    protected Evictor _evictor = null;
  +    private Evictor _evictor = null;
  +    
  +    private CursorableLinkedList.Cursor _evictionCursor = null;
   }
  
  
  

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


Mime
View raw message