KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > module > builders > PropertiesProbe


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 package org.mmbase.module.builders;
11
12 import java.util.*;
13
14 import org.mmbase.module.core.*;
15 import org.mmbase.storage.search.*;
16 import org.mmbase.storage.search.implementation.*;
17
18 import org.mmbase.util.logging.*;
19
20 /**
21  * admin module, keeps track of all the worker pools
22  * and adds/kills workers if needed (depending on
23  * there load and info from the config module).
24  *
25  * @sql
26  * @version $Id: PropertiesProbe.java,v 1.11 2005/01/25 12:45:19 pierre Exp $
27  * @author Daniel Ockeloen
28  */

29 public class PropertiesProbe implements Runnable JavaDoc {
30
31     private static Logger log = Logging.getLoggerInstance(PropertiesProbe.class.getName());
32
33     Thread JavaDoc kicker = null;
34     Properties parent = null;
35
36     public PropertiesProbe(Properties parent) {
37         this.parent = parent;
38         init();
39     }
40
41     public void init() {
42         this.start();
43     }
44
45
46     /**
47      * Starts the admin Thread.
48      */

49     public void start() {
50         /* Start up the main thread */
51         if (kicker == null) {
52             kicker = new Thread JavaDoc(this,"PropertiesProbe");
53             kicker.setDaemon(true);
54             kicker.start();
55         }
56     }
57
58     /**
59      * Stops the admin Thread.
60      */

61     public void stop() {
62         /* Stop thread */
63         kicker.interrupt();
64         kicker = null;
65     }
66
67     /**
68      */

69     public void run () {
70         while (kicker != null) {
71             try {
72                 Thread.sleep(10000);
73             } catch (InterruptedException JavaDoc e) {
74                 return;
75             }
76             if (parent.getMachineName().equals("test1")) doExpire();
77         }
78     }
79
80     private void doExpire() {
81         try {
82             NodeSearchQuery query = new NodeSearchQuery(parent);
83             StepField keyField = query.getField(parent.getField("key"));
84             BasicFieldValueConstraint constraint1 = new BasicFieldValueConstraint(keyField, "LASTVISIT");
85             StepField valueField = query.getField(parent.getField("value"));
86             BasicFieldValueConstraint constraint2 = new BasicFieldValueConstraint(valueField, new Integer JavaDoc(10536));
87             constraint2.setOperator(FieldCompareConstraint.LESS);
88
89             BasicCompositeConstraint constraint = new BasicCompositeConstraint(CompositeConstraint.LOGICAL_AND);
90             constraint.addChild(constraint1);
91             constraint.addChild(constraint2);
92
93             query.setConstraint(constraint);
94
95             List nodes = parent.getNodes(query);
96             int max=0;
97             for (Iterator i = nodes.iterator(); i.hasNext() && max<1000;) {
98                 MMObjectNode node = (MMObjectNode)i.next();
99                 int number = node.getIntValue("parent");
100                 log.info("Want delete on : " + number);
101                 deleteProperties(number);
102                 deleteUser(number);
103                 max++;
104             }
105         } catch (Exception JavaDoc e) {}
106     }
107
108     private void deleteProperties(int id) {
109         /* quicker, but ugly, so don't use
110
111             try {
112                 DataSource dataSource = (DataSource) parent.mmb.getStorageManagerFactory().getAttribute(Attributes.DATA_SOURCE);
113                 Connection con = dataSource.getConnection();
114                 Statement stmt = con.createStatement();
115                 stmt.executeUpdate("delete from " + parent.mmb.baseName+"_" + parent.tableName + " where parent=" + id);
116                 stmt.close();
117                 con.close();
118             } catch (Exception e) {}
119         */

120         try {
121             NodeSearchQuery query = new NodeSearchQuery(parent);
122             List nodes = parent.getNodes(query);
123             for (Iterator i = nodes.iterator(); i.hasNext();) {
124                 MMObjectNode node = (MMObjectNode)i.next();
125                 parent.removeNode(node);
126             }
127         } catch (Exception JavaDoc e) {}
128     }
129
130     private void deleteUser(int id) {
131         MMObjectNode user = parent.getNode(id);
132         if (user != null) {
133             user.getBuilder().removeNode(user);
134         }
135     }
136 }
137
Popular Tags