1 10 11 package org.mmbase.applications.community.modules; 12 13 import java.util.*; 14 15 import org.mmbase.module.core.*; 16 17 30 public class NodeBreaker implements Runnable { 31 32 private ArrayList ids = new ArrayList(); 38 private ArrayList expirationtimes = new ArrayList(); 39 private long checkInterval = 10 * 60 * 1000; 41 private Thread kicker = null; 43 private TemporaryNodeManager tmpNodeManager; 45 46 52 public NodeBreaker(long checkInterval, TemporaryNodeManager tmpNodeManager) { 53 this.checkInterval = checkInterval; 54 this.tmpNodeManager = tmpNodeManager; 55 } 56 57 63 public synchronized void add(String id, long expireTime) { 64 ids.add(id); 65 expirationtimes.add(new Long (expireTime)); 66 if (kicker == null) { 67 kicker = new Thread (this,"NodeBreaker"); 68 kicker.setDaemon(true); 69 kicker.start(); 70 } 71 } 72 73 78 public synchronized boolean update(String id, long expireTime) { 79 int i=ids.indexOf(id); 80 if (i==-1) return false; 81 expirationtimes.set(i,new Long (expireTime)); 82 return true; 83 } 84 85 90 public synchronized void remove(String id) { 91 int i = ids.indexOf(id); 92 if (i >= 0) { 93 remove(i); 94 } 95 } 96 97 102 private synchronized void remove(int i) { 103 String id=(String )ids.remove(i); 104 expirationtimes.remove(i); 105 String owner = id.substring(0, id.indexOf("_")); 106 String key = id.substring(id.indexOf("_") + 1); 107 tmpNodeManager.deleteTmpNode(owner, key); 108 } 109 110 113 public synchronized void stop() { 114 kicker = null; 115 notify(); 116 } 117 118 121 public void run() { 122 long currentTime; 123 while (kicker!=null) { 124 try { 125 Thread.sleep(checkInterval); 126 } catch(InterruptedException e) {} 127 if (kicker==null) return; 128 currentTime = System.currentTimeMillis(); 129 for (int i = expirationtimes.size()-1; i>=0; i--) { 130 Long time = (Long )expirationtimes.get(i); 131 if (time.longValue() < currentTime) { 132 remove(i); 133 } 134 } 135 } 136 } 137 } 138 | Popular Tags |