01: /*
02: * <copyright>
03: *
04: * Copyright 1997-2004 BBNT Solutions, LLC
05: * under sponsorship of the Defense Advanced Research Projects
06: * Agency (DARPA).
07: *
08: * You can redistribute this software and/or modify it under the
09: * terms of the Cougaar Open Source License as published on the
10: * Cougaar Open Source Website (www.cougaar.org).
11: *
12: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
13: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
14: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
15: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
16: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
17: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
18: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
22: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23: *
24: * </copyright>
25: */
26:
27: package org.cougaar.mlm.plugin.ldm;
28:
29: /** A QueryHandler which wants to be execute()ed periodically.
30: * The requested frequency is specified as the parameter Frequency.
31: * if Frequency is not supplied or is <= 0, the execute method
32: * will be called exactly once.
33: * Frequency is x calls per second!
34: **/
35:
36: public abstract class PeriodicQuery extends QueryHandler {
37: public PeriodicQuery() {
38: }
39:
40: private int myFrequency = 0;
41:
42: protected int getFrequency() {
43: return myFrequency;
44: }
45:
46: /** main interface to PeriodicQuery. Called each time
47: * to run the query.
48: **/
49: public void start() {
50: String fs = getParameter("Frequency");
51: if (fs != null) {
52: myFrequency = 1000 * Integer.parseInt(fs);
53: }
54:
55: execute();
56:
57: if (myFrequency > 0) {
58: Thread myThread = new Thread(new Runnable() {
59: public void run() {
60: while (true) {
61: try {
62: Thread.sleep(getFrequency());
63: } catch (Exception e) {
64: }
65:
66: try {
67: execute();
68: } catch (Exception e) {
69: System.err.println("PeriodicQuery loop "
70: + this + " Caught " + e);
71: return;
72: }
73: }
74: }
75: }, this .toString() + "/PeriodicQuery("
76: + (getFrequency() / 1000) + ")");
77: myThread.start();
78: }
79: }
80:
81: protected void execute() {
82: startQuery(); // let the query have some state
83:
84: String q = getQuery();
85: myLDMPlugin.executeSQL(q, this );
86:
87: endQuery(); // tell the query it is done.
88: }
89:
90: public String getQueryString(String query) {
91: return getParameter(query);
92: }
93: }
|