001: /*
002:
003: This software is OSI Certified Open Source Software.
004: OSI Certified is a certification mark of the Open Source Initiative.
005:
006: The license (Mozilla version 1.0) can be read at the MMBase site.
007: See http://www.MMBase.org/license
008:
009: */
010: package org.mmbase.module.builders;
011:
012: import java.util.*;
013:
014: import org.mmbase.module.core.*;
015: import org.mmbase.storage.search.*;
016: import org.mmbase.storage.search.implementation.*;
017:
018: import org.mmbase.util.logging.*;
019:
020: /**
021: * admin module, keeps track of all the worker pools
022: * and adds/kills workers if needed (depending on
023: * there load and info from the config module).
024: *
025: * @sql
026: * @version $Id: PropertiesProbe.java,v 1.13 2007/02/25 17:56:59 nklasens Exp $
027: * @author Daniel Ockeloen
028: */
029: public class PropertiesProbe implements Runnable {
030:
031: private static Logger log = Logging
032: .getLoggerInstance(PropertiesProbe.class.getName());
033:
034: Thread kicker = null;
035: Properties parent = null;
036:
037: public PropertiesProbe(Properties parent) {
038: this .parent = parent;
039: init();
040: }
041:
042: public void init() {
043: this .start();
044: }
045:
046: /**
047: * Starts the admin Thread.
048: */
049: public void start() {
050: /* Start up the main thread */
051: if (kicker == null) {
052: kicker = new Thread(this , "PropertiesProbe");
053: kicker.setDaemon(true);
054: kicker.start();
055: }
056: }
057:
058: /**
059: * Stops the admin Thread.
060: */
061: public void stop() {
062: /* Stop thread */
063: kicker.interrupt();
064: kicker = null;
065: }
066:
067: /**
068: */
069: public void run() {
070: while (kicker != null) {
071: try {
072: Thread.sleep(10000);
073: } catch (InterruptedException e) {
074: return;
075: }
076: if (parent.getMachineName().equals("test1"))
077: doExpire();
078: }
079: }
080:
081: private void doExpire() {
082: try {
083: NodeSearchQuery query = new NodeSearchQuery(parent);
084: StepField keyField = query.getField(parent.getField("key"));
085: BasicFieldValueConstraint constraint1 = new BasicFieldValueConstraint(
086: keyField, "LASTVISIT");
087: StepField valueField = query.getField(parent
088: .getField("value"));
089: BasicFieldValueConstraint constraint2 = new BasicFieldValueConstraint(
090: valueField, 10536);
091: constraint2.setOperator(FieldCompareConstraint.LESS);
092:
093: BasicCompositeConstraint constraint = new BasicCompositeConstraint(
094: CompositeConstraint.LOGICAL_AND);
095: constraint.addChild(constraint1);
096: constraint.addChild(constraint2);
097:
098: query.setConstraint(constraint);
099:
100: List<MMObjectNode> nodes = parent.getNodes(query);
101: int max = 0;
102: for (Iterator<MMObjectNode> i = nodes.iterator(); i
103: .hasNext()
104: && max < 1000;) {
105: MMObjectNode node = i.next();
106: int number = node.getIntValue("parent");
107: log.info("Want delete on : " + number);
108: deleteProperties(number);
109: deleteUser(number);
110: max++;
111: }
112: } catch (Exception e) {
113: }
114: }
115:
116: private void deleteProperties(int id) {
117: /* quicker, but ugly, so don't use
118:
119: try {
120: DataSource dataSource = (DataSource) parent.mmb.getStorageManagerFactory().getAttribute(Attributes.DATA_SOURCE);
121: Connection con = dataSource.getConnection();
122: Statement stmt = con.createStatement();
123: stmt.executeUpdate("delete from " + parent.mmb.baseName+"_" + parent.tableName + " where parent=" + id);
124: stmt.close();
125: con.close();
126: } catch (Exception e) {}
127: */
128: try {
129: NodeSearchQuery query = new NodeSearchQuery(parent);
130: List<MMObjectNode> nodes = parent.getNodes(query);
131: for (MMObjectNode node : nodes) {
132: parent.removeNode(node);
133: }
134: } catch (Exception e) {
135: }
136: }
137:
138: private void deleteUser(int id) {
139: MMObjectNode user = parent.getNode(id);
140: if (user != null) {
141: user.getBuilder().removeNode(user);
142: }
143: }
144: }
|