001    /*
002     * Copyright 2001-2004 The Apache Software Foundation
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *     http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.apache.commons.cache;
017    
018    import java.util.NoSuchElementException;
019    import java.io.Serializable;
020    
021    /**
022     * tk.
023     * @version $Id: StaleObjectEvictor.java 155435 2005-02-26 13:17:27Z dirkv $
024     * @author Rodney Waldhoff
025     */
026    public class StaleObjectEvictor implements Runnable, Serializable {
027      public transient static final int DEFAULT_OBJECTS_BETWEEN_NAPS;
028      static {
029        int objsTweenNaps = 10;
030        try {
031          objsTweenNaps = Integer.parseInt(System.getProperty("org.apache.commons.cache.StaleObjectEvictor.objs-between-naps","10"));
032        } catch(Exception e) {
033          objsTweenNaps = 10;
034        }
035        DEFAULT_OBJECTS_BETWEEN_NAPS = objsTweenNaps;
036      }
037    
038      public transient static final long DEFAULT_SLEEP_TIME_MILLIS;
039      static {
040        long sleepTime = 10000;
041        try {
042          sleepTime = Long.parseLong(System.getProperty("org.apache.commons.cache.StaleObjectEvictor.sleep-time-millis","10000"));
043        } catch(Exception e) {
044          sleepTime = 10000;
045        }
046        DEFAULT_SLEEP_TIME_MILLIS = sleepTime;
047      }
048    
049      protected int _objsBetweenNaps = DEFAULT_OBJECTS_BETWEEN_NAPS;
050      protected long _sleepTimeMillis = DEFAULT_SLEEP_TIME_MILLIS;
051      protected CachedObjectIterator _iterator;
052      protected boolean _cancelled = false;
053    
054      public StaleObjectEvictor(CachedObjectIterator it) {
055        this(it,DEFAULT_OBJECTS_BETWEEN_NAPS,DEFAULT_SLEEP_TIME_MILLIS);
056      }
057    
058      public StaleObjectEvictor(CachedObjectIterator it, int objsBetweenNaps) {
059        this(it,objsBetweenNaps,DEFAULT_SLEEP_TIME_MILLIS);
060      }
061    
062      public StaleObjectEvictor(CachedObjectIterator it, long sleepTimeMillis) {
063        this(it,DEFAULT_OBJECTS_BETWEEN_NAPS,sleepTimeMillis);
064      }
065    
066      public StaleObjectEvictor(CachedObjectIterator it, int objsBetweenNaps, long sleepTimeMillis) {
067        _iterator = it;
068        _objsBetweenNaps = objsBetweenNaps;
069        _sleepTimeMillis = sleepTimeMillis;
070      }
071    
072      public void cancel() {
073        _cancelled = true;
074      }
075    
076      public void run() {
077        while(!_cancelled) {
078          for(int i=0;i<_objsBetweenNaps;i++) {
079            CachedObjectInfo info = null;
080            try {
081              info = _iterator.getNext();
082            } catch(NoSuchElementException e) {
083              info = null;
084            }
085            if(null != info) {
086              Long expiry = info.getExpirationTs();
087              if(null != expiry) {
088                if(System.currentTimeMillis() >= expiry.longValue()) {
089                try {
090                    _iterator.remove();
091                  } catch(Exception e) {
092                    // ignored
093                  }
094                }
095              }
096            } else {
097              break;
098            }
099          }
100          try {
101            Thread.sleep(_sleepTimeMillis);
102          } catch(InterruptedException e) {
103            // ignored
104          }
105        }
106      }
107    
108      /*
109      public void passivate() {
110        _cancelled = true;
111      }
112    
113      public void activate() {
114        _cancelled = false;
115      }
116      */
117    
118    
119    }