KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > nava > informa > utils > cleaner > Cleaner


1 //
2
// Informa -- RSS Library for Java
3
// Copyright (c) 2002 by Niko Schmuck
4
//
5
// Niko Schmuck
6
// http://sourceforge.net/projects/informa
7
// mailto:niko_schmuck@users.sourceforge.net
8
//
9
// This library is free software.
10
//
11
// You may redistribute it and/or modify it under the terms of the GNU
12
// Lesser General Public License as published by the Free Software Foundation.
13
//
14
// Version 2.1 of the license should be included with this distribution in
15
// the file LICENSE. If the license is not included with this distribution,
16
// you may find a copy at the FSF web site at 'www.gnu.org' or 'www.fsf.org',
17
// or you may write to the Free Software Foundation, 675 Mass Ave, Cambridge,
18
// MA 02139 USA.
19
//
20
// This library is distributed in the hope that it will be useful,
21
// but WITHOUT ANY WARRANTY; without even the implied waranty of
22
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23
// Lesser General Public License for more details.
24
//
25
// $Id: Cleaner.java,v 1.1 2004/08/24 18:07:54 spyromus Exp $
26
//
27

28 package de.nava.informa.utils.cleaner;
29
30 import de.nava.informa.utils.toolkit.WorkerThread;
31
32 import de.nava.informa.core.ChannelIF;
33 import de.nava.informa.utils.toolkit.Scheduler;
34 import de.nava.informa.utils.toolkit.SchedulerCallbackIF;
35 import de.nava.informa.utils.toolkit.WorkersManager;
36 import de.nava.informa.utils.toolkit.ChannelRecord;
37 import de.nava.informa.utils.toolkit.WorkerThreadFactoryIF;
38
39 /**
40  * Cleaner is an utility class, which is intended to help applications with detection of
41  * unwanted (old, corrupted and etc) items.
42  * <p>
43  * Cleaner works in background. It accepts unlimited number of observers, objects of
44  * <code>CleanerObserverIF</code> type, that are notified each time main engine finds
45  * unwanted items. In order to decide which item is unwanted engine uses matchers,
46  * objects of <code>CleanerMatcherIF</code> type. Each matcher is a rule. You can create
47  * unlimited number of rules to match items and tell that they are unwanted in some way.</p>
48  * <p>
49  * Engine accepts individual channels for registration. You can register channel either
50  * with global or custom periods. Global period defaults to <code>DEFAULT_CLEANING_PERIOD</code>
51  * right after creation and can be changed with call to <code>setPeriod(long)</code> method.</p>
52  * <p>
53  * At any time you can ask cleaner to unregister channel. After that it will never be cleaned
54  * by engine again and it's promised that all references to the object will be removed.</p>
55  * <p>
56  * Cleaner uses <code>WorkersManager</code> to maintain asynchronous processing. This means
57  * that there will be several working threads in memory, which will be processing scheduled
58  * requests for channel cleaning. At any time you can change number of running worker threads
59  * starting from 1. If number of threads you require is less than current number of threads
60  * extra threads will be marked for termination and will quit right after finishing their current
61  * jobs.</p>
62  *
63  * @author Aleksey Gureev (spyromus@noizeramp.com)
64  */

