%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
org.apache.turbine.services.schedule.JobQueue |
|
|
1 | package org.apache.turbine.services.schedule; |
|
2 | ||
3 | /* |
|
4 | * Copyright 2001-2005 The Apache Software Foundation. |
|
5 | * |
|
6 | * Licensed under the Apache License, Version 2.0 (the "License") |
|
7 | * you may not use this file except in compliance with the License. |
|
8 | * You may obtain a copy of the License at |
|
9 | * |
|
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
11 | * |
|
12 | * Unless required by applicable law or agreed to in writing, software |
|
13 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15 | * See the License for the specific language governing permissions and |
|
16 | * limitations under the License. |
|
17 | */ |
|
18 | ||
19 | import java.util.Collections; |
|
20 | import java.util.Comparator; |
|
21 | import java.util.List; |
|
22 | import java.util.Vector; |
|
23 | ||
24 | import org.apache.turbine.util.TurbineException; |
|
25 | ||
26 | /** |
|
27 | * Queue for the scheduler. |
|
28 | * |
|
29 | * @author <a href="mailto:mbryson@mont.mindspring.com">Dave Bryson</a> |
|
30 | * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a> |
|
31 | * @version $Id: JobQueue.java 264148 2005-08-29 14:21:04Z henning $ |
|
32 | */ |
|
33 | public class JobQueue |
|
34 | { |
|
35 | /** |
|
36 | * The queue of <code>JobEntry</code> objects. |
|
37 | */ |
|
38 | 2 | private Vector queue = null; |
39 | ||
40 | /** |
|
41 | * Creates a new instance. |
|
42 | */ |
|
43 | public JobQueue() |
|
44 | 2 | { |
45 | 2 | queue = new Vector(10); |
46 | 2 | } |
47 | ||
48 | /** |
|
49 | * Return the next job off the top of the queue, or <code>null</code> if |
|
50 | * there are no jobs in the queue. |
|
51 | * |
|
52 | * @return The next job in the queue. |
|
53 | */ |
|
54 | public JobEntry getNext() |
|
55 | { |
|
56 | 4 | if (queue.size() > 0) |
57 | { |
|
58 | 4 | return (JobEntry) queue.elementAt(0); |
59 | } |
|
60 | else |
|
61 | { |
|
62 | 0 | return null; |
63 | } |
|
64 | } |
|
65 | ||
66 | /** |
|
67 | * Return a specific job. |
|
68 | * |
|
69 | * @param je The JobEntry we are looking for. |
|
70 | * @return A JobEntry. |
|
71 | */ |
|
72 | public JobEntry getJob(JobEntry je) |
|
73 | { |
|
74 | 2 | int index = -1; |
75 | ||
76 | 2 | if (je != null) |
77 | { |
|
78 | 2 | index = queue.indexOf(je); |
79 | } |
|
80 | ||
81 | 2 | if (index < 0) |
82 | { |
|
83 | 0 | return null; |
84 | } |
|
85 | else |
|
86 | { |
|
87 | 2 | return (JobEntry) queue.elementAt(index); |
88 | } |
|
89 | } |
|
90 | ||
91 | /** |
|
92 | * List jobs in the queue. This is used by the scheduler UI. |
|
93 | * |
|
94 | * @return A Vector of <code>JobEntry</code> objects. |
|
95 | */ |
|
96 | public Vector list() |
|
97 | { |
|
98 | 6 | if (queue != null && queue.size() > 0) |
99 | { |
|
100 | 6 | return (Vector) queue.clone(); |
101 | } |
|
102 | else |
|
103 | { |
|
104 | 0 | return null; |
105 | } |
|
106 | } |
|
107 | ||
108 | /** |
|
109 | * Add a job to the queue. |
|
110 | * |
|
111 | * @param je A JobEntry job. |
|
112 | */ |
|
113 | public synchronized void add(JobEntry je) |
|
114 | { |
|
115 | 2 | queue.addElement(je); |
116 | 2 | sortQueue(); |
117 | 2 | } |
118 | ||
119 | /** |
|
120 | * Batch load jobs. Retains any already enqueued jobs. Called on |
|
121 | * <code>SchedulerService</code> start-up. |
|
122 | * |
|
123 | * @param jobEntries A list of the <code>JobEntry</code> objects to load. |
|
124 | */ |
|
125 | public synchronized void batchLoad(List jobEntries) |
|
126 | { |
|
127 | 2 | if (jobEntries != null) |
128 | { |
|
129 | 2 | queue.addAll(jobEntries); |
130 | 2 | sortQueue(); |
131 | } |
|
132 | ||
133 | 2 | } |
134 | ||
135 | /** |
|
136 | * Remove a job from the queue. |
|
137 | * |
|
138 | * @param je A JobEntry with the job to remove. |
|
139 | */ |
|
140 | public synchronized void remove(JobEntry je) |
|
141 | { |
|
142 | 4 | queue.removeElement(je); |
143 | 4 | sortQueue(); |
144 | 4 | } |
145 | ||
146 | /** |
|
147 | * Modify a job on the queue. |
|
148 | * |
|
149 | * @param je A JobEntry with the job to modify |
|
150 | */ |
|
151 | public synchronized void modify(JobEntry je) |
|
152 | throws TurbineException |
|
153 | { |
|
154 | 2 | remove(je); |
155 | 2 | je.calcRunTime(); |
156 | 2 | this.add(je); |
157 | 2 | sortQueue(); |
158 | 2 | } |
159 | ||
160 | /** |
|
161 | * Update the job for its next run time. |
|
162 | * |
|
163 | * @param je A JobEntry to be updated. |
|
164 | * @exception TurbineException a generic exception. |
|
165 | */ |
|
166 | public synchronized void updateQueue(JobEntry je) |
|
167 | throws TurbineException |
|
168 | { |
|
169 | 0 | je.calcRunTime(); |
170 | 0 | sortQueue(); |
171 | 0 | } |
172 | ||
173 | /** |
|
174 | * Re-sort the existing queue. Consumers of this method should be |
|
175 | * <code>synchronized</code>. |
|
176 | */ |
|
177 | private void sortQueue() |
|
178 | { |
|
179 | 10 | Comparator aComparator = new Comparator() |
180 | { |
|
181 | public int compare(Object o1, Object o2) |
|
182 | { |
|
183 | Long time1 = new Long(((JobEntry) o1).getNextRuntime()); |
|
184 | Long time2 = new Long(((JobEntry) o2).getNextRuntime()); |
|
185 | return (time1.compareTo(time2)); |
|
186 | } |
|
187 | }; |
|
188 | ||
189 | 10 | Collections.sort(queue, aComparator); |
190 | 10 | } |
191 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |