KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > backgroundtask > BackgroundTaskManager


1 //The contents of this file are subject to the Mozilla Public License Version 1.1
2
//(the "License"); you may not use this file except in compliance with the
3
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
4
//
5
//Software distributed under the License is distributed on an "AS IS" basis,
6
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
7
//for the specific language governing rights and
8
//limitations under the License.
9
//
10
//The Original Code is "The Columba Project"
11
//
12
//The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
13
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
14
//
15
//All Rights Reserved.
16
package org.columba.core.backgroundtask;
17
18 import java.awt.EventQueue JavaDoc;
19 import java.awt.Toolkit JavaDoc;
20 import java.awt.event.ActionEvent JavaDoc;
21 import java.awt.event.ActionListener JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.Vector JavaDoc;
24 import java.util.logging.Level JavaDoc;
25 import java.util.logging.Logger JavaDoc;
26
27 import javax.swing.Timer JavaDoc;
28
29 import org.columba.api.backgroundtask.IBackgroundTaskManager;
30 import org.columba.core.command.TaskManager;
31
32 /**
33  * This manager runs in background.
34  * <p>
35  * If the user doesn't do anything with Columba, it starts some cleanup workers,
36  * like saving configuration, saving header-cache, etc.
37  *
38  * @author fdietz
39  */

40 public class BackgroundTaskManager implements ActionListener JavaDoc,
41     IBackgroundTaskManager {
42
43     private static final Logger JavaDoc LOG = Logger
44         .getLogger("org.columba.api.backgroundtask"); //$NON-NLS-1$
45

46     // one second (=1000 ms)
47
private static final int ONE_SECOND = 1000;
48
49     // sleep 5 minutes
50
private static final int SLEEP_TIME = ONE_SECOND * 60 * 5;
51
52     private Timer JavaDoc timer;
53
54     private List JavaDoc<Runnable JavaDoc> list;
55
56     private static BackgroundTaskManager instance = new BackgroundTaskManager();
57
58     public BackgroundTaskManager() {
59     super();
60     // TODO we should check if we need an vector or better another list
61
// implementation; checking also, if the list
62
// must be syncronized or not (performance)
63
this.list = new Vector JavaDoc<Runnable JavaDoc>();
64
65     this.timer = new Timer JavaDoc(SLEEP_TIME, this);
66     this.timer.start();
67     }
68
69     public static BackgroundTaskManager getInstance() {
70     return instance;
71     }
72
73     /**
74          * @see org.columba.api.backgroundtask.IBackgroundTaskManager#register(java.lang.Runnable)
75          */

76     public void register(Runnable JavaDoc runnable) {
77     this.list.add(runnable);
78     }
79
80     /*
81          * (non-Javadoc)
82          *
83          * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
84          */

85     public void actionPerformed(ActionEvent JavaDoc event) {
86     // test if a task is already running
87
EventQueue JavaDoc queue = Toolkit.getDefaultToolkit().getSystemEventQueue();
88
89     if ((queue.peekEvent() == null)
90         && (TaskManager.getInstance().count() == 0)) {
91         // no java task running -> start background tasks
92
if (LOG.isLoggable(Level.FINE)) {
93         LOG.fine("Starting background tasks..."); //$NON-NLS-1$
94
}
95         runTasks();
96     }
97     }
98
99     public void runTasks() {
100     for (Runnable JavaDoc task : this.list) {
101         task.run();
102     }
103     }
104
105     public void stop() {
106     this.timer.stop();
107     }
108 }
109
Popular Tags