65 public class Cleaner {
66
67   private static final long DEFAULT_CLEANING_PERIOD = 3600000; // 1 hour
68
private static final int DEFAULT_WORKER_THREADS = 2;
69
70   private WorkersManager workersManager;
71   private Scheduler scheduler;
72
73   private CompositeObserver compositeObserver;
74   private CompositeMatcher compositeMatcher;
75
76   private long globalPollPeriod = DEFAULT_CLEANING_PERIOD;
77
78   /**
79    * Creates cleaner with default number of worker threads.
80    * It's possible to change number of worker threads later
81    * using <code>setWorkerThreads(int)</code> method.
82    */

83   public Cleaner() {
84     this(DEFAULT_WORKER_THREADS);
85   }
86
87   /**
88    * Creates cleaner with specified number of worker threads.
89    * It's possible to change number of worker threads later
90    * using <code>setWorkerThreads(int)</code> method.
91    *
92    * @param workerThreads number of worker threads.
93    */

94   public Cleaner(int workerThreads) {
95     // Create composite objects.
96
this.compositeMatcher = new CompositeMatcher();
97     this.compositeObserver = new CompositeObserver();
98
99     // Initialize workers manager.
100
workersManager = new WorkersManager(new CleanerThreadFactory(), workerThreads);
101
102     // Initialize scheduler.
103
scheduler = new Scheduler(new SchedulerCallback());
104   }
105
106   /**
107    * Adds observer to the list of interested parties.
108    *
109    * @param observer new observer.
110    */

111   public final void addObserver(CleanerObserverIF observer) {
112     compositeObserver.add(observer);
113   }
114
115   /**
116    * Adds matcher to the list.
117    *
118    * @param matcher new matcher.
119    */

120   public final void addMatcher(CleanerMatcherIF matcher) {
121     compositeMatcher.add(matcher);
122   }
123
124   /**
125    * Removes observer from the list.
126    *
127    * @param observer observer to remove.
128    */

129   public final void removeObserver(CleanerObserverIF observer) {
130     compositeObserver.remove(observer);
131   }
132
133   /**
134    * Removes matcher from the list.
135    *
136    * @param matcher matcher to remove.
137    */

138   public final void removeMatcher(CleanerMatcherIF matcher) {
139     compositeMatcher.remove(matcher);
140   }
141
142   /**
143    * Registers channel for scheduled cleaning with default period (1 hour).
144    *
145    * @param channel channel to schedule.
146    */

147   public final void registerChannel(ChannelIF channel) {
148     scheduler.schedule(channel, globalPollPeriod, ChannelRecord.PRIO_NORMAL);
149   }
150
151   /**
152    * Registers channel for scheduled cleaning with given period.
153    *
154    * @param channel channel to schedule.
155    * @param period period to use.
156    */

157   public final void registerChannel(ChannelIF channel, long period) {
158     scheduler.schedule(channel, period, ChannelRecord.PRIO_NORMAL);
159   }
160
161   /**
162    * Unregisters channel from cleaning.
163    *
164    * @param channel channel to unregister.
165    */

166   public final void unregisterChannel(ChannelIF channel) {
167     scheduler.unschedule(channel);
168   }
169
170   /**
171    * Performs immediate cleaning of the channel and reworks the schedule starting from current time.
172    * If channel isn't registered yet it will be registered with normal priority.
173    *
174    * @param channel channel to update.
175    */

176   public final void cleanChannel(ChannelIF channel) {
177     scheduler.triggerNow(channel);
178   }
179
180   /**
181    * Changes the number of worker threads. In case when new number of threads is less than
182    * current extra threads will not be terminated right away while they are doing their jobs.
183    * Instead of this they will be marked for termination and finish their existence after
184    * job is complete.
185    *
186    * @param count new number of worker threads.
187    */

188   public final void setWorkerThreads(int count) {
189     workersManager.setWorkerThreads(count);
190   }
191
192   /**
193    * Sets global cleaning period to the specified value. All tasks will be rescheduled.
194    *
195    * @param period period in millis.
196    */

197   public final void setPeriod(long period) {
198     this.globalPollPeriod = period;
199     scheduler.rescheduleAll(period);
200   }
201
202   /**
203    * Factory of working threads.
204    */

205   private class CleanerThreadFactory implements WorkerThreadFactoryIF {
206     /**
207      * Creates new worker thread object.
208      *
209      * @return worker thread object.
210      */

211     public WorkerThread create() {
212       return new CleanerWorkerThread(compositeObserver, compositeMatcher);
213     }
214   }
215
216   /**
217    * Callback from scheduler.
218    */

219   private class SchedulerCallback implements SchedulerCallbackIF {
220     /**
221      * Invoked by scheduler when time to process channel information comes.
222      *
223      * @param record channel record.
224      */

225     public void process(ChannelRecord record) {
226       workersManager.process(record);
227     }
228   }
229 }
230
Popular Tags