| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| JobQueue |
|
| 1.8181818181818181;1,818 | ||||
| JobQueue$1 |
|
| 1.8181818181818181;1,818 |
| 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 | 4 | private Vector<J> queue = null; |
| 43 | ||
| 44 | /** | |
| 45 | * Creates a new instance. | |
| 46 | */ | |
| 47 | public JobQueue() | |
| 48 | 4 | { |
| 49 | 4 | queue = new Vector<J>(10); |
| 50 | 4 | } |
| 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 | 9 | if (queue.size() > 0) |
| 61 | { | |
| 62 | 9 | return queue.elementAt(0); |
| 63 | } | |
| 64 | else | |
| 65 | { | |
| 66 | 0 | 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 | 1 | int index = -1; |
| 79 | ||
| 80 | 1 | if (je != null) |
| 81 | { | |
| 82 | 1 | index = queue.indexOf(je); |
| 83 | } | |
| 84 | ||
| 85 | 1 | if (index < 0) |
| 86 | { | |
| 87 | 0 | return null; |
| 88 | } | |
| 89 | else | |
| 90 | { | |
| 91 | 1 | 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 | 5 | if (queue != null && queue.size() > 0) |
| 104 | { | |
| 105 | 5 | return (Vector<J>) queue.clone(); |
| 106 | } | |
| 107 | else | |
| 108 | { | |
| 109 | 0 | 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 | 1 | queue.addElement(je); |
| 121 | 1 | sortQueue(); |
| 122 | 1 | } |
| 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 | 4 | if (jobEntries != null) |
| 133 | { | |
| 134 | 4 | queue.addAll(jobEntries); |
| 135 | 4 | sortQueue(); |
| 136 | } | |
| 137 | ||
| 138 | 4 | } |
| 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 | 2 | queue.removeElement(je); |
| 148 | 2 | sortQueue(); |
| 149 | 2 | } |
| 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 | 1 | remove(je); |
| 160 | 1 | je.calcRunTime(); |
| 161 | 1 | this.add(je); |
| 162 | 1 | sortQueue(); |
| 163 | 1 | } |
| 164 | ||
| 165 | /** | |
| 166 | * Update the job for its next run time. | |
| 167 | * | |
| 168 | * @param je A JobEntry to be updated. | |
| 169 | * @exception TurbineException a generic exception. | |
| 170 | */ | |
| 171 | public synchronized void updateQueue(J je) | |
| 172 | throws TurbineException | |
| 173 | { | |
| 174 | 1 | je.calcRunTime(); |
| 175 | 1 | sortQueue(); |
| 176 | 1 | } |
| 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 | 9 | Comparator<J> aComparator = new Comparator<J>() |
| 185 | 11 | { |
| 186 | @Override | |
| 187 | public int compare(J o1, J o2) | |
| 188 | { | |
| 189 | 2 | Long time1 = Long.valueOf(o1.getNextRuntime()); |
| 190 | 2 | Long time2 = Long.valueOf(o2.getNextRuntime()); |
| 191 | 2 | return (time1.compareTo(time2)); |
| 192 | } | |
| 193 | }; | |
| 194 | ||
| 195 | 9 | Collections.sort(queue, aComparator); |
| 196 | 9 | } |
| 197 | } |