1 package org.apache.turbine.services.schedule;
2
3 /* ====================================================================
4 * The Apache Software License, Version 1.1
5 *
6 * Copyright (c) 2001 The Apache Software Foundation. All rights
7 * reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
19 * distribution.
20 *
21 * 3. The end-user documentation included with the redistribution,
22 * if any, must include the following acknowledgment:
23 * "This product includes software developed by the
24 * Apache Software Foundation (http://www.apache.org/)."
25 * Alternately, this acknowledgment may appear in the software itself,
26 * if and wherever such third-party acknowledgments normally appear.
27 *
28 * 4. The names "Apache" and "Apache Software Foundation" and
29 * "Apache Turbine" must not be used to endorse or promote products
30 * derived from this software without prior written permission. For
31 * written permission, please contact apache@apache.org.
32 *
33 * 5. Products derived from this software may not be called "Apache",
34 * "Apache Turbine", nor may "Apache" appear in their name, without
35 * prior written permission of the Apache Software Foundation.
36 *
37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48 * SUCH DAMAGE.
49 * ====================================================================
50 *
51 * This software consists of voluntary contributions made by many
52 * individuals on behalf of the Apache Software Foundation. For more
53 * information on the Apache Software Foundation, please see
54 * <http://www.apache.org/>.
55 */
56
57 import java.util.List;
58 import java.util.Vector;
59 import org.apache.turbine.util.Comparable;
60 import org.apache.turbine.util.QuickSort;
61
62 /***
63 * Queue for the scheduler.
64 *
65 * @author <a href="mailto:mbryson@mont.mindspring.com">Dave Bryson</a>
66 * @version $Id: JobQueue.java,v 1.1.1.1 2001/08/16 05:09:14 jvanzyl Exp $
67 */
68 public class JobQueue
69 implements Comparable
70 {
71 /***
72 * The queue of <code>JobEntry</code> objects.
73 */
74 private Vector queue = null;
75
76 /***
77 * Creates a new instance.
78 *
79 * @exception Exception A generic exception.
80 */
81 public JobQueue()
82 throws Exception
83 {
84 queue = new Vector(10);
85 }
86
87 /***
88 * Return the next job off the top of the queue, or <code>null</code> if
89 * there are no jobs in the queue.
90 *
91 * @return The next job in the queue.
92 */
93 public JobEntry getNext()
94 {
95 if ( queue.size() > 0 )
96 {
97 return (JobEntry)queue.elementAt(0);
98 }
99 else
100 {
101 return null;
102 }
103 }
104
105 /***
106 * Return a specific job.
107 *
108 * @param je The JobEntry we are looking for.
109 * @return A JobEntry.
110 */
111 public JobEntry getJob(JobEntry je)
112 {
113 int index = -1;
114
115 if ( je != null )
116 {
117 index = queue.indexOf(je);
118 }
119
120 if ( index < 0 )
121 {
122 return null;
123 }
124 else
125 {
126 return (JobEntry)queue.elementAt(index);
127 }
128 }
129
130 /***
131 * List jobs in the queue. This is used by the scheduler UI.
132 *
133 * @return A Vector of <code>JobEntry</code> objects.
134 */
135 public Vector list()
136 {
137 if ( queue != null && queue.size() > 0 )
138 {
139 return (Vector)queue.clone();
140 }
141 else
142 {
143 return null;
144 }
145 }
146
147 /***
148 * Add a job to the queue.
149 *
150 * @param je A JobEntry job.
151 */
152 public synchronized void add(JobEntry je)
153 {
154 queue.addElement(je);
155 sortQueue();
156 }
157
158 /***
159 * Batch load jobs. Retains any already enqueued jobs. Called on
160 * <code>SchedulerService</code> start-up.
161 *
162 * @param jobEntries A list of the <code>JobEntry</code> objects to load.
163 */
164 public synchronized void batchLoad(List jobEntries)
165 {
166 if (jobEntries != null)
167 {
168 queue.addAll(jobEntries);
169 sortQueue();
170 }
171
172 }
173
174 /***
175 * Remove a job from the queue.
176 *
177 * @param je A JobEntry with the job to remove.
178 */
179 public synchronized void remove(JobEntry je)
180 {
181 queue.removeElement(je);
182 sortQueue();
183 }
184
185 /***
186 * Modify a job on the queue.
187 *
188 * @param je A JobEntry with the job to modify
189 */
190 public synchronized void modify(JobEntry je)
191 {
192 sortQueue();
193 }
194
195 /***
196 * Update the job for its next run time.
197 *
198 * @param je A JobEntry to be updated.
199 * @exception Exception, a generic exception.
200 */
201 public synchronized void updateQueue(JobEntry je)
202 throws Exception
203 {
204 je.calcRunTime();
205 sortQueue();
206 }
207
208 /***
209 * Re-sort the existing queue. Consumers of this method should be
210 * <code>synchronized</code>.
211 */
212 private void sortQueue()
213 {
214 // First sort.
215 Object data[] = new Object[queue.size()];
216 queue.copyInto(data);
217 QuickSort.quickSort(data, 0, data.length - 1, this);
218 queue.removeAllElements();
219
220 for (int i = 0; i < data.length; i++)
221 {
222 queue.add(data[i]);
223 }
224 }
225
226 /***
227 * Compare to another <code>JobEntry</code>. Used by the
228 * <code>QuickSort</code> class to determine sort order.
229 *
230 * @param je1 The first <code>JobEntry</code> object.
231 * @param je2 The second <code>JobEntry</code> object.
232 * @return An <code>int</code> indicating the result of the comparison.
233 */
234 public int compare(Object je1, Object je2)
235 {
236 long obj1Time = ((JobEntry)je1).getNextRuntime();
237 long obj2Time = ((JobEntry)je2).getNextRuntime();
238
239 if (obj1Time > obj2Time)
240 return 1;
241 else if (obj1Time < obj2Time)
242 return -1;
243 else
244 return 0;
245 }
246 }
This page was automatically generated by Maven