Coverage Report - org.apache.commons.cache.StaleObjectEvictor
 
Classes in this File Line Coverage Branch Coverage Complexity
StaleObjectEvictor
0%
0/47
0%
0/10
2.333
 
 1  
 /*
 2  
  * Copyright 2001-2004 The Apache Software Foundation
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *     http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.apache.commons.cache;
 17  
 
 18  
 import java.util.NoSuchElementException;
 19  
 import java.io.Serializable;
 20  
 
 21  
 /**
 22  
  * tk.
 23  
  * @version $Id: StaleObjectEvictor.java 155435 2005-02-26 13:17:27Z dirkv $
 24  
  * @author Rodney Waldhoff
 25  
  */
 26  
 public class StaleObjectEvictor implements Runnable, Serializable {
 27  
   public transient static final int DEFAULT_OBJECTS_BETWEEN_NAPS;
 28  
   static {
 29  0
     int objsTweenNaps = 10;
 30  
     try {
 31  0
       objsTweenNaps = Integer.parseInt(System.getProperty("org.apache.commons.cache.StaleObjectEvictor.objs-between-naps","10"));
 32  0
     } catch(Exception e) {
 33  0
       objsTweenNaps = 10;
 34  0
     }
 35  0
     DEFAULT_OBJECTS_BETWEEN_NAPS = objsTweenNaps;
 36  
   }
 37  
 
 38  
   public transient static final long DEFAULT_SLEEP_TIME_MILLIS;
 39  
   static {
 40  0
     long sleepTime = 10000;
 41  
     try {
 42  0
       sleepTime = Long.parseLong(System.getProperty("org.apache.commons.cache.StaleObjectEvictor.sleep-time-millis","10000"));
 43  0
     } catch(Exception e) {
 44  0
       sleepTime = 10000;
 45  0
     }
 46  0
     DEFAULT_SLEEP_TIME_MILLIS = sleepTime;
 47  0
   }
 48  
 
 49  0
   protected int _objsBetweenNaps = DEFAULT_OBJECTS_BETWEEN_NAPS;
 50  0
   protected long _sleepTimeMillis = DEFAULT_SLEEP_TIME_MILLIS;
 51  
   protected CachedObjectIterator _iterator;
 52  0
   protected boolean _cancelled = false;
 53  
 
 54  
   public StaleObjectEvictor(CachedObjectIterator it) {
 55  0
     this(it,DEFAULT_OBJECTS_BETWEEN_NAPS,DEFAULT_SLEEP_TIME_MILLIS);
 56  0
   }
 57  
 
 58  
   public StaleObjectEvictor(CachedObjectIterator it, int objsBetweenNaps) {
 59  0
     this(it,objsBetweenNaps,DEFAULT_SLEEP_TIME_MILLIS);
 60  0
   }
 61  
 
 62  
   public StaleObjectEvictor(CachedObjectIterator it, long sleepTimeMillis) {
 63  0
     this(it,DEFAULT_OBJECTS_BETWEEN_NAPS,sleepTimeMillis);
 64  0
   }
 65  
 
 66  0
   public StaleObjectEvictor(CachedObjectIterator it, int objsBetweenNaps, long sleepTimeMillis) {
 67  0
     _iterator = it;
 68  0
     _objsBetweenNaps = objsBetweenNaps;
 69  0
     _sleepTimeMillis = sleepTimeMillis;
 70  0
   }
 71  
 
 72  
   public void cancel() {
 73  0
     _cancelled = true;
 74  0
   }
 75  
 
 76  
   public void run() {
 77  0
     while(!_cancelled) {
 78  0
       for(int i=0;i<_objsBetweenNaps;i++) {
 79  0
         CachedObjectInfo info = null;
 80  
         try {
 81  0
           info = _iterator.getNext();
 82  0
         } catch(NoSuchElementException e) {
 83  0
           info = null;
 84  0
         }
 85  0
         if(null != info) {
 86  0
           Long expiry = info.getExpirationTs();
 87  0
           if(null != expiry) {
 88  0
             if(System.currentTimeMillis() >= expiry.longValue()) {
 89  
             try {
 90  0
                 _iterator.remove();
 91  0
               } catch(Exception e) {
 92  
                 // ignored
 93  0
               }
 94  
             }
 95  
           }
 96  
         } else {
 97  
           break;
 98  
         }
 99  
       }
 100  
       try {
 101  0
         Thread.sleep(_sleepTimeMillis);
 102  0
       } catch(InterruptedException e) {
 103  
         // ignored
 104  0
       }
 105  
     }
 106  0
   }
 107  
 
 108  
   /*
 109  
   public void passivate() {
 110  
     _cancelled = true;
 111  
   }
 112  
 
 113  
   public void activate() {
 114  
     _cancelled = false;
 115  
   }
 116  
   */
 117  
 
 118  
 
 119  
 }