KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > applications > community > modules > NodeBreaker


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10
11 package org.mmbase.applications.community.modules;
12
13 import java.util.*;
14
15 import org.mmbase.module.core.*;
16
17 /**
18  * NodeBreaker stores temporary nodes with an expiration time.
19  * After this has expired the node is removed.
20  * <br />
21  * @deprecated The NodeBreaker is a temporary solution (not the best code I ever wrote,
22  * either, but it should not be too slow and will have to do for now).
23  * It would be much better to incorporate 'expiration time' in the TemporaryNodeManager
24  * or a temporary node cloud. As such, avoid using this class in the future.
25  *
26  * @author Dirk-Jan Hoekstra
27  * @author Pierre van Rooden
28  * @version $Id: NodeBreaker.java,v 1.9 2005/10/05 10:59:39 michiel Exp $
29  */

30 public class NodeBreaker implements Runnable JavaDoc {
31
32     // Lists of ids and expirationtimes temporary node manager
33
// These are co-related: ids[x] and expirationtimes[x] belong to the same
34
// objkect: ids[x] is the object number or key, expirationtimes[x] determines
35
// when it is to be removed.
36
// This is not brilliant coding, I know, but it is fast.
37
private ArrayList ids = new ArrayList();
38     private ArrayList expirationtimes = new ArrayList();
39     // The interval at which the node breaker checks for expired relations
40
private long checkInterval = 10 * 60 * 1000;
41     // used to control starting and stopping the thread
42
private Thread JavaDoc kicker = null;
43     // The temporary node manager that holds the relations.
44
private TemporaryNodeManager tmpNodeManager;
45
46     /**
47      * Creates a new node breaker.
48      * Used by the Channel builder.
49      * @param checkInterval the interval at which the relation breaker checks for expired relations
50      * @param tmpNodeManager the temporary node manager that holds the relations.
51      */

52     public NodeBreaker(long checkInterval, TemporaryNodeManager tmpNodeManager) {
53         this.checkInterval = checkInterval;
54         this.tmpNodeManager = tmpNodeManager;
55     }
56
57     /**
58      * Adds a node to be watched.
59      * This method starts the breaker if it hasn't been done already.
60      * @param id the id of the referred node
61      * @param expireTime expiration time of the node
62      */

63     public synchronized void add(String JavaDoc id, long expireTime) {
64         ids.add(id);
65         expirationtimes.add(new Long JavaDoc(expireTime));
66         if (kicker == null) {
67             kicker = new Thread JavaDoc(this,"NodeBreaker");
68             kicker.setDaemon(true);
69             kicker.start();
70         }
71    }
72
73     /**
74      * Updates a node to be watched, preventing it from being removed prematurely.
75      * @param id the id of the referred node
76      * @param expireTime the new expiration time of the node
77      */

78     public synchronized boolean update(String JavaDoc id, long expireTime) {
79         int i=ids.indexOf(id);
80         if (i==-1) return false;
81         expirationtimes.set(i,new Long JavaDoc(expireTime));
82         return true;
83     }
84
85     /**
86      * Removes a node.
87      * This also removes the node from the temporary node manager cache.
88      * @param id the id of the referred node
89      */

90     public synchronized void remove(String JavaDoc id) {
91         int i = ids.indexOf(id);
92         if (i >= 0) {
93             remove(i);
94         }
95     }
96
97     /**
98      * Removes a node
99      * This also removes the node from the temporary node manager cache.
100      * @param i the iindex in the list of nodes
101      */

102     private synchronized void remove(int i) {
103         String JavaDoc id=(String JavaDoc)ids.remove(i);
104         expirationtimes.remove(i);
105         String JavaDoc owner = id.substring(0, id.indexOf("_"));
106         String JavaDoc key = id.substring(id.indexOf("_") + 1);
107         tmpNodeManager.deleteTmpNode(owner, key);
108     }
109
110     /**
111      * Stop the breaker.
112      */

113     public synchronized void stop() {
114         kicker = null;
115         notify();
116     }
117
118     /**
119      * Runs the thread that checks for expired relations.
120      */

121     public void run() {
122         long currentTime;
123         while (kicker!=null) {
124             try {
125                 Thread.sleep(checkInterval);
126             } catch(InterruptedException JavaDoc e) {}
127             if (kicker==null) return;
128             currentTime = System.currentTimeMillis();
129             for (int i = expirationtimes.size()-1; i>=0; i--) {
130                 Long JavaDoc time = (Long JavaDoc)expirationtimes.get(i);
131                 if (time.longValue() < currentTime) {
132                     remove(i);
133                 }
134             }
135         }
136     }
137 }
138
Popular Tags