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