001: //The contents of this file are subject to the Mozilla Public License Version 1.1
002: //(the "License"); you may not use this file except in compliance with the
003: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
004: //
005: //Software distributed under the License is distributed on an "AS IS" basis,
006: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
007: //for the specific language governing rights and
008: //limitations under the License.
009: //
010: //The Original Code is "The Columba Project"
011: //
012: //The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
013: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
014: //
015: //All Rights Reserved.
016: package org.columba.core.backgroundtask;
017:
018: import java.awt.EventQueue;
019: import java.awt.Toolkit;
020: import java.awt.event.ActionEvent;
021: import java.awt.event.ActionListener;
022: import java.util.List;
023: import java.util.Vector;
024: import java.util.logging.Level;
025: import java.util.logging.Logger;
026:
027: import javax.swing.Timer;
028:
029: import org.columba.api.backgroundtask.IBackgroundTaskManager;
030: import org.columba.core.command.TaskManager;
031:
032: /**
033: * This manager runs in background.
034: * <p>
035: * If the user doesn't do anything with Columba, it starts some cleanup workers,
036: * like saving configuration, saving header-cache, etc.
037: *
038: * @author fdietz
039: */
040: public class BackgroundTaskManager implements ActionListener,
041: IBackgroundTaskManager {
042:
043: private static final Logger LOG = Logger
044: .getLogger("org.columba.api.backgroundtask"); //$NON-NLS-1$
045:
046: // one second (=1000 ms)
047: private static final int ONE_SECOND = 1000;
048:
049: // sleep 5 minutes
050: private static final int SLEEP_TIME = ONE_SECOND * 60 * 5;
051:
052: private Timer timer;
053:
054: private List<Runnable> list;
055:
056: private static BackgroundTaskManager instance = new BackgroundTaskManager();
057:
058: public BackgroundTaskManager() {
059: super ();
060: // TODO we should check if we need an vector or better another list
061: // implementation; checking also, if the list
062: // must be syncronized or not (performance)
063: this .list = new Vector<Runnable>();
064:
065: this .timer = new Timer(SLEEP_TIME, this );
066: this .timer.start();
067: }
068:
069: public static BackgroundTaskManager getInstance() {
070: return instance;
071: }
072:
073: /**
074: * @see org.columba.api.backgroundtask.IBackgroundTaskManager#register(java.lang.Runnable)
075: */
076: public void register(Runnable runnable) {
077: this .list.add(runnable);
078: }
079:
080: /*
081: * (non-Javadoc)
082: *
083: * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
084: */
085: public void actionPerformed(ActionEvent event) {
086: // test if a task is already running
087: EventQueue queue = Toolkit.getDefaultToolkit()
088: .getSystemEventQueue();
089:
090: if ((queue.peekEvent() == null)
091: && (TaskManager.getInstance().count() == 0)) {
092: // no java task running -> start background tasks
093: if (LOG.isLoggable(Level.FINE)) {
094: LOG.fine("Starting background tasks..."); //$NON-NLS-1$
095: }
096: runTasks();
097: }
098: }
099:
100: public void runTasks() {
101: for (Runnable task : this .list) {
102: task.run();
103: }
104: }
105:
106: public void stop() {
107: this.timer.stop();
108: }
109: }
|