KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.mmbase.module.core.TemporaryNodeManager;
17
18 import org.mmbase.util.logging.Logger;
19 import org.mmbase.util.logging.Logging;
20
21 /**
22  * RelationBreaker stores relation numbers with an expiretime.
23  * After the expiretime has expired the relation is removed.
24  * Unfortunately, this class doesn't work. See docs for more info.
25  *
26  * @deprecated use NodeBreaker instead
27  *
28  * @author Dirk-Jan Hoekstra
29  * @version $Id: RelationBreaker.java,v 1.9 2005/10/05 10:59:39 michiel Exp $
30  */

31
32 public class RelationBreaker extends Thread JavaDoc {
33
34     // logger
35
private static Logger log = Logging.getLoggerInstance(RelationBreaker.class.getName());
36     // List of RelationHolder objects, which reference relations in the
37
// temporary node manager
38
private Vector relations = new Vector();
39     // The interval at which the relation breaker checks for expired relations
40
private long checkInterval = 10 * 60 * 1000;
41     // Reference to MMBase
42
private MMBase mmb;
43     // boolean to make sure the breaker is started when called for the first time,
44
// and to stop the thread later on.
45
private boolean shouldRun = false;
46     // The temporary node manager that holds the relations.
47
private TemporaryNodeManager tmpNodeManager;
48
49
50     /**
51      * Creates a new relation breaker.
52      * Used by the Channel builder.
53      * @param mmb reference to MMBase
54      * @param checkInterval the interval at which the relation breaker checks for expired relations
55      * @param tmpNodeManager the temporary node manager that holds the relations.
56      */

57     public RelationBreaker(MMBase mmb, long checkInterval, TemporaryNodeManager tmpNodeManager) {
58         this.mmb = mmb;
59         this.checkInterval = checkInterval;
60         this.tmpNodeManager = tmpNodeManager;
61     }
62
63     /**
64      * Adds a relation to be watched.
65      * This method starts the breaker if it hasn't been done already.
66      * @param id the id of the referred relation
67      * @param expireTime expiration time of the relation
68      */

69     public synchronized void add(String JavaDoc id, long expireTime) {
70         relations.add(new RelationHolder(id, expireTime));
71         log.debug("add");
72         if (!shouldRun) {
73             shouldRun = true;
74             start();
75         }
76     }
77
78     /**
79      * Updates a relation to be watched, preventing it from being removed prematurely.
80      * @param id the id of the referred relation
81      * @param expireTime the new expiration time of the relation
82      */

83     public synchronized boolean update(String JavaDoc id, long expireTime) {
84         RelationHolder relationHolder = (RelationHolder)relations.elementAt(relations.indexOf(id));
85         if (relationHolder != null)
86         { relationHolder.setExpireTime(expireTime);
87             return true;
88         }
89         return false;
90     }
91
92     /**
93      * Removes a relation.
94      * This also removes the relation from the temporary node manager cache.
95      * XXX: doesn't work
96      * @param id the id of the referred relation
97      */

98     public synchronized void remove(String JavaDoc id) {
99         String JavaDoc owner = id.substring(0, id.indexOf("_"));
100         String JavaDoc key = id.substring(id.indexOf("_") + 1);
101 // searching on id doesn't work!
102
int i = relations.indexOf(id);
103         if (i > 0) relations.remove(i);
104         tmpNodeManager.deleteTmpNode(owner, key);
105     }
106
107     /**
108      * Removes a relation.
109      * This also removes the relation from the temporary node manager cache.
110      * XXX: doesn't work
111      * @param relationHolder the RelationHolder of the referred relation
112      * @param i index of the holder in the internal list
113      */

114     public synchronized void remove(RelationHolder relationHolder, int i) {
115         //relations.remove(i);
116
log.debug(relationHolder.id);
117         String JavaDoc owner = relationHolder.id.substring(0, relationHolder.id.indexOf("_"));
118         String JavaDoc key = relationHolder.id.substring(relationHolder.id.indexOf("_") + 1);
119         tmpNodeManager.deleteTmpNode(owner, key);
120     }
121
122     /**
123      * Runs the thread that checks for expired relations.
124      */

125     public void run() {
126         mmb.getInsRel();
127         long currentTime;
128
129         while (shouldRun) {
130             try {
131                 sleep(checkInterval);
132             } catch(Exception JavaDoc e) {
133                 log.error("run(): can't sleep.");
134                 shouldRun = false;
135                 return;
136             }
137
138             currentTime = System.currentTimeMillis();
139
140             log.debug("search for expired");
141             int i = 0;
142             while (i < relations.size()) {
143                 RelationHolder relationHolder = (RelationHolder)relations.elementAt(i);
144                 if (relationHolder.getExpireTime() < currentTime)
145                     remove(relationHolder, i);
146
147                 i++;
148             }
149         }
150     }
151 }
152
153
154 /**
155  * Holds a reference to a relation.
156  */

157 class RelationHolder {
158     // The id of the referred relation.
159
public String JavaDoc id;
160     // expiration time of the relation
161
private long expireTime;
162
163     /**
164      * Creates a relation reference.
165      * @param id the id of the referred relation
166      * @param expireTime expiration time of the relation
167      */

168     public RelationHolder(String JavaDoc id, long expireTime) {
169         this.id = id;
170         this.expireTime = expireTime;
171     }
172
173     /**
174      * Compares the relation references to another object.
175      * In this specific case, the object should be equal to the id.
176      */

177     public synchronized boolean equals(Object JavaDoc anObject) {
178         return (id.equals(anObject));
179     }
180     
181     
182     /**
183      * @see java.lang.Object#hashCode()
184      */

185     public synchronized int hashCode() {
186         return id.hashCode();
187     }
188
189     /**
190      * Sets the expiration time of the relation.
191      */

192     public synchronized void setExpireTime(long expireTime) {
193         this.expireTime = expireTime;
194     }
195
196     /**
197      * Retrieves the expiration time of the relation
198      */

199     public synchronized long getExpireTime() {
200         return expireTime;
201     }
202 }
203
Popular Tags