1 package org.apache.turbine.services.schedule; 2 3 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 33 public class JobQueue 34 { 35 38 private Vector queue = null; 39 40 43 public JobQueue() 44 { 45 queue = new Vector (10); 46 } 47 48 54 public JobEntry getNext() 55 { 56 if (queue.size() > 0) 57 { 58 return (JobEntry) queue.elementAt(0); 59 } 60 else 61 { 62 return null; 63 } 64 } 65 66 72 public JobEntry getJob(JobEntry je) 73 { 74 int index = -1; 75 76 if (je != null) 77 { 78 index = queue.indexOf(je); 79 } 80 81 if (index < 0) 82 { 83 return null; 84 } 85 else 86 { 87 return (JobEntry) queue.elementAt(index); 88 } 89 } 90 91 96 public Vector list() 97 { 98 if (queue != null && queue.size() > 0) 99 { 100 return (Vector ) queue.clone(); 101 } 102 else 103 { 104 return null; 105 } 106 } 107 108 113 public synchronized void add(JobEntry je) 114 { 115 queue.addElement(je); 116 sortQueue(); 117 } 118 119 125 public synchronized void batchLoad(List jobEntries) 126 { 127 if (jobEntries != null) 128 { 129 queue.addAll(jobEntries); 130 sortQueue(); 131 } 132 133 } 134 135 140 public synchronized void remove(JobEntry je) 141 { 142 queue.removeElement(je); 143 sortQueue(); 144 } 145 146 151 public synchronized void modify(JobEntry je) 152 throws TurbineException 153 { 154 remove(je); 155 je.calcRunTime(); 156 this.add(je); 157 sortQueue(); 158 } 159 160 166 public synchronized void updateQueue(JobEntry je) 167 throws TurbineException 168 { 169 je.calcRunTime(); 170 sortQueue(); 171 } 172 173 177 private void sortQueue() 178 { 179 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 Collections.sort(queue, aComparator); 190 } 191 } 192 | Popular Tags |