home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2007 April / PCpro_2007_04.ISO / files / dsl / jNetTool.exe / org / xbill / DNS / Cache$CacheCleaner.class (.txt) < prev    next >
Encoding:
Java Class File  |  2005-06-05  |  2.2 KB  |  66 lines

  1. package org.xbill.DNS;
  2.  
  3. import java.lang.ref.Reference;
  4. import java.lang.ref.WeakReference;
  5. import java.util.ConcurrentModificationException;
  6. import java.util.Iterator;
  7.  
  8. class Cache$CacheCleaner extends Thread {
  9.    private Reference cacheref;
  10.    private long interval;
  11.  
  12.    public Cache$CacheCleaner(Cache cache, int cleanInterval) {
  13.       this.cacheref = new WeakReference(cache);
  14.       this.interval = (long)(cleanInterval * 60 * 1000);
  15.       this.setDaemon(true);
  16.       this.setName("org.xbill.DNS.Cache.CacheCleaner");
  17.       this.start();
  18.    }
  19.  
  20.    private boolean clean(Cache cache) {
  21.       Iterator it = cache.names();
  22.  
  23.       while(it.hasNext()) {
  24.          Name name;
  25.          try {
  26.             name = (Name)it.next();
  27.          } catch (ConcurrentModificationException var7) {
  28.             return false;
  29.          }
  30.  
  31.          Object[] elements = cache.findExactSets(name);
  32.  
  33.          for(int i = 0; i < elements.length; ++i) {
  34.             Cache.Element element = (Cache.Element)elements[i];
  35.             if (element.expired()) {
  36.                cache.removeSet(name, element.getType(), element);
  37.             }
  38.          }
  39.       }
  40.  
  41.       return true;
  42.    }
  43.  
  44.    public void run() {
  45.       while(true) {
  46.          long now = System.currentTimeMillis();
  47.  
  48.          for(long next = now + this.interval; now < next; now = System.currentTimeMillis()) {
  49.             try {
  50.                Thread.sleep(next - now);
  51.             } catch (InterruptedException var7) {
  52.                return;
  53.             }
  54.          }
  55.  
  56.          Cache cache = (Cache)this.cacheref.get();
  57.          if (cache == null) {
  58.             return;
  59.          }
  60.  
  61.          for(int i = 0; i < 4 && !this.clean(cache); ++i) {
  62.          }
  63.       }
  64.    }
  65. }
  66.