KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > riot > job > support > TaskList


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2006
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.riot.job.support;
25
26 import java.util.HashSet JavaDoc;
27 import java.util.Iterator JavaDoc;
28
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.riotfamily.riot.job.persistence.JobDetail;
32
33 public class TaskList {
34
35     private static Log log = LogFactory.getLog(TaskList.class);
36     
37     private HashSet JavaDoc activeTasks = new HashSet JavaDoc();
38     
39     /**
40      * Returns the JobTask for the given JobDetail or <code>null</code> if
41      * no such task exists.
42      */

43     public synchronized JobTask getJobTask(JobDetail detail) {
44         Iterator JavaDoc it = activeTasks.iterator();
45         while (it.hasNext()) {
46             JobTask task = (JobTask) it.next();
47             if (task.getDetail().getId().equals(detail.getId())) {
48                 return task;
49             }
50         }
51         return null;
52     }
53     
54     /**
55      * Adds the given task to the list of active tasks and notifies waiting
56      * threads.
57      */

58     public synchronized void addTask(JobTask task) {
59         activeTasks.add(task);
60         notifyAll();
61     }
62     
63     /**
64      * Removes the given task from the list of active tasks.
65      */

66     public synchronized void removeTask(JobTask task) {
67         activeTasks.remove(task);
68     }
69         
70     public void interrupt(JobDetail detail) {
71         JobTask task = getJobTask(detail);
72         if (task != null) {
73             task.interrupt();
74             removeTask(task);
75         }
76     }
77     
78     public void interruptAll() {
79         //TODO Synchronization ...
80
Iterator JavaDoc it = activeTasks.iterator();
81         while (it.hasNext()) {
82             JobTask task = (JobTask) it.next();
83             log.info("Interrupting task " + task.getDetail().getId());
84             task.interrupt();
85         }
86         activeTasks.clear();
87     }
88     
89     /**
90      * Iterates over the active tasks and invokes
91      * {@link JobTask#updateExecutionTime() task.updateExecutionTime()}.
92      */

93     public synchronized void updateExecutionTimes() {
94         Iterator JavaDoc it = activeTasks.iterator();
95         while (it.hasNext()) {
96             JobTask task = (JobTask) it.next();
97             task.updateExecutionTime();
98         }
99     }
100     
101     /**
102      * Waits until at least one active task is present.
103      */

104     public synchronized void waitForTasks() {
105         if (activeTasks.isEmpty()) {
106             try {
107                 wait();
108             }
109             catch (InterruptedException JavaDoc e) {
110             }
111         }
112     }
113     
114 }
115
Popular Tags