KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > websphinx > Chronicle


1 /*
2  * WebSphinx web-crawling toolkit
3  *
4  * Copyright (c) 1998-2002 Carnegie Mellon University. All rights
5  * reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in
16  * the documentation and/or other materials provided with the
17  * distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND
20  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
23  * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  *
31  */

32
33 package websphinx;
34
35 import rcm.util.Timer;
36
37 /**
38  * Run a crawler periodically.
39  */

40 public class Chronicle extends Timer implements Runnable JavaDoc {
41     Crawler crawler;
42     int interval;
43     boolean running = false;
44     boolean triggered = false;
45
46     /**
47      * Make a Chronicle.
48      * @param crawler Crawler to run periodically
49      * @param interval Invocation interval, in seconds. Crawler is invoked
50      * every interval seconds. If the crawler is still running
51      * when interval seconds have elapsed, it is aborted.
52      *
53      */

54     public Chronicle (Crawler crawler, int interval) {
55         this.crawler = crawler;
56         this.interval = interval;
57     }
58
59     /**
60      * Start chronicling. Starts a background thread which
61      * starts the crawler immediately, then re-runs the crawler
62      * every interval seconds from now until stop() is called.
63      */

64     public void start () {
65         if (running)
66             return;
67
68         running = true;
69         set (interval * 1000, true);
70         Thread JavaDoc thread = new Thread JavaDoc (this, crawler.getName ());
71         thread.start ();
72     }
73
74     /**
75      * Stop chronicling. Also stops the crawler, if it's currently running.
76      */

77     public synchronized void stop () {
78         if (!running)
79             return;
80
81         running = false;
82         crawler.stop ();
83         notify ();
84         cancel ();
85     }
86
87     /**
88      * Background thread that runs the crawler. Clients shouldn't
89      * call this.
90      */

91     public synchronized void run () {
92         try {
93             while (running) {
94                 crawler.run ();
95                 while (!triggered)
96                     wait ();
97                 triggered = false;
98             }
99         } catch (InterruptedException JavaDoc e) {}
100     }
101
102     protected synchronized void alarm () {
103         crawler.stop ();
104         triggered = true;
105         notify ();
106     }
107
108 //#ifdef JDK1.1
109
// FIX: allow crawler class name (starting up Workbench to configure it)
110
public static void main (String JavaDoc[] args) throws Exception JavaDoc {
111     java.io.ObjectInputStream JavaDoc in =
112       new java.io.ObjectInputStream JavaDoc (new java.io.FileInputStream JavaDoc (args[0]));
113     Crawler loadedCrawler = (Crawler)in.readObject ();
114     in.close ();
115
116     EventLog.monitor (loadedCrawler);
117
118     Chronicle track = new Chronicle (loadedCrawler, Integer.parseInt (args[1]));
119     track.start ();
120   }
121 //#endif JDK1.1
122
}
123
Popular Tags