User: lqd     
  Date: 02/02/22 03:15:48

  Modified:    src/main/org/jboss/pool Tag: Branch_2_4 ObjectPool.java
                        PoolObjectFactory.java
  Log:
  - apply patch #515411
  - remove legacy logging w/ PrintWriter, switch to log4j
  
  Revision  Changes    Path
  No                   revision
  
  
  No                   revision
  
  
  1.2.2.4   +123 -74   jbosspool/src/main/org/jboss/pool/ObjectPool.java
  
  Index: ObjectPool.java
  ===================================================================
  RCS file: /cvsroot/jboss/jbosspool/src/main/org/jboss/pool/ObjectPool.java,v
  retrieving revision 1.2.2.3
  retrieving revision 1.2.2.4
  diff -u -r1.2.2.3 -r1.2.2.4
  --- ObjectPool.java   27 Dec 2001 20:50:40 -0000      1.2.2.3
  +++ ObjectPool.java   22 Feb 2002 11:15:48 -0000      1.2.2.4
  @@ -3,7 +3,6 @@
    */
   package org.jboss.pool;
   
  -import java.io.PrintWriter;
   import java.util.ConcurrentModificationException;
   import java.util.HashMap;
   import java.util.HashSet;
  @@ -62,7 +61,6 @@
      private int blockingTimeout = -1;// meaning forever
      private boolean trackLastUsed = false;
      private boolean invalidateOnError = false;
  -   private PrintWriter logWriter = null;
      private Object resizeLock = new Object();
      
      /**
  @@ -157,8 +155,7 @@
         if(poolName != null && !poolName.equals(name))
            throw new IllegalStateException("Cannot change pool name once set!");
         poolName = name;
  -      String category = ObjectPool.class.getName() + "." + name;
  -      log = Logger.getLogger(category);
  +      log = Logger.getLogger( ObjectPool.class.getName() + "." + name);
      }
      
      /**
  @@ -168,27 +165,6 @@
      {return poolName;}
      
      /**
  -    * Gets a log writer used to record pool events.
  -    */
  -   public PrintWriter getLogWriter()
  -   {
  -      return logWriter;
  -   }
  -   
  -   /**
  -    * Sets a log writer used to record pool events.
  -    * @throws java.lang.IllegalStateException
  -    *    Occurs when you try to set the log writer after the pool has been
  -    *    initialized.
  -    */
  -   public void setLogWriter(PrintWriter writer)
  -   {
  -      if(objects != null)
  -         throw new IllegalStateException(INITIALIZED);
  -      logWriter = writer;
  -   }
  -   
  -   /**
       * Sets the minimum size of the pool.  The pool will create this many
       * instances at startup, and once running, it will never shrink below this
       * size.  The default is zero.
  @@ -213,7 +189,9 @@
       * @see #setMinSize
       */
      public int getMinSize()
  -   {return minSize;}
  +   {
  +      return minSize;
  +   }
      
      /**
       * Sets the maximum size of the pool.  Once the pool has grown to hold this
  @@ -237,7 +215,7 @@
         if(maxSize != 0 && minSize > maxSize)
         {
            minSize = maxSize;
  -         log.warn("pool min size set to "+minSize+" to stay <= max size");
  +         log.warn("pool min size set to " + minSize + " to stay <= max size");
         }
      }
      
  @@ -246,7 +224,9 @@
       * @see #setMaxSize
       */
      public int getMaxSize()
  -   {return maxSize;}
  +   {
  +      return maxSize;
  +   }
      
      /**
       * Sets whether the pool should release instances that have not been used
  @@ -348,7 +328,9 @@
       * @see #setMaxIdleTimeoutPercent
       */
      public float getMaxIdleTimeoutPercent()
  -   {return maxIdleShrinkPercent;}
  +   {
  +      return maxIdleShrinkPercent;
  +   }
      
      /**
       * Sets the minimum idle time to release an unused object from the pool.  If
  @@ -368,6 +350,9 @@
         if(objects != null)
            throw new IllegalStateException(INITIALIZED);
         idleTimeoutMillis = millis;
  +
  +      if( log.isDebugEnabled() )
  +         log.debug( "setIdleTimeout(" + millis + ")" );
      }
      
      /**
  @@ -375,7 +360,9 @@
       * @see #setIdleTimeout
       */
      public long getIdleTimeout()
  -   {return idleTimeoutMillis;}
  +   {
  +      return idleTimeoutMillis;
  +   }
      
      /**
       * Sets the minimum idle time to make an object eligible for garbage
  @@ -396,6 +383,9 @@
         if(objects != null)
            throw new IllegalStateException(INITIALIZED);
         gcMinIdleMillis = millis;
  +
  +      if( log.isDebugEnabled() )
  +         log.debug( "setGCMinIdleTime(" + millis + ")" );
      }
      
      /**
  @@ -404,7 +394,9 @@
       * @see #setGCMinIdleTime
       */
      public long getGCMinIdleTime()
  -   {return gcMinIdleMillis;}
  +   {
  +      return gcMinIdleMillis;
  +   }
      
      /**
       * Sets the length of time between garbage collection and idle timeout runs.
  @@ -424,7 +416,11 @@
      {
         if(objects != null)
            throw new IllegalStateException(INITIALIZED);
  +
         gcIntervalMillis = millis;
  +
  +      if( log.isDebugEnabled() )
  +         log.debug( "setGCInterval(" + gcIntervalMillis + ")" );
      }
      
      /**
  @@ -432,7 +428,9 @@
       * @see #setGCInterval
       */
      public long getGCInterval()
  -   {return gcIntervalMillis;}
  +   {
  +      return gcIntervalMillis;
  +   }
      
      /**
       * Sets whether a request for an object will block if the pool size is
  @@ -458,7 +456,9 @@
       * @see #setBlocking
       */
      public boolean isBlocking()
  -   {return blocking;}
  +   {
  +      return blocking;
  +   }
      
      /** sets how long to wait for a free object when blocking, -1 indicating
       *  forever.
  @@ -488,14 +488,20 @@
      {
         if(objects != null)
            throw new IllegalStateException(INITIALIZED);
  +
         trackLastUsed = timestamp;
  +
  +      if( log.isDebugEnabled() )
  +         log.debug( "setTimestampUsed(" + timestamp + ")" );
      }
      
      /**
       * Gets whether object clients can update the last used time.
       */
      public boolean isTimestampUsed()
  -   {return trackLastUsed;}
  +   {
  +      return trackLastUsed;
  +   }
      
      /**
       * Sets the response for object errors.  If this flag is set and an error
  @@ -537,7 +543,7 @@
            throw new IllegalStateException("Cannot initialize more than once!");
         deadObjects = new HashSet();
         objects = new HashMap();
  -      factory.poolStarted(this, logWriter);
  +      factory.poolStarted( this );
         lastGC = System.currentTimeMillis();
         int max = maxSize <= 0 ? minSize : Math.min(minSize, maxSize);
         for(int i=0; i<max; i++)
  @@ -555,7 +561,12 @@
      {
         collector.removePool(this);
         factory.poolClosing(this);
  -      HashMap localObjects = objects;
  +      HashMap localObjects;
  +
  +      synchronized( resizeLock )
  +      {
  +         localObjects = objects;
  +      }
         objects = null;
         
         // close all objects
  @@ -608,7 +619,12 @@
         boolean shouldBlock = blocking;
         while(true)
         {
  -         Iterator it = objects.values().iterator();
  +         Iterator it;
  +         synchronized( resizeLock )
  +         {
  +           it = objects.values().iterator();
  +         }
  +
            while(it.hasNext())
            {
               ObjectRecord rec = (ObjectRecord)it.next();
  @@ -650,18 +666,20 @@
            
            if(shouldBlock)
            {
  -            if( log.isTraceEnabled() )
  -               log.trace("Pool "+this+" waiting for a free object");
               synchronized(this)
               {
                  try
                  {
                     if (blockingTimeout > 0)
                     {
  +                     if( log.isTraceEnabled() )
  +                        log.trace("Pool " + this + " waiting for a free object (" + 
blockingTimeout + "ms)");
                        wait(blockingTimeout);
                        shouldBlock = false; //don't wait again
                     } else
                     {
  +                     if( log.isTraceEnabled() )
  +                        log.trace("Pool " + this + " waiting for a free object 
(indefinite)");
                        wait();
                     }
                  } catch(InterruptedException e)
  @@ -738,6 +756,9 @@
         if(objects == null)
            throw new IllegalStateException("Tried to use pool before it was 
Initialized or after it was ShutDown!");
         boolean removed = false;  // Whether we returned to the pool, or threw out 
entirely
  +
  +      if( log.isTraceEnabled() )
  +         log.trace("Pool " + this + " object released: " + object);
         
         synchronized(object)
         {
  @@ -806,7 +827,8 @@
         while(it.hasNext())
         {
            ObjectRecord or = (ObjectRecord)it.next();
  -         if(or.isInUse()) ++total;
  +         if( or != null && or.isInUse() )
  +            ++total;
         }
         return total;
      }
  @@ -852,19 +874,33 @@
      
      long getNextGCMillis(long now)
      {
  -      if(!runGC) return Long.MAX_VALUE;
  -      return lastGC + gcIntervalMillis - now;
  +      long t = lastGC + gcIntervalMillis - now;
  +      if( log.isTraceEnabled() )
  +         log.trace("getNextGCMillis(): returning " + t);
  +
  +      if(!runGC)
  +         return Long.MAX_VALUE;
  +
  +      return t;
      }
      
      // Allow GC if we're within 10% of the desired interval
      boolean isTimeToGC()
      {
  -      return System.currentTimeMillis() >=
  -      lastGC + Math.round((float)gcIntervalMillis * 0.9f);
  +      long now;
  +      now = System.currentTimeMillis();
  +      if( log.isTraceEnabled() )
  +         log.trace("isTimeToGC(): " + (now >= lastGC + 
Math.round((float)gcIntervalMillis * 0.9f)));
  +
  +      return now >= lastGC + Math.round((float)gcIntervalMillis * 0.9f);
  +      
      }
      
      void runGCandShrink()
      {
  +      if( log.isTraceEnabled() )
  +         log.trace("runGCandShrink(): runGC = " + runGC + "; idleTimeout = " + 
idleTimeout);
  +
         if (runGC || idleTimeout)
         {
            HashSet objsCopy;
  @@ -878,7 +914,7 @@
               while(it.hasNext())
               {
                  ObjectRecord rec = (ObjectRecord)it.next();
  -               if(rec.isInUse() && rec.getMillisSinceLastUse() >= gcMinIdleMillis)
  +               if( rec != null && rec.isInUse() && rec.getMillisSinceLastUse() >= 
gcMinIdleMillis )
                  {
                     releaseObject(rec.getClientObject());
                  }
  @@ -892,7 +928,7 @@
               while(it.hasNext())
               {
                  ObjectRecord rec = (ObjectRecord)it.next();
  -               if(!rec.isInUse() && rec.getMillisSinceLastUse() > idleTimeoutMillis)
  +               if( rec != null && !rec.isInUse() && rec.getMillisSinceLastUse() > 
idleTimeoutMillis )
                     eligible.add(rec);
               }
               // Calculate max number of objects to remove without replacing
  @@ -907,19 +943,22 @@
                  {
                     // Delete the object
                     ObjectRecord rec = (ObjectRecord)it.next();
  -                  rec.setInUse(true);  // Don't let someone use it while we destroy 
it
  -                  Object pooled = rec.getObject();
  -                  removeObject(pooled);
  -                  try
  +                  if( rec != null )
                     {
  -                     factory.deleteObject(pooled);
  +                     rec.setInUse(true);  // Don't let someone use it while we 
destroy it
  +                     Object pooled = rec.getObject();
  +                     removeObject(pooled);
  +                     try
  +                     {
  +                        factory.deleteObject(pooled);
  +                     }
  +                     catch(Exception e)
  +                     {
  +                        log.error("Pool "+this+" factory 
("+factory.getClass().getName()+" delete error: ", e);
  +                     }
  +                     rec.close();
  +                     ++count;
                     }
  -                  catch(Exception e)
  -                  {
  -                     log.error("Pool "+this+" factory 
("+factory.getClass().getName()+" delete error: ", e);
  -                  }
  -                  rec.close();
  -                  ++count;
                     
                     if(count > max || objects.size() < minSize)
                        createNewObject(null, false);
  @@ -969,38 +1008,46 @@
                  if(forImmediateUse)
                  {
                     Object result = factory.prepareObject(ob);
  -                  if(result != ob) rec.setClientObject(result);
  +                  if(result != ob)
  +                     rec.setClientObject(result);
                     if(result instanceof PooledObject)
                        ((PooledObject)result).addPoolEventListener(this);
                     message = "Pool "+this+" gave out new object: "+result;
                     ob = result;
  -               } else message = "Pool "+poolName+" created a new object: "+ob;
  +               }
  +               else
  +               {
  +                  message = "Pool "+poolName+" created a new object: "+ob;
  +               }
                  objects = newMap;
  -            } else message = "Pool "+poolName+" factory "+factory+" unable to 
create new object!";
  -         } else message = "Pool "+poolName+" is full 
("+objects.size()+"/"+maxSize+")!";
  +            }
  +            else
  +            {
  +               message = "Pool "+poolName+" factory "+factory+" unable to create 
new object!";
  +            }
  +         }
  +         else
  +         {
  +            message = "Pool "+poolName+" is full ("+objects.size()+"/"+maxSize+")!";
  +         }
         }
         if(message != null && log.isTraceEnabled() )
            log.trace(message);
         return ob;
      }
  -   
  -   private void log(String message)
  -   {
  -      if(logWriter != null)
  -         logWriter.println(message);
  -   }
   }
   
   class BeanFactory extends PoolObjectFactory
   {
      private Class beanClass;
  -   private PrintWriter logger;
  +
  +   private Logger log = Logger.getLogger( BeanFactory.class );
      
  -   public BeanFactory(Class beanClass)
  +   public BeanFactory( Class beanClass )
      {
         try
         {
  -         beanClass.getConstructor(new Class[0]);
  +         beanClass.getConstructor( new Class[0] );
         } catch(NoSuchMethodException e)
         {
            throw new IllegalArgumentException("Bean class doesn't have no-arg 
constructor!");
  @@ -1008,10 +1055,9 @@
         this.beanClass = beanClass;
      }
      
  -   public void poolStarted(ObjectPool pool, PrintWriter log)
  +   public void poolStarted( ObjectPool pool )
      {
  -      super.poolStarted(pool, log);
  -      logger = log;
  +      super.poolStarted( pool );
      }
      
      public Object createObject(Object parameters)
  @@ -1021,8 +1067,11 @@
            return beanClass.newInstance();
         } catch(Exception e)
         {
  -         logger.println("Unable to create instance of "+beanClass.getName()+": "+e);
  +         log.error( "Unable to create instance of " + beanClass.getName(), e);
         }
         return null;
      }
   }
  +/*
  +vim:tabstop=3:et:shiftwidth=3
  +*/
  
  
  
  1.1.1.1.2.1 +1 -5      jbosspool/src/main/org/jboss/pool/PoolObjectFactory.java
  
  Index: PoolObjectFactory.java
  ===================================================================
  RCS file: /cvsroot/jboss/jbosspool/src/main/org/jboss/pool/PoolObjectFactory.java,v
  retrieving revision 1.1.1.1
  retrieving revision 1.1.1.1.2.1
  diff -u -r1.1.1.1 -r1.1.1.1.2.1
  --- PoolObjectFactory.java    15 May 2001 07:58:24 -0000      1.1.1.1
  +++ PoolObjectFactory.java    22 Feb 2002 11:15:48 -0000      1.1.1.1.2.1
  @@ -3,8 +3,6 @@
    */
   package org.jboss.pool;
   
  -import java.io.PrintWriter;
  -
   /**
    * Creates objects to be used in an object pool.  This is a class instead of
    * an interface so you can ignore any of the methods you don't need.
  @@ -42,12 +40,10 @@
        * @param pool The pool that is starting.  You may decide to allow
        *    multiple pools you use your factory, or to restrict it to a one-to-one
        *    relationship.
  -     * @param log A writer you can use to log messages.  Use this in preference
  -     *    to System.xxx.println.
        * @throws java.lang.IllegalArgumentException
        *         Occurs when the pool is null.
        */
  -    public void poolStarted(ObjectPool pool, PrintWriter log) {
  +    public void poolStarted( ObjectPool pool ) {
           if(pool == null)
               throw new IllegalArgumentException("Cannot start factory with null 
pool!");
       }
  
  
  

_______________________________________________
Jboss-development mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/jboss-development

Reply via email to